On this page
JavaScript API
Autocomplete
Respond to slash command autocomplete interactions with dynamic choices.
Autocomplete lets slash commands suggest options as the user types. The interaction global is available in autocomplete handlers.
Responding with choices
await interaction.respond(choices)
Send up to 25 suggestions. Each choice needs name (display) and value (stored value):
const focused = interaction.options.getFocused();
const filtered = fruits
.filter((f) => f.toLowerCase().includes(focused.toLowerCase()))
.slice(0, 25)
.map((f) => ({ name: f, value: f }));
await interaction.respond(filtered);
Return an empty array when no matches:
await interaction.respond([]);
Reading the focused option
interaction.options.getFocused(required?)
Returns the partial string the user has typed so far.
const query = interaction.options.getFocused(true);
When autocomplete runs
- Triggered when a user types in a slash option configured for autocomplete.
- You must respond within Discord’s interaction timeout.
- Use
interaction.isAutocomplete()to distinguish from command execution.
Related
- interaction — slash command replies
- Interactions overview — BDScript autocomplete with
((interaction.kind))