Skip to content
GitHub

Autocomplete

function in meocord/decorator Since 4.0.0

Autocomplete<_R = unknown>(
  commandPath: string,
  optionName?: string,
): <P extends Record<string, any>, R>(
  target: object,
  propertyKey: string,
  _descriptor:
    | TypedPropertyDescriptor<
        (interaction: AutocompleteInteraction, params: P) => R
      >
    | TypedPropertyDescriptor<(interaction: AutocompleteInteraction) => R>
    | TypedPropertyDescriptor<() => R>,
) => void

Suggests values for an option of a chat input command as the user types.

Use it for an option with more possible values than a fixed list of choices holds, such as a search over your own data. Enable it on the option with setAutocomplete(true) in the command's builder.

Where it runs

Parameters

NameTypeDescription
commandPathstring

The command, such as search, or a subcommand's path, such as settings notify email.

optionName?string

The option to complete. Leave it out to handle every option, branching on interaction.options.getFocused(true).

Returns

Returns a method decorator.

<P extends Record<string, any>, R>(
  target: object,
  propertyKey: string,
  _descriptor:
    | TypedPropertyDescriptor<
        (interaction: AutocompleteInteraction, params: P) => R
      >
    | TypedPropertyDescriptor<(interaction: AutocompleteInteraction) => R>
    | TypedPropertyDescriptor<() => R>,
) => void

Examples

TypeScript
@Autocomplete('fruit', 'name')
async completeFruit(interaction: AutocompleteInteraction) {
  const { value } = interaction.options.getFocused(true)
  const names = ['apple', 'banana', 'cherry'].filter(name => name.startsWith(value))
  await interaction.respond(names.map(name => ({ name, value: name })))
}