@@ -11,13 +11,13 @@ import {
11
11
type RESTPostAPIChatInputApplicationCommandsJSONBody ,
12
12
type RESTPostAPIContextMenuApplicationCommandsJSONBody
13
13
} from 'discord-api-types/v10' ;
14
- import type {
15
- ApplicationCommand ,
16
- ApplicationCommandManager ,
17
- ChatInputApplicationCommandData ,
14
+ import {
18
15
Collection ,
19
- MessageApplicationCommandData ,
20
- UserApplicationCommandData
16
+ type ApplicationCommand ,
17
+ type ApplicationCommandManager ,
18
+ type ChatInputApplicationCommandData ,
19
+ type MessageApplicationCommandData ,
20
+ type UserApplicationCommandData
21
21
} from 'discord.js' ;
22
22
import { InternalRegistryAPIType , RegisterBehavior } from '../../types/Enums' ;
23
23
import { allGuildIdsToFetchCommandsFor , getDefaultBehaviorWhenNotIdentical , getDefaultGuildIds } from './ApplicationCommandRegistries' ;
@@ -26,14 +26,61 @@ import { getCommandDifferences, getCommandDifferencesFast } from './computeDiffe
26
26
import { convertApplicationCommandToApiData , normalizeChatInputCommand , normalizeContextMenuCommand } from './normalizeInputs' ;
27
27
28
28
export class ApplicationCommandRegistry {
29
+ /**
30
+ * The piece this registry is for.
31
+ */
29
32
public readonly commandName : string ;
30
33
34
+ /**
35
+ * A set of all chat input command names and ids that point to this registry.
36
+ * You should not use this field directly, but instead use {@link ApplicationCommandRegistry.globalChatInputCommandIds}
37
+ */
31
38
public readonly chatInputCommands = new Set < string > ( ) ;
39
+
40
+ /**
41
+ * A set of all context menu command names and ids that point to this registry.
42
+ * You should not use this field directly, but instead use {@link ApplicationCommandRegistry.globalContextMenuCommandIds}
43
+ */
32
44
public readonly contextMenuCommands = new Set < string > ( ) ;
45
+
46
+ /**
47
+ * The guild ids that we need to fetch the commands for.
48
+ */
33
49
public readonly guildIdsToFetch = new Set < string > ( ) ;
34
50
51
+ /**
52
+ * The global slash command id for this command.
53
+ * @deprecated This field will only show the first global command id registered for this registry.
54
+ * Use {@link ApplicationCommandRegistry.globalChatInputCommandIds} instead.
55
+ */
35
56
public globalCommandId : string | null = null ;
36
- public readonly guildCommandIds = new Map < string , string > ( ) ;
57
+
58
+ /**
59
+ * A set of all registered and valid global chat input command ids that point to this registry.
60
+ */
61
+ public readonly globalChatInputCommandIds = new Set < string > ( ) ;
62
+
63
+ /**
64
+ * A set of all registered and valid global context menu command ids that point to this registry.
65
+ */
66
+ public readonly globalContextMenuCommandIds = new Set < string > ( ) ;
67
+
68
+ /**
69
+ * The guild command ids for this command.
70
+ * @deprecated This field will only show the first guild command id registered for this registry per guild.
71
+ * Use {@link ApplicationCommandRegistry.guildIdToChatInputCommandIds} and {@link ApplicationCommandRegistry.guildIdToContextMenuCommandIds} instead.
72
+ */
73
+ public readonly guildCommandIds = new Collection < string , string > ( ) ;
74
+
75
+ /**
76
+ * A map of guild ids to a set of registered and valid chat input command ids that point to this registry.
77
+ */
78
+ public readonly guildIdToChatInputCommandIds = new Collection < string , Set < string > > ( ) ;
79
+
80
+ /**
81
+ * A map of guild ids to a set of registered and valid context menu command ids that point to this registry.
82
+ */
83
+ public readonly guildIdToContextMenuCommandIds = new Collection < string , Set < string > > ( ) ;
37
84
38
85
private readonly apiCalls : InternalAPICall [ ] = [ ] ;
39
86
@@ -225,6 +272,42 @@ export class ApplicationCommandRegistry {
225
272
}
226
273
}
227
274
275
+ protected handleIdAddition ( type : InternalRegistryAPIType , id : string , guildId ?: string | null ) {
276
+ switch ( type ) {
277
+ case InternalRegistryAPIType . ChatInput : {
278
+ this . addChatInputCommandIds ( id ) ;
279
+
280
+ if ( guildId ) {
281
+ this . guildIdToChatInputCommandIds . ensure ( guildId , ( ) => new Set ( ) ) . add ( id ) ;
282
+ } else {
283
+ this . globalChatInputCommandIds . add ( id ) ;
284
+ }
285
+ break ;
286
+ }
287
+ case InternalRegistryAPIType . ContextMenu : {
288
+ this . addContextMenuCommandIds ( id ) ;
289
+
290
+ if ( guildId ) {
291
+ this . guildIdToContextMenuCommandIds . ensure ( guildId , ( ) => new Set ( ) ) . add ( id ) ;
292
+ } else {
293
+ this . globalContextMenuCommandIds . add ( id ) ;
294
+ }
295
+ break ;
296
+ }
297
+ }
298
+
299
+ // Old field handling
300
+ if ( guildId ) {
301
+ // Old, wrongly typed field (thx kyra for spotting >_>)
302
+ if ( ! this . guildCommandIds . has ( guildId ) ) {
303
+ this . guildCommandIds . set ( guildId , id ) ;
304
+ }
305
+ } else {
306
+ // First come, first serve (thx kyra for spotting >_>)
307
+ this . globalCommandId ??= id ;
308
+ }
309
+ }
310
+
228
311
private getGuildIdsToRegister ( options ?: ApplicationCommandRegistryRegisterOptions ) {
229
312
let guildIdsToRegister : ApplicationCommandRegistry . RegisterOptions [ 'guildIds' ] = undefined ;
230
313
@@ -298,16 +381,8 @@ export class ApplicationCommandRegistry {
298
381
const globalCommand = globalCommands . find ( findCallback ) ;
299
382
300
383
if ( globalCommand ) {
301
- switch ( apiCall . type ) {
302
- case InternalRegistryAPIType . ChatInput :
303
- this . addChatInputCommandIds ( globalCommand . id ) ;
304
- break ;
305
- case InternalRegistryAPIType . ContextMenu :
306
- this . addContextMenuCommandIds ( globalCommand . id ) ;
307
- break ;
308
- }
309
-
310
384
this . debug ( `Checking if command "${ commandName } " is identical with global ${ type } command with id "${ globalCommand . id } "` ) ;
385
+ this . handleIdAddition ( apiCall . type , globalCommand . id ) ;
311
386
await this . handleCommandPresent ( globalCommand , builtData , behaviorIfNotEqual , null ) ;
312
387
} else if ( registerOptions . registerCommandIfMissing ?? true ) {
313
388
this . debug ( `Creating new global ${ type } command with name "${ commandName } "` ) ;
@@ -332,16 +407,7 @@ export class ApplicationCommandRegistry {
332
407
333
408
if ( existingGuildCommand ) {
334
409
this . debug ( `Checking if guild ${ type } command "${ commandName } " is identical to command "${ existingGuildCommand . id } "` ) ;
335
-
336
- switch ( apiCall . type ) {
337
- case InternalRegistryAPIType . ChatInput :
338
- this . addChatInputCommandIds ( existingGuildCommand . id ) ;
339
- break ;
340
- case InternalRegistryAPIType . ContextMenu :
341
- this . addContextMenuCommandIds ( existingGuildCommand . id ) ;
342
- break ;
343
- }
344
-
410
+ this . handleIdAddition ( apiCall . type , existingGuildCommand . id , guildId ) ;
345
411
await this . handleCommandPresent ( existingGuildCommand , builtData , behaviorIfNotEqual , guildId ) ;
346
412
} else if ( registerOptions . registerCommandIfMissing ?? true ) {
347
413
this . debug ( `Creating new guild ${ type } command with name "${ commandName } " for guild "${ guildId } "` ) ;
@@ -358,12 +424,6 @@ export class ApplicationCommandRegistry {
358
424
behaviorIfNotEqual : RegisterBehavior ,
359
425
guildId : string | null
360
426
) {
361
- if ( guildId ) {
362
- this . guildCommandIds . set ( guildId , applicationCommand . id ) ;
363
- } else {
364
- this . globalCommandId = applicationCommand . id ;
365
- }
366
-
367
427
if ( behaviorIfNotEqual === RegisterBehavior . BulkOverwrite ) {
368
428
this . debug (
369
429
`Command "${ this . commandName } " has the behaviorIfNotEqual set to "BulkOverwrite" which is invalid. Using defaultBehaviorWhenNotIdentical instead`
@@ -467,12 +527,6 @@ export class ApplicationCommandRegistry {
467
527
try {
468
528
const result = await commandsManager . create ( apiData , guildId ) ;
469
529
470
- if ( guildId ) {
471
- this . guildCommandIds . set ( guildId , result . id ) ;
472
- } else {
473
- this . globalCommandId = result . id ;
474
- }
475
-
476
530
this . info (
477
531
`Successfully created ${ type } ${ guildId ? ' guild' : '' } command "${ apiData . name } " with id "${
478
532
result . id
@@ -481,13 +535,15 @@ export class ApplicationCommandRegistry {
481
535
482
536
switch ( apiData . type ) {
483
537
case undefined :
484
- case ApplicationCommandType . ChatInput :
485
- this . addChatInputCommandIds ( result . id ) ;
538
+ case ApplicationCommandType . ChatInput : {
539
+ this . handleIdAddition ( InternalRegistryAPIType . ChatInput , result . id , guildId ) ;
486
540
break ;
541
+ }
487
542
case ApplicationCommandType . Message :
488
- case ApplicationCommandType . User :
489
- this . addContextMenuCommandIds ( result . id ) ;
543
+ case ApplicationCommandType . User : {
544
+ this . handleIdAddition ( InternalRegistryAPIType . ContextMenu , result . id , guildId ) ;
490
545
break ;
546
+ }
491
547
}
492
548
} catch ( err ) {
493
549
this . error (
0 commit comments