所以我正在尝试组装一个机器人,但我发现无论出于何种原因,它都不会响应任何包含大写字母的命令。我的意思是它已经定义了包含大写字母的命令,但机器人实际上不会在具有大写字母的命令中运行代码。我在下面输入的 switch/case 中定义了我的命令,它使用从主文件传递到命令文件的对象的属性。
switch (command.call) {
case 'ping':
message.reply(`Pong! This message had a latency of ${latency}ms.`);
console.log(`Received message $ping, responded with latency of ${latency}ms.`);
break;
case 'help':
message.reply('Current commands available:\n \`$ping\`: Get the latency of the bot.\n \`$help\`: Show this message.\n \`$setPrefix\` \`[arg]\`: Set the prefix to a given string, \`[arg]\`');
console.log(`Received message $help, responded with latency of ${latency}ms.`);
break;
case 'setPrefix':
message.reply(`Set the new prefix to ${config.prefix}`);
console.log(`Received message $setPrefix, responded with latency of ${latency}ms.`);
break;
case 'Test':
message.reply('I got your message!');
console.log(`Received message $test, responded with latency of ${latency}ms.`);
break;
}
}
因此命令 ping
和 help
可以正常工作,但是当我尝试调用 setPrefix
或 Test
时,机器人不执行任何操作。
这是我将命令传递给定义命令的文件的代码:
commandBody = message.content.slice(config.prefix.length), //Get the body of the command
args = commandBody.split(' '), //Get the command arguments
call = args.shift().toLowerCase() //Get the command
const command = {
commandBody: commandBody,
args: args,
call: call
}
commands(command, message); //Call the given command from commands.js
调用 commands() 的最后一行引用了为机器人定义命令的文件
答案 0 :(得分:-1)
添加到您的代码 .toLowerCase()