This repository has been archived by the owner on Feb 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
YorClient.ts
605 lines (529 loc) · 18.1 KB
/
YorClient.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
import { Collection } from '@discordjs/collection';
import {
APIChatInputApplicationCommandInteraction,
APIInteraction,
APIInteractionResponse,
APIMessageApplicationCommandInteraction,
APIUserApplicationCommandInteraction,
ApplicationCommandType,
InteractionType,
RESTPutAPIApplicationCommandsResult,
} from '@discordjs/core/http-only';
import { REST, RESTOptions } from '@discordjs/rest';
import { InteractionResponseType, verifyKey } from 'discord-interactions';
import { AutocompleteCommandContext } from './Contexts/AutocompleteCommandContext';
import { CommandContext } from './Contexts/CommandContext';
import { ComponentContext } from './Contexts/ComponentContext';
import { MessageContextMenuCommandInteraction } from './Contexts/ContextMenu/MessageContextMenuCommandInteraction';
import { UserContextMenuCommandInteraction } from './Contexts/ContextMenu/UserContextMenuCommandInteraction';
import { ModalContext } from './Contexts/ModalContext';
import { YorClientAPI } from './YorClientAPI';
import { YorClientError } from './YorClientError';
import {
YorMessageContextMenuCommand,
YorUserContextMenuCommand,
} from './YorContextMenuCommand';
import { YorInteractionComponent } from './YorInteractionComponent';
import { YorInteractionModal } from './YorInteractionModal';
import { YorSlashCommand } from './YorSlashCommand';
export interface YorClientOptions {
token: string;
application: {
id: string;
publicKey: string;
};
rest?: RESTOptions;
}
export type MiddlewareFunctionNames =
| 'command'
| 'component'
| 'modal'
| 'autocomplete'
| 'message'
| 'user';
export type MiddlewareFunction<T extends MiddlewareFunctionNames> =
T extends 'command'
? (context: CommandContext) => Promise<void> | void
: T extends 'component'
? (context: ComponentContext) => Promise<void> | void
: T extends 'modal'
? (context: ModalContext) => Promise<void> | void
: T extends 'autocomplete'
? (context: AutocompleteCommandContext) => Promise<void> | void
: T extends 'message'
? (
context: MessageContextMenuCommandInteraction,
) => Promise<void> | void
: T extends 'user'
? (context: CommandContext) => Promise<void> | void
: never;
export class YorClient {
public readonly options: YorClientOptions;
public readonly commands = new Collection<string, YorSlashCommand>();
public readonly components = new Collection<
string,
YorInteractionComponent
>();
public readonly modals = new Collection<string, YorInteractionModal>();
public readonly messageCommands = new Collection<
string,
YorMessageContextMenuCommand
>();
public readonly userCommands = new Collection<
string,
YorUserContextMenuCommand
>();
public rest: REST;
public api: YorClientAPI;
public middlewares: Record<
MiddlewareFunctionNames,
MiddlewareFunction<MiddlewareFunctionNames>[]
>;
/**
* Creates a new instance of the class.
*
* @param {YorClientOptions} options - The configuration options for the client.
*/
constructor(options: YorClientOptions) {
this.options = options;
this.rest = new REST({
version: options.rest?.version || '10',
...options.rest,
}).setToken(this.options.token);
this.api = new YorClientAPI(this.rest);
this.middlewares = {
command: [],
component: [],
autocomplete: [],
modal: [],
message: [],
user: [],
};
}
/**
* Registers a slash command.
*
* @param {YorSlashCommand} command - The slash command to register.
* @return {Collection<string, YorSlashCommand>} - The updated commands collection.
*/
public registerCommand(
command: YorSlashCommand,
): Collection<string, YorSlashCommand> {
if (this.commands.has(command.builder.name))
throw new YorClientError(
`Command with name ${command.builder.name} already exists.`,
);
this.commands.set(command.builder.name, command);
return this.commands;
}
/**
* Registers the provided slash commands.
*
* @param {YorSlashCommand[]} commands - The slash commands to register.
* @return {Collection<string, YorSlashCommand>} - The updated commands map.
*/
public registerCommands(
commands: YorSlashCommand[],
): Collection<string, YorSlashCommand> {
for (const command of commands) {
if (this.commands.has(command.builder.name))
throw new YorClientError(
`Command with name ${command.builder.name} already exists.`,
);
this.commands.set(command.builder.name, command);
}
return this.commands;
}
/**
* Unregisters a slash command.
*
* @param {YorSlashCommand} command - The slash command to unregister.
* @return {Collection<string, YorSlashCommand>} - The updated commands collection.
*/
public unregisterCommand(
command: YorSlashCommand,
): Collection<string, YorSlashCommand> {
this.commands.delete(command.builder.name);
return this.commands;
}
/**
* Unregisters the provided slash commands.
*
* @param {YorSlashCommand[]} commands - The slash commands to unregister.
* @return {Collection<string, YorSlashCommand>} - The updated commands map.
*/
public unregisterCommands(
commands: YorSlashCommand[],
): Collection<string, YorSlashCommand> {
for (const command of commands) {
this.commands.delete(command.builder.name);
}
return this.commands;
}
/**
* Unregisters all slash commands.
*
* @return {Collection<string, YorSlashCommand>} - The updated commands map.
*/
public unregisterAllCommands(): Collection<string, YorSlashCommand> {
this.commands.clear();
return this.commands;
}
/**
* Registers a message command.
*
* @param {YorMessageContextMenuCommand} command - The message command to register.
* @return {Collection<string, YorMessageContextMenuCommand>} - The updated message commands collection.
*/
public registerMessageCommand(
command: YorMessageContextMenuCommand,
): Collection<string, YorMessageContextMenuCommand> {
this.messageCommands.set(command.builder.name, command);
return this.messageCommands;
}
/**
* Unregisters a message command.
*
* @param {YorMessageContextMenuCommand} command - The message command to unregister.
* @return {Collection<string, YorMessageContextMenuCommand>} - The updated message commands collection.
*/
public unregisterMessageCommand(
command: YorMessageContextMenuCommand,
): Collection<string, YorMessageContextMenuCommand> {
this.messageCommands.delete(command.builder.name);
return this.messageCommands;
}
/**
* Registers a user command.
*
* @param {YorUserContextMenuCommand} command - The user command to register.
* @return {Collection<string, YorUserContextMenuCommand>} - The updated user commands collection.
*/
public registerUserCommand(
command: YorUserContextMenuCommand,
): Collection<string, YorUserContextMenuCommand> {
this.userCommands.set(command.builder.name, command);
return this.userCommands;
}
/**
* Unregisters a user command.
*
* @param {YorUserContextMenuCommand} command - The user command to unregister.
* @return {Collection<string, YorUserContextMenuCommand>} - The updated user commands collection.
*/
public unregisterUserCommand(
command: YorUserContextMenuCommand,
): Collection<string, YorUserContextMenuCommand> {
this.userCommands.delete(command.builder.name);
return this.userCommands;
}
/**
* Deploys the commands to the API application and returns the result.
*
* @return {Promise<RESTPutAPIApplicationCommandsResult>} The result of the deployment.
*/
public async deployCommands(
guild?: string,
): Promise<RESTPutAPIApplicationCommandsResult> {
const commands = [
...this.commands.values(),
...this.messageCommands.values(),
...this.userCommands.values(),
].map((command) => command.builder.toJSON());
const response = guild
? await this.api.applicationCommands.bulkOverwriteGuildCommands(
this.options.application.id,
guild,
commands,
)
: await this.api.applicationCommands.bulkOverwriteGlobalCommands(
this.options.application.id,
commands,
);
return response;
}
/**
* Registers a component in the collection.
*
* @param {YorInteractionComponent} component - The component to register.
* @return {Collection<string, YorInteractionComponent>} - The updated collection of components.
*/
public registerComponent(
component: YorInteractionComponent,
): Collection<string, YorInteractionComponent> {
if (this.components.has(component.id))
throw new YorClientError(
`Component with id ${component.id} already exists.`,
);
this.components.set(component.id, component);
return this.components;
}
/**
* Registers the given components in the collection.
*
* @param {YorInteractionComponent[]} components - The components to register.
* @return {Collection<string, YorInteractionComponent>} - The updated collection of components.
*/
public registerComponents(
components: YorInteractionComponent[],
): Collection<string, YorInteractionComponent> {
for (const component of components) {
if (this.components.has(component.id))
throw new YorClientError(
`Component with id ${component.id} already exists.`,
);
this.components.set(component.id, component);
}
return this.components;
}
/**
* Deletes the specified component from the collection and returns the updated collection.
*
* @param {YorInteractionComponent} component - The component to unregister.
* @return {Collection<string, YorInteractionComponent>} - The updated collection of components.
*/
public unregisterComponent(
component: YorInteractionComponent,
): Collection<string, YorInteractionComponent> {
this.components.delete(component.id);
return this.components;
}
/**
* Unregisters components from the collection.
*
* @param {YorInteractionComponent[]} components - The components to unregister.
* @return {Collection<string, YorInteractionComponent>} - The updated components collection.
*/
public unregisterComponents(
components: YorInteractionComponent[],
): Collection<string, YorInteractionComponent> {
for (const component of components) {
this.components.delete(component.id);
}
return this.components;
}
/**
* Registers a modal in the collection.
*
* @param {YorInteractionModal} modal - The modal to be registered.
* @return {Collection<string, YorInteractionModal>} - The updated collection of modals.
*/
public registerModal(
modal: YorInteractionModal,
): Collection<string, YorInteractionModal> {
if (this.modals.has(modal.id))
throw new YorClientError(`Modal with id ${modal.id} already exists.`);
this.modals.set(modal.id, modal);
return this.modals;
}
/**
* Registers the given modals in the collection.
*
* @param {YorInteractionModal[]} modals - The modals to be registered.
* @return {Collection<string, YorInteractionModal>} - The updated collection of modals.
*/
public registerModals(
modals: YorInteractionModal[],
): Collection<string, YorInteractionModal> {
for (const modal of modals) {
if (this.modals.has(modal.id))
throw new YorClientError(`Modal with id ${modal.id} already exists.`);
this.modals.set(modal.id, modal);
}
return this.modals;
}
/**
* Deletes the specified modal from the collection and returns the updated collection.
*
* @param {YorInteractionModal} modal - The modal to unregister.
* @return {Collection<string, YorInteractionModal>} - The updated collection of modals.
*/
public unregisterModal(
modal: YorInteractionModal,
): Collection<string, YorInteractionModal> {
this.modals.delete(modal.id);
return this.modals;
}
/**
* Unregisters modals from the collection.
*
* @param {YorInteractionModal[]} modals - The modals to unregister.
* @return {Collection<string, YorInteractionModal>} - The updated collection of modals.
*/
public unregisterModals(
modals: YorInteractionModal[],
): Collection<string, YorInteractionModal> {
for (const modal of modals) {
this.modals.delete(modal.id);
}
return this.modals;
}
/**
* Creates middleware for a given name and adds it to the middlewares array.
*
* @param {T} name - The name of the middleware.
* @param {MiddlewareFunction<T>} middleware - The middleware function.
*/
public createMiddleware<T extends MiddlewareFunctionNames>(
name: T,
middleware: MiddlewareFunction<T>,
) {
this.middlewares[name].push(middleware);
}
/**
* Handles an interaction request from the API.
*
* @param {Request} request - The request object containing the interaction data.
* @return {Promise<APIInteractionResponse>} - An object containing a promise for the response.
*/
public async handleInteraction(
request: Request,
): Promise<APIInteractionResponse> {
if (request.method !== 'POST') {
throw new YorClientError('Invalid request method');
}
const body = await request.clone().text();
const signature = request.headers.get('X-Signature-Ed25519');
const timestamp = request.headers.get('X-Signature-Timestamp');
if (!signature || !timestamp) {
throw new YorClientError('Missing signature or timestamp');
}
const isValid = verifyKey(
body,
signature,
timestamp,
this.options.application.publicKey,
);
if (!isValid) {
throw new YorClientError('Invalid signature');
}
const data = JSON.parse(body) as APIInteraction;
// @ts-expect-error - ts(2345)
return this.handleInteractionRequest(data);
}
/**
* Handles the interaction request.
* @param {APIInteraction} data - The interaction data.
*/
private async handleInteractionRequest(data: APIInteraction) {
if (data.type === InteractionType.Ping) {
return { type: InteractionResponseType.PONG };
}
if (data.type === InteractionType.ApplicationCommand) {
if (data.data.type === ApplicationCommandType.ChatInput) {
const command = this.commands.get(data.data.name);
if (!command) {
throw new YorClientError(
`Command with name ${data.data.name} not found.`,
);
}
const context = new CommandContext(
this,
data as APIChatInputApplicationCommandInteraction,
);
for await (const middleware of this.middlewares.command) {
// @ts-expect-error - ts(2345)
await middleware(context);
}
await command.execute(context);
return {
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
};
}
if (data.data.type === ApplicationCommandType.Message) {
const command = this.messageCommands
.filter((command) => command.builder)
.get(data.data.name);
if (!command) {
throw new YorClientError(
`Command with name ${data.data.name} not found.`,
);
}
const context = new MessageContextMenuCommandInteraction(
this,
data as APIMessageApplicationCommandInteraction,
);
for await (const middleware of this.middlewares.message) {
// @ts-expect-error - ts(2345)
await middleware(context);
}
await command.execute(context);
}
if (data.data.type === ApplicationCommandType.User) {
const command = this.userCommands
.filter((command) => command.builder)
.get(data.data.name);
if (!command) {
throw new YorClientError(
`Command with name ${data.data.name} not found.`,
);
}
const context = new UserContextMenuCommandInteraction(
this,
data as APIUserApplicationCommandInteraction,
);
for await (const middleware of this.middlewares.user) {
// @ts-expect-error - ts(2345)
await middleware(context);
}
await command.execute(context);
}
}
if (data.type === InteractionType.ApplicationCommandAutocomplete) {
const command = this.commands
.filter((command) => command.autocomplete)
.get(data.data.name);
if (!command) {
throw new YorClientError(
`Command with name ${data.data.name} not found.`,
);
}
const context = new AutocompleteCommandContext(this, data);
for await (const middleware of this.middlewares.autocomplete) {
// @ts-expect-error - ts(2345)
await middleware(context);
}
await command.autocomplete(context);
return {
type: InteractionResponseType.APPLICATION_COMMAND_AUTOCOMPLETE_RESULT,
};
}
if (data.type === InteractionType.MessageComponent) {
const component = this.components.get(data.data.custom_id);
if (!component) {
throw new YorClientError(
`Component with id ${data.data.custom_id} not found.`,
);
}
const context = new ComponentContext(this, data);
for await (const middleware of this.middlewares.component) {
// @ts-expect-error - ts(2345)
await middleware(context);
}
await component.execute(context);
return {
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
};
}
if (data.type === InteractionType.ModalSubmit) {
const modal = this.modals.get(data.data.custom_id);
if (!modal) {
throw new YorClientError(
`Modal with id ${data.data.custom_id} not found.`,
);
}
const context = new ModalContext(this, data);
for await (const middleware of this.middlewares.modal) {
// @ts-expect-error - ts(2345)
await middleware(context);
}
await modal.execute(context);
return {
type: InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
};
}
return {
type: InteractionResponseType.PONG,
};
}
}