Skip to content

Instantly share code, notes, and snippets.

@svemat01
Last active January 16, 2022 00:33
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svemat01/80bdd2020916d4b5e52ce4226514d4d5 to your computer and use it in GitHub Desktop.
Save svemat01/80bdd2020916d4b5e52ce4226514d4d5 to your computer and use it in GitHub Desktop.
Discord.JS AutoComplete Quick docs help

AutoComplete DJS v13.3.0

What is autoComplete?

"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

How do we use it?

For this guide, our command will be named autocomplete, this is not some built in command.

Prepare option for autocomplete

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

Receiving interactions

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')
	}
});

Responding with options

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.

Filtering options based on current value

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.

Final notes

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.

@advaith1
Copy link

that notion doc was written by devsnek (the Discord dev who made the feature) and released during the DDevs stage event, it isn't mine

@jnspleet
Copy link

This helped me so much!! Thank you!

@RedCrafter07
Copy link

Tysm! It helped me making my first test - a calculator ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment