use of net.dv8tion.jda.api.interactions.AutoCompleteQuery in project BotCommands by freya022.
the class SlashCommandInfo method getAutocompletionHandlerName.
@Nullable
public String getAutocompletionHandlerName(CommandAutoCompleteInteractionEvent event) {
final AutoCompleteQuery autoCompleteQuery = event.getFocusedOption();
int optionIndex = 0;
final List<String> optionNames = event.getGuild() != null ? getLocalizedOptions(event.getGuild()) : null;
for (final SlashCommandParameter parameter : commandParameters) {
final ApplicationOptionData applicationOptionData = parameter.getApplicationOptionData();
if (parameter.isOption()) {
final String optionName = optionNames == null ? applicationOptionData.getEffectiveName() : optionNames.get(optionIndex);
if (optionName == null) {
throw new IllegalArgumentException(String.format("Option name #%d (%s) could not be resolved for %s", optionIndex, applicationOptionData.getEffectiveName(), Utils.formatMethodShort(getMethod())));
}
optionIndex++;
if (optionName.equals(autoCompleteQuery.getName())) {
return applicationOptionData.getAutocompletionHandlerName();
}
}
}
return null;
}
use of net.dv8tion.jda.api.interactions.AutoCompleteQuery in project BotCommands by freya022.
the class AutocompleteAlgorithms method fuzzyMatching.
/**
* Sorts the item with fuzzing matching, the {@value OptionData#MAX_CHOICES} most relevant results should appear at the top
* <br>This algorithm is different from {@link #fuzzyMatchingWithContinuity(Collection, ToStringFunction, CommandAutoCompleteInteractionEvent)}
*
* @param items The items to sort
* @param toStringFunction The function to transform an item into a String
* @param event The autocompletion event
* @param <T> Type of the items
* @return A list of extract results with the scores of each item
*/
public static <T> List<BoundExtractedResult<T>> fuzzyMatching(Collection<T> items, ToStringFunction<T> toStringFunction, CommandAutoCompleteInteractionEvent event) {
final List<T> list = items.stream().sorted(Comparator.comparing(toStringFunction::apply)).toList();
final AutoCompleteQuery autoCompleteQuery = event.getFocusedOption();
// First sort the results by similarities but by taking into account an incomplete input
final List<BoundExtractedResult<T>> bigLengthDiffResults = FuzzySearch.extractTop(autoCompleteQuery.getValue(), list, toStringFunction, FuzzySearch::partialRatio, OptionData.MAX_CHOICES);
// Then sort the results by similarities but don't take length into account
return FuzzySearch.extractTop(autoCompleteQuery.getValue(), bigLengthDiffResults.stream().map(BoundExtractedResult::getReferent).toList(), toStringFunction, FuzzySearch::ratio, OptionData.MAX_CHOICES);
}
use of net.dv8tion.jda.api.interactions.AutoCompleteQuery in project JDA by DV8FromTheWorld.
the class CommandAutoCompleteInteractionImpl method findFocused.
private void findFocused(DataArray options) {
for (int i = 0; i < options.length(); i++) {
DataObject option = options.getObject(i);
switch(OptionType.fromKey(option.getInt("type"))) {
case SUB_COMMAND:
case SUB_COMMAND_GROUP:
findFocused(option.getArray("options"));
break;
default:
if (option.getBoolean("focused")) {
OptionMapping opt = getOption(option.getString("name"));
focused = new AutoCompleteQuery(opt);
break;
}
}
}
}
use of net.dv8tion.jda.api.interactions.AutoCompleteQuery in project BotCommands by freya022.
the class AutocompleteAlgorithms method fuzzyMatchingWithContinuity.
/**
* Sorts the item with fuzzing matching, the {@value OptionData#MAX_CHOICES} most relevant results should appear at the top
* <br>Additionally this removes items which do not start with the query string
*
* @param items The items to sort
* @param toStringFunction The function to transform an item into a String
* @param event The autocompletion event
* @param <T> Type of the items
* @return A list of extract results with the scores of each item
*/
public static <T> List<BoundExtractedResult<T>> fuzzyMatchingWithContinuity(Collection<T> items, ToStringFunction<T> toStringFunction, CommandAutoCompleteInteractionEvent event) {
final AutoCompleteQuery autoCompleteQuery = event.getFocusedOption();
final String query = autoCompleteQuery.getValue();
final List<T> list = items.stream().filter(s -> Utils.startsWithIgnoreCase(toStringFunction.apply(s), query)).sorted(Comparator.comparing(toStringFunction::apply)).toList();
return FuzzySearch.extractTop(query, list, toStringFunction, FuzzySearch::ratio, OptionData.MAX_CHOICES);
}
Aggregations