Skip to content
Advertisement

Send a message if there is a role Discord.js

Bot send a message if the User who clicked the reaction has the ROLE "ID"

I decided to try this, but it didn’t work out

if(message.member.roles.cache.has(role.id)) {
  console.log(`Yay, the author of the message has the role!`);
} else {
  console.log(`Nope, noppers, nadda.`);
}

====Here is the main Code====

       sentMessage.react("✅");
        message.delete({ timeout: 100 });
        const filter = (reaction, user) => {
          return !user.bot && ["✅"].includes(reaction.emoji.name);
        };

        sentMessage
          .awaitReactions(filter, {
            max: 1,
            time: 60000,
          })
          .then((collected) => {
            const reaction = collected.first();

            if (reaction.emoji.name === "✅") {
              const member = reaction.users.cache.find((user) => !user.bot);
              
              message.author.send(Hello)

Advertisement

Answer

You should check the role of the member who reacted (the member found in reaction.users.cache). reaction.users.cache returns a user, and you need a guild member to get their roles. You can use message.guild.members.fetch() or message.guild.member() for this. Now you can check if the returned member has the role:

sentMessage.awaitReactions(filter, {
  max: 1,
  time: 60000,
})
.then(async (collected) => {
  const reaction = collected.first();

  if (reaction.emoji.name === '1️⃣') {
    // find the first user who reacted and is not a bot
    const userReacted = reaction.users.cache.find((user) => !user.bot);
    // get the guild member
    const member = await message.guild.member(userReacted);

    if (!member.roles.cache.has('ROLE_ID')) return;

    message.author.send({
      embed: {
        color: 3447003,
        title: 'Вызов принят',
        description: `**Сотрудник:** ${member}`,
        timestamp: new Date(),
      },
    });
  }
})
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement