Skip to content
This repository has been archived by the owner on Dec 24, 2021. It is now read-only.

Mention prefix option #349

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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 true;
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
18 changes: 14 additions & 4 deletions src/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,12 @@ 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;

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 +286,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
2 changes: 1 addition & 1 deletion src/extensions/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -113,7 +113,7 @@ declare module 'discord.js-commando' {
private _commandPatterns: object;
private _results: Map<string, CommandoMessage>;

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<void>;
private inhibit(cmdMsg: CommandoMessage): Inhibition;
Expand Down Expand Up @@ -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<SettingProvider>): Promise<void>;
Expand Down