Check if a reacting user has a role

2019-08-27 16:06发布

问题:

So, I was making a platform for a suggestion channel. The idea was that there is a voting, and when it reaches a such cap, it auto accepts or rejects. I was also making staff can approve or reject. Well, i had a problem in this.

An image of this platform (I censored out the nicknames): The image

When it reaches the approve cap (10 white check marks), it is supposed to delete the embed and post a new one meaning that the suggestion is approved.

Reproducible sample of code:

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();
      }
    }
  }
})

When adding a console.log of it reaching, heroku logs don't put out anything. I was looking for the problem for over a hour.

回答1:

Please never do this.

Discord.js has a guide on what to do in this situation https://discordjs.guide/#/popular-topics/reactions?id=awaiting-reactions

I would follow their example and use message.awaitReactions To sum it up, use the filter to set the possible reactions you will consider, makes everything much easier.

This is from that link.

message.react('