@@ -3,8 +3,8 @@ import type { CommandKitOptions } from './types';
3
3
import colors from './utils/colors' ;
4
4
import { createElement , Fragment } from './components' ;
5
5
import { EventInterceptor } from './components/common/EventInterceptor' ;
6
- import { Awaitable , Events , Locale , Message } from 'discord.js' ;
7
- import { findAppDirectory } from './utils/utilities' ;
6
+ import { Awaitable , Client , Events , Locale , Message } from 'discord.js' ;
7
+ import { createProxy , findAppDirectory , SimpleProxy } from './utils/utilities' ;
8
8
import { join } from 'node:path' ;
9
9
import { AppCommandHandler } from './app/handlers/AppCommandHandler' ;
10
10
import { CommandsRouter , EventsRouter } from './app/router' ;
@@ -26,9 +26,6 @@ export interface CommandKitConfiguration {
26
26
getMessageCommandPrefix : ( message : Message ) => Awaitable < string | string [ ] > ;
27
27
}
28
28
29
- // @ts -ignore
30
- export let commandkit : CommandKit ;
31
-
32
29
export type BootstrapFunction =
33
30
| GenericFunction < [ CommandKit ] >
34
31
| AsyncFunction < [ CommandKit ] > ;
@@ -74,7 +71,8 @@ export function onApplicationBootstrap<F extends BootstrapFunction>(
74
71
75
72
export class CommandKit extends EventEmitter {
76
73
#started = false ;
77
- private options : CommandKitOptions ;
74
+ #clientProxy: SimpleProxy < Client > | null = createProxy ( { } as Client ) ;
75
+ #client! : Client ;
78
76
public eventInterceptor ! : EventInterceptor ;
79
77
80
78
public static readonly createElement = createElement ;
@@ -103,7 +101,7 @@ export class CommandKit extends EventEmitter {
103
101
* @param options - The default CommandKit configuration.
104
102
* @see {@link https://commandkit.js.org/docs/guide/commandkit-setup }
105
103
*/
106
- constructor ( options : CommandKitOptions ) {
104
+ constructor ( options : CommandKitOptions = { } ) {
107
105
if ( CommandKit . instance ) {
108
106
process . emitWarning (
109
107
'CommandKit instance already exists. Having multiple instance in same project is discouraged and it may lead to unexpected behavior.' ,
@@ -113,24 +111,17 @@ export class CommandKit extends EventEmitter {
113
111
) ;
114
112
}
115
113
116
- if ( ! options . client ) {
117
- throw new Error (
118
- colors . red ( '"client" is required when instantiating CommandKit.' ) ,
119
- ) ;
120
- }
121
-
122
114
super ( ) ;
123
115
124
- this . options = options ;
125
-
126
116
if ( ! CommandKit . instance ) {
127
117
CommandKit . instance = this ;
128
118
}
129
119
130
- this . plugins = new CommandKitPluginRuntime ( this ) ;
120
+ if ( options ?. client ) {
121
+ this . setClient ( options . client ) ;
122
+ }
131
123
132
- // @ts -ignore
133
- commandkit = CommandKit . instance ;
124
+ this . plugins = new CommandKitPluginRuntime ( this ) ;
134
125
135
126
this . #bootstrapHooks( ) ;
136
127
}
@@ -171,7 +162,7 @@ export class CommandKit extends EventEmitter {
171
162
async start ( token ?: string | false ) {
172
163
if ( this . #started) return ;
173
164
174
- if ( ! this . options . client ) {
165
+ if ( ! this . # client) {
175
166
throw new Error (
176
167
colors . red ( '"client" is required when starting CommandKit.' ) ,
177
168
) ;
@@ -199,7 +190,7 @@ export class CommandKit extends EventEmitter {
199
190
this . commandHandler . registerCommandHandler ( ) ;
200
191
this . incrementClientListenersCount ( ) ;
201
192
202
- if ( token !== false && ! this . options . client . isReady ( ) ) {
193
+ if ( token !== false && ! this . client . isReady ( ) ) {
203
194
this . client . once ( Events . ClientReady , async ( ) => {
204
195
await this . commandHandler . registrar . register ( ) ;
205
196
} ) ;
@@ -210,16 +201,16 @@ export class CommandKit extends EventEmitter {
210
201
211
202
const botToken =
212
203
token ??
213
- this . options . client . token ??
204
+ this . client . token ??
214
205
process . env . TOKEN ??
215
206
process . env . DISCORD_TOKEN ;
216
207
217
- await this . options . client . login ( botToken ) ;
208
+ await this . client . login ( botToken ) ;
218
209
219
210
await this . plugins . execute ( ( ctx , plugin ) => {
220
211
return plugin . onAfterClientLogin ?.( ctx ) ;
221
212
} ) ;
222
- } else if ( this . options . client . isReady ( ) ) {
213
+ } else if ( this . client . isReady ( ) ) {
223
214
await this . commandHandler . registrar . register ( ) ;
224
215
}
225
216
@@ -272,8 +263,33 @@ export class CommandKit extends EventEmitter {
272
263
/**
273
264
* Get the client attached to this CommandKit instance.
274
265
*/
275
- get client ( ) {
276
- return this . options . client ;
266
+ get client ( ) : Client {
267
+ const client = this . #client || this . #clientProxy?. proxy ;
268
+
269
+ if ( ! client ) {
270
+ throw new Error ( 'Client instance is not set' ) ;
271
+ }
272
+
273
+ return client ;
274
+ }
275
+
276
+ /**
277
+ * Sets the client attached to this CommandKit instance.
278
+ * @param client The client to set.
279
+ */
280
+ setClient ( client : Client ) {
281
+ this . #client = client ;
282
+
283
+ // update the proxy target if it exists
284
+ if ( this . #clientProxy) {
285
+ // this is a hack to update the proxy target
286
+ // because some of the dependencies of commandkit may
287
+ // depend on the client instance
288
+ this . #clientProxy. setTarget ( client ) ;
289
+ this . #clientProxy = null ;
290
+ }
291
+
292
+ return this ;
277
293
}
278
294
279
295
async #init( ) {
@@ -350,18 +366,14 @@ export class CommandKit extends EventEmitter {
350
366
* Increment the client listeners count.
351
367
*/
352
368
incrementClientListenersCount ( ) {
353
- this . options . client . setMaxListeners (
354
- this . options . client . getMaxListeners ( ) + 1 ,
355
- ) ;
369
+ this . client . setMaxListeners ( this . client . getMaxListeners ( ) + 1 ) ;
356
370
}
357
371
358
372
/**
359
373
* Decrement the client listeners count.
360
374
*/
361
375
decrementClientListenersCount ( ) {
362
- this . options . client . setMaxListeners (
363
- this . options . client . getMaxListeners ( ) - 1 ,
364
- ) ;
376
+ this . client . setMaxListeners ( this . client . getMaxListeners ( ) - 1 ) ;
365
377
}
366
378
367
379
/**
@@ -388,3 +400,5 @@ export class CommandKit extends EventEmitter {
388
400
}
389
401
}
390
402
}
403
+
404
+ export const commandkit = CommandKit . instance || new CommandKit ( ) ;
0 commit comments