Javascript 在一个案例中侦听多个命令
我试图得到它,这样当一个用户说一个命令像'!救命啊!commands'返回帮助消息,为了节省代码中的空间而不使用2个案例,如何将其转换为1个Javascript 在一个案例中侦听多个命令,javascript,discord,discord.js,Javascript,Discord,Discord.js,我试图得到它,这样当一个用户说一个命令像'!救命啊!commands'返回帮助消息,为了节省代码中的空间而不使用2个案例,如何将其转换为1个 client.on('message', message => { let args = message.content.substring(prefix.length).split(" "); switch (args[0]) { case ('help' || 'commands'): 我
client.on('message', message => {
let args = message.content.substring(prefix.length).split(" ");
switch (args[0]) {
case ('help' || 'commands'):
我不知道该怎么办,它只响应“帮助”,而不响应命令。有什么想法吗?switch语句有一个功能,您可以在其中创建一个空的
案例
。如果case
返回true,它将使用code执行下一个case
语句
开关('someString')){
案例“someString”:
案例“someOtherString”:{
log('仍将执行');
打破
};
};
开关语句有一个功能,您可以在其中创建一个空的案例
。如果case
返回true,它将使用code执行下一个case
语句
开关('someString')){
案例“someString”:
案例“someOtherString”:{
log('仍将执行');
打破
};
};
您不应该做的是,首先使用开关来处理您的命令
您应该做的是使用命令处理程序。这样,您可以将所有命令导出到单独的文件中,并使用名为别名的文件
首先在与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
文件添加一些内容。
需要文件系统模块fs
和Discord
。创建两个新集合
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));
};
接下来,需要将所有名称和别名添加到两个新集合中
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{…})
中构建命令处理程序
是一个没有别名键的指南。您不应该做的是首先使用开关来处理您的命令
您应该做的是使用命令处理程序。这样,您可以将所有命令导出到单独的文件中,并使用名为别名的文件
首先在与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
文件添加一些内容。
需要文件系统模块fs
和Discord
。创建两个新集合
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));
};
接下来,需要将所有名称和别名添加到两个新集合中
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{…})
中构建命令处理程序
是没有别名键的指南。这是否回答了您的问题?这回答了你的问题吗?