From 064c2982cfa35417a8512545286ef333b4d6f2e6 Mon Sep 17 00:00:00 2001 From: shayaantx <5449086+shayaantx@users.noreply.github.com> Date: Thu, 2 Jun 2022 00:43:44 -0400 Subject: [PATCH] Fix bug where non command text gets processed incorrectly by command processor (#79) --- .../com/botdarr/clients/ChatClientBootstrap.java | 2 +- .../java/com/botdarr/commands/CommandProcessor.java | 13 +++++++++---- src/main/resources/version.txt | 2 +- .../com/botdarr/commands/CommandProcessorTests.java | 4 ++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/botdarr/clients/ChatClientBootstrap.java b/src/main/java/com/botdarr/clients/ChatClientBootstrap.java index 6128217..49a478f 100644 --- a/src/main/java/com/botdarr/clients/ChatClientBootstrap.java +++ b/src/main/java/com/botdarr/clients/ChatClientBootstrap.java @@ -74,7 +74,7 @@ protected void runAndProcessCommands(String prefi .setUsername(username); LOGGER.debug("Processing command " + message + " for username " + username + " with prefix " + prefix); List commandResponses = - commandProcessor.processCommand(prefix, buildConfig().getCommands(), message, username); + commandProcessor.processCommand(prefix, buildConfig().getCommands(), message); if (commandResponses != null) { //if there is a response, format it for given response builder for (CommandResponse commandResponse : commandResponses) { diff --git a/src/main/java/com/botdarr/commands/CommandProcessor.java b/src/main/java/com/botdarr/commands/CommandProcessor.java index 6738e91..fd95fa2 100644 --- a/src/main/java/com/botdarr/commands/CommandProcessor.java +++ b/src/main/java/com/botdarr/commands/CommandProcessor.java @@ -12,13 +12,13 @@ public class CommandProcessor { public List processCommand(String commandPrefix, List apiCommands, - String strippedMessage, - String username) { + String strippedMessage) { try { String rawMessage = strippedMessage.toLowerCase(); + final boolean isFreeFormCommand = rawMessage.startsWith(commandPrefix); final String processedCommand; - if (rawMessage.startsWith(commandPrefix)) { + if (isFreeFormCommand) { //remove the prefix character from the message processedCommand = rawMessage.trim().substring(1); } else { @@ -32,7 +32,12 @@ public List processCommand(Strin return apiCommand.execute(commandOperation.trim()); } } - return Collections.singletonList(new ErrorResponse("Invalid command - type " + commandPrefix + "help for command usage")); + if (isFreeFormCommand) { + // only print this command for commands that come in with a prefix + // since these are free form and not automatically created by something like slash commands + return Collections.singletonList(new ErrorResponse("Invalid command - type " + commandPrefix + "help for command usage")); + } + return Collections.emptyList(); } catch (Throwable e) { LOGGER.error("Error trying to execute command " + strippedMessage, e); return Collections.singletonList(new ErrorResponse("Error trying to parse command " + strippedMessage + ", error=" + e.getMessage())); diff --git a/src/main/resources/version.txt b/src/main/resources/version.txt index 1e20ec3..04edabd 100644 --- a/src/main/resources/version.txt +++ b/src/main/resources/version.txt @@ -1 +1 @@ -5.4.0 \ No newline at end of file +5.4.1 \ No newline at end of file diff --git a/src/test/java/com/botdarr/commands/CommandProcessorTests.java b/src/test/java/com/botdarr/commands/CommandProcessorTests.java index edbbacb..8d2bc50 100644 --- a/src/test/java/com/botdarr/commands/CommandProcessorTests.java +++ b/src/test/java/com/botdarr/commands/CommandProcessorTests.java @@ -341,7 +341,7 @@ private void validateInvalidCommandIdentifier(String invalidCommand) { private List validateValidCommand(String validCommand) { CommandProcessor commandProcessor = new CommandProcessor(); List commandResponses = - commandProcessor.processCommand(CommandContext.getConfig().getPrefix(), getCommandsToTest(), validCommand, "user1"); + commandProcessor.processCommand(CommandContext.getConfig().getPrefix(), getCommandsToTest(), validCommand); //we are just making sure no error responses are returned since they signify a failure with the command if (commandResponses != null) { for (CommandResponse response: commandResponses) { @@ -355,7 +355,7 @@ private List validateValidCommand(String validCommand) { private void validateInvalidCommand(String invalidCommand, String expectedErrorResponseString) { CommandProcessor commandProcessor = new CommandProcessor(); List commandResponses = - commandProcessor.processCommand(CommandContext.getConfig().getPrefix(), getCommandsToTest(), invalidCommand, "user1"); + commandProcessor.processCommand(CommandContext.getConfig().getPrefix(), getCommandsToTest(), invalidCommand); if (commandResponses != null) { Assert.assertEquals(1, commandResponses.size()); CommandResponse commandResponse = commandResponses.get(0);