From 7a6b78ad3476b3701ab4eb1d49312e3c6d55a5d0 Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Sat, 30 Jan 2021 03:18:08 +0100 Subject: [PATCH 1/8] feat(command parser): add regexPrefix client option With a `regexPrefix` you can have a natural language prefix for your bot BREAKING CHANGE: The events `prefixedMessage`, `unknownCommandName`, and `unknownCommand` 3rd parameter now have the type `string | RegExp` BREAKING CHANGE: `CommandContext.prefix` now has the type `string | RegExp` --- .../command-handler/CoreMessageParser.ts | 3 +++ .../command-handler/CorePrefixedMessage.ts | 5 +++-- src/lib/SapphireClient.ts | 19 +++++++++++++++++++ src/lib/structures/Command.ts | 6 ++++-- src/lib/types/Events.ts | 6 +++--- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/events/command-handler/CoreMessageParser.ts b/src/events/command-handler/CoreMessageParser.ts index 8bdf3c108..ac10992e9 100644 --- a/src/events/command-handler/CoreMessageParser.ts +++ b/src/events/command-handler/CoreMessageParser.ts @@ -19,6 +19,7 @@ export class CoreEvent extends Event { let prefix = null; const mentionPrefix = this.getMentionPrefix(message.content); + const { regexPrefix } = this.context.client.options; if (mentionPrefix) { if (message.content.length === mentionPrefix.length) { message.client.emit(Events.MentionPrefixOnly, message); @@ -26,6 +27,8 @@ export class CoreEvent extends Event { } prefix = mentionPrefix; + } else if (regexPrefix) { + prefix = regexPrefix; } else { const prefixes = await message.client.fetchPrefix(message); const parsed = this.getPrefix(message.content, prefixes); diff --git a/src/events/command-handler/CorePrefixedMessage.ts b/src/events/command-handler/CorePrefixedMessage.ts index 809f55526..e6b06b90a 100644 --- a/src/events/command-handler/CorePrefixedMessage.ts +++ b/src/events/command-handler/CorePrefixedMessage.ts @@ -8,9 +8,10 @@ export class CoreEvent extends Event { super(context, { event: Events.PrefixedMessage }); } - public run(message: Message, prefix: string) { + public run(message: Message, prefix: string | RegExp) { // Retrieve the command name and validate: - const prefixLess = message.content.slice(prefix.length).trim(); + const trimLength = prefix instanceof RegExp ? prefix.exec(message.content)?.[0].length ?? 0 : prefix.length; + const prefixLess = message.content.slice(trimLength).trim(); const spaceIndex = prefixLess.indexOf(' '); const name = spaceIndex === -1 ? prefixLess : prefixLess.slice(0, spaceIndex); if (!name) { diff --git a/src/lib/SapphireClient.ts b/src/lib/SapphireClient.ts index 66e635ed6..e40634709 100644 --- a/src/lib/SapphireClient.ts +++ b/src/lib/SapphireClient.ts @@ -48,6 +48,25 @@ export interface SapphireClientOptions { */ defaultPrefix?: SapphirePrefix; + /** + * The regex prefix, an alternative to a mention or regular prefix to allow creating natural language command messages + * @since 1.0.0 + * @default undefined + * @example + * ```ts + * /^(hey +)?(?:bot)[,! ]/i + * /** + * Matches: + * - hey bot, + * - hey bot! + * - hey bot + * - bot, + * - bot! + * - bot + * ``` + */ + regexPrefix?: RegExp; + /** * The prefix hook, by default it is a callback function that returns [[SapphireClientOptions.defaultPrefix]]. * @since 1.0.0 diff --git a/src/lib/structures/Command.ts b/src/lib/structures/Command.ts index 6eedc29be..d49b77ac1 100644 --- a/src/lib/structures/Command.ts +++ b/src/lib/structures/Command.ts @@ -138,9 +138,11 @@ export interface CommandOptions extends AliasPieceOptions { export interface CommandContext extends Record { /** - * The prefix used to run this command + * The prefix used to run this command. + * + * This is a string for the mention and default prefix, and a RegExp for the `regexPrefix`. */ - prefix: string; + prefix: string | RegExp; /** * The alias used to run this command */ diff --git a/src/lib/types/Events.ts b/src/lib/types/Events.ts index 5de2d62c8..9213811e2 100644 --- a/src/lib/types/Events.ts +++ b/src/lib/types/Events.ts @@ -120,9 +120,9 @@ declare module 'discord.js' { [Events.PiecePostLoad]: [Store, Piece]; [Events.MentionPrefixOnly]: [Message]; [Events.EventError]: [Error, EventErrorPayload]; - [Events.PrefixedMessage]: [Message, string]; - [Events.UnknownCommandName]: [Message, string]; - [Events.UnknownCommand]: [Message, string, string]; + [Events.PrefixedMessage]: [Message, string | RegExp]; + [Events.UnknownCommandName]: [Message, string | RegExp]; + [Events.UnknownCommand]: [Message, string, string | RegExp]; [Events.PreCommandRun]: [PreCommandRunPayload]; [Events.CommandDenied]: [UserError, CommandDeniedPayload]; [Events.CommandAccepted]: [CommandAcceptedPayload]; From 550aff19bdbaa1270a4b7cd5aab63486868f495c Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Sat, 30 Jan 2021 10:10:30 +0100 Subject: [PATCH 2/8] Update src/events/command-handler/CorePrefixedMessage.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/events/command-handler/CorePrefixedMessage.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/events/command-handler/CorePrefixedMessage.ts b/src/events/command-handler/CorePrefixedMessage.ts index e6b06b90a..ae8f22876 100644 --- a/src/events/command-handler/CorePrefixedMessage.ts +++ b/src/events/command-handler/CorePrefixedMessage.ts @@ -10,7 +10,7 @@ export class CoreEvent extends Event { public run(message: Message, prefix: string | RegExp) { // Retrieve the command name and validate: - const trimLength = prefix instanceof RegExp ? prefix.exec(message.content)?.[0].length ?? 0 : prefix.length; + const trimLength = typeof prefix === 'string' ? prefix.length : prefix.exec(message.content)?.[0].length ?? 0; const prefixLess = message.content.slice(trimLength).trim(); const spaceIndex = prefixLess.indexOf(' '); const name = spaceIndex === -1 ? prefixLess : prefixLess.slice(0, spaceIndex); From 3b6d567c8a999985c6442427bc400f86b1926928 Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Sat, 30 Jan 2021 10:10:48 +0100 Subject: [PATCH 3/8] Update src/lib/SapphireClient.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/lib/SapphireClient.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/SapphireClient.ts b/src/lib/SapphireClient.ts index e40634709..00ec69139 100644 --- a/src/lib/SapphireClient.ts +++ b/src/lib/SapphireClient.ts @@ -51,7 +51,6 @@ export interface SapphireClientOptions { /** * The regex prefix, an alternative to a mention or regular prefix to allow creating natural language command messages * @since 1.0.0 - * @default undefined * @example * ```ts * /^(hey +)?(?:bot)[,! ]/i From f4a15f0269e73860ca3b5a20b02b033e9c04375c Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Sat, 30 Jan 2021 10:11:11 +0100 Subject: [PATCH 4/8] Update src/lib/SapphireClient.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/lib/SapphireClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/SapphireClient.ts b/src/lib/SapphireClient.ts index 00ec69139..5da3cf88b 100644 --- a/src/lib/SapphireClient.ts +++ b/src/lib/SapphireClient.ts @@ -53,7 +53,7 @@ export interface SapphireClientOptions { * @since 1.0.0 * @example * ```ts - * /^(hey +)?(?:bot)[,! ]/i + * /^(hey +)?bot[,! ]/i * /** * Matches: * - hey bot, From d43a233752bfb999800fad9adfbbd29ff4b17481 Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Sat, 30 Jan 2021 10:11:28 +0100 Subject: [PATCH 5/8] Update src/lib/SapphireClient.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/lib/SapphireClient.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/SapphireClient.ts b/src/lib/SapphireClient.ts index 5da3cf88b..2681f0034 100644 --- a/src/lib/SapphireClient.ts +++ b/src/lib/SapphireClient.ts @@ -54,7 +54,7 @@ export interface SapphireClientOptions { * @example * ```ts * /^(hey +)?bot[,! ]/i - * /** + * * Matches: * - hey bot, * - hey bot! From 88386cb74b9e30de13c0ffc98ce554cea8809e85 Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Sat, 30 Jan 2021 10:11:47 +0100 Subject: [PATCH 6/8] Update src/lib/SapphireClient.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/lib/SapphireClient.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/lib/SapphireClient.ts b/src/lib/SapphireClient.ts index 2681f0034..f98329c17 100644 --- a/src/lib/SapphireClient.ts +++ b/src/lib/SapphireClient.ts @@ -55,13 +55,13 @@ export interface SapphireClientOptions { * ```ts * /^(hey +)?bot[,! ]/i * - * Matches: - * - hey bot, - * - hey bot! - * - hey bot - * - bot, - * - bot! - * - bot + * // Matches: + * // - hey bot, + * // - hey bot! + * // - hey bot + * // - bot, + * // - bot! + * // - bot * ``` */ regexPrefix?: RegExp; From 9864d916d2bad42b0d13d7d102fab5ddd74f0087 Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Sat, 30 Jan 2021 14:01:37 +0100 Subject: [PATCH 7/8] chore: fix it --- src/events/command-handler/CoreMessageParser.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/events/command-handler/CoreMessageParser.ts b/src/events/command-handler/CoreMessageParser.ts index ac10992e9..fa29eefaa 100644 --- a/src/events/command-handler/CoreMessageParser.ts +++ b/src/events/command-handler/CoreMessageParser.ts @@ -19,23 +19,24 @@ export class CoreEvent extends Event { let prefix = null; const mentionPrefix = this.getMentionPrefix(message.content); - const { regexPrefix } = this.context.client.options; + const { client } = this.context; + const { regexPrefix } = client.options; if (mentionPrefix) { if (message.content.length === mentionPrefix.length) { - message.client.emit(Events.MentionPrefixOnly, message); + client.emit(Events.MentionPrefixOnly, message); return; } prefix = mentionPrefix; - } else if (regexPrefix) { + } else if (regexPrefix && regexPrefix.test(message.content)) { prefix = regexPrefix; } else { - const prefixes = await message.client.fetchPrefix(message); + const prefixes = await client.fetchPrefix(message); const parsed = this.getPrefix(message.content, prefixes); if (parsed !== null) prefix = parsed; } - if (prefix !== null) message.client.emit(Events.PrefixedMessage, message, prefix); + if (prefix !== null) client.emit(Events.PrefixedMessage, message, prefix); } private async canRunInChannel(message: Message): Promise { From 12d5a77dee1a04992e3330cc308adb47d5946179 Mon Sep 17 00:00:00 2001 From: Jeroen Claassens Date: Sat, 30 Jan 2021 16:24:27 +0100 Subject: [PATCH 8/8] what's 2021 @kyranet? MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/events/command-handler/CoreMessageParser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/events/command-handler/CoreMessageParser.ts b/src/events/command-handler/CoreMessageParser.ts index fa29eefaa..7fec0e6c7 100644 --- a/src/events/command-handler/CoreMessageParser.ts +++ b/src/events/command-handler/CoreMessageParser.ts @@ -28,7 +28,7 @@ export class CoreEvent extends Event { } prefix = mentionPrefix; - } else if (regexPrefix && regexPrefix.test(message.content)) { + } else if (regexPrefix?.test(message.content)) { prefix = regexPrefix; } else { const prefixes = await client.fetchPrefix(message);