javascript逻辑运算符不起作用

时间:2020-09-02 14:32:07

标签: javascript

我正在创建discord bot,它将进行coinflip,但是我遇到了一些逻辑运算符错误,无法弄清出什么问题了。我希望该机器人如果命令不完整并且在某些字符串之间不起作用时将对命令做出响应。这是怎么回事-如果我输入!coinflip blue-可以,如果我输入“ coinflip red”,机器人会回应我“选择货币”的提示-然后机器人仍然认为!coinflip之后没有任何提示,并且会说“选择一面”。请帮忙。

if (args[0] !== ('blue'||'red')) {
    message.reply('If you do not know how to create coinflip type **!help**');
    message.reply('Choose a side [blue, red]. [Example: !coinflip **blue**].');

  } else if (args[0] === ( 'blue' || 'red' )) {
        if (args[1] !== 'ref' || args[1] !== 'key') {
          message.reply('*If you do not know how to create coinflip type **!help***');
          message.reply('Choose a currency [ref, key]. [Example: !coinflip blue **key**].');
    } else if (args[1] === 'ref' || args[1] === 'key') {etc...}

2 个答案:

答案 0 :(得分:0)

重写此内容:

if (args[0] !== ('blue'||'red')) {

以下任何一项:

if (args[0] !== 'blue' && args[0] !== 'red') {
if (!['blue', 'red'].includes(args[0])) {
if (!/blue|red/.test(args[0])) {

最后一个选项与您当前的尝试最为相似。

答案 1 :(得分:0)

if (args[0] !== ('blue'||'red')) {

需要成为

if ((args[0] !== 'blue') && (args[0] !== 'red')) {

'blue'||'red'只是'blue',这不是您想要的,假设您想将args [0]与'blue'和'red'比较

相关问题