Recently, I came to know about the Autocomplete feature in discord.js@13.6.0 and I wanted to try it out. I was able to do the basics by seeing the answer on this post => How to use AutoComplete in Discord.js v13
I was able to set up the autocomplete and was also able to respond to the autocomplete interaction by using:
client.on('interactionCreate', interaction => {
if (interaction.isAutocomplete()) {
interaction.respond([
{
name: 'Command Help',
value: 'help'
}
]);
}
})
But my doubt is that is there any way to get the string which the user typed which caused the autocomplete interaction. For example: there is an array of options, and based on what the user typed, I would be able to filter the options and then show the correct ones?
Yes you can do so very easily by using
interaction.options.getFocused()
this will return whatever the user is currently typing but if you do
interaction.options.getFocused(true)
it will return an object with the the name of the option and the value that the user has typed so far.
You can find the documentation here