This is to add support for autocomplete for string/number fields.
The suggested structure is as follows, each option is given a name, for example for a to-do list, you may have a remove method which can be used to lookup the available tasks to remove.
To do this, we would want to support, general catch all's where the context can be given appropriately but you can control how you want to access each autocomplete, and then additionally lower level so you can say which option and which subcommand if applicable.
To do this, we shall add a new method decorator as: @On.Autocomplete
the parameters would be:
optionName
- An optional parameter that defaults to *
, but if passed will only allow autocomplete for that specific option name.
subCommand
- An optional parameter, again defaulting to *
that will verify if a subcommand is there, then only allow that.
By default all autocompletions would be managed inside of a command class, and would be processed under that command, so if you had 3 different commands, you would need at the minimum, 3 autocomplete commands inside of each command class.
An example of the above may look like:
Assume the below method would be part of the TodoCommand
class.
@On.Autocomplete()
public async onAutocomplete(context: Context): Promise<void> {
const interaction = context.getInteraction<CommandInteraction>();
const query = interaction.options.getFocused() as string;
interaction.respond(
this.tasks.filter(task =>
// If no query, return all.
if (!query || String(query).length === 0) return true;
// Otherwise filter against name matches.
return task.name.toLowerCase().includes(query.toLowerCase());
})
.map(task => {
return {
name: task.name,
value: task.id,
};
});
);
}