How could I display YouTube autofill suggestions while a user is inputting a string? I'm using DisTube and the searchSongs
option but that only affects the result of already inputting a text. I've seen bots such as Soul Music effectively pull it off so it's clearly possible but I'm not sure how they exactly did it.
CodePudding user response:
This is achieved with the help of something called Autocomplete in discord.js
. It's actually pretty simple to do something like this. The first step is to declare an autocomplete command. This can be done in the SlashCommandBuilder
by using the option .setAutocomplete()
. An example:
const data = new SlashCommandBuilder()
.setName('command-name')
.setDescription('command-description')
.addStringOption(option => {
option
.setName('option-name')
.setDescription('option-description')
.setAutocomplete(true)
})
Then, you just have to create an event listener for the interactionCreate
event, and then when it fires, check if the interaction was an Autocomplete request and respond with a value. An example:
client.on('interactionCreate', (interaction) => {
if (!interaction.isAutocomplete()) return
const enteredValue = interaction.options.getFocused()
const values = [
{
name: 'option-name',
value: 'option-value',
}
]
interaction.respond(values)
})
If you want to learn more about the Autocomplete feature, I'd suggest you go here => Autocomplete | discord.js