Skip to content

Autocomplete

Danny edited this page Apr 22, 2022 · 1 revision

The autocomplete functionality is offered by Discord and allows you to create interactive lists that you can search against for string and number slash command options.

To do this, we shall use the @On.Autocomplete decorator, that optionally takes an optionName and subcommand. If you do not pass any parameters to the decorator, it will catch ALL autocompletions for the command, if you offer an option name, it will match the option name against what you supplied and only work for that one, lastly if you add an option name AND a subcommand, it will only listen to events from an autocomplete interaction that matches both option name and subcommand.

To use auto complete, in a Command class you can do the following method:

@On.Autocomplete()
public async onAutocomplete(context: Context): Promise<void> {
  const interaction = context.getInteraction<AutocompleteInteraction>();
  const query = interaction.options.getFocused() as string;

  // Do filtering here, and return an array of options (Type: `{ name: string, value: string }[]`), example:
  await interaction.respond([
    { name: 'Hello', value: 'hello' },
    { name: 'Goodbye', value: 'goodbye' },
  ]);
}

Additionally if I have a subcommand called "books" and an option called "name", I can do:

@On.Autocomplete('name', 'books')
Clone this wiki locally