【问题标题】:if it has footer.text it reacts, but I want it to do the opposite如果它有 footer.text 它会做出反应,但我希望它做相反的事情
【发布时间】:2021-11-16 12:03:21
【问题描述】:

它的作用是在嵌入有 footer.text 时添加反应 :pray:,但我不希望这样,我希望它在没有 footer.text 时作出反应

if(message.embeds.length >= 0) 
    // Check if the Message has embed or not
    {
      let embed = message.embeds
      // console.log(embed) just a console.log
      for(let i = 0; i < embed.length; i++)
      // Loop it since in v13 you can send multiple embed in single message
      {
        if (!embed[i] || !embed[i].footer || embed[i].footer.text === null) return;
        // check each embed if it has footer.text or not, if it doesnt then do nothing
        {
          setTimeout(function(){
            message.react(':pray:')
          }, 1000);
        }
      }
    }

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    你只需要像这样切换你的条件:

    if (embed[i] || embed[i].footer || embed[i].footer.text !== null) {
        setTimeout(function() {
            message.react('?')
        }, 1000);
    }
    

    其中!== 代表Strict Inequality,所以你实际上在做的是检查以下条件

    • 如果有嵌入到消息中
    • 如果该嵌入对象有页脚
    • 如果页脚的内容不为空

    此外,由于所有这些条件都是必需的,我建议使用 Logical AND (&amp;&amp;) 运算符,它仅在所有操作数的检查都已完成时才返回 true,因此您的代码将如下所示:

    if (embed[i] && embed[i].footer && embed[i].footer.text !== null) {
        setTimeout(function() {
            message.react('?')
        }, 1000);
    }
    

    【讨论】:

    • 我使用了第二个,但我必须添加 return; 才能使其工作 if (embed[i] &amp;&amp; embed[i].footer &amp;&amp; embed[i].footer.text !== null) return;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多