File tree Expand file tree Collapse file tree 4 files changed +32
-10
lines changed Expand file tree Collapse file tree 4 files changed +32
-10
lines changed Original file line number Diff line number Diff line change @@ -69,11 +69,17 @@ const AIConfig: Required<ConfigureAI> = {
69
69
const prefixOrPrefixes =
70
70
await commandkit . appConfig . getMessageCommandPrefix ( message ) ;
71
71
72
- const prefixes = Array . isArray ( prefixOrPrefixes )
73
- ? prefixOrPrefixes
74
- : [ prefixOrPrefixes ] ;
72
+ const prefixes =
73
+ prefixOrPrefixes instanceof RegExp
74
+ ? prefixOrPrefixes
75
+ : Array . isArray ( prefixOrPrefixes )
76
+ ? prefixOrPrefixes
77
+ : [ prefixOrPrefixes ] ;
75
78
76
- const possiblyCommand = prefixes . some ( ( p ) => message . content . startsWith ( p ) ) ;
79
+ const possiblyCommand =
80
+ prefixes instanceof RegExp
81
+ ? prefixes . test ( message . content )
82
+ : prefixes . some ( ( p ) => message . content . startsWith ( p ) ) ;
77
83
78
84
// TODO: figure out a workaround for mention prefixes
79
85
return (
Original file line number Diff line number Diff line change @@ -56,12 +56,12 @@ export class MessageCommandParser {
56
56
/**
57
57
* Creates a new message command parser.
58
58
* @param message - The Discord message to parse
59
- * @param prefix - Array of valid command prefixes
59
+ * @param prefix - Array of valid command prefixes or a regular expression
60
60
* @param schema - Function that returns the options schema for a command
61
61
*/
62
62
public constructor (
63
63
public message : Message ,
64
- private prefix : string [ ] ,
64
+ private prefix : string [ ] | RegExp ,
65
65
private schema : ( command : string ) => MessageCommandOptionsSchema ,
66
66
) { }
67
67
@@ -123,6 +123,11 @@ export class MessageCommandParser {
123
123
* @returns The matched prefix or undefined
124
124
*/
125
125
public getPrefix ( ) {
126
+ if ( this . prefix instanceof RegExp ) {
127
+ const match = this . message . content . match ( this . prefix ) ;
128
+ return match ?. [ 0 ] ;
129
+ }
130
+
126
131
for ( const p of this . prefix ) {
127
132
if ( this . message . content . startsWith ( p ) ) {
128
133
return p ;
Original file line number Diff line number Diff line change @@ -450,11 +450,20 @@ export class AppCommandHandler {
450
450
const prefix =
451
451
await this . commandkit . appConfig . getMessageCommandPrefix ( source ) ;
452
452
453
- if ( ! prefix || ! prefix . length ) return null ;
453
+ if (
454
+ ! prefix ||
455
+ ( ( typeof prefix === 'string' || Array . isArray ( prefix ) ) &&
456
+ ! prefix . length )
457
+ )
458
+ return null ;
454
459
455
460
parser = new MessageCommandParser (
456
461
source ,
457
- Array . isArray ( prefix ) ? prefix : [ prefix ] ,
462
+ prefix instanceof RegExp
463
+ ? prefix
464
+ : Array . isArray ( prefix )
465
+ ? prefix
466
+ : [ prefix ] ,
458
467
( command : string ) => {
459
468
// Find the command by name
460
469
const loadedCommand = this . findCommandByName ( command ) ;
Original file line number Diff line number Diff line change @@ -39,7 +39,9 @@ export interface CommandKitConfiguration {
39
39
* @param message The message to get the command prefix for.
40
40
* @returns The command prefix or an array of prefixes.
41
41
*/
42
- getMessageCommandPrefix : ( message : Message ) => Awaitable < string | string [ ] > ;
42
+ getMessageCommandPrefix : (
43
+ message : Message ,
44
+ ) => Awaitable < string | string [ ] | RegExp > ;
43
45
}
44
46
45
47
/**
@@ -321,7 +323,7 @@ export class CommandKit extends EventEmitter {
321
323
* @param resolver The resolver function.
322
324
*/
323
325
setPrefixResolver (
324
- resolver : ( message : Message ) => Awaitable < string | string [ ] > ,
326
+ resolver : ( message : Message ) => Awaitable < string | string [ ] | RegExp > ,
325
327
) {
326
328
this . appConfig . getMessageCommandPrefix = resolver ;
327
329
return this ;
You can’t perform that action at this time.
0 commit comments