From 93ad9c425b562ac7666ef61605918bba99cd9828 Mon Sep 17 00:00:00 2001 From: Magnus Jensen Date: Wed, 23 Dec 2020 11:04:47 +0100 Subject: [PATCH 1/8] Added the docs/check for if mentionPrefix is disabled. --- src/client.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/client.js b/src/client.js index f2e46c9e8..ebd8a2605 100644 --- a/src/client.js +++ b/src/client.js @@ -16,6 +16,7 @@ class CommandoClient extends discord.Client { * @property {boolean} [nonCommandEditable=true] - Whether messages without commands can be edited to a command * @property {string|string[]|Set} [owner] - ID of the bot owner's Discord user, or multiple IDs * @property {string} [invite] - Invite URL to the bot's support server + * @property {boolean} [mentionPrefix=true] Enable whether the bot mention, should also be a command prefix. */ /** @@ -26,6 +27,7 @@ class CommandoClient extends discord.Client { if(options.commandPrefix === null) options.commandPrefix = ''; if(typeof options.commandEditableDuration === 'undefined') options.commandEditableDuration = 30; if(typeof options.nonCommandEditable === 'undefined') options.nonCommandEditable = true; + if(options.mentionPrefix === null) options.mentionPrefix = true; super(options); /** @@ -166,6 +168,15 @@ class CommandoClient extends discord.Client { return undefined; } + get mentionPrefix() { + if(!this.options.mentionPrefix) return null; + return this.options.mentionPrefix; + } + + set mentionPrefix(value) { + this.options.mentionPrefix = value; + } + async destroy() { await super.destroy(); if(this.provider) await this.provider.destroy(); From be32ea793cdc51d1cff5b633148b5e44c6746cf2 Mon Sep 17 00:00:00 2001 From: Magnus Jensen Date: Wed, 23 Dec 2020 11:05:57 +0100 Subject: [PATCH 2/8] Added the check for mention prefix, in the usage command, so it doesn't display @mention as a usage --- src/commands/base.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/commands/base.js b/src/commands/base.js index 822134f33..875d29e9b 100644 --- a/src/commands/base.js +++ b/src/commands/base.js @@ -476,9 +476,10 @@ class Command { * @param {string} command - A command + arg string * @param {string} [prefix] - Prefix to use for the prefixed command format * @param {User} [user] - User to use for the mention command format + * @param {boolean} [mentionPrefix] - Value, wether or not mention as prefix is enabled. * @return {string} */ - static usage(command, prefix = null, user = null) { + static usage(command, prefix = null, user = null, mentionPrefix = null) { const nbcmd = command.replace(/ /g, '\xa0'); if(!prefix && !user) return `\`\`${nbcmd}\`\``; @@ -490,9 +491,11 @@ class Command { } let mentionPart; - if(user) mentionPart = `\`\`@${user.username.replace(/ /g, '\xa0')}#${user.discriminator}\xa0${nbcmd}\`\``; + if(user && mentionPrefix) { + mentionPart = `\`\`@${user.username.replace(/ /g, '\xa0')}#${user.discriminator}\xa0${nbcmd}\`\``; + } - return `${prefixPart || ''}${prefix && user ? ' or ' : ''}${mentionPart || ''}`; + return `${prefixPart || ''}${prefix && user && mentionPrefix ? ' or ' : ''}${mentionPart || ''}`; } /** From 1c34cf85819318e537141f2beb418d5a3eb650c8 Mon Sep 17 00:00:00 2001 From: Magnus Jensen Date: Wed, 23 Dec 2020 11:08:12 +0100 Subject: [PATCH 3/8] Added the mentionPrefix as a argument to buildCommandPattern, and added extra checks, to build the regex, depending on the mentionPrefix value, and added a default prefix ! if both mentionPrefix is disabled and no prefix is set. --- src/dispatcher.js | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/dispatcher.js b/src/dispatcher.js index d235476f8..e640f4245 100644 --- a/src/dispatcher.js +++ b/src/dispatcher.js @@ -252,9 +252,14 @@ class CommandDispatcher { } } + // Find the command to run with default command handling const prefix = message.guild ? message.guild.commandPrefix : this.client.commandPrefix; - if(!this._commandPatterns[prefix]) this.buildCommandPattern(prefix); + const mentionAsPrefix = this.client.mentionPrefix; + + console.log(prefix); + console.log(this._commandPatterns[prefix]); + if(!this._commandPatterns[prefix]) this.buildCommandPattern(prefix, mentionAsPrefix); let cmdMsg = this.matchDefault(message, this._commandPatterns[prefix], 2); if(!cmdMsg && !message.guild) cmdMsg = this.matchDefault(message, /^([^\s]+)/i, 1, true); return cmdMsg; @@ -283,18 +288,25 @@ class CommandDispatcher { /** * Creates a regular expression to match the command prefix and name in a message * @param {?string} prefix - Prefix to build the pattern for + * @param {boolean} mentionPrefix - Boolean value, for mention as a prefix. * @return {RegExp} * @private */ - buildCommandPattern(prefix) { + buildCommandPattern(prefix, mentionPrefix) { let pattern; - if(prefix) { + if(prefix && mentionPrefix) { const escapedPrefix = escapeRegex(prefix); pattern = new RegExp( `^(<@!?${this.client.user.id}>\\s+(?:${escapedPrefix}\\s*)?|${escapedPrefix}\\s*)([^\\s]+)`, 'i' ); - } else { + } else if(!prefix && mentionPrefix) { pattern = new RegExp(`(^<@!?${this.client.user.id}>\\s+)([^\\s]+)`, 'i'); + } else if(prefix && !mentionPrefix) { + const escapedPrefix = escapeRegex(prefix); + pattern = new RegExp(`((?:${escapedPrefix}\\s*)?|${escapedPrefix}\\s*)([^\\s]+)`, 'i'); + } else { + const escapedDefaultPrefix = escapeRegex('!'); + pattern = new RegExp(`((?:${escapedDefaultPrefix}\\s*)?|${escapedDefaultPrefix}\\s*)([^\\s]+)`, 'i'); } this._commandPatterns[prefix] = pattern; this.client.emit('debug', `Built command pattern for prefix "${prefix}": ${pattern}`); From 57ed336427ffbdc2db94e211e9162d7f4d678218 Mon Sep 17 00:00:00 2001 From: Magnus Jensen Date: Wed, 23 Dec 2020 11:09:21 +0100 Subject: [PATCH 4/8] added the argument mentionPrefix to Command.usage --- src/extensions/message.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extensions/message.js b/src/extensions/message.js index ddd24b1ca..55649cb0a 100644 --- a/src/extensions/message.js +++ b/src/extensions/message.js @@ -95,7 +95,7 @@ module.exports = Structures.extend('Message', Message => { if(this.guild) prefix = this.guild.commandPrefix; else prefix = this.client.commandPrefix; } - return Command.usage(command, prefix, user); + return Command.usage(command, prefix, user, this.client.mentionPrefix); } /** From 33a5874a6ec83c55e39f236cc0509effe65f0d3e Mon Sep 17 00:00:00 2001 From: Magnus Jensen Date: Wed, 23 Dec 2020 11:09:53 +0100 Subject: [PATCH 5/8] added typings for the added mentionPrefix --- typings/index.d.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index 5813fcdc2..a3d36d585 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -103,7 +103,7 @@ declare module 'discord.js-commando' { public unload(): void; public usage(argString?: string, prefix?: string, user?: User): string; - public static usage(command: string, prefix?: string, user?: User): string; + public static usage(command: string, prefix?: string, user?: User, mentionPrefix?: boolean): string; } export class CommandDispatcher { @@ -113,7 +113,7 @@ declare module 'discord.js-commando' { private _commandPatterns: object; private _results: Map; - private buildCommandPattern(prefix: string): RegExp; + private buildCommandPattern(prefix: string, mentionPrefix: boolean): RegExp; private cacheCommandoMessage(message: Message, oldMessage: Message, cmdMsg: CommandoMessage, responses: Message | Message[]): void; private handleMessage(messge: Message, oldMessage?: Message): Promise; private inhibit(cmdMsg: CommandoMessage): Inhibition; @@ -223,6 +223,7 @@ declare module 'discord.js-commando' { public provider: SettingProvider; public registry: CommandoRegistry; public settings: GuildSettingsHelper; + public mentionPrefix: boolean; public isOwner(user: UserResolvable): boolean; public setProvider(provider: SettingProvider | Promise): Promise; From e4fd992eb18c8aff112024622de99879d444f401 Mon Sep 17 00:00:00 2001 From: Magnus Jensen Date: Wed, 23 Dec 2020 22:02:52 +0100 Subject: [PATCH 6/8] made default return value, be true, when getting the mention prefix. If none is set. --- src/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.js b/src/client.js index ebd8a2605..da10dd598 100644 --- a/src/client.js +++ b/src/client.js @@ -169,7 +169,7 @@ class CommandoClient extends discord.Client { } get mentionPrefix() { - if(!this.options.mentionPrefix) return null; + if(!this.options.mentionPrefix) return true; return this.options.mentionPrefix; } From dc9450ccb2e0fc1a9e85e44ce2c7eb13f5e3b2c7 Mon Sep 17 00:00:00 2001 From: Magnus Jensen Date: Wed, 23 Dec 2020 22:03:03 +0100 Subject: [PATCH 7/8] removed test, logs. --- src/dispatcher.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/dispatcher.js b/src/dispatcher.js index e640f4245..02427bdc8 100644 --- a/src/dispatcher.js +++ b/src/dispatcher.js @@ -257,8 +257,6 @@ class CommandDispatcher { const prefix = message.guild ? message.guild.commandPrefix : this.client.commandPrefix; const mentionAsPrefix = this.client.mentionPrefix; - console.log(prefix); - console.log(this._commandPatterns[prefix]); if(!this._commandPatterns[prefix]) this.buildCommandPattern(prefix, mentionAsPrefix); let cmdMsg = this.matchDefault(message, this._commandPatterns[prefix], 2); if(!cmdMsg && !message.guild) cmdMsg = this.matchDefault(message, /^([^\s]+)/i, 1, true); From f59bd78384cc8bf8dd978a0d964cef48596e8dbd Mon Sep 17 00:00:00 2001 From: Magnus Jensen Date: Thu, 24 Dec 2020 12:24:45 +0100 Subject: [PATCH 8/8] Remove the check, since we are working with boolean values. And we check for null in the constructor of the client. --- src/client.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/client.js b/src/client.js index da10dd598..7e0b45502 100644 --- a/src/client.js +++ b/src/client.js @@ -169,7 +169,6 @@ class CommandoClient extends discord.Client { } get mentionPrefix() { - if(!this.options.mentionPrefix) return true; return this.options.mentionPrefix; }