Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(command parser): add regexPrefix client option #145

Merged
merged 9 commits into from
Jan 30, 2021
Merged
3 changes: 3 additions & 0 deletions src/events/command-handler/CoreMessageParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ export class CoreEvent extends Event<Events.Message> {

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);
return;
}

prefix = mentionPrefix;
} else if (regexPrefix) {
prefix = regexPrefix;
} else {
const prefixes = await message.client.fetchPrefix(message);
const parsed = this.getPrefix(message.content, prefixes);
Expand Down
5 changes: 3 additions & 2 deletions src/events/command-handler/CorePrefixedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export class CoreEvent extends Event<Events.PrefixedMessage> {
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;
favna marked this conversation as resolved.
Show resolved Hide resolved
const prefixLess = message.content.slice(trimLength).trim();
const spaceIndex = prefixLess.indexOf(' ');
const name = spaceIndex === -1 ? prefixLess : prefixLess.slice(0, spaceIndex);
if (!name) {
Expand Down
19 changes: 19 additions & 0 deletions src/lib/SapphireClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
favna marked this conversation as resolved.
Show resolved Hide resolved
* @example
* ```ts
* /^(hey +)?(?:bot)[,! ]/i
favna marked this conversation as resolved.
Show resolved Hide resolved
* /**
favna marked this conversation as resolved.
Show resolved Hide resolved
* Matches:
* - hey bot,
* - hey bot!
* - hey bot
* - bot,
* - bot!
* - bot
favna marked this conversation as resolved.
Show resolved Hide resolved
* ```
*/
regexPrefix?: RegExp;

/**
* The prefix hook, by default it is a callback function that returns [[SapphireClientOptions.defaultPrefix]].
* @since 1.0.0
Expand Down
6 changes: 4 additions & 2 deletions src/lib/structures/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ export interface CommandOptions extends AliasPieceOptions {

export interface CommandContext extends Record<PropertyKey, unknown> {
/**
* 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
*/
Expand Down
6 changes: 3 additions & 3 deletions src/lib/types/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ declare module 'discord.js' {
[Events.PiecePostLoad]: [Store<Piece>, 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];
Expand Down