Fix #111 by building a RegEx match with the full command all its aliases

This commit is contained in:
WasserEsser 2021-03-12 23:15:24 +01:00 committed by Szymon Uglis
parent 29f83718d6
commit a5c8c21ff2
No known key found for this signature in database
GPG key ID: 112376C5BEE91FE2
3 changed files with 28 additions and 28 deletions

View file

@ -18,20 +18,20 @@ abstract class CommandEntity {
/// Parent of entity
CommandEntity? get parent;
/// Returns true if provided String [str] is entity name or alias
bool isEntityName(String str) =>
str == this.name || this.aliases.any((element) => element == str);
/// A list of valid command names
List<String> get commandNames => [this.name, ...this.aliases];
/// Full qualified command name with its parents names
String getFullCommandName() {
var commandName = this.name;
for(var e = this.parent; e != null; e = e.parent) {
commandName = "${e.name} $commandName";
/// RegEx matching the fully qualified command name with its parents and all aliases
String getFullCommandMatch() {
var parentMatch = "";
if (parent != null) {
parentMatch = "${parent!.getFullCommandMatch()} ";
}
return commandName.trim();
return '$parentMatch(${this.commandNames.join('|')})';
}
/// Returns true if provided String [str] is entity name or alias
bool isEntityName(String str) => commandNames.contains(str);
}
/// Creates command group. Pass a [name] to crated command and commands added

View file

@ -94,14 +94,26 @@ class Commander with ICommandRegistrable {
}
// Find matching command with given message content
final messageContentParts = event.message.content.replaceFirst(prefix, "").trim().split(" ");
final matchingCommand = _commandEntities._findMatchingCommand(messageContentParts) as CommandHandler?;
final matchingCommand = _commandEntities._findMatchingCommand(event.message.content.replaceFirst(prefix, "").trim().split(" ")) as CommandHandler?;
if(matchingCommand == null) {
return;
}
final fullCommandName = _getFullCommandFromMessage(matchingCommand, messageContentParts);
// Builds a RegEx that matches the full command including their parents and all possible
// aliases of the final command entity and their parents.
// Example: (?<finalCommand>(quote|quotes) (remove|rm))
// This will match the command `quote remove`, `quotes remove`, `quote rm` and `quotes rm`
final match = RegExp("(?<finalCommand>${matchingCommand.getFullCommandMatch()})").firstMatch(
event.message.content,
);
final finalCommand = match?.namedGroup("finalCommand");
if (finalCommand == null) {
return;
}
// construct commandcontext
final context = CommandContext._new(
@ -109,8 +121,7 @@ class Commander with ICommandRegistrable {
event.message.author,
event.message is GuildMessage ? (event.message as GuildMessage).guild.getFromCache()! : null,
event.message,
"$prefix$fullCommandName"
);
"$prefix$finalCommand");
// Invoke before handler for commands
if (!(await _invokeBeforeHandler(matchingCommand, context))) {
@ -136,7 +147,7 @@ class Commander with ICommandRegistrable {
}
// execute logger callback
_loggerHandlerFunction(context, fullCommandName, this._logger);
_loggerHandlerFunction(context, finalCommand, this._logger);
// invoke after handler of command
await _invokeAfterHandler(matchingCommand, context);

View file

@ -1,16 +1,5 @@
part of nyxx_commander;
String _getFullCommandFromMessage(CommandHandler commandHandler, List<String> messageParts) {
var iterator = 1;
for(var e = commandHandler.parent; e != null; e = e.parent) {
iterator++;
}
messageParts.removeRange(iterator, messageParts.length);
return messageParts.join(" ");
}
// TODO: FIX. I think that is just awful but is does its job
extension _CommandMatcher on Iterable<CommandEntity> {
CommandEntity? _findMatchingCommand(Iterable<String> messageParts) {