Skip to content
This repository was archived by the owner on Dec 24, 2021. It is now read-only.
11 changes: 11 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} [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.
*/

/**
Expand All @@ -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);

/**
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 6 additions & 3 deletions src/commands/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}\`\``;

Expand All @@ -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 || ''}`;
}

/**
Expand Down
20 changes: 16 additions & 4 deletions src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}`);
Expand Down