-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
deploy.ts
187 lines (172 loc) · 5.01 KB
/
deploy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { Interaction } from './src/structures/interactions.ts'
import {
ApplicationCommandsManager,
InteractionsClient,
ApplicationCommandHandler,
ApplicationCommandHandlerCallback,
AutocompleteHandlerCallback
} from './src/interactions/mod.ts'
import {
InteractionResponseType,
InteractionType
} from './src/types/interactions.ts'
import { ApplicationCommandType } from './src/types/applicationCommand.ts'
export interface DeploySlashInitOptions {
env?: boolean
publicKey?: string
token?: string
path?: string
}
/** Current Slash Client being used to handle commands */
let client: InteractionsClient
/** Manage Slash Commands right in Deploy */
let commands: ApplicationCommandsManager
/**
* 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; path?: string }): void
export function init(options: {
publicKey: string
token?: string
path?: string
}): void
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 InteractionsClient({
token: options.token,
publicKey: options.publicKey
})
commands = client.commands
const cb = async (evt: {
respondWith: CallableFunction
request: Request
}): Promise<void> => {
if (options.path !== undefined) {
if (new URL(evt.request.url).pathname !== options.path) return
}
try {
// we have to wrap because there are some weird scope errors
const d = await client.verifyFetchEvent({
respondWith: (...args: unknown[]) => evt.respondWith(...args),
request: evt.request
})
if (d === false) {
await evt.respondWith(
new Response('Not Authorized', {
status: 400
})
)
return
}
if (d.type === InteractionType.PING) {
await d.respond({ type: InteractionResponseType.PONG })
client.emit('ping')
return
}
await client._process(d)
} catch (e) {
await client.emit('interactionError', e as Error)
}
}
addEventListener('fetch', cb as unknown as EventListener)
}
/**
* 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(
cmd: string,
handler: ApplicationCommandHandlerCallback
): void
export function handle(cmd: ApplicationCommandHandler): void
export function handle(
cmd: string,
handler: ApplicationCommandHandlerCallback,
type: ApplicationCommandType | keyof typeof ApplicationCommandType
): void
export function handle(
cmd: string | ApplicationCommandHandler,
handler?: ApplicationCommandHandlerCallback,
type?: ApplicationCommandType | keyof typeof ApplicationCommandType
): void {
if (client === undefined)
throw new Error('Interaction Client not initialized. Call `init` first')
if (
typeof cmd === 'string' &&
typeof handler === 'function' &&
typeof type !== 'undefined'
) {
client.handle(cmd, handler, type)
} else if (typeof cmd === 'string' && typeof handler === 'function') {
client.handle(cmd, handler)
} else if (typeof cmd === 'object') {
client.handle(cmd)
} else throw new Error('Invalid overload for `handle` function')
}
export function autocomplete(
cmd: string,
option: string,
callback: AutocompleteHandlerCallback
): void {
client.autocomplete(cmd, option, callback)
}
/** Listen for Interactions Event */
export function interactions(
cb: (i: Interaction) => unknown | Promise<unknown>
): void {
client.on('interaction', cb)
}
export { commands, client }
export * from './src/types/applicationCommand.ts'
export * from './src/types/interactions.ts'
export * from './src/structures/applicationCommand.ts'
export * from './src/interactions/mod.ts'
export * from './src/types/channel.ts'
export * from './src/structures/interactions.ts'
export * from './src/structures/message.ts'
export * from './src/structures/embed.ts'
export * from './src/types/messageComponents.ts'