feat: added getUser, getBot Queries

This commit is contained in:
원더 2021-01-05 11:35:22 +09:00
parent c8570a7c7e
commit fcce01c9b7

View File

@ -3,6 +3,9 @@ import jwt from 'jsonwebtoken'
import fs from 'fs' import fs from 'fs'
import knsexy from 'knex' import knsexy from 'knex'
const publicPem = fs.readFileSync('./public.pem')
const privateKey = fs.readFileSync('./private.key')
export const knex = knsexy({ export const knex = knsexy({
client: 'mysql', client: 'mysql',
connection: { connection: {
@ -13,10 +16,29 @@ export const knex = knsexy({
}, },
}) })
export async function getBot(id: string) {} export async function getBot(id: string, owners=true) {
const res = await knex('bots').select(['id', 'owners', 'lib', 'prefix', 'votes', 'servers', 'intro', 'desc', 'web', 'git', 'url', 'category', 'status', 'name', 'avatar', 'tag', 'verified', 'trusted', 'partnered', 'discord', 'boosted', 'state', 'vanity', 'bg', 'banner']).where({ id }).orWhere({ vanity: id, boosted: 1 })
export async function getUser(id: string) { if(res[0]) {
const res = await knex('users') res[0].category = JSON.parse(res[0].category)
.select(['id', 'avatar', 'tag', 'username', 'perm', 'github']) res[0].owners = JSON.parse(res[0].owners)
.where({ id }) if(owners) res[0].owners = res[0].owners.map(async (u: string) => await getUser(u))
res[0].owners = await Promise.all(res[0].owners.filter((el: any)=> el))
res[0].vanity = res[0].vanity && ( res[0].boosted || res[0].trusted || res[0].partnered )
}
return res[0] || null
}
export async function getUser(id: string, bots=true) {
const res = await knex('users').select(['id', 'avatar', 'tag', 'username', 'perm', 'github']).where({ id })
if(res[0]) {
const owned = await knex('bots').select(['id']).where('owners', 'like', `%${id}%`)
if(bots) res[0].bots = owned.map(async b=> await getBot(b.id, false))
else res[0].bots = owned.map(async b=> b.id)
res[0].bots = await Promise.all(res[0].bots.filter(el=> el))
}
return res[0] || null
} }