所以我一直在使用Discord.JS库处理一个不和机器人,我遇到了一个问题。很确定这个问题更多地涉及Javascript和Discord.JS,所以我会在这里问一下,希望得到一些帮助。Discord Bot | DiscordJS | Javascript
我有一个名为commandManager.js的文件,它包含我所有的基本命令功能。其中之一是自动加载/ commands /文件夹中的命令,并根据它们的类别(通过导出在命令中指定)将它们分配给一个数组。
global.userCommands = {};
global.modCommands = {};
global.adminCommands = {};
global.ownerCommands = {};
exports.init = function(bot)
{
fs.readdir("./commands/", (error, files) =>
{
if (error)
{
logger.error(error);
}
files.forEach(file =>
{
let commandFile = require(`../commands/${file}`);
let commandName = file.split(".")[0];
if (commandFile.info.category == "User")
{
userCommands[commandName] = commandFile;
}
else if (commandFile.info.category == "Mod")
{
modCommands[commandName] = commandFile;
}
else if (commandFile.info.category == "Admin")
{
adminCommands[commandName] = commandFile;
}
else if (commandFile.info.category == "Owner")
{
ownerCommands[commandName] = commandFile;
}
else
{
logger.warn("Could not add the command " + commandName + " to any of the categories");
}
});
logger.info("Loaded " + files.length + " command(s)");
});
}
然后在此之后,我可以在消息比特实际的机器人,我做了如下使用命令:
exports.run = function(bot, msg)
{
const args = msg.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const cleanCommand = command.slice(config.prefix.length);
if (msg.author.bot)
{
return;
}
else if (msg.content.indexOf(config.prefix) !== 0)
{
return;
}
else if (has.call(userCommands, cleanCommand))
{
msg.reply("user");
}
else if (has.call(modCommands, cleanCommand))
{
}
else if (has.call(adminCommands, cleanCommand))
{
}
else if (has.call(ownerCommands, cleanCommand))
{
}
else
{
msg.reply(`that command does not even exist! You can say ${config.prefix}help for a list of commands!`);
}
}
所以当我说的命令,例如“平”它应该回复msg.reply(“用户”),但它表示它不存在任何。我宣称已经像这样一个全球性的事情,因此你很好奇。
global.has = Object.prototype.hasOwnProperty;
如果你想看到它是如下命令:
exports.info =
{
name: "Ping",
permission: 10,
category: "User",
about: "Makes the bot respond with pong, useful to see if the bot is working."
};
exports.run = function(msg)
{
msg.channel.send("Pong!");
}
任何提示,引用的例子,或只是普通的勺子喂养是100%的欢迎。另外,如果你想分享一些更好的技术来做我正在做的事情,请让我知道,因为我只是JS的初学者。
为什么不'userCommands [cleanCommand]'?而类别应该是什么? –
那么,如果我将来要做一个命令列表,那么类别就是我将它们分开的想法。不知道为什么不诚实与你。任何类别的更好的想法,或我可以做什么,而不是得到相同的结果? –