Skip to content

Commit 190ffa5

Browse files
committed
feat: regex prefix
1 parent 862e336 commit 190ffa5

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

packages/ai/src/configure.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,17 @@ const AIConfig: Required<ConfigureAI> = {
6969
const prefixOrPrefixes =
7070
await commandkit.appConfig.getMessageCommandPrefix(message);
7171

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];
7578

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));
7783

7884
// TODO: figure out a workaround for mention prefixes
7985
return (

packages/commandkit/src/app/commands/MessageCommandParser.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ export class MessageCommandParser {
5656
/**
5757
* Creates a new message command parser.
5858
* @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
6060
* @param schema - Function that returns the options schema for a command
6161
*/
6262
public constructor(
6363
public message: Message,
64-
private prefix: string[],
64+
private prefix: string[] | RegExp,
6565
private schema: (command: string) => MessageCommandOptionsSchema,
6666
) {}
6767

@@ -123,6 +123,11 @@ export class MessageCommandParser {
123123
* @returns The matched prefix or undefined
124124
*/
125125
public getPrefix() {
126+
if (this.prefix instanceof RegExp) {
127+
const match = this.message.content.match(this.prefix);
128+
return match?.[0];
129+
}
130+
126131
for (const p of this.prefix) {
127132
if (this.message.content.startsWith(p)) {
128133
return p;

packages/commandkit/src/app/handlers/AppCommandHandler.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,11 +450,20 @@ export class AppCommandHandler {
450450
const prefix =
451451
await this.commandkit.appConfig.getMessageCommandPrefix(source);
452452

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;
454459

455460
parser = new MessageCommandParser(
456461
source,
457-
Array.isArray(prefix) ? prefix : [prefix],
462+
prefix instanceof RegExp
463+
? prefix
464+
: Array.isArray(prefix)
465+
? prefix
466+
: [prefix],
458467
(command: string) => {
459468
// Find the command by name
460469
const loadedCommand = this.findCommandByName(command);

packages/commandkit/src/commandkit.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ export interface CommandKitConfiguration {
3939
* @param message The message to get the command prefix for.
4040
* @returns The command prefix or an array of prefixes.
4141
*/
42-
getMessageCommandPrefix: (message: Message) => Awaitable<string | string[]>;
42+
getMessageCommandPrefix: (
43+
message: Message,
44+
) => Awaitable<string | string[] | RegExp>;
4345
}
4446

4547
/**
@@ -321,7 +323,7 @@ export class CommandKit extends EventEmitter {
321323
* @param resolver The resolver function.
322324
*/
323325
setPrefixResolver(
324-
resolver: (message: Message) => Awaitable<string | string[]>,
326+
resolver: (message: Message) => Awaitable<string | string[] | RegExp>,
325327
) {
326328
this.appConfig.getMessageCommandPrefix = resolver;
327329
return this;

0 commit comments

Comments
 (0)