feat: add api route builder

This commit is contained in:
DjDeveloperr 2020-12-20 14:12:24 +05:30
parent 9c46287e61
commit 5fae38fce6
2 changed files with 103 additions and 47 deletions

View File

@ -51,15 +51,61 @@ export interface RateLimit {
bucket: string | null
}
const METHODS = ['get', 'post', 'patch', 'put', 'delete', 'head']
export type MethodFunction = (
body?: unknown,
maxRetries?: number,
bucket?: string | null,
rawResponse?: boolean
) => Promise<any>
export interface APIMap extends MethodFunction {
get: APIMap
post: APIMap
patch: APIMap
put: APIMap
delete: APIMap
head: APIMap
[name: string]: APIMap
}
export const builder = (rest: RESTManager, acum = '/'): APIMap => {
const routes = {}
const proxy = new Proxy(routes, {
get: (_, p, __) => {
if (p === 'toString') return () => acum
if (METHODS.includes(String(p))) {
const method = ((rest as unknown) as {
[name: string]: MethodFunction
})[String(p)]
return async (...args: any[]) =>
await method.bind(rest)(
`${baseEndpoints.DISCORD_API_URL}/v${rest.version}${acum.substring(
0,
acum.length - 1
)}`,
...args
)
}
return builder(rest, acum + String(p) + '/')
}
})
return (proxy as unknown) as APIMap
}
export class RESTManager {
client?: Client
queues: { [key: string]: QueuedItem[] } = {}
rateLimits = new Collection<string, RateLimit>()
globalRateLimit: boolean = false
processing: boolean = false
version: number = 8
api: APIMap
constructor(client?: Client) {
this.client = client
this.api = builder(this)
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.handleRateLimits()
}

View File

@ -7,7 +7,8 @@ import {
groupslash,
CommandContext,
Extension,
Collection
Collection,
SlashCommandOptionType
} from '../../mod.ts'
import { LL_IP, LL_PASS, LL_PORT, TOKEN } from './config.ts'
import {
@ -60,12 +61,12 @@ class MyClient extends CommandClient {
}
@subslash('cmd', 'sub-cmd-no-grp')
subCmdNoGrp(d: Interaction): void {
subCmdNoGroup(d: Interaction): void {
d.respond({ content: 'sub-cmd-no-group worked' })
}
@groupslash('cmd', 'sub-cmd-group', 'sub-cmd')
subCmdGrp(d: Interaction): void {
subCmdGroup(d: Interaction): void {
d.respond({ content: 'sub-cmd-group worked' })
}
@ -74,53 +75,62 @@ class MyClient extends CommandClient {
console.log(d.name)
}
@event()
raw(evt: string, d: any): void {
if (!evt.startsWith('APPLICATION')) return
console.log(evt, d)
}
@event()
ready(): void {
console.log(`Logged in as ${this.user?.tag}!`)
this.manager.init(this.user?.id as string)
// client.slash.commands.create(
// {
// name: 'cmd',
// description: 'Parent command',
// options: [
// {
// name: 'sub-cmd-group',
// type: SlashCommandOptionType.SUB_COMMAND_GROUP,
// description: 'Sub Cmd Group',
// options: [
// {
// name: 'sub-cmd',
// type: SlashCommandOptionType.SUB_COMMAND,
// description: 'Sub Cmd'
// }
// ]
// },
// {
// name: 'sub-cmd-no-grp',
// type: SlashCommandOptionType.SUB_COMMAND,
// description: 'Sub Cmd'
// },
// {
// name: 'sub-cmd-grp-2',
// type: SlashCommandOptionType.SUB_COMMAND_GROUP,
// description: 'Sub Cmd Group 2',
// options: [
// {
// name: 'sub-cmd-1',
// type: SlashCommandOptionType.SUB_COMMAND,
// description: 'Sub Cmd 1'
// },
// {
// name: 'sub-cmd-2',
// type: SlashCommandOptionType.SUB_COMMAND,
// description: 'Sub Cmd 2'
// }
// ]
// }
// ]
// },
// '783319033205751809'
// )
// this.rest.api.users['422957901716652033'].get().then(console.log)
client.slash.commands.create(
{
name: 'cmd',
description: 'Parent command!',
options: [
{
name: 'sub-cmd-group',
type: SlashCommandOptionType.SUB_COMMAND_GROUP,
description: 'Sub Cmd Group',
options: [
{
name: 'sub-cmd',
type: SlashCommandOptionType.SUB_COMMAND,
description: 'Sub Cmd'
}
]
},
{
name: 'sub-cmd-no-grp',
type: SlashCommandOptionType.SUB_COMMAND,
description: 'Sub Cmd'
},
{
name: 'sub-cmd-grp-2',
type: SlashCommandOptionType.SUB_COMMAND_GROUP,
description: 'Sub Cmd Group 2',
options: [
{
name: 'sub-cmd-1',
type: SlashCommandOptionType.SUB_COMMAND,
description: 'Sub Cmd 1'
},
{
name: 'sub-cmd-2',
type: SlashCommandOptionType.SUB_COMMAND,
description: 'Sub Cmd 2'
}
]
}
]
},
'783319033205751809'
)
// client.slash.commands.delete('788719077329207296', '783319033205751809')
}
}
@ -166,7 +176,7 @@ class VCExtension extends Extension {
await player.play(track)
ctx.channel.send(`Now playing ${info.title}!`)
ctx.channel.send(`Now playing ${info.title}!\nDebug Track: ${track}`)
}
@command()