feat(docs): add jsdoc for deploy

This commit is contained in:
DjDeveloperr 2021-04-15 18:20:47 +05:30
parent ca3d68e6a0
commit d0e3dc9fba
1 changed files with 41 additions and 3 deletions

View File

@ -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,26 @@ 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!")
* })
* ```
*
* @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)
} }