Sunar

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())
	);
 
	interaction.respond(results);
});
 
export { slash, autocomplete };

Reference

AutocompleteOptions

PropTypeDefault
name
string | RegExp
-
commandName
string | RegExp
-

Last updated on

On this page

GitHubEdit on Github ↗