mirror of
https://github.com/koreanbots/core.git
synced 2025-12-16 06:20:24 +00:00
feat: added assignToken method
This commit is contained in:
parent
cc525def5f
commit
c8ae254c33
@ -1,23 +1,19 @@
|
|||||||
import fetch from 'node-fetch'
|
import fetch from 'node-fetch'
|
||||||
// import jwt from 'jsonwebtoken'
|
|
||||||
// import fs from 'fs'
|
|
||||||
import { TLRU } from 'tlru'
|
import { TLRU } from 'tlru'
|
||||||
import StreamCache from 'stream-cache'
|
import StreamCache from 'stream-cache'
|
||||||
import DataLoader from 'dataloader'
|
import DataLoader from 'dataloader'
|
||||||
import { User as DiscordUser } from 'discord.js'
|
import { User as DiscordUser } from 'discord.js'
|
||||||
import { Stream } from 'stream'
|
import { Stream } from 'stream'
|
||||||
|
|
||||||
import { Bot, User, ListType, BotList } from '@types'
|
import { Bot, User, ListType, BotList, TokenRegister } from '@types'
|
||||||
import { cats } from './Constants'
|
import { cats } from './Constants'
|
||||||
|
|
||||||
import knex from './Knex'
|
import knex from './Knex'
|
||||||
import DiscordBot from './DiscordBot'
|
import DiscordBot from './DiscordBot'
|
||||||
|
import { sign, verify } from './Jwt'
|
||||||
|
|
||||||
export const imageRateLimit = new TLRU<unknown, number>({ maxAgeMs: 60000 })
|
export const imageRateLimit = new TLRU<unknown, number>({ maxAgeMs: 60000 })
|
||||||
|
|
||||||
// const publicPem = fs.readFileSync('./public.pem')
|
|
||||||
// const privateKey = fs.readFileSync('./private.key')
|
|
||||||
|
|
||||||
async function getBot(id: string, owners=true) {
|
async function getBot(id: string, owners=true) {
|
||||||
const res = await knex('bots')
|
const res = await knex('bots')
|
||||||
.select([
|
.select([
|
||||||
@ -210,6 +206,22 @@ async function getDiscordUser(id: string):Promise<DiscordUser> {
|
|||||||
return DiscordBot.users.cache.get(id) ?? await DiscordBot.users.fetch(id, false, true).then(u => u).catch(()=>null)
|
return DiscordBot.users.cache.get(id) ?? await DiscordBot.users.fetch(id, false, true).then(u => u).catch(()=>null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function assignToken(info: TokenRegister):Promise<string> {
|
||||||
|
const token = await knex('users').select(['token']).where({ id: info.id })
|
||||||
|
let t: string
|
||||||
|
if(token.length === 0) {
|
||||||
|
await knex('users').insert({ 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 }) })
|
||||||
|
} 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 })
|
||||||
|
console.log(verify(token[0]?.token))
|
||||||
|
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 addRequest(ip: string, map: TLRU<unknown, number>) {
|
async function addRequest(ip: string, map: TLRU<unknown, number>) {
|
||||||
if(!map.has(ip)) map.set(ip, 0)
|
if(!map.has(ip)) map.set(ip, 0)
|
||||||
map.set(ip, map.get(ip) + 1)
|
map.set(ip, map.get(ip) + 1)
|
||||||
@ -260,6 +272,10 @@ export const get = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const update = {
|
||||||
|
assignToken
|
||||||
|
}
|
||||||
|
|
||||||
export const ratelimit = {
|
export const ratelimit = {
|
||||||
image: (ip: string) => {
|
image: (ip: string) => {
|
||||||
addRequest(ip, imageRateLimit)
|
addRequest(ip, imageRateLimit)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user