core/utils/Query.ts
Junseo Park ae47f741b8
v2 Release (#154)
* chore(deps): update dependency typescript to v4.2.4 (#314)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix(deps): update dependency core-js to v3.10.1 (#315)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* feat: camo images in bot desc

* chore: added bot delete api

* feat: delete button working

* feat: added bot remove method

* chore: added csrfCaptchaSchema

* deps: update

* fix: some error at callback

* fix(deps): pin dependency abort-controller to 3.0.0 (#313)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency @types/node-fetch to v2.5.10 (#316)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency eslint-plugin-react to v7.23.2 (#317)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* style: fixed for deepscan

* chore: improved user login interaction

* fix(deps): update dependency @sentry/webpack-plugin to v1.15.0 (#318)

* chore(deps): update dependency eslint to v7.24.0 (#320)

* fix(deps): update dependency postcss to v8.2.10 (#321)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* ci: updated ci stuff

* style: removed unnecessary script

* fix: not using SENTRY_RELEASE env

* chore: defaulting mysql password

* chore: added sentry_dsn env and only uploading for master

* ci: updated trigger

* ci: passing source branch env only at push

* chore(deps): update typescript-eslint monorepo to v4.22.0 (#322)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency eslint-config-prettier to v8.2.0 (#323)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency @types/react-select to v4.0.15 (#325)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency eslint-plugin-prettier to v3.4.0 (#326)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency @types/sanitize-html to v2 (#328)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency @types/node to v14.14.41 (#324)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency ts-jest to v26.5.5 (#327)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* ci: debugging

* Update components/DeveloperLayout.tsx

Co-authored-by: zero734kr <zero734kr@gmail.com>

* Update components/Loader.tsx

Co-authored-by: zero734kr <zero734kr@gmail.com>

* Update components/ColorCard.tsx

Co-authored-by: zero734kr <zero734kr@gmail.com>

* Update components/ColorCard.tsx

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix(deps): update dependency core-js to v3.11.0 (#329)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency @types/sanitize-html to v2.3.1 (#330)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix(deps): update dependency postcss to v8.2.13 (#333)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix(deps): update dependency tailwindcss to v2.1.2 (#334)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency eslint to v7.25.0 (#335)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix(deps): update dependency @sentry/webpack-plugin to v1.15.1 (#332)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency @types/node to v14.14.43 (#339)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency @types/jest to v26.0.23 (#337)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency eslint-config-prettier to v8.3.0 (#336)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency @types/react to v17.0.4 (#338)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* Update utils/ShowdownExtensions.ts

Co-authored-by: zero734kr <zero734kr@gmail.com>

* style: fixed some styles

* chore: updated api-docs git

* refactor: made change on sentry

* style: removed debug code

* deps: removed node-mock

* ci: removed env

* style: code style

* test: module names

* chore: docker using python

* chore: docker using build-base

* ci: fixed syntax error

* chore: changed sql type

* feat: added vote

* fix: version for v1

* feat: added v1 bot vote check

* feat: clearing cache for deleted bot

* chore: delete bot real working

IMPORTANT: NOW DELETE BOT REAL WORKS!

* fix: router called at non-client

* style: removed space

* feat: added vote check endpoint

* fix: router called at non-client

* fix(deps): update sentry monorepo to v6.3.5 (#331)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix(deps): update dependency core-js to v3.11.2 (#340)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency @types/react to v17.0.5 (#345)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update typescript-eslint monorepo to v4.22.1 (#343)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix: BotCard button component rendered as Tag

* feat: update docs

* feat: using koreanbots cdn for og image

* fix: missing querystring label

* docs: some text change

https://github.com/koreanbots/v2-testing/issues/72#issuecomment-807929228

* fix: removed unexpected char

close: https://github.com/koreanbots/v2-testing/issues/76

* fix: redirecting at serverside

* fix(deps): pin dependencies (#342)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency ts-jest to v26.5.6 (#347)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix(deps): update dependency postcss to v8.2.14 (#349)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix(deps): update dependency core-js to v3.11.3 (#348)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix: router instance called at serverside while rendering

* Merge branch 'master' of https://github.com/koreanbots/koreanbots

* feat: Sentry enabled only at production

* fix: menu not closing

close: https://github.com/koreanbots/v2-testing/issues/50

* chore: improved mobile design

* fix: tooltip overflows screen

close: https://github.com/koreanbots/v2-testing/issues/28

* fix: router called at server-side

close: https://github.com/koreanbots/v2-testing/issues/77

* typo: fixed typo issue

* typo: improved typo

* fix: router called at serverside

* chore: removed custom scrollbar style

* style: fixed null checks

* feat: added owner transfer and edit

* chore: clearing cache for updates

* chore: redirecting on update

* chore: added button margin

* feat: disabled webhook

* chore: added some spaces

* feat: added padding for ad

* feat: remove wave

* feat: added security page

* chore: some margin

* feat: added bug reporters

* style: fixed eslint

* fix(developers): https://github.com/koreanbots/v2-testing/issues/74

* chore: improved ad

* feat: migrated to @sentry/nextjs

* fix: card invite button fixed

* chore: not releasing

* chore: debugging

* chore: skiping sentry auto release

* feat: added docker hub build hook

* fix: docker hook

* fix: docker hook geting sentry dsn as build-arg

* chore: added sentry envs

* chore(docker): cleanup

* fix: bugs at card

* typo: fixed

* chore: margin top at message

* fix: card building weird

* fix: sentry disabled

* fix: query string invalid

fix: https://github.com/koreanbots/v2-testing/issues/92

* fix: https://github.com/koreanbots/v2-testing/issues/94

* chore: improved style

close: https://github.com/koreanbots/v2-testing/issues/83

* fix: scrollbar shown even its not overflowed

fix: https://github.com/koreanbots/v2-testing/issues/86

* fix: home not displayed at dev portal

fix: https://github.com/koreanbots/v2-testing/issues/84

* types: searchParams is optional prop

* feat: added required field notice

close: https://github.com/koreanbots/v2-testing/issues/90

* typo: fixed typo issues

For https://github.com/koreanbots/v2-testing/issues/79

* fix: causing error on other git url

ISSUE: https://sentry.io/share/issue/a13341dc1aab4e5aa994fee8857afff7/

* fix: handle AbortError

* chore(deps): update dependency eslint to v7.26.0 (#353)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix(deps): update dependency core-js to v3.12.1 (#350)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore: reordered bot section

* typo: fixed typo issue

from https://github.com/koreanbots/v2-testing/issues/79

* feat: opening new tab for discord link

close: https://github.com/koreanbots/v2-testing/issues/99

* feat: added opensearch

* Update renovate.json

* chore: prevent clickjacking

* chore: added moz SearchForm for opensearch xml

* fix(deps): update dependency rc-tooltip to v5 (#351)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix(deps): update sentry monorepo to v6.3.6 (#354)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency prettier to v2.3.0 (#355)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update typescript-eslint monorepo to v4.23.0 (#356)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix(deps): update dependency postcss to v8.2.15 (#357)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix(deps): update dependency react-select to v4.3.1 (#358)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* fix(deps): update dependency knex to v0.95.5 (#359)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* style: added space

* feat: added get botSubmits list api

* chore: updated endpoint

* typo: fixed and improved typo issues

* chore: improved message for empty category

close: https://github.com/koreanbots/v2-testing/issues/100

* feat: support pwa

* types: added missing typing

* chore: changed manifest

* fix: catching error for ga blocked

* fix: added missing argument

* chore: made some changes

* style: could be null

* chore: improved pwa

* fix: https://github.com/koreanbots/v2-testing/issues/105

* feat: added staff missing permission

* fix: https://github.com/koreanbots/v2-testing/issues/104

* feat: added width style

* Update pages/_app.tsx

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* style: suggestions at review

* feat: updated api-docs

* chore: added dest option

* chore: changed icon path

* feat: just commiting service worker

* feat: added bug bounty group

* ci: removed reviewdog

* feat: added google optimize

* chore: added maskable icon and changed short_name

* ci: made some changes on renovate

* Create SECURITY.md

* feat: fetching docs from github

* feat: added tos at footer

* feat(iOS): added pwa splash screen

* types: improved component typing

* feat: discord rebranded

* ci: configured renovate ignore

* [TYPO] 기여 규칙 링크 수정 (#367)

* fix(deps): update sentry monorepo to v6.4.0 (#364)

* feat: added logging

* style: reordered import

* feat: improved logging

* feat: private api changes

* feat: added OG

* chore: updated migrate.sql

* ci: updated renovate

* fix: seo

* feat: added approve api

* chore: some changes at deny

* feat: added approve

* refactor: using next-seo for seo

* ci(renovate): removed unused option

* chore: not passing pwa at navbar

* style: removed line break

* fix: https://github.com/koreanbots/v2-testing/issues/89

* feat: directly fetching from discord

* feat: support searching with index

* style: fix deepscan

* fix: invalid avatar url

* fix: https://github.com/koreanbots/v2-testing/issues/110

reopen: https://github.com/koreanbots/v2-testing/issues/89

* feat: added error message at submit button

* fix: https://github.com/koreanbots/v2-testing/issues/89

* feat: added deny presets article

* feat: added query aliases

* chore: update docs

* chore: remvoed empty file

* feat: increased ratelimit

* feat: added bot lists

* style: removed unused variable

* fix(deps): update dependency knex to v0.95.6 (#365)

* chore(deps): update typescript-eslint monorepo to v4.24.0 (#366)

* chore(deps): update dependency @types/react to v17.0.6 (#368)

* fix(deps): update dependency formik to v2.2.8 (#369)

* fix(deps): update dependency next to v10.2.2 (#370)

* fix(deps): update sentry monorepo to v6.4.1 (#371)

* fix(deps): update dependency sanitize-html to v2.4.0 (#372)

* fix(deps): update dependency postcss to v8.3.0 (#373)

* docs: updated license

* feat: added refresh data

* feat: better image size

close: https://github.com/koreanbots/v2-testing/issues/81

* chore: changed slogan

* fix: invalid v1 api

* fix: forbidden error

* feat: added char count at textarea

close: https://github.com/koreanbots/v2-testing/issues/112

* feat: changed edit page route

* fix(deps): update dependency next to v10.2.3 (#376)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency typescript to v4.3.2 (#383)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* chore(deps): update dependency eslint to v7.27.0 (#374)

Co-authored-by: Renovate Bot <bot@renovateapp.com>

* deps: removed core-js

* deps: lock updated

* feat: added stable docker compose file

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: zero734kr <zero734kr@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: MintyU <deathcat@outlook.kr>
2021-05-27 22:25:29 +09:00

509 lines
18 KiB
TypeScript

import fetch from 'node-fetch'
import { TLRU } from 'tlru'
import DataLoader from 'dataloader'
import { User as DiscordUser } from 'discord.js'
import { Bot, User, ListType, BotList, TokenRegister, BotFlags, DiscordUserFlags, SubmittedBot } from '@types'
import { categories, imageSafeHost, SpecialEndPoints, VOTE_COOLDOWN } from './Constants'
import knex from './Knex'
import { DiscordBot, getMainGuild } from './DiscordBot'
import { sign, verify } from './Jwt'
import { camoUrl, formData, serialize } from './Tools'
import { AddBotSubmit, ManageBot } from './Yup'
import { markdownImage } from './Regex'
export const imageRateLimit = new TLRU<unknown, number>({ maxAgeMs: 60000 })
async function getBot(id: string, owners=true):Promise<Bot> {
const res = await knex('bots')
.select([
'id',
'flags',
'owners',
'lib',
'prefix',
'votes',
'servers',
'intro',
'desc',
'web',
'git',
'url',
'category',
'status',
'trusted',
'partnered',
'discord',
'state',
'vanity',
'bg',
'banner',
])
.where({ id })
.orWhere({ vanity: id, trusted: true })
.orWhere({ vanity: id, partnered: true })
if (res[0]) {
const discordBot = await DiscordBot.users.fetch(res[0].id)
await getMainGuild()?.members?.fetch(res[0].id).catch(e=> e)
if(!discordBot) return null
res[0].flags = res[0].flags | (discordBot.flags && DiscordUserFlags.VERIFIED_BOT ? BotFlags.verified : 0) | (res[0].trusted ? BotFlags.trusted : 0) | (res[0].partnered ? BotFlags.partnered : 0)
res[0].tag = discordBot.discriminator
res[0].avatar = discordBot.avatar
res[0].name = discordBot.username
res[0].category = JSON.parse(res[0].category)
res[0].owners = JSON.parse(res[0].owners)
res[0].status = discordBot.presence?.activities?.find(r => r.type === 'STREAMING') ? 'streaming' : discordBot.presence?.status || null
delete res[0].trusted
delete res[0].partnered
if (owners)
{
res[0].owners = await Promise.all(
res[0].owners.map(async (u: string) => await get._rawUser.load(u))
)
res[0].owners = res[0].owners.filter((el: User | null) => el).map((row: User) => ({ ...row }))
}
}
return res[0] ?? null
}
async function getUser(id: string, bots = true):Promise<User> {
const res = await knex('users')
.select(['id', 'flags', 'github'])
.where({ id })
if (res[0]) {
const owned = await knex('bots')
.select(['id'])
.where('owners', 'like', `%${id}%`)
const discordUser = await get.discord.user.load(id)
res[0].tag = discordUser?.discriminator || '0000'
res[0].username = discordUser?.username || 'Unknown User'
if (bots) {
res[0].bots = await Promise.all(owned.map(async b => await get._rawBot.load(b.id)))
res[0].bots = res[0].bots.filter((el: Bot | null) => el).map((row: User) => ({ ...row }))
}
else res[0].bots = owned.map(el => el.id)
}
return res[0] || null
}
async function getBotList(type: ListType, page = 1, query?: string):Promise<BotList> {
let res: { id: string }[]
let count:string|number
if (type === 'VOTE') {
count = (await knex('bots').count())[0]['count(*)']
res = await knex('bots')
.orderBy('votes', 'desc')
.orderBy('servers', 'desc')
.limit(16)
.offset(((page ? Number(page) : 1) - 1) * 16)
.select(['id'])
} else if (type === 'TRUSTED') {
count = (
await knex('bots')
.where({ trusted: true })
.count()
)[0]['count(*)']
res = await knex('bots')
.where({ trusted: true })
.orderByRaw('RAND()')
.limit(16)
.offset(((page ? Number(page) : 1) - 1) * 16)
.select(['id'])
} else if (type === 'NEW') {
count = (
await knex('bots')
.count()
)[0]['count(*)']
res = await knex('bots')
.orderBy('date', 'desc')
.limit(16)
.offset(((page ? Number(page) : 1) - 1) * 16)
.select(['id'])
} else if (type === 'PARTNERED') {
count = (
await knex('bots')
.where({ partnered: true })
.count()
)[0]['count(*)']
res = await knex('bots')
.where({ partnered: true })
.orderByRaw('RAND()')
.limit(16)
.offset(((page ? Number(page) : 1) - 1) * 16)
.select(['id'])
} else if (type === 'CATEGORY') {
if (!query) throw new Error('쿼리가 누락되었습니다.')
if (!categories.includes(query)) throw new Error('알 수 없는 카테고리입니다.')
count = (
await knex('bots')
.where('category', 'like', `%${decodeURI(query)}%`)
.count()
)[0]['count(*)']
res = await knex('bots')
.where('category', 'like', `%${decodeURI(query)}%`)
.orderBy('votes', 'desc')
.orderBy('servers', 'desc')
.limit(16)
.offset(((page ? Number(page) : 1) - 1) * 16)
.select(['id'])
} else if (type === 'SEARCH') {
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]
} else {
count = 1
res = []
}
return { type, data: (await Promise.all(res.map(async el => await getBot(el.id)))).map(r=> ({...r})), currentPage: page, totalPage: Math.ceil(Number(count) / 16) }
}
async function getBotSubmit(id: string, date: number): Promise<SubmittedBot> {
const res = await knex('submitted').select(['id', 'date', 'category', 'lib', 'prefix', 'intro', 'desc', 'url', 'web', 'git', 'discord', 'state', 'owners', 'reason']).where({ id, date })
if(res.length === 0) return null
res[0].category = JSON.parse(res[0].category)
res[0].owners = await Promise.all(JSON.parse(res[0].owners).map(async (u: string)=> await get.user.load(u)))
return res[0]
}
async function getBotSubmits(id: string): Promise<SubmittedBot[]> {
if(!id) return []
let res = await knex('submitted').select(['id', 'date', 'category', 'lib', 'prefix', 'intro', 'desc', 'url', 'web', 'git', 'discord', 'state', 'owners', 'reason']).orderBy('date', 'desc').where('owners', 'LIKE', `%${id}%`)
res = await Promise.all(res.map(async el=> {
el.category = JSON.parse(el.category)
el.owners = await Promise.all(JSON.parse(el.owners).map(async (u: string)=> await get.user.load(u)))
return el
}))
return res
}
/**
* @param userID
* @param botID
* @returns Timestamp
*/
async function getBotVote(userID: string, botID: string): Promise<number|null> {
const user = await knex('users').select(['votes']).where({ id: userID })
if(user.length === 0) return null
const data = JSON.parse(user[0].votes)
return data[botID] || 0
}
async function voteBot(userID: string, botID: string): Promise<number|boolean> {
const user = await knex('users').select(['votes']).where({ id: userID })
if(user.length === 0) return null
const date = +new Date()
const data = JSON.parse(user[0].votes)
const lastDate = data[botID] || 0
if(date - lastDate < VOTE_COOLDOWN) return VOTE_COOLDOWN - (date - lastDate)
data[botID] = date
await knex('bots').where({ id: botID }).increment('votes', 1)
await knex('users').where({ id: userID }).update({ votes: JSON.stringify(data) })
return true
}
/**
* @returns 1 - Has pending Bots
* @returns 2 - Already submitted ID
* @returns 3 - Bot User does not exists
* @returns 4 - Discord not Joined
* @returns obj - Success
*/
async function submitBot(id: string, data: AddBotSubmit):Promise<1|2|3|4|SubmittedBot> {
const submits = await knex('submitted').select(['id']).where({ state: 0 }).andWhere('owners', 'LIKE', `%${id}%`)
if(submits.length > 1) return 1
const botId = data.id
const date = Math.round(+new Date()/1000)
const sameID = await knex('submitted').select(['id']).where({ id: botId, state: 0 })
const bot = await get.bot.load(data.id)
if(sameID.length !== 0 || bot) return 2
const user = await DiscordBot.users.fetch(data.id)
if(!user) return 3
const member = await getMainGuild().members.fetch(id).then(() => true).catch(() => false)
if(!member) return 4
await knex('submitted').insert({
id: botId,
date: date,
owners: JSON.stringify([ id ]),
lib: data.library,
prefix: data.prefix,
intro: data.intro,
desc: data.desc,
web: data.website,
git: data.git,
url: data.url,
category: JSON.stringify(data.category),
discord: data.discord,
state: 0
})
return await getBotSubmit(botId, date)
}
async function getBotSpec(id: string, userID: string) {
const res = await knex('bots').select(['id', 'token', 'webhook']).where({ id }).andWhere('owners', 'like', `%${userID}%`)
if(!res[0]) return null
return serialize(res[0])
}
async function deleteBot(id: string): Promise<boolean> {
const bot = await knex('bots').where({ id }).del()
get.bot.clear(id)
return !!bot
}
async function updateBot(id: string, data: ManageBot) {
const res = await knex('bots').where({ id })
if(res.length === 0 || res[0].state !== 'ok') return 0
await knex('bots').update({
id,
prefix: data.prefix,
lib: data.library,
web: data.website,
git: data.git,
url: data.url,
discord: data.discord,
category: JSON.stringify(data.category),
intro: data.intro,
desc: data.desc
}).where({ id })
return 1
}
/**
* @returns 1 - Limit of 100k servers
* @returns 2 - Limit of 10M servers
*/
async function updateServer(id: string, servers: number) {
const bot = await get.bot.load(id)
if(bot.servers < 10000 && servers >= 10000) return 1
if(bot.servers < 1000000 && servers >= 1000000) return 2
await knex('bots').update({ servers }).where({ id })
return
}
async function updateBotApplication(id: string, value: { webhook: string }) {
const bot = await knex('bots').update({ webhook: value.webhook }).where({ id })
if(bot !== 1) return false
return true
}
async function updateOwner(id: string, owners: string[]): Promise<void> {
await knex('bots').where({ id }).update({ owners: JSON.stringify(owners) })
get.bot.clear(id)
}
async function resetBotToken(id: string, beforeToken: string) {
const token = sign({ id })
const bot = await knex('bots').update({ token }).where({ id, token: beforeToken })
if(bot !== 1) return null
return token
}
async function Github(id: string, github: string) {
const user = await knex('users').where({ github }).whereNot({ id })
if(github && user.length !== 0) return 0
await knex('users').update({ github }).where({ id })
return 1
}
async function getImage(url: string) {
const res = await fetch(url)
if(!res.ok) return null
return await res.buffer()
}
async function getDiscordUser(id: string):Promise<DiscordUser> {
return DiscordBot.users.cache.get(id) ?? await DiscordBot.users.fetch(id, false, true).then(u => u.toJSON()).catch(()=>null)
}
async function assignToken(info: TokenRegister):Promise<string> {
let token = await knex('users').select(['token']).where({ id: info.id })
let t: string
if(token.length === 0) {
t = sign({ id: info.id }, { expiresIn: '30d' })
await knex('users').insert({ token: t, date: Math.round(Number(new Date()) / 1000), id: info.id, email: info.email, tag: info.discriminator, username: info.username, discord: sign({ access_token: info.access_token, expires_in: info.expires_in, refresh_token: info.refresh_token }) })
token = await knex('users').select(['token']).where({ id: info.id })
} else await knex('users').update({ email: info.email, tag: info.discriminator, username: info.username, discord: sign({ access_token: info.access_token, expires_in: info.expires_in, refresh_token: info.refresh_token }) }).where({ id: info.id })
if(!verify(token[0]?.token ?? '')) {
t = sign({ id: info.id }, { expiresIn: '30d' })
await knex('users').update({ token: t }).where({ id: info.id })
} else t = token[0].token
return t
}
async function Authorization(token: string):Promise<string|false> {
const tokenInfo = verify(token ?? '')
const user = await knex('users').select(['id']).where({ id: tokenInfo?.id ?? '', token: token ?? '' })
if(user.length === 0) return false
else return user[0].id
}
async function BotAuthorization(token: string):Promise<string|false> {
const tokenInfo = verify(token ?? '')
const bot = await knex('bots').select(['id']).where({ id: tokenInfo?.id ?? '', token: token ?? '' })
if(bot.length === 0) return false
else return bot[0].id
}
async function addRequest(ip: string, map: TLRU<unknown, number>) {
if(!map.has(ip)) map.set(ip, 0)
map.set(ip, map.get(ip) + 1)
}
export async function CaptchaVerify(response: string): Promise<boolean> {
const res:{ success: boolean } = await fetch(SpecialEndPoints.HCaptcha.Verify, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: formData({
response,
secret: process.env.HCAPTCHA_KEY
})
}).then(r=> r.json())
return res.success
}
// Private APIs
async function getBotSubmitList() {
const res = await knex('submitted').select(['id', 'date']).where({ state: 0 })
return await Promise.all(res.map(b => get.botSubmit.load(JSON.stringify({ id: b.id, date: b.date }))))
}
async function denyBotSubmission(id: string, date: number, reason?: string) {
await knex('submitted').update({ state: 2, reason: reason || null }).where({ state: 0, id, date })
}
async function approveBotSubmission(id: string, date: number) {
const data = await knex('submitted').select(['id', 'date', 'category', 'lib', 'prefix', 'intro', 'desc', 'url', 'web', 'git', 'discord', 'state', 'owners', 'reason']).where({ state: 0, id, date })
if(!data[0]) return false
await knex('submitted').where({ state: 0, id, date }).update({ state: 1 })
await knex('bots').insert({ id, date, owners: data[0].owners, lib: data[0].lib, prefix: data[0].prefix, intro: data[0].intro, desc: data[0].desc, url: data[0].url, web: data[0].web, git: data[0].git, category: data[0].category, discord: data[0].discord, token: sign({ id }) })
return true
}
export const get = {
discord: {
user: new DataLoader(
async (ids: string[]) =>
(await Promise.all(ids.map(async (id: string) => await getDiscordUser(id))))
, { cacheMap: new TLRU({ maxStoreSize: 5000, maxAgeMs: 43200000 }) }),
},
bot: new DataLoader(
async (ids: string[]) =>
(await Promise.all(ids.map(async (el: string) => await getBot(el)))).map(row => serialize(row))
, { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 60000 }) }),
_rawBot: new DataLoader(
async (ids: string[]) =>
(await Promise.all(ids.map(async (el: string) => await getBot(el, false)))).map(row => serialize(row))
, { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 60000 }) }),
botDescSafe: async (id: string) => {
const bot = await get.bot.load(id)
return bot?.desc.replace(markdownImage, (matches: string, alt: string|undefined, link: string|undefined, description: string|undefined): string => {
try {
const url = new URL(link)
return `![${alt || description || ''}](${imageSafeHost.find(el => url.host.endsWith(el)) ? link : camoUrl(link) })`
} catch {
return matches
}
}) || null
},
user: new DataLoader(
async (ids: string[]) =>
(await Promise.all(ids.map(async (el: string) => await getUser(el)))).map(row => serialize(row))
, { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 60000 }) }),
_rawUser: new DataLoader(
async (ids: string[]) =>
(await Promise.all(ids.map(async (el: string) => await getUser(el, false)))).map(row => serialize(row))
, { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 60000 }) }),
botSubmits: new DataLoader(
async (ids: string[]) =>
(await Promise.all(ids.map(async (el: string) => await getBotSubmits(el)))).map(row => serialize(row))
, { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 60000 }) }),
botSubmit: new DataLoader(
async (key: string[]) =>
(await Promise.all(key.map(async (el: string) => {
const json = JSON.parse(el)
return await getBotSubmit(json.id, json.date)
}))).map(row => serialize(row))
, { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 60000 }) }),
botSpec: getBotSpec,
list: {
category: new DataLoader(
async (key: string[]) =>
(await Promise.all(key.map(async (k: string) => {
const json = JSON.parse(k)
return await getBotList('CATEGORY', json.page, json.category)
}))).map(row => serialize(row))
, { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 3000000 }) }),
search: new DataLoader(
async (key: string[]) =>
(await Promise.all(key.map(async (k: string) => {
const json = JSON.parse(k)
const res = await getBotList('SEARCH', json.page, json.query)
return { ...res, totalPage: Number(res.totalPage), currentPage: Number(res.currentPage) }
}))).map(row => serialize(row))
, { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 3000000 }) }),
votes: new DataLoader(
async (pages: number[]) =>
(await Promise.all(pages.map(async (page: number) => await getBotList('VOTE', page)))).map(row => serialize(row))
, { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 3000000 }) }),
new: new DataLoader(
async (pages: number[]) =>
(await Promise.all(pages.map(async (page: number) => await getBotList('NEW', page)))).map(row => serialize(row))
, { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 1800000 }) }),
trusted: new DataLoader(
async (pages: number[]) =>
(await Promise.all(pages.map(async (page: number) => await getBotList('TRUSTED', page)))).map(row => serialize(row))
, { cacheMap: new TLRU({ maxStoreSize: 50, maxAgeMs: 3600000 }) }),
},
images: {
user: new DataLoader(
async (urls: string[]) =>
(await Promise.all(urls.map(async (url: string) => await getImage(url))))
, { cacheMap: new TLRU({ maxStoreSize: 500, maxAgeMs: 3600000 }) }),
},
botVote: getBotVote,
Authorization,
BotAuthorization,
botSubmitList: getBotSubmitList
}
export const update = {
assignToken,
updateBotApplication,
resetBotToken,
updateServer,
Github,
bot: updateBot,
botOwners: updateOwner,
denyBotSubmission,
approveBotSubmission
}
export const put = {
voteBot,
submitBot
}
export const remove = {
bot: deleteBot
}
export const ratelimit = {
image: (ip: string) => {
addRequest(ip, imageRateLimit)
return imageRateLimit.get(ip)
}
}