"Autocomplete is basically choices but dynamic. every time the user presses a key in an autocomplete option, the bot gets an event and it can respond with what strings it wants discord to display as choices instead of having the choices be static." - Urcute#3119 on Discord
For this guide, our command will be named autocomplete, this is not some built in command.
Before we can start to recieve autocomplete events on an option, we have to actually tell discord that we want it. This can be achived in multiple ways.
Using the Slash command builder: This not yet possible.
Custom system:
If you're using a custom slash command building system, then I assume you know how to set your option values. In 13.3.0 a new setting has been added, the autocomplete
setting. For more, read the docs for ApplicationCommandOption
AutoCompleteInteractions gets sent on the same event as the other interactions, specifically the interactionCreate
event. Below is an example on we can catch AutoCompleteInteractions. Here we also filter out per command which is most likely something you will have to do.
client.on('interactionCreate', interaction => {
if (!interaction.isAutocomplete()) return; // We return if the interaction is not a AutoCompleteInteraction
if (interaction.commandName === 'autocomplete') { // check if they used the command /autocomplete
console.log('Typing in our command')
}
});
To start we'll just respond with the same options without checking what the user has typed so far. .respond Docs
client.on('interactionCreate', interaction => {
if (!interaction.isAutocomplete()) return;
if (interaction.commandName === 'autocomplete') {
interaction.respond([
{
name: 'Option 1',
value: 'option1',
},
{
name: 'Option 2',
value: 'option2',
},
]).then(console.log).catch(console.error);
}
});
This will send back our two options to discord when someone uses our command, discord will then display our two options to the user.
Many of us will want to change the options depending on what the current inputed value is. This can be done with interaction.options which returns a CommandInteractionOptionResolver which has a .getFocused method. This method will return the currently typed value for the option. This means that if someone has so far typed /autocomplete name: foo
then it wil return foo
Let's do a demo where it should give you Foobar
and Food
as options when the current value starts with Foo
and where it should give you Test1
and Test2
as options if it starts with Test
.
client.on('interactionCreate', interaction => {
if (!interaction.isAutocomplete()) return;
if (interaction.commandName === 'autocomplete') {
const currentValue = interaction.options.getFocused();
if (currentValue.startsWith('Foo')) {
interaction.respond([
{
name: 'Foobar',
value: 'Foobar',
},
{
name: 'Food',
value: 'Food',
},
]).then(console.log).catch(console.error);
return;
}
if (currentValue.startsWith('Test')) {
interaction.respond([
{
name: 'Test1',
value: 'Test1',
},
{
name: 'Test2',
value: 'Test2',
},
]).then(console.log).catch(console.error);
return;
}
}
});
This Demo might not be so usefull but you should now know how to get the current value and how to give back different options depending on what is currently entered.
I hope that this guide-ish has been to some sort of help to you and good luck with your bot.
To read more about how it work's on a deeper level, check out Advaith's notion post.
Tysm! It helped me making my first test - a calculator ^^