简体   繁体   English

如何将超过 25 个选项添加到自动完成选项 discord.js v14

[英]How to add more than 25 choices to autocomplete option discord.js v14

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.我正在尝试创建一个具有自动完成功能但可以使用超过 25 个选项的选项,我已经看到其他机器人这样做了,我只是迷失了如何做到这一点。 I have the basic autocomplete setup already it just won't let me add more than 25 choices.我已经有了基本的自动完成设置,只是不会让我添加超过 25 个选项。 I'm using discord.js v14 (I have 28 options added rn, it only works with 25 though. Ty in advance!)我正在使用 discord.js v14(我添加了 28 个选项 rn,但它仅适用于 25。提前 Ty!)

        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 })),
            );
        }

The simple answer is you can't.简单的答案是你不能。 Discord has a 25-option limit on this, nothing you can do to get around it or add more than 25 options to one selector Discord 对此有 25 个选项的限制,您无法绕过它或向一个选择器添加超过 25 个选项

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:您现在需要做的就是将过滤后的数组分割为最多显示 25 个,这样它最初就不会产生错误:

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.可能有一种更严格的方法可以编写上面的内容,但只是为了回答这个问题,我将切片部分与一个额外的options变量集成在一起,以维护您在上面已经编写的内容。

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.您可能想要做的其他事情是通过在过滤器 function 中执行诸如focusedOption.value.toLowerCase()之类的操作来使focusedOption区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM