@@ -151,14 +151,6 @@ export class CommandClient extends Client implements CommandClientOptions {
151
151
this . globalCommandCooldown = options . globalCommandCooldown ?? 0
152
152
this . globalCooldown = options . globalCooldown ?? 0
153
153
154
- const self = this as any
155
- if ( self . _decoratedCommands !== undefined ) {
156
- Object . values ( self . _decoratedCommands ) . forEach ( ( entry : any ) => {
157
- this . commands . add ( entry )
158
- } )
159
- self . _decoratedCommands = undefined
160
- }
161
-
162
154
this . on (
163
155
'messageCreate' ,
164
156
async ( msg : Message ) => await this . processMessage ( msg )
@@ -527,49 +519,62 @@ export class CommandClient extends Client implements CommandClientOptions {
527
519
* Command decorator. Decorates the function with optional metadata as a Command registered upon constructing class.
528
520
*/
529
521
export function command ( options ?: CommandOptions ) {
530
- return function ( target : CommandClient | Extension , name : string ) {
531
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
532
- const c = target as any
533
- if ( c . _decoratedCommands === undefined ) c . _decoratedCommands = { }
534
-
535
- const prop = c [ name ]
536
-
537
- if ( typeof prop !== 'function' )
538
- throw new Error ( '@command decorator can only be used on class methods' )
539
-
540
- const command = new Command ( )
541
-
542
- command . name = name
543
- command . execute = prop
544
-
545
- if ( options !== undefined ) Object . assign ( command , options )
522
+ return function (
523
+ original : ( ...args : any [ ] ) => any ,
524
+ {
525
+ name,
526
+ addInitializer,
527
+ private : _private
528
+ } : ClassMethodDecoratorContext < CommandClient | Extension >
529
+ ) {
530
+ if ( _private === true )
531
+ throw new TypeError ( 'Not supported on private methods.' )
532
+
533
+ addInitializer ( function ( ) {
534
+ const command = new Command ( )
535
+
536
+ command . name = name . toString ( )
537
+ command . execute = original . bind ( this )
538
+
539
+ if ( options !== undefined ) Object . assign ( command , options )
540
+
541
+ if ( this instanceof Extension ) {
542
+ command . extension = this
543
+ }
546
544
547
- if ( target instanceof Extension ) command . extension = target
545
+ this . commands . add ( command )
546
+ } )
548
547
549
- c . _decoratedCommands [ command . name ] = command
548
+ return original
550
549
}
551
550
}
552
551
553
552
/**
554
553
* Sub Command decorator. Decorates the function with optional metadata as a Sub Command registered upon constructing class.
555
554
*/
556
555
export function subcommand ( options ?: CommandOptions ) {
557
- return function ( target : Command , name : string ) {
558
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
559
- const c = target as any
560
- if ( c . _decoratedSubCommands === undefined ) c . _decoratedSubCommands = [ ]
561
-
562
- const prop = c [ name ]
563
-
564
- if ( typeof prop !== 'function' )
565
- throw new Error ( '@command decorator can only be used on class methods' )
566
-
567
- const command = new Command ( )
568
-
569
- command . name = name
570
- command . execute = prop
571
-
572
- if ( options !== undefined ) Object . assign ( command , options )
573
- c . _decoratedSubCommands . push ( command )
556
+ return function (
557
+ original : ( ...args : any [ ] ) => any ,
558
+ {
559
+ name,
560
+ addInitializer,
561
+ private : _private
562
+ } : ClassMethodDecoratorContext < Command >
563
+ ) {
564
+ if ( _private === true )
565
+ throw new TypeError ( 'Not supported on private methods.' )
566
+
567
+ addInitializer ( function ( ) {
568
+ const command = new Command ( )
569
+
570
+ command . name = name . toString ( )
571
+ command . execute = original . bind ( this )
572
+
573
+ if ( options !== undefined ) Object . assign ( command , options )
574
+ if ( this . subCommands === undefined ) this . subCommands = [ ]
575
+ this . subCommands . push ( command )
576
+ } )
577
+
578
+ return original
574
579
}
575
580
}
0 commit comments