@@ -7,9 +7,15 @@ export interface CommandRegistration {
7
7
listener : ( msg : Message , content : string ) => Promise < void > ;
8
8
}
9
9
10
+ interface Command {
11
+ admin : boolean ;
12
+ aliases : string [ ] ;
13
+ description ?: string ;
14
+ listener : ( msg : Message , content : string ) => Promise < void > ;
15
+ }
16
+
10
17
export class Bot {
11
- commands : CommandRegistration [ ] = [ ] ;
12
- adminCommands : CommandRegistration [ ] = [ ] ;
18
+ commands = new Map < string , Command > ( ) ;
13
19
14
20
constructor ( public client : Client < true > ) {
15
21
client . on ( 'messageCreate' , msg => {
@@ -26,11 +32,7 @@ export class Bot {
26
32
triggerWithPrefix . substring ( matchingPrefix . length ) ,
27
33
) ;
28
34
29
- if (
30
- ! command ||
31
- ( this . adminCommands . includes ( command ) &&
32
- ! this . isAdmin ( msg . author ) )
33
- ) {
35
+ if ( ! command || ( command . admin && ! this . isAdmin ( msg . author ) ) ) {
34
36
return ;
35
37
}
36
38
command . listener ( msg , content ) . catch ( err => {
@@ -40,17 +42,28 @@ export class Bot {
40
42
} ) ;
41
43
}
42
44
43
- registerCommand ( command : CommandRegistration ) {
44
- this . commands . push ( command ) ;
45
+ registerCommand ( registration : CommandRegistration ) {
46
+ const command : Command = {
47
+ ...registration ,
48
+ admin : false ,
49
+ } ;
50
+ for ( const a of command . aliases ) {
51
+ this . commands . set ( a , command ) ;
52
+ }
45
53
}
46
54
47
- registerAdminCommand ( command : CommandRegistration ) {
48
- this . adminCommands . push ( command ) ;
55
+ registerAdminCommand ( registration : CommandRegistration ) {
56
+ const command : Command = {
57
+ ...registration ,
58
+ admin : true ,
59
+ } ;
60
+ for ( const a of command . aliases ) {
61
+ this . commands . set ( a , command ) ;
62
+ }
49
63
}
50
64
51
- getByTrigger ( trigger : string ) : CommandRegistration | undefined {
52
- const match = ( c : CommandRegistration ) => c . aliases . includes ( trigger ) ;
53
- return this . commands . find ( match ) || this . adminCommands . find ( match ) ;
65
+ getByTrigger ( trigger : string ) : Command | undefined {
66
+ return this . commands . get ( trigger ) ;
54
67
}
55
68
56
69
isMod ( member : GuildMember | null ) {
@@ -80,18 +93,18 @@ export class Bot {
80
93
const mentioned = msg . mentions . members ?. first ( ) ?. user ;
81
94
if ( mentioned ) return mentioned ;
82
95
83
- if ( query ) {
84
- // Search by ID
85
- const queriedUser = await this . client . users
86
- . fetch ( query )
87
- . catch ( ( ) => undefined ) ;
88
- if ( queriedUser ) return queriedUser ;
89
-
90
- // Search by name, likely a better way to do this...
91
- for ( const user of this . client . users . cache . values ( ) ) {
92
- if ( user . tag === query || user . username === query ) {
93
- return user ;
94
- }
96
+ if ( ! query ) return ;
97
+
98
+ // Search by ID
99
+ const queriedUser = await this . client . users
100
+ . fetch ( query )
101
+ . catch ( ( ) => undefined ) ;
102
+ if ( queriedUser ) return queriedUser ;
103
+
104
+ // Search by name, likely a better way to do this...
105
+ for ( const user of this . client . users . cache . values ( ) ) {
106
+ if ( user . tag === query || user . username === query ) {
107
+ return user ;
95
108
}
96
109
}
97
110
}
0 commit comments