Search in sources :

Example 1 with AutoCompleteQuery

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;
}
Also used : AutoCompleteQuery(net.dv8tion.jda.api.interactions.AutoCompleteQuery) ApplicationOptionData(com.freya02.botcommands.internal.ApplicationOptionData) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with AutoCompleteQuery

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);
}
Also used : AutoCompleteQuery(net.dv8tion.jda.api.interactions.AutoCompleteQuery) BoundExtractedResult(me.xdrop.fuzzywuzzy.model.BoundExtractedResult) FuzzySearch(me.xdrop.fuzzywuzzy.FuzzySearch)

Example 3 with AutoCompleteQuery

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;
                }
        }
    }
}
Also used : DataObject(net.dv8tion.jda.api.utils.data.DataObject) AutoCompleteQuery(net.dv8tion.jda.api.interactions.AutoCompleteQuery)

Example 4 with AutoCompleteQuery

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);
}
Also used : AutoCompleteQuery(net.dv8tion.jda.api.interactions.AutoCompleteQuery) FuzzySearch(me.xdrop.fuzzywuzzy.FuzzySearch)

Aggregations

AutoCompleteQuery (net.dv8tion.jda.api.interactions.AutoCompleteQuery)4 FuzzySearch (me.xdrop.fuzzywuzzy.FuzzySearch)2 ApplicationOptionData (com.freya02.botcommands.internal.ApplicationOptionData)1 BoundExtractedResult (me.xdrop.fuzzywuzzy.model.BoundExtractedResult)1 DataObject (net.dv8tion.jda.api.utils.data.DataObject)1 Nullable (org.jetbrains.annotations.Nullable)1