I'm trying to create an option that has autocomplete but works with more than 25 choices, I've seen other bots do it I'm just lost on how I would be able to do that. I have the basic autocomplete setup already it just won't let me add more than 25 choices. I'm using discord.js v14 (I have 28 options added rn, it only works with 25 though. Ty in advance!)
if (interaction.options.getSubcommand() === "botanical" ) {
const focusedOption = interaction.options.getFocused(true);
let choices;
if (focusedOption.name === 'search') {
choices = ['agrimony', 'allspice', 'almond', 'aloe', 'anise', 'apple', 'avocado', 'basil', 'bayLaurel', 'bergamot', 'birch', 'calendula', 'cardamom', 'chamomile', 'cinnamon', 'comfrey', 'hemp', 'lavender', 'mint', 'motherwort', 'mugwort', 'rose', 'rosemary', 'sage', 'thyme', 'valerian', 'vervain', 'yarrow', 'valerian', 'vervain', 'yarrow'];
}
const filtered = choices.filter(choice => choice.startsWith(focusedOption.value));
await interaction.respond(
filtered.map(choice => ({ name: choice, value: choice })),
);
}
What you have is actually very close to being able to achieve your desired functionality; all you need to do now is to slice the filtered array to only show up to 25 so that it won't yield an error initially:
if (interaction.options.getSubcommand() === "botanical" ) {
const focusedOption = interaction.options.getFocused(true);
let choices;
if (focusedOption.name === 'search') {
choices = ['agrimony', 'allspice', 'almond', 'aloe', 'anise', 'apple', 'avocado', 'basil', 'bayLaurel', 'bergamot', 'birch', 'calendula', 'cardamom', 'chamomile', 'cinnamon', 'comfrey', 'hemp', 'lavender', 'mint', 'motherwort', 'mugwort', 'rose', 'rosemary', 'sage', 'thyme', 'valerian', 'vervain', 'yarrow', 'valerian', 'vervain', 'yarrow'];
}
const filtered = choices.filter(choice => choice.startsWith(focusedOption.value));
let options;
if (filtered.length > 25) {
options = filtered.slice(0, 25);
} else {
options = filtered;
}
await interaction.respond(
options.map(choice => ({ name: choice, value: choice })),
);
}
There's probably a tighter way you could write the above, but just for the sake of answering this question, I've integrated the slicing portion with an additional options
variable to maintain what you've already written above.
Hope this helps!
Addendum:
Something else you may want to do is make it so that you make the focusedOption
case-insenstive by doing something like focusedOption.value.toLowerCase()
in the filter function.