Merge pull request #130 from DjDeveloperr/refactor
feat(docs): add jsdoc for deploy
This commit is contained in:
		
						commit
						229f5f079c
					
				
					 1 changed files with 52 additions and 3 deletions
				
			
		
							
								
								
									
										55
									
								
								deploy.ts
									
										
									
									
									
								
							
							
						
						
									
										55
									
								
								deploy.ts
									
										
									
									
									
								
							|  | @ -10,25 +10,46 @@ export interface DeploySlashInitOptions { | ||||||
|   env?: boolean |   env?: boolean | ||||||
|   publicKey?: string |   publicKey?: string | ||||||
|   token?: string |   token?: string | ||||||
|   id?: string |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | /** Current Slash Client being used to handle commands */ | ||||||
| let client: SlashClient | let client: SlashClient | ||||||
|  | /** Manage Slash Commands right in Deploy */ | ||||||
| let commands: SlashCommandsManager | let commands: SlashCommandsManager | ||||||
| 
 | 
 | ||||||
|  | /** | ||||||
|  |  * Initialize Slash Commands Handler for [Deno Deploy](https://deno.com/deploy).
 | ||||||
|  |  * Easily create Serverless Slash Commands on the fly. | ||||||
|  |  * | ||||||
|  |  * **Examples** | ||||||
|  |  * | ||||||
|  |  * ```ts
 | ||||||
|  |  * init({ | ||||||
|  |  *   publicKey: "my public key", | ||||||
|  |  *   token: "my bot's token", // only required if you want to manage slash commands in code
 | ||||||
|  |  * }) | ||||||
|  |  * ``` | ||||||
|  |  * | ||||||
|  |  * ```ts
 | ||||||
|  |  * // takes up `PUBLIC_KEY` and `TOKEN` from ENV
 | ||||||
|  |  * init({ env: true }) | ||||||
|  |  * ``` | ||||||
|  |  * | ||||||
|  |  * @param options Initialization options | ||||||
|  |  */ | ||||||
|  | export function init(options: { env: boolean }): void | ||||||
|  | export function init(options: { publicKey: string; token?: string }): void | ||||||
| export function init(options: DeploySlashInitOptions): void { | export function init(options: DeploySlashInitOptions): void { | ||||||
|   if (client !== undefined) throw new Error('Already initialized') |   if (client !== undefined) throw new Error('Already initialized') | ||||||
|   if (options.env === true) { |   if (options.env === true) { | ||||||
|     options.publicKey = Deno.env.get('PUBLIC_KEY') |     options.publicKey = Deno.env.get('PUBLIC_KEY') | ||||||
|     options.token = Deno.env.get('TOKEN') |     options.token = Deno.env.get('TOKEN') | ||||||
|     options.id = Deno.env.get('ID') |  | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   if (options.publicKey === undefined) |   if (options.publicKey === undefined) | ||||||
|     throw new Error('Public Key not provided') |     throw new Error('Public Key not provided') | ||||||
| 
 | 
 | ||||||
|   client = new SlashClient({ |   client = new SlashClient({ | ||||||
|     id: options.id, |  | ||||||
|     token: options.token, |     token: options.token, | ||||||
|     publicKey: options.publicKey |     publicKey: options.publicKey | ||||||
|   }) |   }) | ||||||
|  | @ -40,6 +61,7 @@ export function init(options: DeploySlashInitOptions): void { | ||||||
|     request: Request |     request: Request | ||||||
|   }): Promise<void> => { |   }): Promise<void> => { | ||||||
|     try { |     try { | ||||||
|  |       // we have to wrap because there are some weird scope errors
 | ||||||
|       const d = await client.verifyFetchEvent({ |       const d = await client.verifyFetchEvent({ | ||||||
|         respondWith: (...args: any[]) => evt.respondWith(...args), |         respondWith: (...args: any[]) => evt.respondWith(...args), | ||||||
|         request: evt.request |         request: evt.request | ||||||
|  | @ -68,10 +90,37 @@ export function init(options: DeploySlashInitOptions): void { | ||||||
|   addEventListener('fetch', cb as any) |   addEventListener('fetch', cb as any) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | /** | ||||||
|  |  * Register Slash Command handler. | ||||||
|  |  * | ||||||
|  |  * Example: | ||||||
|  |  * | ||||||
|  |  * ```ts
 | ||||||
|  |  * handle("ping", (interaction) => { | ||||||
|  |  *   interaction.reply("Pong!") | ||||||
|  |  * }) | ||||||
|  |  * ``` | ||||||
|  |  * | ||||||
|  |  * Also supports Sub Command and Group handling out of the box! | ||||||
|  |  * ```ts
 | ||||||
|  |  * handle("command-name group-name sub-command", (i) => { | ||||||
|  |  *   // ...
 | ||||||
|  |  * }) | ||||||
|  |  * | ||||||
|  |  * handle("command-name sub-command", (i) => { | ||||||
|  |  *   // ...
 | ||||||
|  |  * }) | ||||||
|  |  * ``` | ||||||
|  |  * | ||||||
|  |  * @param cmd Command to handle. Either Handler object or command name followed by handler function in next parameter. | ||||||
|  |  * @param handler Handler function (required if previous argument was command name) | ||||||
|  |  */ | ||||||
| export function handle( | export function handle( | ||||||
|   cmd: string | SlashCommandHandler, |   cmd: string | SlashCommandHandler, | ||||||
|   handler?: SlashCommandHandlerCallback |   handler?: SlashCommandHandlerCallback | ||||||
| ): void { | ): void { | ||||||
|  |   if (client === undefined) | ||||||
|  |     throw new Error('Slash Client not initialized. Call `init` first') | ||||||
|   client.handle(cmd, handler) |   client.handle(cmd, handler) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue