From a2796bee043e418cc2345540378692ba10827b8a Mon Sep 17 00:00:00 2001 From: p5nbTgip0r Date: Mon, 4 Jan 2021 22:27:32 -0800 Subject: [PATCH] Fix JDA mentioned user argument parsing if a mention is not given. If only `ParserMode.MENTION` was set as a parsing mode, the initial check for `input.endsWith(">")` would be bypassed and the input would begin to be parsed like a mention. If the input was less than three characters long then the `substring` calls would give a very ambiguous error along the lines of `begin 2, end X-1, length X`. This commit fixes this issue by adding a check to make sure the input is an actual user mention. If the input isn't a mention then an exception will be set stating that the sender's input was not a user mention. In addition, this commit also removes the `modes.size() == 1` check in the mention parser. I'm not sure why this was there to begin with, but I don't think that the input parsing check should be bypassed because only one parsing mode is specified. I settled on a check in the `UserParser` constructor to make sure that the parsing modes list isn't empty in order to ensure that at least one parser mode handles the input. --- .../commandframework/jda/parsers/UserArgument.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/cloud-discord/cloud-jda/src/main/java/cloud/commandframework/jda/parsers/UserArgument.java b/cloud-discord/cloud-jda/src/main/java/cloud/commandframework/jda/parsers/UserArgument.java index 05e286ba..3f0a0362 100644 --- a/cloud-discord/cloud-jda/src/main/java/cloud/commandframework/jda/parsers/UserArgument.java +++ b/cloud-discord/cloud-jda/src/main/java/cloud/commandframework/jda/parsers/UserArgument.java @@ -158,8 +158,13 @@ public final class UserArgument extends CommandArgument { * Construct a new argument parser for {@link User} * * @param modes List of parsing modes to use when parsing + * @throws java.lang.IllegalStateException If no parsing modes were provided */ public UserParser(final @NonNull Set modes) { + if (modes.isEmpty()) { + throw new IllegalArgumentException("At least one parsing mode is required"); + } + this.modes = modes; } @@ -180,7 +185,7 @@ public final class UserArgument extends CommandArgument { Exception exception = null; if (modes.contains(ParserMode.MENTION)) { - if (input.endsWith(">") || modes.size() == 1) { + if (input.startsWith("<@") && input.endsWith(">")) { final String id; if (input.startsWith("<@!")) { id = input.substring(3, input.length() - 1); @@ -195,6 +200,10 @@ public final class UserArgument extends CommandArgument { } catch (final UserNotFoundParseException | NumberFormatException e) { exception = e; } + } else { + exception = new IllegalArgumentException( + String.format("Input '%s' is not a user mention.", input) + ); } }