harmony/deploy.ts

83 lines
2.0 KiB
TypeScript
Raw Normal View History

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
id?: string
}
let client: SlashClient
let commands: SlashCommandsManager
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')
options.id = Deno.env.get('ID')
}
if (options.publicKey === undefined)
throw new Error('Public Key not provided')
client = new SlashClient({
id: options.id,
token: options.token,
publicKey: options.publicKey
})
commands = client.commands
const cb = async (evt: {
respondWith: CallableFunction
request: Request
}): Promise<void> => {
try {
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)
}
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-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 09:29:56 +00:00
// export * from './src/types/channel.ts'