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.
This commit is contained in:
parent
bb9bc4e579
commit
a2796bee04
1 changed files with 10 additions and 1 deletions
|
|
@ -158,8 +158,13 @@ public final class UserArgument<C> extends CommandArgument<C, User> {
|
||||||
* Construct a new argument parser for {@link User}
|
* Construct a new argument parser for {@link User}
|
||||||
*
|
*
|
||||||
* @param modes List of parsing modes to use when parsing
|
* @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<ParserMode> modes) {
|
public UserParser(final @NonNull Set<ParserMode> modes) {
|
||||||
|
if (modes.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException("At least one parsing mode is required");
|
||||||
|
}
|
||||||
|
|
||||||
this.modes = modes;
|
this.modes = modes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -180,7 +185,7 @@ public final class UserArgument<C> extends CommandArgument<C, User> {
|
||||||
Exception exception = null;
|
Exception exception = null;
|
||||||
|
|
||||||
if (modes.contains(ParserMode.MENTION)) {
|
if (modes.contains(ParserMode.MENTION)) {
|
||||||
if (input.endsWith(">") || modes.size() == 1) {
|
if (input.startsWith("<@") && input.endsWith(">")) {
|
||||||
final String id;
|
final String id;
|
||||||
if (input.startsWith("<@!")) {
|
if (input.startsWith("<@!")) {
|
||||||
id = input.substring(3, input.length() - 1);
|
id = input.substring(3, input.length() - 1);
|
||||||
|
|
@ -195,6 +200,10 @@ public final class UserArgument<C> extends CommandArgument<C, User> {
|
||||||
} catch (final UserNotFoundParseException | NumberFormatException e) {
|
} catch (final UserNotFoundParseException | NumberFormatException e) {
|
||||||
exception = e;
|
exception = e;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
exception = new IllegalArgumentException(
|
||||||
|
String.format("Input '%s' is not a user mention.", input)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue