|
3 | 3 | import com.freya02.botcommands.api.application.slash.autocomplete.AutocompleteAlgorithms;
|
4 | 4 | import com.freya02.botcommands.api.pagination.paginator.PaginatorBuilder;
|
5 | 5 |
|
| 6 | +import me.xdrop.fuzzywuzzy.model.BoundExtractedResult; |
| 7 | + |
6 | 8 | import net.dv8tion.jda.api.Permission;
|
7 | 9 | import net.dv8tion.jda.api.entities.Member;
|
8 | 10 | import net.dv8tion.jda.api.entities.User;
|
|
21 | 23 |
|
22 | 24 | import java.util.Collection;
|
23 | 25 | import java.util.List;
|
| 26 | +import java.util.function.Function; |
24 | 27 |
|
25 | 28 |
|
26 | 29 | /**
|
@@ -65,24 +68,22 @@ public static boolean userHasChannelPermission(@NotNull Interaction interaction,
|
65 | 68 | @NotNull
|
66 | 69 | public static List<Command.Choice> sortChoicesFuzzy(@NotNull CommandAutoCompleteInteractionEvent event, @NotNull Collection<Command.Choice> collection) {
|
67 | 70 | final OptionType type = event.getFocusedOption().getType();
|
| 71 | + final Function<BoundExtractedResult<Command.Choice>, Command.Choice> mapping = switch (type) { |
| 72 | + case STRING -> BoundExtractedResult::getReferent; |
| 73 | + case INTEGER -> result -> { |
| 74 | + final Command.Choice choice = result.getReferent(); |
| 75 | + final Long value = Mapper.toLong(choice.getAsString()); |
| 76 | + return value == null ? null : new Command.Choice(choice.getName(), value); |
| 77 | + }; |
| 78 | + case NUMBER -> result -> { |
| 79 | + final Command.Choice choice = result.getReferent(); |
| 80 | + final Double value = Mapper.toDouble(choice.getAsString()); |
| 81 | + return value == null ? null : new Command.Choice(choice.getName(), value); |
| 82 | + }; |
| 83 | + default -> throw new IllegalArgumentException("Invalid autocompletion option type: " + type); |
| 84 | + }; |
68 | 85 | return AutocompleteAlgorithms.fuzzyMatching(collection, Command.Choice::getName, event).stream()
|
69 |
| - .map(result -> { |
70 |
| - final Command.Choice choice = result.getReferent(); |
71 |
| - final String name = choice.getName(); |
72 |
| - final String value = choice.getAsString(); |
73 |
| - return switch (type) { |
74 |
| - case STRING -> new Command.Choice(name, value); |
75 |
| - case INTEGER -> { |
76 |
| - final Long valueLong = Mapper.toLong(value); |
77 |
| - yield valueLong == null ? null : new Command.Choice(name, valueLong); |
78 |
| - } |
79 |
| - case NUMBER -> { |
80 |
| - final Double valueDouble = Mapper.toDouble(value); |
81 |
| - yield valueDouble == null ? null : new Command.Choice(name, valueDouble); |
82 |
| - } |
83 |
| - default -> throw new IllegalArgumentException("Invalid autocompletion option type: " + type); |
84 |
| - }; |
85 |
| - }) |
| 86 | + .map(mapping) |
86 | 87 | .toList();
|
87 | 88 | }
|
88 | 89 |
|
|
0 commit comments