feat: using flags

This commit is contained in:
원더 2021-02-10 18:51:55 +09:00
parent fbfa9ddcf7
commit 2a6227ab83
6 changed files with 57 additions and 31 deletions

View File

@ -10,7 +10,7 @@ import { Bot, User } from '@types'
import { git, Status } from '@utils/Constants'
import { get } from '@utils/Query'
import Day from '@utils/Day'
import { checkPerm, formatNumber, parseCookie } from '@utils/Tools'
import { checkBotFlag, checkUserFlag, formatNumber, parseCookie } from '@utils/Tools'
import NotFound from '../404'
@ -60,7 +60,7 @@ const Bots: NextPage<BotsProps> = ({ data, date, user }) => {
/>
<h1 className='mb-2 mt-3 text-4xl font-bold'>
{data.name}{' '}
{data.trusted ? (
{checkBotFlag(data.flags, 'trusted') ? (
<Tooltip text='해당봇은 한국 디스코드봇 리스트에서 엄격한 기준을 통과한 봇입니다!' direction='left' size='large' href='/verification'>
<span className='text-koreanbots-blue text-3xl'>
<i className='fas fa-award' />
@ -92,7 +92,7 @@ const Bots: NextPage<BotsProps> = ({ data, date, user }) => {
</span>
</LongButton>
{
((data.owners as User[]).find(el => el.id === user.id) || checkPerm(user.perm, 'staff')) && <LongButton href={`/manage/${data.id}`}>
((data.owners as User[]).find(el => el.id === user.id) || checkUserFlag(user.flags, 'staff')) && <LongButton href={`/manage/${data.id}`}>
<h4>
<i className='fas fa-cogs' />
</h4>
@ -120,7 +120,7 @@ const Bots: NextPage<BotsProps> = ({ data, date, user }) => {
</div>
<div>{Day(date).fromNow(false)}</div>
{
data.verified ?
checkBotFlag(data.flags, 'trusted') ?
<Tooltip direction='left' text='해당 봇은 디스코드측에서 인증된 봇입니다.'>
<div>
<i className='fas fa-check text-discord-blurple' />

View File

@ -6,7 +6,7 @@ import { josa } from 'josa'
import { Bot, User } from '@types'
import * as Query from '@utils/Query'
import { checkPerm } from '@utils/Tools'
import { checkUserFlag } from '@utils/Tools'
import NotFound from '../404'
@ -50,14 +50,14 @@ const Users: NextPage<UserProps> = ({ data }) => {
<span className='ml-0.5 text-gray-400 text-4xl font-semibold'>#{data.tag}</span>
<br />
<div className='badges flex'>
{checkPerm(data.perm, 'staff') && (
{checkUserFlag(data.flags, 'staff') && (
<Tooltip text='한국 디스코드봇 리스트 스탭입니다.' direction='left'>
<div className='pr-5 text-koreanbots-blue text-2xl'>
<i className='fas fa-hammer' />
</div>
</Tooltip>
)}
{checkPerm(data.perm, 'bughunter') && (
{checkUserFlag(data.flags, 'bughunter') && (
<Tooltip text='버그를 많이 제보해주신 분입니다.' direction='left'>
<div className='pr-5 text-green-500 text-2xl'>
<i className='fas fa-bug' />

View File

@ -1,5 +1,3 @@
export type UserPemissionFlags = 'general' | 'staff' | 'bughunter' | 'booster'
export interface Bot {
id: string
name: string
@ -19,9 +17,6 @@ export interface Bot {
git: string | null
url: string | null
discord: string | null
verified: boolean
trusted: boolean
partnered: boolean
vanity: string | null
bg: string
banner: string
@ -33,11 +28,44 @@ export interface User {
avatar: string
tag: string
username: string
perm: number
flags: number
github: string
bots: Bot[] | string[]
}
export enum UserFlags {
general = 0 << 0,
staff = 1 << 0,
bughunter = 1 << 1,
premium = 1 << 2
}
export enum BotFlags {
general = 0 << 0,
official = 1 << 0,
trusted = 1 << 2,
partnered = 1 << 3,
verifed = 1 << 4,
premium = 1 << 5,
hackerthon = 1 << 6
}
export enum DiscordUserFlags {
DISCORD_EMPLOYEE = 1 << 0,
DISCORD_PARTNER = 1 << 1,
HYPESQUAD_EVENTS = 1 << 2,
BUGHUNTER_LEVEL_1 = 1 << 3,
HOUSE_BRAVERY = 1 << 6,
HOUSE_BRILLIANCE = 1 << 7,
HOUSE_BALANCE = 1 << 8,
EARLY_SUPPORTER = 1 << 9,
TEAM_USER = 1 << 10,
SYSTEM = 1 << 12,
BUGHUNTER_LEVEL_2 = 1 << 14,
VERIFIED_BOT = 1 << 16,
VERIFIED_DEVELOPER = 1 << 17
}
export interface BotList {
type: ListType
data: Bot[]

View File

@ -32,13 +32,6 @@ export const Status = {
},
}
export const perms = {
general: 0x0,
staff: 0x1,
bughunter: 0x4,
booster: 0x8,
}
export const library = [
'discord.js',
'Eris',

View File

@ -5,7 +5,7 @@ import DataLoader from 'dataloader'
import { User as DiscordUser } from 'discord.js'
import { Stream } from 'stream'
import { Bot, User, ListType, BotList, TokenRegister } from '@types'
import { Bot, User, ListType, BotList, TokenRegister, BotFlags, DiscordUserFlags } from '@types'
import { categories } from './Constants'
import knex from './Knex'
@ -32,7 +32,6 @@ async function getBot(id: string, owners=true):Promise<Bot> {
'url',
'category',
'status',
'verified',
'trusted',
'partnered',
'discord',
@ -48,6 +47,7 @@ async function getBot(id: string, owners=true):Promise<Bot> {
if (res[0]) {
const discordBot = await get.discord.user.load(res[0].id)
if(!discordBot) return null
res[0].flags = res[0].flags | (discordBot.flags && DiscordUserFlags.VERIFIED_BOT ? BotFlags.verifed : 0) | res[0].trusted ? BotFlags.trusted : 0
res[0].tag = discordBot.discriminator
res[0].name = discordBot.username
res[0].category = JSON.parse(res[0].category)
@ -69,7 +69,7 @@ async function getBot(id: string, owners=true):Promise<Bot> {
async function getUser(id: string, bots = true):Promise<User> {
const res = await knex('users')
.select(['id', 'perm', 'github'])
.select(['id', 'flags', 'perm', 'github'])
.where({ id })
if (res[0]) {
const owned = await knex('bots')
@ -151,7 +151,6 @@ async function getBotList(type: ListType, page = 1, query?: string):Promise<BotL
if (!query) throw new Error('쿼리가 누락되었습니다.')
count = (await knex.raw('SELECT count(*) FROM bots WHERE MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode)', [decodeURI(query)]))[0][0]['count(*)']
res = (await knex.raw('SELECT id, votes, MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode) as relevance FROM bots WHERE MATCH(`name`, `intro`, `desc`) AGAINST(? in boolean mode) ORDER BY relevance DESC, votes DESC LIMIT 16 OFFSET ?', [decodeURI(query), decodeURI(query), ((page ? Number(page) : 1) - 1) * 16]))[0]
console.log(res)
} else {
count = 1
res = []

View File

@ -2,8 +2,8 @@ import { Readable } from 'stream'
import { NextPageContext } from 'next'
import cookie from 'cookie'
import { Bot, ImageOptions, UserPemissionFlags } from '@types'
import { KoreanbotsEndPoints, Oauth, perms } from './Constants'
import { BotFlags, ImageOptions, UserFlags } from '@types'
import { KoreanbotsEndPoints, Oauth } from './Constants'
import { NextRouter } from 'next/router'
export function formatNumber(value: number):string {
@ -17,18 +17,24 @@ export function formatNumber(value: number):string {
return shortValue+suffixes[suffixNum]
}
export function checkPerm(base: number, required: number | UserPemissionFlags):boolean {
required = typeof required === 'number' ? required : perms[required]
if (typeof required !== 'number' && !required) throw new Error('올바르지 않은 권한입니다.')
function checkFlag(base: number, required: number) {
return (base & required) === required
}
export function checkUserFlag(base: number, required: number | keyof typeof UserFlags):boolean {
return checkFlag(base, typeof required === 'number' ? required : UserFlags[required])
}
export function checkBotFlag(base: number, required: number | keyof typeof BotFlags):boolean {
return checkFlag(base, typeof required === 'number' ? required : BotFlags[required])
}
export function makeImageURL(root:string, { format='png', size=256 }:ImageOptions):string {
return `${root}.${format}?size=${size}`
}
export function makeBotURL(bot: { partnered?: boolean, trusted?: boolean, vanity?: string, id: string }): string {
return `/bots/${(bot.partnered || bot.trusted) && bot.vanity ? bot.vanity : bot.id}`
export function makeBotURL({id, vanity, flags=0}: { flags?: number, vanity?:string, id: string }): string {
return `/bots/${(checkBotFlag(flags, 'trusted') || checkBotFlag(flags, 'partnered')) && vanity ? vanity : id}`
}
export function serialize<T>(data: T): T {