1
- import { Inject , Injectable , Logger } from '@nestjs/common' ;
2
- import { NECORD_MODULE_OPTIONS } from '../necord.module-definition' ;
1
+ import { Injectable , Logger } from '@nestjs/common' ;
3
2
import { Client , Collection } from 'discord.js' ;
4
- import { NecordModuleOptions } from '../necord-options.interface' ;
5
- import { ContextMenusService } from './context-menus' ;
6
3
import { CommandDiscovery } from './command.discovery' ;
4
+ import { ContextMenusService } from './context-menus' ;
7
5
import { SlashCommandsService } from './slash-commands' ;
8
6
9
7
@Injectable ( )
10
8
export class CommandsService {
11
9
private readonly logger = new Logger ( CommandsService . name ) ;
12
10
13
- public readonly cache = new Collection < string , CommandDiscovery [ ] > ( [ [ undefined , [ ] ] ] ) ;
14
-
15
11
public constructor (
16
12
private readonly client : Client ,
17
- @Inject ( NECORD_MODULE_OPTIONS )
18
- private readonly options : NecordModuleOptions ,
19
13
private readonly contextMenusService : ContextMenusService ,
20
14
private readonly slashCommandsService : SlashCommandsService
21
15
) { }
22
16
23
- private onModuleInit ( ) {
24
- if ( this . options . skipRegistration ) {
25
- return ;
26
- }
27
-
28
- return this . client . once ( 'ready' , async ( ) => this . register ( ) ) ;
29
- }
30
-
31
- private onApplicationBootstrap ( ) {
32
- const commands : CommandDiscovery [ ] = [
33
- ...this . contextMenusService . cache . values ( ) ,
34
- ...this . slashCommandsService . cache . values ( )
35
- ] ;
36
-
37
- for ( const command of commands ) {
38
- const guilds = Array . isArray ( this . options . development )
39
- ? this . options . development
40
- : command . getGuilds ( ) ?? [ undefined ] ;
41
-
42
- for ( const guildId of guilds ) {
43
- const visitedCommands = this . cache . get ( guildId ) ?? [ ] ;
44
- this . cache . set ( guildId , visitedCommands . concat ( command ) ) ;
45
- }
46
- }
47
- }
48
-
49
17
public async register ( ) {
50
18
if ( this . client . application . partial ) {
51
19
await this . client . application . fetch ( ) ;
52
20
}
53
21
54
22
this . logger . log ( `Started refreshing application commands.` ) ;
55
- for ( const guild of this . cache . keys ( ) ) {
56
- if ( this . getGuildCommands ( guild ) . length === 0 ) {
23
+ for ( const [ guild , commands ] of this . getCommandsByGuilds ( ) . entries ( ) ) {
24
+ if ( commands . length === 0 ) {
57
25
this . logger . log (
58
26
`Skipping ${ guild ? `guild ${ guild } ` : 'global' } as it has no commands.`
59
27
) ;
60
28
continue ;
61
29
}
62
30
63
- await this . registerInGuild ( guild ) . catch ( error => {
31
+ const rawCommands = commands . flatMap ( command => command . toJSON ( ) ) ;
32
+
33
+ await this . client . application . commands . set ( rawCommands , guild ) . catch ( error => {
64
34
this . logger . error (
65
35
`Failed to register application commands (${
66
36
guild ? `in guild ${ guild } ` : 'global'
@@ -80,23 +50,40 @@ export class CommandsService {
80
50
}
81
51
82
52
public getCommands ( ) : CommandDiscovery [ ] {
83
- return [ ...this . cache . values ( ) ] . flat ( ) ;
53
+ return [
54
+ ...this . contextMenusService . cache . values ( ) ,
55
+ ...this . slashCommandsService . cache . values ( )
56
+ ] . flat ( ) ;
57
+ }
58
+
59
+ public getCommandsByGuilds ( ) : Collection < string , CommandDiscovery [ ] > {
60
+ const collection = new Collection < string , CommandDiscovery [ ] > ( ) ;
61
+ const commands = this . getCommands ( ) ;
62
+
63
+ for ( const command of commands ) {
64
+ for ( const guildId of command . getGuilds ( ) ) {
65
+ const visitedCommands = collection . get ( guildId ) ?? [ ] ;
66
+ collection . set ( guildId , visitedCommands . concat ( command ) ) ;
67
+ }
68
+ }
69
+
70
+ return collection ;
84
71
}
85
72
86
73
public getCommandByName ( name : string ) : CommandDiscovery {
87
74
return this . getCommands ( ) . find ( command => command . getName ( ) === name ) ;
88
75
}
89
76
90
77
public getGlobalCommands ( ) : CommandDiscovery [ ] {
91
- return this . cache . get ( undefined ) ?? [ ] ;
78
+ return this . getCommandsByGuilds ( ) . get ( undefined ) ?? [ ] ;
92
79
}
93
80
94
81
public getGlobalCommandByName ( name : string ) : CommandDiscovery {
95
82
return this . getGlobalCommands ( ) . find ( command => command . getName ( ) === name ) ;
96
83
}
97
84
98
85
public getGuildCommands ( guildId : string ) : CommandDiscovery [ ] {
99
- return this . cache . get ( guildId ) ?? [ ] ;
86
+ return this . getCommandsByGuilds ( ) . get ( guildId ) ?? [ ] ;
100
87
}
101
88
102
89
public getGuildCommandByName ( guildId : string , name : string ) : CommandDiscovery {
0 commit comments