Skip to content

Commit

Permalink
Fix bug where non command text gets processed incorrectly by command …
Browse files Browse the repository at this point in the history
…processor (#79)
  • Loading branch information
shayaantx authored Jun 2, 2022
1 parent 342040c commit 064c298
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/botdarr/clients/ChatClientBootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected <T extends ChatClientResponse> void runAndProcessCommands(String prefi
.setUsername(username);
LOGGER.debug("Processing command " + message + " for username " + username + " with prefix " + prefix);
List<CommandResponse> 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) {
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/botdarr/commands/CommandProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
public class CommandProcessor {
public <T extends ChatClientResponse> List<CommandResponse> processCommand(String commandPrefix,
List<Command> 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 {
Expand All @@ -32,7 +32,12 @@ public <T extends ChatClientResponse> List<CommandResponse> 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()));
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.4.0
5.4.1
4 changes: 2 additions & 2 deletions src/test/java/com/botdarr/commands/CommandProcessorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ private void validateInvalidCommandIdentifier(String invalidCommand) {
private List<CommandResponse> validateValidCommand(String validCommand) {
CommandProcessor commandProcessor = new CommandProcessor();
List<CommandResponse> 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) {
Expand All @@ -355,7 +355,7 @@ private List<CommandResponse> validateValidCommand(String validCommand) {
private void validateInvalidCommand(String invalidCommand, String expectedErrorResponseString) {
CommandProcessor commandProcessor = new CommandProcessor();
List<CommandResponse> 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);
Expand Down

0 comments on commit 064c298

Please sign in to comment.