听一个案例中的多个命令



我正在尝试获取它,以便当用户说出类似"!救命!命令'它返回帮助消息,为了节省代码中的空间而不使用2种情况,我如何将其变成1?

client.on('message', message => {
let args = message.content.substring(prefix.length).split(" ");
switch (args[0]) {
case ('help' || 'commands'):

我不知道该怎么办,它回应道:;帮助;而不是命令。有什么想法吗?

switch语句有一个功能,您可以在其中创建一个空的case。如果case返回true,它将简单地执行带有代码的下一个case语句。

switch ('someString') {
case 'someString':
case 'someOtherString': {
console.log('This will still execute');
break;
};
};

您不应该首先使用switch来处理您的命令。

您应该使用命令处理程序。这样,您就可以将所有命令导出到单独的文件中,并使用aliases

首先在与index.js相同的目录中创建一个commands文件夹。每个文件都需要是一个包含以下内容的.js文件。

module.exports = {
name: 'your command name', // needs to be completly lowercase
aliases: ["all", "of", "your", "aliases"],
description: 'Your description',
execute: (message, args) => {
// the rest of your code
}
}

接下来,您需要在index.js文件中添加一些内容。需要文件系统模块fsDiscord。创建两个新集合。

const fs = require('fs');
const Discord = require('discord.js');
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();

接下来,您需要将所有名称和别名添加到您的两个新集合中。

// Read all files in the commands folder and that ends in .js
const commands = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
// Loop over the commands, and add all of them to a collection
// If there's no name found, prevent it from returning an error
for (let file of commands) {
const command = require(`./commands/${file}`);
// Check if the command has both a name and a description
if (command.name && command.description) {
client.commands.set(command.name, command);
} else {
console.log("A file is missing something")
}

// check if there is an alias and if that alias is an array
if (command.aliases && Array.isArray(command.aliases))
command.aliases.forEach(alias => client.aliases.set(alias, command.name));
};

现在我们将所有命令添加到集合中,我们需要在client.on('message', message {...})中构建命令处理程序。

client.on('message', message => {
// check if the message comes through a DM
//console.log(message.guild)
if (message.guild === null) {
return message.reply("Hey there, no reason to DM me anything. I won't answer anyway :wink:");
}
// check if the author is a bot
if (message.author.bot) return;    
// set a prefix and check if the message starts with it
const prefix = "!";
if (!message.content.startsWith(prefix)) {
return;
}
// slice off the prefix and convert the rest of the message into an array
const args = message.content.slice(prefix.length).trim().split(/ +/g);    
// convert all arguments to lowercase
const cmd = args.shift().toLowerCase();
// check if there is a message after the prefix
if (cmd.length === 0) return;
// look for the specified command in the collection of commands
let command = client.commands.get(cmd);
// If no command is found check the aliases
if (!command) command = client.commands.get(client.aliases.get(cmd));
// if there is no command we return with an error message
if (!command) return message.reply(``${prefix + cmd}` doesn't exist!`);
// finally run the command
command.execute(message, args);
});

这是一个没有别名键的指南。

最新更新