首页 > 解决方案 > 我的不和谐机器人忽略了其中包含大写字母的命令

问题描述

所以我正在尝试组装一个机器人,我发现无论出于何种原因,它都不会响应任何包含大写字母的命令。我的意思是它已经定义了包含大写字母的命令,但机器人实际上不会运行包含大写字母的命令中的代码。我在下面输入的开关/案例中定义了我的命令,它使用从主文件传递给命令文件的对象的属性。

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时机器人没有做任何事情。setPrefixTest

这是我将命令传递给定义命令的文件的代码:

  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() 的最后一行引用了定义机器人命令的文件

标签: javascriptdiscorddiscord.js

解决方案


添加到您的代码.toLowerCase()


推荐阅读