1
- import {
2
- ApplicationCommandType ,
3
- REST ,
4
- Routes ,
5
- ApplicationCommandOptionType ,
6
- APIApplicationCommandSubcommandGroupOption ,
7
- APIApplicationCommandSubcommandOption ,
8
- } from 'discord.js' ;
1
+ import { ApplicationCommandType , REST , Routes } from 'discord.js' ;
9
2
import { CommandKit } from '../../CommandKit' ;
10
3
import { CommandData } from '../../types' ;
11
4
import { Logger } from '../../logger/Logger' ;
12
- import { Command } from '../router/CommandsRouter' ;
5
+
6
+ export interface PreRegisterCommandsEvent {
7
+ preventDefault ( ) : void ;
8
+ commands : CommandData [ ] ;
9
+ }
13
10
14
11
export class CommandRegistrar {
15
12
private api = new REST ( ) ;
@@ -80,11 +77,30 @@ export class CommandRegistrar {
80
77
* Registers loaded commands.
81
78
*/
82
79
public async register ( ) {
80
+ const commands = this . getCommandsData ( ) ;
81
+
82
+ let preRegistrationPrevented = false ;
83
+ const preRegisterEvent : PreRegisterCommandsEvent = {
84
+ preventDefault ( ) {
85
+ preRegistrationPrevented = true ;
86
+ } ,
87
+ commands,
88
+ } ;
89
+
90
+ await this . commandkit . plugins . execute ( async ( ctx , plugin ) => {
91
+ if ( preRegistrationPrevented ) return ;
92
+ return plugin . onBeforeRegisterCommands ( ctx , preRegisterEvent ) ;
93
+ } ) ;
94
+
95
+ if ( preRegistrationPrevented ) return ;
96
+
97
+ // we check this after the plugin event
98
+ // because plugins may be able to register commands
99
+ // before the client is ready
83
100
if ( ! this . commandkit . client . isReady ( ) ) {
84
101
throw new Error ( 'Cannot register commands before the client is ready' ) ;
85
102
}
86
103
87
- const commands = this . getCommandsData ( ) ;
88
104
const guildCommands = commands
89
105
. filter ( ( command ) => command . guilds ?. filter ( Boolean ) . length )
90
106
. map ( ( c ) => ( {
@@ -105,6 +121,19 @@ export class CommandRegistrar {
105
121
public async updateGlobalCommands ( commands : CommandData [ ] ) {
106
122
if ( ! commands . length ) return ;
107
123
124
+ let prevented = false ;
125
+ const preRegisterEvent : PreRegisterCommandsEvent = {
126
+ preventDefault ( ) {
127
+ prevented = true ;
128
+ } ,
129
+ commands,
130
+ } ;
131
+
132
+ await this . commandkit . plugins . execute ( async ( ctx , plugin ) => {
133
+ if ( prevented ) return ;
134
+ return plugin . onBeforeRegisterGlobalCommands ( ctx , preRegisterEvent ) ;
135
+ } ) ;
136
+
108
137
try {
109
138
const data = ( await this . api . put (
110
139
Routes . applicationCommands ( this . commandkit . client . user ! . id ) ,
@@ -128,6 +157,24 @@ export class CommandRegistrar {
128
157
* Updates the guild commands.
129
158
*/
130
159
public async updateGuildCommands ( commands : CommandData [ ] ) {
160
+ if ( ! commands . length ) return ;
161
+
162
+ let prevented = false ;
163
+ const preRegisterEvent : PreRegisterCommandsEvent = {
164
+ preventDefault ( ) {
165
+ prevented = true ;
166
+ } ,
167
+ commands,
168
+ } ;
169
+ await this . commandkit . plugins . execute ( async ( ctx , plugin ) => {
170
+ if ( prevented ) return ;
171
+ return plugin . onBeforePrepareGuildCommandsRegistration (
172
+ ctx ,
173
+ preRegisterEvent ,
174
+ ) ;
175
+ } ) ;
176
+ if ( prevented ) return ;
177
+
131
178
try {
132
179
const guildCommandsMap = new Map < string , CommandData [ ] > ( ) ;
133
180
@@ -148,6 +195,21 @@ export class CommandRegistrar {
148
195
let count = 0 ;
149
196
150
197
for ( const [ guild , guildCommands ] of guildCommandsMap ) {
198
+ let prevented = false ;
199
+ const preRegisterEvent : PreRegisterCommandsEvent = {
200
+ preventDefault ( ) {
201
+ prevented = true ;
202
+ } ,
203
+ commands : guildCommands ,
204
+ } ;
205
+
206
+ await this . commandkit . plugins . execute ( async ( ctx , plugin ) => {
207
+ if ( prevented ) return ;
208
+ return plugin . onBeforeRegisterGuildCommands ( ctx , preRegisterEvent ) ;
209
+ } ) ;
210
+
211
+ if ( prevented ) continue ;
212
+
151
213
const data = ( await this . api . put (
152
214
Routes . applicationGuildCommands (
153
215
this . commandkit . client . user ! . id ,
0 commit comments