【问题标题】:How to count the total reactions on a message? (Discord.Js)如何计算消息的总反应? (Discord.Js)
【发布时间】:2021-04-01 00:15:12
【问题描述】:

就上下文而言,我不是指reactions.cache.size。相反,我想知道是否有办法获得对消息的总反应量。

例如,我不希望它返回数字 2,就像 cache.size 会做的那样。相反,我希望它返回 3。如何计算对单个消息的反应总数?

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    你可以使用map()对象的reduce函数。

    这是一个例子:

    client.channels.cache.get("Your Channel ID").messages.fetch("Your Message ID").then(msg => {
      let totalReactionsCount = msg.reactions.cache.map(reaction => reaction.count).reduce(function(tot, arr) {
        return tot + arr;
      },0);
    });
    

    这将返回消息的反应总数。

    【讨论】:

      【解决方案2】:
      const emojisList = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣']
      
      
       do {
                                  var reactionCount = reactions.get(emojisList[i]);
                                  //console.log('Reaction count is' + reactionCount.count)
                                  reactionArray.push(reactionCount.count)
                                  //console.log("Total reactions is "+total)
                                  var percentage = '‎‎‏‏‎ ‎'
                                  resultsEmbed.addField(`The option *${questionOptionsArray[i]}* got a total of ` + reactionArray[i] + ' votes.', percentage)
                                  i++
                                  console.log(`i = ${i}`)
                              }
                              while (i < reactions.size)
      

      这允许我在一个循环中完成它。我引用了我存储表情符号的数组来计算它。因为我不知道会有多少表情符号,所以我必须执行此循环并让它继续,直到它耗尽了对消息的反应量。这可能不是最理想的方式,但我找不到任何其他方式与我的程序的工作方式。

      【讨论】:

        猜你喜欢
        • 2021-03-31
        • 2021-03-23
        • 2020-03-03
        • 2021-05-13
        • 2020-06-25
        • 2021-01-03
        • 2021-05-14
        • 1970-01-01
        • 2021-06-09
        相关资源
        最近更新 更多