autocomplete slash commands matching feature.
When a user uses slash commands and there are options that are given but they can also search for options. Discord ui shows 'matching'. it basically searchs the data and gives u ones that match the search. how would u do this with autocomplete interactions though? imagine u have an array of 70 options. regular slash commands does not allow you to have that many so you would have to use autocomplete slash commands so how would u create a "matching" feature just like this one that regular slash commands offer
33 Replies
• What's your exact discord.js
npm list discord.js
and node node -v
version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out Interactions: Autocomplete
read more String.prototype.includes()
The includes() method performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Array.prototype.filter()
The filter() method creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
But for the one discord validates they r just using string.includes() ?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
I mean in regular string options. The one in my screen shot. When u type in values it's just filtering from the array the developer passed with .includes() ?
Actually this makes sense ty.
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Can u provide a description on a autocomplete field like a regular one?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
getFocused() is better
What does that method return instead
getFocused() without options returns a string or number, it just depends on the option type
here is an example
But why that instead of .getString()
because it will get the option that has the autocomplete option, it's not nullable, u don't have to provide the option id and is recommended in the djs guide https://discordjs.guide/interactions/autocomplete.html#enabling-autocomplete
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
getFocused() will default to an empty string
What if there are multiple autocomplete fields
getFocused(true) which will return the option object (ApplicationCommandOptionChoice), where u can then condition the option name
Also what's the option name max length because imagine one of the fields is very long and app crashes
Discord Developer Portal
Discord Developer Portal — API Docs for Bots and Developers
Integrate your service with Discord — whether it's a bot or a game or whatever your wildest imagination can come up with.
an option name can be 1-32 characters long
So u would also trim off the string here if u got all the data from a DB or something
oops, i ran the command with getSubcommand() instead of getFocused()
i recommend u take a look at https://discordjs.guide/interactions/autocomplete.html#handling-multiple-autocomplete-options
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
Would it crash if string is too long or be automatically cut off by discord?
And also is there a way to pass a default option so as soon as user clicks on the autocomplete option it suggests some top results that others r searching or whatever
if ur filtering an array with string includes, it'll show results without typing anything because string#includes() returns true on an empty string (eg.
"hello".includes("")
returns true)
if the option name was longer than 32 characters, djs would throw an error when registering the commandThat's true ty
Is it possible to get other fields through autocomplete
Like let's say user puts a certain option as someone can we access that option when they r searching in the autocomplete field
So they don't actually have to send it for it to be accessible?
All they have to do is type something in an autocomplete field
Ty
This works just like discords but it doesn't actually sort the array correctly which is what I want to do
Using the same condition for the filter?
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
String.prototype.localeCompare()
The localeCompare() method returns a number indicating whether a reference string comes before, or after, or is the same as the given string in sort order. In implementations with Intl.Collator API support, this method simply calls Intl.Collator.
yes but i need to sort it to the query of the user. so im not sure how to sort it by the closest results
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View