2021-03-30 09:51:29 +00:00
|
|
|
import {
|
|
|
|
SlashCommandsManager,
|
|
|
|
SlashClient,
|
2021-04-04 08:21:08 +00:00
|
|
|
SlashCommandHandlerCallback,
|
|
|
|
SlashCommandHandler
|
2021-04-04 05:42:15 +00:00
|
|
|
} from './src/interactions/mod.ts'
|
2021-03-30 09:51:29 +00:00
|
|
|
import { InteractionResponseType, InteractionType } from './src/types/slash.ts'
|
|
|
|
|
|
|
|
export interface DeploySlashInitOptions {
|
|
|
|
env?: boolean
|
|
|
|
publicKey?: string
|
|
|
|
token?: string
|
|
|
|
}
|
|
|
|
|
2021-04-15 12:50:47 +00:00
|
|
|
/** Current Slash Client being used to handle commands */
|
2021-03-30 09:51:29 +00:00
|
|
|
let client: SlashClient
|
2021-04-15 12:50:47 +00:00
|
|
|
/** Manage Slash Commands right in Deploy */
|
2021-03-30 09:51:29 +00:00
|
|
|
let commands: SlashCommandsManager
|
|
|
|
|
2021-04-15 12:50:47 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2021-03-30 09:51:29 +00:00
|
|
|
export function init(options: DeploySlashInitOptions): void {
|
|
|
|
if (client !== undefined) throw new Error('Already initialized')
|
|
|
|
if (options.env === true) {
|
|
|
|
options.publicKey = Deno.env.get('PUBLIC_KEY')
|
|
|
|
options.token = Deno.env.get('TOKEN')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.publicKey === undefined)
|
|
|
|
throw new Error('Public Key not provided')
|
|
|
|
|
|
|
|
client = new SlashClient({
|
|
|
|
token: options.token,
|
|
|
|
publicKey: options.publicKey
|
|
|
|
})
|
|
|
|
|
|
|
|
commands = client.commands
|
|
|
|
|
|
|
|
const cb = async (evt: {
|
|
|
|
respondWith: CallableFunction
|
|
|
|
request: Request
|
|
|
|
}): Promise<void> => {
|
|
|
|
try {
|
2021-04-15 12:50:47 +00:00
|
|
|
// we have to wrap because there are some weird scope errors
|
2021-03-30 10:07:13 +00:00
|
|
|
const d = await client.verifyFetchEvent({
|
|
|
|
respondWith: (...args: any[]) => evt.respondWith(...args),
|
2021-04-04 05:42:15 +00:00
|
|
|
request: evt.request
|
2021-03-30 10:07:13 +00:00
|
|
|
})
|
2021-03-30 09:51:29 +00:00
|
|
|
if (d === false) {
|
|
|
|
await evt.respondWith(
|
2021-03-30 10:05:52 +00:00
|
|
|
new Response('Not Authorized', {
|
2021-03-30 09:51:29 +00:00
|
|
|
status: 400
|
|
|
|
})
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d.type === InteractionType.PING) {
|
|
|
|
await d.respond({ type: InteractionResponseType.PONG })
|
2021-03-30 10:07:13 +00:00
|
|
|
client.emit('ping')
|
2021-03-30 09:51:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
await (client as any)._process(d)
|
|
|
|
} catch (e) {
|
|
|
|
await client.emit('interactionError', e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addEventListener('fetch', cb as any)
|
|
|
|
}
|
|
|
|
|
2021-04-15 12:50:47 +00:00
|
|
|
/**
|
|
|
|
* Register Slash Command handler.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* handle("ping", (interaction) => {
|
|
|
|
* interaction.reply("Pong!")
|
|
|
|
* })
|
|
|
|
* ```
|
|
|
|
*
|
2021-04-15 12:53:26 +00:00
|
|
|
* 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) => {
|
|
|
|
* // ...
|
|
|
|
* })
|
|
|
|
* ```
|
|
|
|
*
|
2021-04-15 12:50:47 +00:00
|
|
|
* @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)
|
|
|
|
*/
|
2021-03-30 09:51:29 +00:00
|
|
|
export function handle(
|
2021-04-04 08:21:08 +00:00
|
|
|
cmd: string | SlashCommandHandler,
|
|
|
|
handler?: SlashCommandHandlerCallback
|
2021-03-30 09:51:29 +00:00
|
|
|
): void {
|
2021-04-15 12:50:47 +00:00
|
|
|
if (client === undefined)
|
|
|
|
throw new Error('Slash Client not initialized. Call `init` first')
|
2021-04-04 08:21:08 +00:00
|
|
|
client.handle(cmd, handler)
|
2021-03-30 09:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export { commands, client }
|
|
|
|
export * from './src/types/slash.ts'
|
|
|
|
export * from './src/structures/slash.ts'
|
2021-04-04 05:42:15 +00:00
|
|
|
export * from './src/interactions/mod.ts'
|
2021-04-04 10:27:28 +00:00
|
|
|
export * from './src/types/channel.ts'
|