WJS*_*ent 2 javascript bots node.js discord discord.js
我正在尝试创建一个具有自动完成功能但适用于超过 25 个选择的选项,我已经看到其他机器人这样做了,我只是不知道如何才能做到这一点。我已经有了基本的自动完成设置,但它不允许我添加超过 25 个选择。我正在使用discord.js v14(我添加了28个选项,但它只适用于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 })),
);
}
Run Code Online (Sandbox Code Playgroud)
您所拥有的实际上非常接近能够实现您想要的功能;现在您需要做的就是将过滤后的数组切片,最多只显示 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 })),
);
}
Run Code Online (Sandbox Code Playgroud)
您可能有一种更紧凑的方式来编写上面的内容,但只是为了回答这个问题,我将切片部分与一个附加options变量集成在一起,以维护您上面已经编写的内容。
希望这可以帮助!
附录:
您可能想做的其他事情是focusedOption通过执行类似于focusedOption.value.toLowerCase()过滤器功能的操作来使大小写不敏感。
| 归档时间: |
|
| 查看次数: |
4094 次 |
| 最近记录: |