Autocomplete
Autocomplete commands enhance the user experience by providing suggestions while the user is typing. They are particularly useful for commands with multiple options or extensive inputs.
Usage
import { Autocomplete, execute } from 'sunar';
const autocomplete = new Autocomplete({
name: 'example',
});
execute(autocomplete, (interaction, option) => {
// handle execution
});
export { autocomplete };Implementation
The following example demonstrates how to implement an autocomplete command using Sunar:
import { Autocomplete, Slash, execute } from 'sunar';
import { ApplicationCommandOptionType } from 'discord.js';
const slash = new Slash({
name: 'eat',
description: 'Eat a fruit',
options: [
{
name: 'fruit',
description: 'Select the fruit',
type: ApplicationCommandOptionType.String,
autocomplete: true,
required: true,
},
],
});
execute(slash, (interaction) => {
const fruit = interaction.options.getString('fruit', true);
interaction.reply({ content: `You ate the **${fruit}**.` });
});
const autocomplete = new Autocomplete({
name: 'fruit',
commandName: 'eat', // optional
});
execute(autocomplete, (interaction, option) => {
const data = [
{ name: 'Apple', value: 'apple' },
{ name: 'Kiwi', value: 'kiwi' },
{ name: 'Watermelon', value: 'watermelon' },
{ name: 'Banana', value: 'banana' },
{ name: 'Strawberry', value: 'strawberry' },
];
const results = data.filter((e) =>
e.name.toLowerCase().includes(option.value.toLowerCase())
);
return interaction.respond(results);
});
export { slash, autocomplete };Reference
AutocompleteOptions
Prop
Type
How is this guide?
Last updated on
Verify member roles
Implement role verification to restrict command usage to members with specific roles. Follow this guide to learn how to use middleware in Sunar for role-based command access.
Button
Buttons are interactive elements users can click to trigger specific actions. They are ideal for creating interactive messages, such as confirmation prompts or menu navigation.