Skip to content

Autocomplete

Gellért Dániel edited this page Jul 5, 2023 · 12 revisions

An argument that extends AutocompletableInputArgument can be compeleted automatically by suggesting values based on several aspects. Discord limits the autocomplete to option types. Currently only options with STRING or LONG type can be completed automatically.

A suggestion is an instance of Choice that contains a key and a value. The key is what the user sees, and the value is behind the key what the user chooses. The Choice's value's type must be the same as the option's. The list of suggestions can be null.

Regular autocomplete

Serves static data. Expects a list of Choice as parameter and returns it.

Usage:

Autocomplete autocomplete = new Autocomplete(
    new Choice("One", 1),
    new Choice("Two", 2),
    new Choice("Three", 3)
);

Conditional autocomplete

Returns choices based on a custom condition. Expects a list of Choice based on condition(s), and for that it gives the current state of the autocomplete that is an instance of AutocompleteState, that contains the following data:

  • The command,
  • sender,
  • channel if presents,
  • current argument and its value,
  • and the already specified arguments and its values

Usage:

ConditionalAutocomplete autocomplete = new ConditionalAutocomplete(state -> {
    if (...) {
        return Arrays.asList(
            new Choice("One", 1),
            new Choice("Two", 2),
            new Choice("Three", 3)
        )
    }
    return null;
});

Search autocomplete

Expects a datasource and searches for values in it based on the user input and the search type. It works like a search bar. There are different search types:

  • For STRING option types
    • STARTS_WITH: Searches for strings that starts with the user input
    • ENDS_WITH: Searches for strings that ends with the user input
    • CONTAINS: Searches for strings that contains the user input
  • For LONG option types
    • GREATER_THAN: Searches for numbers that greater than the user input
    • LESS_THAN: Searches for numbers that less than the user input

Settings:

  • ignoreCase(): Ignores the case when searching for strings (This only can be used with STRING option types)
  • limit(int): limits the displayed number of result
  • minCharToSearch(int): Sets the minimum number of characters that the input's lenght has to be
  • sort(SortType): Sorts the result by the type (ASCENDING or DESCENDING)

Usage:

SearchAutocomplete autocomplete = new SearchAutocomplete(SearchAutocomplete.SearchType.CONTAINS, Arrays.asList(
    "Hello", "World"
))
.ignoreCase();

Adding to autocomplete to argument

For that AutocompletableInputArgument#addAutocomplete(Autocomplete) method has to be used with an Autocomplete as parameter:

autocompletable.addAutocomplete(autocomplete);

Example

Create two different autocomplete. In the first autocomplete the user can search for values. The second autocomplete's choices depends on the value of the first ones.

StringArgument textArg = new StringArgument("text", "Start typing");
SearchAutocomplete searchAutocomplete = new SearchAutocomplete(SearchAutocomplete.SearchType.CONTAINS, Arrays.asList(
        "skate loose apple weigh marsh blank tempt level penny feast brand tiger dairy noble party grant theme begin muggy flush swipe world lobby tooth novel thigh adult brink creed touch enfix wreck shame ankle hobby brush slant ample truck arrow burst elbow enemy drink whole petty issue swing mayor irony"
                .split(" ")
))
        .ignoreCase();
textArg.addAutocomplete(searchAutocomplete);

NumberArgument numArg = new NumberArgument("num", "Just a number");
ConditionalAutocomplete conditionalAutocomplete = new ConditionalAutocomplete(state -> {
    /* Get the textArg's value that is an autocompleted StringArgument */
    String textArgValue = (String) state.getArgumentValues().get(textArg);

    /* The user can specify this firstly, therefore it can be null */
    if (textArgValue != null) {
        if (textArgValue.startsWith("a")) {
            return Arrays.asList(
                    new Choice("1", 1),
                    new Choice("2", 2),
                    new Choice("3", 3),
                    new Choice("4", 4),
                    new Choice("5", 5)
            );
        } else if (textArgValue.contains("a")) {
            return Arrays.asList(
                    new Choice("6", 6),
                    new Choice("7", 7),
                    new Choice("8", 8),
                    new Choice("9", 9),
                    new Choice("10", 10)
            );
        }
    }

    return null;
});
numArg.addAutocomplete(conditionalAutocomplete);