diff --git a/pages/api/v2/bots/[id]/limit.ts b/pages/api/v2/bots/[id]/limit.ts new file mode 100644 index 0000000..5182d9d --- /dev/null +++ b/pages/api/v2/bots/[id]/limit.ts @@ -0,0 +1,74 @@ +import { NextApiRequest } from 'next' + +import { KoreanbotsEndPoints } from '@utils/Constants' +import { checkToken } from '@utils/Csrf' +import { discordLog } from '@utils/DiscordBot' +import { CaptchaVerify, get, update } from '@utils/Query' +import RequestHandler from '@utils/RequestHandler' +import ResponseWrapper from '@utils/ResponseWrapper' +import { checkUserFlag, makeDiscordCodeblock } from '@utils/Tools' +import { ExceedLimit, ExceedLimitScehma } from '@utils/Yup' +import { EmbedBuilder } from 'discord.js' + +const BotLimit = RequestHandler().patch(async (req: PostApiRequest, res) => { + const user = await get.Authorization(req.cookies.token) + if (!user) return ResponseWrapper(res, { code: 401 }) + const userinfo = await get.user.load(user) + const bot = await get.bot.load(req.query.id) + if (!bot) return ResponseWrapper(res, { code: 404 }) + if (!checkUserFlag(userinfo.flags, 'staff')) return ResponseWrapper(res, { code: 403 }) + + const validated = await ExceedLimitScehma.validate(req.body, { abortEarly: false }) + .then((el) => el) + .catch((e) => { + ResponseWrapper(res, { code: 400, errors: e.errors }) + return null + }) + if (!validated) return + const csrfValidated = checkToken(req, res, validated._csrf) + if (!csrfValidated) return + const captcha = await CaptchaVerify(validated._captcha) + if (!captcha) return + + const response = await update.updateServer( + bot.id, + validated.servers, + validated.shards == 0 ? undefined : validated.shards, + true + ) + console.log(response) + get.user.clear(user) + await discordLog( + 'BOT/EXCEED_LIMIT', + userinfo.id, + new EmbedBuilder().setDescription( + `${bot.name} - <@${bot.id}> ([${bot.id}](${KoreanbotsEndPoints.URL.bot(bot.id)}))` + ), + null, + makeDiscordCodeblock( + `${bot.servers > validated.servers ? '-' : '+'} ${bot.servers} -> ${ + validated.servers + } (${bot.servers > validated.servers ? '▼' : '▲'}${Math.abs(validated.servers - bot.servers)}) + + ${ + bot.servers >= 1000000 + ? '서버수 제한 해제 (1000000+)' + : bot.servers >= 10000 + ? '서버수 제한 해제 (10000+)' + : '' + } + + ${bot.shards >= 200 ? '샤드수 제한 해제' : ''} + `, + 'diff' + ) + ) + return ResponseWrapper(res, { code: 200 }) +}) + +interface PostApiRequest extends NextApiRequest { + query: { + id: string + } + body: ExceedLimit +} + +export default BotLimit diff --git a/pages/bots/[id]/edit.tsx b/pages/bots/[id]/edit.tsx index 5dddab5..cadf8cf 100644 --- a/pages/bots/[id]/edit.tsx +++ b/pages/bots/[id]/edit.tsx @@ -17,7 +17,7 @@ import { parseCookie, redirectTo, } from '@utils/Tools' -import { ManageBot, getManageBotSchema } from '@utils/Yup' +import { ExceedLimit, ManageBot, getManageBotSchema } from '@utils/Yup' import { botCategories, botCategoryDescription, botEnforcements, library } from '@utils/Constants' import { Bot, Theme, User } from '@types' import { getToken } from '@utils/Csrf' @@ -49,6 +49,7 @@ const ManageBotPage: NextPage = ({ bot, user, csrfToken, theme } const [adminModal, setAdminModal] = useState(false) const [transferModal, setTransferModal] = useState(false) const [deleteModal, setDeleteModal] = useState(false) + const [limitModal, setLimitModal] = useState(false) const router = useRouter() async function submitBot(value: ManageBot) { @@ -136,7 +137,9 @@ const ManageBotPage: NextPage = ({ bot, user, csrfToken, theme } {data.message || '오류가 발생했습니다.'} @@ -664,6 +667,77 @@ const ManageBotPage: NextPage = ({ bot, user, csrfToken, theme } )} + {checkUserFlag(user.flags, 'staff') && ( +
+ +

한디리 스탭 전용

+ +
+
+

서버수/샤드수 제한 해제

+

10000서버, 1000000서버, 200샤드 제한을 해제합니다.

+
+ + setLimitModal(false)} + closeIcon + > + { + const res = await Fetch(`/bots/${bot.id}/limit`, { + method: 'PATCH', + body: JSON.stringify(cleanObject(value)), + }) + if (res.code === 200) { + alert('성공적으로 수정하였습니다.') + router.push(makeBotURL(bot)) + } else alert(res.message) + }} + > + {({ values, setFieldValue }) => ( +
+
+

설정하실 서버 수를 입력해주세요.

+ +

설정하실 샤드 수를 입력해주세요.

+ +
+ setFieldValue('_captcha', k)} + /> + + + )} +
+
+
+
+
+ )} ) } diff --git a/utils/Yup.ts b/utils/Yup.ts index b1ede41..3ca9981 100644 --- a/utils/Yup.ts +++ b/utils/Yup.ts @@ -459,4 +459,16 @@ export interface EditBotOwner { _captcha: string } +export const ExceedLimitScehma: Yup.SchemaOf = Yup.object({ + servers: Yup.number().integer().moreThan(-1, '서버 수는 0보다 커야합니다.').required(), + shards: Yup.number().integer().moreThan(-1, '샤드 수는 0보다 커야합니다.').required(), + _csrf: Yup.string().required(), + _captcha: Yup.string().required(), +}) + +export interface ExceedLimit { + servers: number + shards: number +} + export default Yup