From 327f4b3fcf90b10d6c943e3c00b830c7edf9e22c Mon Sep 17 00:00:00 2001 From: Revive <53571319+DocRevive@users.noreply.github.com> Date: Mon, 10 Oct 2022 03:54:50 -0400 Subject: [PATCH 1/2] Fix command delays --- src/CommandManager.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/CommandManager.ts b/src/CommandManager.ts index 5adab49..686965b 100644 --- a/src/CommandManager.ts +++ b/src/CommandManager.ts @@ -252,15 +252,6 @@ export class CommandManager< const filterContext = context[0]; for (const command of this.commands) { - if (command.preFilters.length) { - let passed = false; - for (const filter of command.preFilters) { - passed = await filter(command, filterContext as TContext); - if (!passed) break; - } - if (!passed) continue; - } - const matchedCommand = await this.tryMatchingCommand(command, str, filterContext as TContext); if (matchedCommand === null) continue; @@ -270,6 +261,15 @@ export class CommandManager< continue; } + if (command.preFilters.length) { + let passed = false; + for (const filter of command.preFilters) { + passed = await filter(command, filterContext as TContext); + if (!passed) break; + } + if (!passed) continue; + } + onlyErrors = false; if (command.postFilters.length) { From 6b6bf01bb99fe83d75b4a7db097d074c74ac78f0 Mon Sep 17 00:00:00 2001 From: Revive <53571319+DocRevive@users.noreply.github.com> Date: Tue, 11 Oct 2022 20:26:54 -0400 Subject: [PATCH 2/2] undo previous, new approach split tryMatchingCommand into prefix & name check and signature validation --- src/CommandManager.ts | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/CommandManager.ts b/src/CommandManager.ts index 686965b..46eb788 100644 --- a/src/CommandManager.ts +++ b/src/CommandManager.ts @@ -252,14 +252,8 @@ export class CommandManager< const filterContext = context[0]; for (const command of this.commands) { - const matchedCommand = await this.tryMatchingCommand(command, str, filterContext as TContext); - if (matchedCommand === null) continue; - - if (isError(matchedCommand)) { - lastError = matchedCommand.error; - lastErrorCmd = command; - continue; - } + const argString = this.takeCommandArguments(command, str); + if (argString === null) continue; if (command.preFilters.length) { let passed = false; @@ -270,6 +264,15 @@ export class CommandManager< if (!passed) continue; } + const matchedCommand = await this.tryMatchingSignature(command, argString, filterContext as TContext); + if (matchedCommand === null) continue; + + if (isError(matchedCommand)) { + lastError = matchedCommand.error; + lastErrorCmd = command; + continue; + } + onlyErrors = false; if (command.postFilters.length) { @@ -295,13 +298,9 @@ export class CommandManager< } /** - * Attempts to match the given command to a string. + * Returns the argument string of the string if it matches the provided command, otherwise null. */ - public async tryMatchingCommand( - command: ICommandDefinition, - str: string, - context: TContext, - ): Promise> | null> { + public takeCommandArguments(command: ICommandDefinition, str: string): string | null { if (command.prefix) { const prefixMatch = str.match(command.prefix); if (!prefixMatch) return null; @@ -316,8 +315,17 @@ export class CommandManager< str = str.slice(triggerMatch[0].length); } } - if (!matchedTrigger) return null; + return matchedTrigger ? str : null; + } + /** + * Attempts to match the given command's signature to an argument string. + */ + public async tryMatchingSignature( + command: ICommandDefinition, + str: string, + context: TContext, + ): Promise> | null> { const parsedArguments = parseArguments(str); const signatures = command.signatures.length > 0 ? command.signatures : [{}]; const safeSignatures = signatures.map((s) => toSafeSignature(s));