feat: added refresh data

This commit is contained in:
wonderlandpark 2021-05-25 21:35:34 +09:00
parent b4b4314268
commit a174ff69d4
2 changed files with 46 additions and 5 deletions

View File

@ -7,8 +7,9 @@ import RequestHandler from '@utils/RequestHandler'
import ResponseWrapper from '@utils/ResponseWrapper'
import { BotStatUpdate, BotStatUpdateSchema } from '@utils/Yup'
import { discordLog } from '@utils/DiscordBot'
import { makeDiscordCodeblock } from '@utils/Tools'
import { checkUserFlag, makeDiscordCodeblock } from '@utils/Tools'
import { KoreanbotsEndPoints } from '@utils/Constants'
import type { User } from '@types'
const limiter = rateLimit({
windowMs: 3 * 60 * 1000,
@ -24,6 +25,19 @@ const limiter = rateLimit({
}
})
const patchLimiter = rateLimit({
windowMs: 2 * 60 * 1000,
max: 6,
statusCode: 429,
skipFailedRequests: true,
handler: (_req, res) => ResponseWrapper(res, { code: 429 }),
keyGenerator: (req) => req.headers['x-forwarded-for'] as string,
skip: (_req, res) => {
res.removeHeader('X-RateLimit-Global')
return false
}
})
const BotStats = RequestHandler().post(limiter)
.post(async (req: PostApiRequest, res) => {
const bot = await get.BotAuthorization(req.headers.authorization)
@ -46,9 +60,25 @@ const BotStats = RequestHandler().post(limiter)
await discordLog('BOT/STATS', botInfo.id, (new MessageEmbed().setDescription(`${botInfo.name} - <@${botInfo.id}> ([${botInfo.id}](${KoreanbotsEndPoints.URL.bot(botInfo.id)}))`)), null, makeDiscordCodeblock(`${botInfo.servers > validated.servers ? '-' : '+'} ${botInfo.servers} -> ${validated.servers} (${botInfo.servers > validated.servers ? '▼' : '▲'}${Math.abs(validated.servers - botInfo.servers)})`, 'diff'))
return ResponseWrapper(res, { code: 200, message: '성공적으로 업데이트 했습니다.'})
})
.patch(patchLimiter).patch(async (req: ApiRequest, res) => {
console.log('1')
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[]).find(el => el.id === user) && !checkUserFlag(userinfo.flags, 'staff')) return ResponseWrapper(res, { code: 403 })
get.bot.clear(req.query.id)
return ResponseWrapper(res, { code: 200 })
})
interface PostApiRequest extends NextApiRequest {
interface ApiRequest extends NextApiRequest {
query: {
id: string
}
}
interface PostApiRequest extends ApiRequest {
query: {
id: string
}

View File

@ -92,7 +92,8 @@ const Bots: NextPage<BotsProps> = ({ data, desc, date, user, theme, csrfToken })
<div className='w-full text-center lg:w-1/4'>
<DiscordAvatar
userID={data.id}
className={`w-full ${router.query.id === 'iu' ? 'cursor-heart' : ''}`}
size={256}
className='w-full'
/>
</div>
<div className='flex-grow px-5 py-12 w-full text-center lg:w-5/12 lg:text-left'>
@ -148,6 +149,17 @@ const Bots: NextPage<BotsProps> = ({ data, desc, date, user, theme, csrfToken })
</h4>
</LongButton>
}
{
((data.owners as User[]).find(el => el.id === user?.id) || checkUserFlag(user?.flags, 'staff')) && <LongButton onClick={async() => {
const res = await Fetch(`/bots/${data.id}/stats`, { method: 'PATCH'} )
if(res.code !== 200) return alert(res.message)
else window.location.reload()
}}>
<h4>
<i className='fas fa-sync' />
</h4>
</LongButton>
}
</div>
</div>
<Divider className='px-5' />
@ -199,8 +211,7 @@ const Bots: NextPage<BotsProps> = ({ data, desc, date, user, theme, csrfToken })
if(!user) return <Login />
else setReportModal(true)
}} aria-hidden='true'>
<i className='far fa-flag' />
<i className='far fa-flag' />
</a>
<Modal header={`${data.name}#${data.tag} 신고하기`} closeIcon isOpen={reportModal} onClose={() => {
setReportModal(false)