core/pages/api/v2/bots/[id]/owners.ts
SKINMAKER 8694e9b19a
deps: change discord.js version to v14 (#503)
* deps: update djs to v14

* refactor: use discord.js v14

* fix: presence not showing properly

* fix: revert Ids

* Update pages/api/v2/bots/[id]/index.ts

Co-authored-by: Junseo Park <wonderlandpark@outlook.kr>

* style: apply code style

* feat: customizable intents

* feat: change node version

* feat: change node version

* fix: misused operator

* deps: fix typescript version to 4.6.4

* fix: fix bot url length (#504)

* refactor: add more advertisements (#508)

* feat: add open review deny log and max denies restriction (#510)

* feat: add open review log channel and embed

* chore: do not include submit page

* chore: mention instead of date

* feat: add max denies error

* typo: 더 이상

Co-authored-by: Junseo Park <wonderlandpark@outlook.kr>

* feat: add exceptions to deny count

* fix: invalid embed used

Co-authored-by: Junseo Park <wonderlandpark@outlook.kr>

* fix: invalid guild

Co-authored-by: Junseo Park <wonderlandpark@outlook.kr>

* fix: invalid position

Co-authored-by: Junseo Park <wonderlandpark@outlook.kr>

* fix: proper reason check position

Co-authored-by: Junseo Park <wonderlandpark@outlook.kr>

* feat: sepcific error message

* refactor: change reason embed format

* fix: knex andWhereNot method to whereNotIn method

Co-authored-by: Junseo Park <wonderlandpark@outlook.kr>

* refactor: detact adblock (#509)

* refactor: detact adblock

* perf: implement mobile detection

* deps: update djs to v14

* refactor: use discord.js v14

* fix: presence not showing properly

* fix: revert Ids

* Update pages/api/v2/bots/[id]/index.ts

Co-authored-by: Junseo Park <wonderlandpark@outlook.kr>

* style: apply code style

* feat: customizable intents

* feat: change node version

* feat: change node version

* fix: misused operator

* deps: fix typescript version to 4.6.4

* refactor: use discord.js v14

* deps: update discord.js to 14.2.0

* style: split options

* style: prettify

* deps: update erlpack

Co-authored-by: Junseo Park <wonderlandpark@outlook.kr>
Co-authored-by: Byungchul Kim <64084503+chul0721@users.noreply.github.com>
Co-authored-by: Eunwoo Choi <61371424+eunwoo1104@users.noreply.github.com>
2022-08-30 23:28:48 +09:00

50 lines
2.6 KiB
TypeScript

import { NextApiRequest } from 'next'
import RequestHandler from '@utils/RequestHandler'
import { CaptchaVerify, get, update } from '@utils/Query'
import ResponseWrapper from '@utils/ResponseWrapper'
import { checkToken } from '@utils/Csrf'
import { checkUserFlag, diff, makeDiscordCodeblock } from '@utils/Tools'
import { EditBotOwner, EditBotOwnerSchema } from '@utils/Yup'
import { User } from '@types'
import { discordLog } from '@utils/DiscordBot'
import { EmbedBuilder } from 'discord.js'
import { KoreanbotsEndPoints } from '@utils/Constants'
const BotOwners = 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((bot.owners as User[])[0].id !== user && !checkUserFlag(userinfo.flags, 'staff')) return ResponseWrapper(res, { code: 403 })
if(['reported', 'blocked', 'archived'].includes(bot.state) && !checkUserFlag(userinfo.flags, 'staff')) return ResponseWrapper(res, { code: 403, message: '해당 봇은 수정할 수 없습니다.', errors: ['오류라고 생각되면 문의해주세요.'] })
const validated = await EditBotOwnerSchema.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 userFetched: User[] = await Promise.all(validated.owners.map((u: string) => get.user.load(u)))
if(userFetched.indexOf(null) !== -1) return ResponseWrapper(res, { code: 400, message: '올바르지 않은 유저 ID를 포함하고 있습니다.' })
if(userFetched.length > 1 && userFetched[0].id !== (bot.owners as User[])[0].id) return ResponseWrapper(res, { code: 400, errors: ['소유자를 이전할 때는 다른 관리자를 포함할 수 없습니다.'] })
await update.botOwners(bot.id, validated.owners)
get.user.clear(user)
await discordLog('BOT/OWNERS', userinfo.id, (new EmbedBuilder().setDescription(`${bot.name} - <@${bot.id}> ([${bot.id}](${KoreanbotsEndPoints.URL.bot(bot.id)}))`)), null, makeDiscordCodeblock(diff(JSON.stringify(bot.owners.map(el => el.id)), JSON.stringify(validated.owners)), 'diff'))
return ResponseWrapper(res, { code: 200 })
})
interface PostApiRequest extends NextApiRequest {
query: {
id: string
},
body: EditBotOwner
}
export default BotOwners