Skip to content

Commit

Permalink
GH-419 Fix Suggestion#of and number formatting. (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rollczi authored Aug 20, 2024
1 parent 06896fd commit ffd2239
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,7 @@ public static Suggestion from(List<String> suggestion) {
}

public static Suggestion of(String suggestion) {
List<String> collected = StringUtil.splitBySpace(suggestion);

if (suggestion.endsWith(" ") && !collected.get(collected.size() - 1).isEmpty()) {
collected.add("");
}

return new Suggestion(suggestion, collected);
return new Suggestion(suggestion, StringUtil.splitBySpace(suggestion));
}

@Override
Expand Down
26 changes: 11 additions & 15 deletions litecommands-jda/src/dev/rollczi/litecommands/jda/JDAPlatform.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.rollczi.litecommands.jda;


import dev.rollczi.litecommands.command.CommandRoute;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.platform.AbstractPlatform;
Expand All @@ -19,7 +18,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

class JDAPlatform extends AbstractPlatform<User, LiteJDASettings> {

Expand Down Expand Up @@ -115,28 +113,26 @@ public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent
List<Command.Choice> choiceList = result.getSuggestions().stream()
.filter(suggestion -> !suggestion.multilevel().isEmpty())
.map(suggestion -> choice(event.getFocusedOption().getType(), suggestion))
.collect(Collectors.toList());
.toList();

event.replyChoices(choiceList).queue();
}

private Command.Choice choice(OptionType optionType, Suggestion suggestion) {
if (optionType == OptionType.INTEGER) {
try {
long parsed = Long.parseLong(suggestion.multilevel());
return new Command.Choice(suggestion.multilevel(), parsed);
}
catch (NumberFormatException e) {
long parsed = (long) Double.parseDouble(suggestion.multilevel());
return new Command.Choice(String.valueOf(parsed), parsed);
String multilevel = suggestion.multilevel();

try {
if (optionType == OptionType.INTEGER) {
return new Command.Choice(multilevel, Long.parseLong(multilevel));
}
}

if (optionType == OptionType.NUMBER) {
return new Command.Choice(suggestion.multilevel(), Double.parseDouble(suggestion.multilevel()));
if (optionType == OptionType.NUMBER) {
return new Command.Choice(multilevel, Double.parseDouble(multilevel));
}
}
catch (NumberFormatException ignored) {}

return new Command.Choice(suggestion.multilevel(), suggestion.multilevel());
return new Command.Choice(multilevel, multilevel);
}

}
Expand Down

0 comments on commit ffd2239

Please sign in to comment.