core/pages/api/v1/bots/servers.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

60 lines
2.5 KiB
TypeScript

import { NextApiRequest} from 'next'
import rateLimit from 'express-rate-limit'
import { EmbedBuilder } from 'discord.js'
import { get, update } from '@utils/Query'
import RequestHandler from '@utils/RequestHandler'
import ResponseWrapper from '@utils/ResponseWrapper'
import { BotStatUpdate, BotStatUpdateSchema } from '@utils/Yup'
import { getStatsLoggingChannel } from '@utils/DiscordBot'
import { KoreanbotsEndPoints } from '@utils/Constants'
import { makeDiscordCodeblock } from '@utils/Tools'
const limiter = rateLimit({
windowMs: 60 * 1000,
max: 1,
statusCode: 429,
handler: (_req, res) => ResponseWrapper(res, { code: 429, version: 1 }),
keyGenerator: (req) => req.headers.authorization,
skip: (req, res) => {
res.removeHeader('X-RateLimit-Global')
if(!req.headers.authorization) return true
else return false
}
})
const BotStats = RequestHandler()
.post(limiter)
.post(async (req: PostApiRequest, res) => {
const bot = await get.BotAuthorization(req.headers.token)
if(!bot) return ResponseWrapper(res, { code: 401, version: 1 })
const validated: BotStatUpdate = await BotStatUpdateSchema.validate(req.body, { abortEarly: false })
.then(el => el)
.catch(e => {
ResponseWrapper(res, { code: 400, errors: e.errors })
return null
})
if(!validated) return
const botInfo = await get.bot.load(bot)
if(!botInfo) return ResponseWrapper(res, { code: 404, message: '존재하지 않는 봇입니다.', version: 1 })
if(botInfo.id !== bot) return ResponseWrapper(res, { code: 403, version: 1 })
const d = await update.updateServer(botInfo.id, validated.servers, undefined)
if(d===1 || d===2) return ResponseWrapper(res, { code: 403, message: `서버 수를 ${[null, '1만', '100만'][d]} 이상으로 설정하실 수 없습니다. 문의해주세요.`, version: 1 })
get.bot.clear(bot)
await getStatsLoggingChannel().send({
content: `[BOT/STATS] <@${botInfo.id}> (${botInfo.id})\n${makeDiscordCodeblock(`${botInfo.servers > validated.servers ? '-' : '+'} ${botInfo.servers} -> ${validated.servers} (${botInfo.servers > validated.servers ? '▼' : '▲'}${Math.abs(validated.servers - botInfo.servers)})`, 'diff')}`,
embeds: [new EmbedBuilder().setDescription(`${botInfo.name} - <@${botInfo.id}> ([${botInfo.id}](${KoreanbotsEndPoints.URL.bot(botInfo.id)}`)]
})
return ResponseWrapper(res, { code: 200, message: '성공적으로 업데이트 했습니다.', version: 1 })
})
interface PostApiRequest extends NextApiRequest {
headers: {
token: string
}
body: BotStatUpdate
}
export default BotStats