在我失去的时间里制作一个小不和谐机器人 已经几个小时了,我现在被困住了..
基本上,这是我的命令帮助的一部分
假设我有一个像这样的数组类别:
let categories = [{name: "cat1", value: "cat1"},{name: "cat3", value: "cat3"},{name: "cat2", value: "cat2"}]
有没有一种方法可以导出它们而不必将它们全部写入? 像这样的东西?
module.exports = {
data: new discord.SlashCommandBuilder()
.setName("help")
.setDescription("Display bot commands")
.addStringOption(option =>
option.setName("category")
.setDescription(`Categories showed in ${prefix}help`)
.addChoices(categories)),
start
};
我看到有一个方法 .setChoices 但不知道如何使用它,因为它不接受数组
我尝试使用 .addChoices 和 .setChoices 但没有成功 尝试寻求帮助
我会尝试将其分解为一个函数,然后您可以迭代命令。我认为该文件看起来像这样:
const { SlashCommandBuilder } = require("discord.js");
//Note you could instead pass categories into this function
const fetchMySlashCommandsFunction = () => {
const categories = [
{ name: "cat1", value: "cat1" },
{ name: "cat3", value: "cat3" },
{ name: "cat2", value: "cat2" },
];
const helpBuilder = new discord.SlashCommandBuilder().setName("help") .setDescription("显示机器人命令")
categories.foreach(cat => {
helpBuilder.addStringOption((cat.name) =>
// not sure where prefix is defined in your code?
.setDescription(`Categories showed in ${prefix}help`)
.addChoices(cat.name)
);
});
return data;
};
export default fetchMySlashCommandsFunction;
要从此函数检索数据,则为
const data = fetchMySlashCommandsFunction()
感谢@Atomic Goblin,刚刚找到了解决我的问题的方法 仍然不确定这是否是目前最好的解决方案,但它有效
// CATEGORIES ARRAY
const categories = [
{name:"commands", value:"commands"},
{name:"weebpack", value:"weebpack"},
{name:"games", value:"games"},
{name:"bot", value:"bot"},
{name:"config", value:"config"}
];
// NEW SLASH COMMAND BUILDER
let data = new discord.SlashCommandBuilder()
.setName("help")
.setDescription("Display bot commands");
// ADD NEW OPTION
data.addStringOption(option =>
option.setName("category")
.setDescription(`Categories showed in ${prefix}`));
// ADD CHOICES ON FIRST OPTION
categories.forEach(cat =>{
data.options[0].addChoices(cat);
})
// EXPORTS DATA(slashcommandbuilder) AND START FUNCTION
module.exports = {
data: data,
start
};
这里我做了一个函数方法,如果它对某人有帮助的话
// Dinamically add choices from json array
// Input : discordSlashCommandBuilder()
// optionName string
// optionDesc string
// choices array [{name: "name", value: "value"},{name: "name2", value: "value2"}]
// isRequired ? boolean
// Return discordSlashCommandBuilder() with option
/**
*
* @param {discord.SlashCommandBuilder} slashCommand
* @param {String} optionName
* @param {String} optionDesc
* @param {Array} aChoices
* @param {Boolean} isRequired
* @returns
*/
function slashCommandAddOptionWithChoices(slashCommand, optionName, optionDesc, aChoices, isRequired){
// get current index
let idx = slashCommand.options.length;
// Create new option
slashCommand.addStringOption(option =>
option.setName(optionName)
.setDescription(optionDesc)
.setRequired(isRequired));
// Add choices to last option
// stop after 25 entries (max choices limit)
let i = 0;
aChoices.forEach(choice =>{
if(i === 25) return;
slashCommand.options[idx].addChoices(choice);
i++;
});
return slashCommand;
}
可能会晚一点重新开放,只是想为可能更容易处理的问题做出贡献:
.addChoices(...categories))