feat: add api route builder
This commit is contained in:
		
							parent
							
								
									9c46287e61
								
							
						
					
					
						commit
						5fae38fce6
					
				
					 2 changed files with 103 additions and 47 deletions
				
			
		|  | @ -51,15 +51,61 @@ export interface RateLimit { | ||||||
|   bucket: string | null |   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 { | export class RESTManager { | ||||||
|   client?: Client |   client?: Client | ||||||
|   queues: { [key: string]: QueuedItem[] } = {} |   queues: { [key: string]: QueuedItem[] } = {} | ||||||
|   rateLimits = new Collection<string, RateLimit>() |   rateLimits = new Collection<string, RateLimit>() | ||||||
|   globalRateLimit: boolean = false |   globalRateLimit: boolean = false | ||||||
|   processing: boolean = false |   processing: boolean = false | ||||||
|  |   version: number = 8 | ||||||
|  |   api: APIMap | ||||||
| 
 | 
 | ||||||
|   constructor(client?: Client) { |   constructor(client?: Client) { | ||||||
|     this.client = client |     this.client = client | ||||||
|  |     this.api = builder(this) | ||||||
|     // eslint-disable-next-line @typescript-eslint/no-floating-promises
 |     // eslint-disable-next-line @typescript-eslint/no-floating-promises
 | ||||||
|     this.handleRateLimits() |     this.handleRateLimits() | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  | @ -7,7 +7,8 @@ import { | ||||||
|   groupslash, |   groupslash, | ||||||
|   CommandContext, |   CommandContext, | ||||||
|   Extension, |   Extension, | ||||||
|   Collection |   Collection, | ||||||
|  |   SlashCommandOptionType | ||||||
| } from '../../mod.ts' | } from '../../mod.ts' | ||||||
| import { LL_IP, LL_PASS, LL_PORT, TOKEN } from './config.ts' | import { LL_IP, LL_PASS, LL_PORT, TOKEN } from './config.ts' | ||||||
| import { | import { | ||||||
|  | @ -60,12 +61,12 @@ class MyClient extends CommandClient { | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   @subslash('cmd', 'sub-cmd-no-grp') |   @subslash('cmd', 'sub-cmd-no-grp') | ||||||
|   subCmdNoGrp(d: Interaction): void { |   subCmdNoGroup(d: Interaction): void { | ||||||
|     d.respond({ content: 'sub-cmd-no-group worked' }) |     d.respond({ content: 'sub-cmd-no-group worked' }) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   @groupslash('cmd', 'sub-cmd-group', 'sub-cmd') |   @groupslash('cmd', 'sub-cmd-group', 'sub-cmd') | ||||||
|   subCmdGrp(d: Interaction): void { |   subCmdGroup(d: Interaction): void { | ||||||
|     d.respond({ content: 'sub-cmd-group worked' }) |     d.respond({ content: 'sub-cmd-group worked' }) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  | @ -74,53 +75,62 @@ class MyClient extends CommandClient { | ||||||
|     console.log(d.name) |     console.log(d.name) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   @event() | ||||||
|  |   raw(evt: string, d: any): void { | ||||||
|  |     if (!evt.startsWith('APPLICATION')) return | ||||||
|  |     console.log(evt, d) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   @event() |   @event() | ||||||
|   ready(): void { |   ready(): void { | ||||||
|     console.log(`Logged in as ${this.user?.tag}!`) |     console.log(`Logged in as ${this.user?.tag}!`) | ||||||
|     this.manager.init(this.user?.id as string) |     this.manager.init(this.user?.id as string) | ||||||
|     // client.slash.commands.create(
 | 
 | ||||||
|     //   {
 |     // this.rest.api.users['422957901716652033'].get().then(console.log)
 | ||||||
|     //     name: 'cmd',
 |     client.slash.commands.create( | ||||||
|     //     description: 'Parent command',
 |       { | ||||||
|     //     options: [
 |         name: 'cmd', | ||||||
|     //       {
 |         description: 'Parent command!', | ||||||
|     //         name: 'sub-cmd-group',
 |         options: [ | ||||||
|     //         type: SlashCommandOptionType.SUB_COMMAND_GROUP,
 |           { | ||||||
|     //         description: 'Sub Cmd Group',
 |             name: 'sub-cmd-group', | ||||||
|     //         options: [
 |             type: SlashCommandOptionType.SUB_COMMAND_GROUP, | ||||||
|     //           {
 |             description: 'Sub Cmd Group', | ||||||
|     //             name: 'sub-cmd',
 |             options: [ | ||||||
|     //             type: SlashCommandOptionType.SUB_COMMAND,
 |               { | ||||||
|     //             description: 'Sub Cmd'
 |                 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-no-grp', | ||||||
|     //       },
 |             type: SlashCommandOptionType.SUB_COMMAND, | ||||||
|     //       {
 |             description: 'Sub Cmd' | ||||||
|     //         name: 'sub-cmd-grp-2',
 |           }, | ||||||
|     //         type: SlashCommandOptionType.SUB_COMMAND_GROUP,
 |           { | ||||||
|     //         description: 'Sub Cmd Group 2',
 |             name: 'sub-cmd-grp-2', | ||||||
|     //         options: [
 |             type: SlashCommandOptionType.SUB_COMMAND_GROUP, | ||||||
|     //           {
 |             description: 'Sub Cmd Group 2', | ||||||
|     //             name: 'sub-cmd-1',
 |             options: [ | ||||||
|     //             type: SlashCommandOptionType.SUB_COMMAND,
 |               { | ||||||
|     //             description: 'Sub Cmd 1'
 |                 name: 'sub-cmd-1', | ||||||
|     //           },
 |                 type: SlashCommandOptionType.SUB_COMMAND, | ||||||
|     //           {
 |                 description: 'Sub Cmd 1' | ||||||
|     //             name: 'sub-cmd-2',
 |               }, | ||||||
|     //             type: SlashCommandOptionType.SUB_COMMAND,
 |               { | ||||||
|     //             description: 'Sub Cmd 2'
 |                 name: 'sub-cmd-2', | ||||||
|     //           }
 |                 type: SlashCommandOptionType.SUB_COMMAND, | ||||||
|     //         ]
 |                 description: 'Sub Cmd 2' | ||||||
|     //       }
 |               } | ||||||
|     //     ]
 |             ] | ||||||
|     //   },
 |           } | ||||||
|     //   '783319033205751809'
 |         ] | ||||||
|     // )
 |       }, | ||||||
|  |       '783319033205751809' | ||||||
|  |     ) | ||||||
|  |     // client.slash.commands.delete('788719077329207296', '783319033205751809')
 | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -166,7 +176,7 @@ class VCExtension extends Extension { | ||||||
| 
 | 
 | ||||||
|     await player.play(track) |     await player.play(track) | ||||||
| 
 | 
 | ||||||
|     ctx.channel.send(`Now playing ${info.title}!`) |     ctx.channel.send(`Now playing ${info.title}!\nDebug Track: ${track}`) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   @command() |   @command() | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue