Interface AutoCompleteHandler
public interface AutoCompleteHandler
Handler for slash command autocomplete.
Provides dynamic suggestions as users type command options.
Implement this interface and register via InteractionManager:
public class CityAutocomplete implements AutoCompleteHandler {
@Override
public String getCommandName() {
return "weather";
}
@Override
public String getOptionName() {
return "city";
}
@Override
public void handle(CommandAutoCompleteInteractionEvent event) {
String input = event.getFocusedOption().getValue();
List<String> cities = findCitiesMatching(input);
List<Command.Choice> choices = cities.stream()
.limit(25)
.map(city -> new Command.Choice(city, city))
.toList();
event.replyChoices(choices).queue();
}
}
Register in your @Plugin class:
@OnEnable
public void onEnable(PluginContext context) {
context.getInteractionManager().registerAutoCompleteHandler("my-plugin", new CityAutocomplete());
}
-
Method Summary
Modifier and TypeMethodDescriptionGet the command name this autocomplete handles.Get the option name this autocomplete handles.voidhandle(net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent event) Handle the autocomplete request.
-
Method Details
-
getCommandName
String getCommandName()Get the command name this autocomplete handles.- Returns:
- the slash command name
-
getOptionName
String getOptionName()Get the option name this autocomplete handles.This is the name of the option that has autocomplete enabled.
- Returns:
- the option name
-
handle
void handle(net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent event) Handle the autocomplete request.Use
event.getFocusedOption().getValue()to get what the user typed. Reply with up to 25 choices usingevent.replyChoices(...).- Parameters:
event- the autocomplete event
-