'How to use AutoComplete in Discord.js v13?
How do you use the Discord autocomplete feature using Discord.js v13.6, @Discord.js/builders v0.11.0?
I couldn't find a guide or tutorial anywhere
Here is what I have so far:
const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageEmbed } = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('autocomplete')
.setDescription('Test command')
async execute(interaction) {
await interaction.replay({ content: "Hello World" })
},
};
Solution 1:[1]
Basically, autocomplete is a way for bots to suggest values for integer and string options for command interactions. When a user uses a slash command (lets call it help) and this command takes a string argument of which command to describe, it might be annoying for the user to know every single command. Autocomplete fixes this by suggesting values (in this case commands).
You can set an option as autocompetable when you use the slash command builder:
import { SlashCommandBuilder } from '@discordjs/builders';
const command = new SlashCommandBuilder()
.setName('help')
.setDescription('command help')
.addStringOption((option) =>
option
.setName('command')
.setDescription('The command to get help for.')
.setRequired(true)
// Enable autocomplete using the `setAutocomplete` method
.setAutocomplete(true)
);
When autocomplete is set to true, every time a user starts using an option, Discord will send a request to your bot (as an interaction) to see what should be suggested. You can handle the response as such:
client.on('interactionCreate', async (interaction) => {
// check if the interaction is a request for autocomplete
if (interaction.isAutocomplete()) {
// respond to the request
interaction.respond([
{
// What is shown to the user
name: 'Command Help',
// What is actually used as the option.
value: 'help'
}
]);
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Harry Allen |
