@@ -12,12 +12,13 @@ import {
12
12
import { parseArgs } from '../utils/command.ts'
13
13
import { Extension , ExtensionsManager } from './extension.ts'
14
14
15
- type PrefixReturnType = string | string [ ] | Promise < string | string [ ] >
15
+ type PrefixType = string | RegExp | Array < string | RegExp >
16
+ type PrefixReturnType = PrefixType | Promise < PrefixType >
16
17
17
18
/** Command Client options extending Client Options to provide a lot of Commands-related customizations */
18
19
export interface CommandClientOptions extends ClientOptions {
19
- /** Global prefix(s) of the bot. */
20
- prefix : string | string [ ]
20
+ /** Global prefix(s) of the bot. Can be a string, a regular expression, or an array including either. */
21
+ prefix : PrefixType
21
22
/** Whether to enable mention prefix or not. */
22
23
mentionPrefix ?: boolean
23
24
/** Method to get a Guild's custom prefix(s). */
@@ -57,7 +58,7 @@ export type CommandContextMiddlewareNext = () => unknown | Promise<unknown>
57
58
* See InteractionsClient (`Client#slash`) for more info about Slash Commands.
58
59
*/
59
60
export class CommandClient extends Client implements CommandClientOptions {
60
- prefix : string | string [ ]
61
+ prefix : PrefixType
61
62
mentionPrefix : boolean
62
63
63
64
getGuildPrefix : ( guildID : string ) => PrefixReturnType
@@ -167,20 +168,23 @@ export class CommandClient extends Client implements CommandClientOptions {
167
168
if ( isGuildBlacklisted ) return
168
169
}
169
170
170
- let prefix : string | string [ ] = [ ]
171
- if ( typeof this . prefix === 'string' ) prefix = [ ...prefix , this . prefix ]
171
+ let prefix : PrefixType = [ ]
172
+ if ( typeof this . prefix === 'string' || this . prefix instanceof RegExp )
173
+ prefix = [ ...prefix , this . prefix ]
172
174
else prefix = [ ...prefix , ...this . prefix ]
173
175
174
176
const userPrefix = await this . getUserPrefix ( msg . author . id )
175
177
if ( userPrefix !== undefined ) {
176
- if ( typeof userPrefix === 'string' ) prefix = [ ...prefix , userPrefix ]
178
+ if ( typeof userPrefix === 'string' || userPrefix instanceof RegExp )
179
+ prefix = [ ...prefix , userPrefix ]
177
180
else prefix = [ ...prefix , ...userPrefix ]
178
181
}
179
182
180
183
if ( msg . guild !== undefined ) {
181
184
const guildPrefix = await this . getGuildPrefix ( msg . guild . id )
182
185
if ( guildPrefix !== undefined ) {
183
- if ( typeof guildPrefix === 'string' ) prefix = [ ...prefix , guildPrefix ]
186
+ if ( typeof guildPrefix === 'string' || guildPrefix instanceof RegExp )
187
+ prefix = [ ...prefix , guildPrefix ]
184
188
else prefix = [ ...prefix , ...guildPrefix ]
185
189
}
186
190
}
@@ -189,9 +193,22 @@ export class CommandClient extends Client implements CommandClientOptions {
189
193
190
194
let mentionPrefix = false
191
195
192
- let usedPrefix = prefix
193
- . filter ( ( v ) => msg . content . startsWith ( v ) )
194
- . sort ( ( b , a ) => a . length - b . length ) [ 0 ]
196
+ const usedPrefixes = [ ]
197
+ for ( const p of prefix ) {
198
+ if ( typeof p === 'string' ) {
199
+ if ( msg . content . startsWith ( p ) ) {
200
+ usedPrefixes . push ( p )
201
+ }
202
+ } else {
203
+ const match = msg . content . match ( p )
204
+ // The regex matches and is at the start of the message
205
+ if ( match !== null && match . index === 0 ) {
206
+ usedPrefixes . push ( match [ 0 ] )
207
+ }
208
+ }
209
+ }
210
+
211
+ let usedPrefix = usedPrefixes . sort ( ( b , a ) => a . length - b . length ) [ 0 ]
195
212
if ( usedPrefix === undefined && this . mentionPrefix ) mentionPrefix = true
196
213
197
214
if ( mentionPrefix ) {
0 commit comments