Skip to content

Commit

Permalink
feat(command): add CommandContext#commandPrefix (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet authored Feb 6, 2021
1 parent 11a6274 commit c8c7417
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
21 changes: 14 additions & 7 deletions src/events/command-handler/CorePrefixedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@ export class CoreEvent extends Event<Events.PrefixedMessage> {
public run(message: Message, prefix: string | RegExp) {
const { client, stores } = this.context;
// Retrieve the command name and validate:
const trimLength = typeof prefix === 'string' ? prefix.length : prefix.exec(message.content)?.[0].length ?? 0;
const prefixLess = message.content.slice(trimLength).trim();
const commandPrefix = this.getCommandPrefix(message.content, prefix);
const prefixLess = message.content.slice(commandPrefix.length).trim();

// The character that separates the command name from the arguments, this will return -1 when '[p]command' is
// passed, and a non -1 value when '[p]command arg' is passed instead.
const spaceIndex = prefixLess.indexOf(' ');
const name = spaceIndex === -1 ? prefixLess : prefixLess.slice(0, spaceIndex);
if (!name) {
const commandName = spaceIndex === -1 ? prefixLess : prefixLess.slice(0, spaceIndex);
if (!commandName) {
client.emit(Events.UnknownCommandName, message, prefix);
return;
}

// Retrieve the command and validate:
const command = stores.get('commands').get(client.options.caseInsensitiveCommands ? name.toLowerCase() : name);
const command = stores.get('commands').get(client.options.caseInsensitiveCommands ? commandName.toLowerCase() : commandName);
if (!command) {
client.emit(Events.UnknownCommand, message, name, prefix);
client.emit(Events.UnknownCommand, message, commandName, prefix);
return;
}

// Run the last stage before running the command:
const parameters = spaceIndex === -1 ? '' : prefixLess.substr(spaceIndex + 1).trim();
client.emit(Events.PreCommandRun, { message, command, parameters, context: { commandName: name, prefix } });
client.emit(Events.PreCommandRun, { message, command, parameters, context: { commandName, commandPrefix, prefix } });
}

private getCommandPrefix(content: string, prefix: string | RegExp): string {
return typeof prefix === 'string' ? prefix : prefix.exec(content)![0];
}
}
7 changes: 6 additions & 1 deletion src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ export interface CommandContext extends Record<PropertyKey, unknown> {
*/
prefix: string | RegExp;
/**
* The alias used to run this command
* The alias used to run this command.
*/
commandName: string;
/**
* The matched prefix, this will always be the same as [[CommandContext.prefix]] if it was a string, otherwise it is
* the result of doing `prefix.exec(content)[0]`.
*/
commandPrefix: string;
}

0 comments on commit c8c7417

Please sign in to comment.