mirror of
https://github.com/koreanbots/core.git
synced 2025-12-15 22:10:24 +00:00
feat: added report route
This commit is contained in:
parent
dbcb662071
commit
ac414b25bc
50
pages/api/v2/bots/[id]/report.ts
Normal file
50
pages/api/v2/bots/[id]/report.ts
Normal file
@ -0,0 +1,50 @@
|
||||
import { NextApiRequest} from 'next'
|
||||
import rateLimit from 'express-rate-limit'
|
||||
|
||||
import { get } from '@utils/Query'
|
||||
import RequestHandler from '@utils/RequestHandler'
|
||||
import ResponseWrapper from '@utils/ResponseWrapper'
|
||||
import { ReportSchema, Report} from '@utils/Yup'
|
||||
import { getReportChannel } from '@utils/DiscordBot'
|
||||
|
||||
const limiter = rateLimit({
|
||||
windowMs: 5 * 60 * 1000,
|
||||
max: 3,
|
||||
statusCode: 429,
|
||||
handler: (_req, res) => ResponseWrapper(res, { code: 429 }),
|
||||
keyGenerator: (req) => req.headers.authorization,
|
||||
skip: (req, res) => {
|
||||
res.removeHeader('X-RateLimit-Global')
|
||||
if(!req.headers.authorization) return true
|
||||
else return false
|
||||
}
|
||||
})
|
||||
|
||||
const BotReport = RequestHandler().post(limiter)
|
||||
.post(async (req: PostApiRequest, res) => {
|
||||
const user = await get.Authorization(req.cookies.token)
|
||||
if(!user) return ResponseWrapper(res, { code: 401 })
|
||||
const bot = await get.bot.load(req.query.id)
|
||||
if(!bot) return ResponseWrapper(res, { code: 404, message: '존재하지 않는 봇입니다.' })
|
||||
if(!req.body) return ResponseWrapper(res, { code: 400 })
|
||||
const validated: Report = await ReportSchema.validate(req.body, { abortEarly: false })
|
||||
.then(el => el)
|
||||
.catch(e => {
|
||||
ResponseWrapper(res, { code: 400, errors: e.errors })
|
||||
return null
|
||||
})
|
||||
|
||||
if(!validated) return
|
||||
await getReportChannel().send(`Reported by <@${user}> (${user})\nReported **${bot.name}** <@${bot.id}> (${bot.id})\nCategory ${req.body.category}\nDesc\n\`\`\`${req.body.description}\`\`\``, { allowedMentions: { parse: ['users'] }})
|
||||
return ResponseWrapper(res, { code: 200, message: '성공적으로 처리되었습니다.' })
|
||||
})
|
||||
|
||||
|
||||
interface PostApiRequest extends NextApiRequest {
|
||||
body: Report | null
|
||||
query: {
|
||||
id: string
|
||||
}
|
||||
}
|
||||
|
||||
export default BotReport
|
||||
Loading…
x
Reference in New Issue
Block a user