检查反应的用户是否具有角色

时间:2018-11-28 18:26:56

标签: javascript discord.js

因此,我正在为建议频道搭建平台。想法是要进行投票,当投票达到这样的上限时,它会自动接受或拒绝。我也让员工可以批准或拒绝。好吧,我在这方面有问题。

该平台的图片(我检查了昵称): The image

到达批准上限(10个白色复选标记)时,应该删除嵌入内容并发布新的内容,表示该建议已获批准。

可复制的代码示例:

client.on("messageReactionAdd", function (messageReaction, member) {
if (member.bot) return;
    if (messageReaction.message.channel.id == "516263179446124555") {
     if (messageReaction.emoji.name == "✅") {
      if (messageReaction.count >= 10) {
        messageReaction.message.channel.send("", {embed: {
          title: "Suggestion Approved",
          description: messageReaction.message.embeds[0].title + "\n" + messageReaction.message.embeds[0].description,
          author: {
            name: messageReaction.message.embeds[0].author.name,
            icon_url: messageReaction.message.embeds[0].author.iconURL
          },
          color: 3394611,
          footer: {
            text: "Why: The message got 10 ✅ reactions."
          }
        }})
      }
      messageReaction.message.delete();
    }
    if (messageReaction.emoji.name == "516258169035554817") {
      if (messageReaction.count >= 8) {
        messageReaction.message.channel.send("", {embed: {
          title: "Suggestion sent to Staff",
          description: messageReaction.message.embeds[0].title + "\n" + messageReaction.message.embeds[0].description,
          author: {
            name: messageReaction.message.embeds[0].author.name,
            icon_url: messageReaction.message.embeds[0].author.iconURL
          },
          color: 16764006,
          footer: {
            text: "Why: The message got 8 <:neutral:516258169035554817> reactions."
          }
        }})
      }

messageReaction.message.guild.channels.get("517331518843125760").send("", {embed: {
        title: "Suggestion to check",
        description: messageReaction.message.embeds[0].title + "\n" + messageReaction.message.embeds[0].description,
        author: {
          name: messageReaction.message.embeds[0].author.name,
          icon_url: messageReaction.message.embeds[0].author.iconURL
        },
        color: 16764006,
        footer: {
          text: "Approving/disapproving this won't change the embed in <#516263179446124555>."
        }
      }})
      messageReaction.message.delete();
    }
    if (messageReaction.emoji.name == "516258587845328906") {
      if (messageReaction.count >= 7) {
        messageReaction.message.channel.send("", {embed: {
          title: "Suggestion Rejected",
          description: messageReaction.message.embeds[0].title + "\n" + messageReaction.message.embeds[0].description,
          author: {
            name: messageReaction.message.embeds[0].author.name,
            icon_url: messageReaction.message.embeds[0].author.iconURL
          },
          color: 16724736,
          footer: {
            text: "Why: The message got 7 <:bad:516258587845328906> reactions."
          }
        }})
      }
    }
    if (messageReaction.emoji.name == "☑") {
      var staffMemberReacted = false;
      messageReaction.message.guild.members.forEach(function(GuildMember) {
        if (messageReaction.users.keyArray().includes(GuildMember.user) && (GuildMember.roles.has("501752627709870080") || GuildMember.roles.has("493436150019784704"))) {
          staffMemberReacted = true;
        }
      })
        console.log("reached manapprove")
      if (staffMemberReacted) {
        messageReaction.message.channel.send("", {embed: {
          title: "Suggestion Approved",
          description: messageReaction.message.embeds[0].title + "\n" + messageReaction.message.embeds[0].description,
          author: {
            name: messageReaction.message.embeds[0].author.name,
            icon_url: messageReaction.message.embeds[0].author.iconURL
          },
          color: 3394611,
          footer: {
            text: "Why: A owner or co-owner manually approved it."
          }
        }})
        messageReaction.message.delete();
      }
    }
    if (messageReaction.emoji.name == "517327626373824522") {
      var staffMemberReacted = false;
      messageReaction.message.guild.members.forEach(function(GuildMember) {
        if (messageReaction.users.keyArray().includes(GuildMember.user) && (GuildMember.id || GuildMember.roles.find)) {
          staffMemberReacted = true;
        }
      })

      if (staffMemberReacted) {
        messageReaction.message.channel.send("", {embed: {
          title: "Suggestion Rejected",
          description: messageReaction.message.embeds[0].title + "\n" + messageReaction.message.embeds[0].description,
          author: {
            name: messageReaction.message.embeds[0].author.name,
            icon_url: messageReaction.message.embeds[0].author.iconURL
          },
          color: 16724736,
          footer: {
            text: "Why: A owner or co-owner manually rejected it."
          }
        }})
        messageReaction.message.delete();
      }
    }
  }
})

在添加console.log时,heroku日志不会输出任何内容。我一直在寻找问题一个多小时。

1 个答案:

答案 0 :(得分:0)

请不要这样做。

Discord.js提供了有关在这种情况下该怎么做的指南https://discordjs.guide/#/popular-topics/reactions?id=awaiting-reactions

我将按照他们的示例进行操作,并使用message.awaitReactions进行总结,请使用过滤器设置您将考虑的可能反应,使一切变得简单得多。

这是来自该链接。

message.react('').then(() => message.react(''));

const filter = (reaction, user) => {
    return ['', ''].includes(reaction.emoji.name) && user.id === message.author.id;
};

message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
    .then(collected => {
        const reaction = collected.first();

        if (reaction.emoji.name === '') {
            message.reply('you reacted with a thumbs up.');
        }
        else {
            message.reply('you reacted with a thumbs down.');
        }
    })
    .catch(collected => {
        console.log(`After a minute, only ${collected.size} out of 4 reacted.`);
        message.reply('you didn\'t react with neither a thumbs up, nor a thumbs down.');
    });