mirror of
https://github.com/koreanbots/core.git
synced 2025-12-15 14:10:22 +00:00
feat: added applications api
This commit is contained in:
parent
02890b3445
commit
cddc4c986a
37
pages/api/v2/applications/bots/[id].ts
Normal file
37
pages/api/v2/applications/bots/[id].ts
Normal file
@ -0,0 +1,37 @@
|
||||
import { NextApiRequest } from 'next'
|
||||
|
||||
import { DeveloperBot, DeveloperBotSchema } from '@utils/Yup'
|
||||
import { get, update } from '@utils/Query'
|
||||
import ResponseWrapper from '@utils/ResponseWrapper'
|
||||
import { checkToken } from '@utils/Csrf'
|
||||
import { User } from '@types'
|
||||
import RequestHandler from '@utils/RequestHandler'
|
||||
|
||||
const BotApplications = RequestHandler
|
||||
.patch(async (req: ApiRequest, res) => {
|
||||
const user = await get.Authorization(req.cookies.token)
|
||||
if(!user) return ResponseWrapper(res, { code: 401 })
|
||||
const csrfValidated = checkToken(req, res, req.body._csrf)
|
||||
if(!csrfValidated) return
|
||||
const validated = await DeveloperBotSchema.validate(req.body, { abortEarly: false }).then(el => el).catch(e => {
|
||||
ResponseWrapper(res, { code: 400, errors: e.errors })
|
||||
return null
|
||||
})
|
||||
|
||||
if(!validated) return
|
||||
const bot = await get.bot.load(req.query.id)
|
||||
if(!bot) return ResponseWrapper(res, { code: 404, message: '존재하지 않는 봇입니다.' })
|
||||
if(!(bot.owners as User[]).find(el => el.id === user)) return ResponseWrapper(res, { code: 403 })
|
||||
await update.updateBotApplication(req.query.id, { webhook: validated.webhook || null })
|
||||
return ResponseWrapper(res, { code: 200 })
|
||||
|
||||
})
|
||||
|
||||
interface ApiRequest extends NextApiRequest {
|
||||
body: DeveloperBot
|
||||
query: {
|
||||
id: string
|
||||
}
|
||||
}
|
||||
|
||||
export default BotApplications
|
||||
Loading…
x
Reference in New Issue
Block a user