Ad

Bot Not Adding Role In Raw Event

- 1 answer

I'm not able to give the user who has reacted any role.

The bot should give the person who is reacting a certain role, but every time I react, it throws an error:

TypeError: Cannot read property 'addRole' of undefined

This is the code:

bot.on("raw", event => {
  if (event.t == "MESSAGE_REACTION_ADD" || event.t == "MESSAGE_REACTION_REMOVE") {
    if (event.d.message_id === "527150993033527296") {
      if (event.d.user_id !== bot.user.id) {
        if (event.d.emoji.id === "527129359895625728") {
          var myRole = "525279851247173644";
          defRole(myRole);
        }

        function defRole() {
          if (event.t === "MESSAGE_REACTION_ADD") {
            event.d.user_id.guild.addRole(myRole);
            var myRole;
          } else {
            var myRole;
          }
        }
      }
    }
  }
});
Ad

Answer

git it working with this code:

bot.on("raw", event => {
    if (event.t == "MESSAGE_REACTION_ADD" || event.t == "MESSAGE_REACTION_REMOVE") {
        if (event.d.message_id === "529692171234705418") { //ID of Message where the Reaction is added
          if (event.d.user_id !== bot.user.id) {
              let reaction = event.d.emoji.id;
              if (reaction === "527129359895625728") { // Reaction Emoji ID
                  var myRole = "525279851247173644";   // Role Id
                  defRole(myRole);
              }
              function defRole() {
                  const myGuilds = bot.guilds.get(event.d.guild_id);
                  if (event.t === "MESSAGE_REACTION_ADD") {
                      myGuilds.fetchMember(event.d.user_id).then(member => member.addRole(myGuilds.roles.get(myRole))).catch(console.error);
                  }
                  else {
                      myGuilds.fetchMember(event.d.user_id).then(member => member.removeRole(myGuilds.roles.get(myRole))).catch(console.error);
                  }
              }
          }
      }
    }
});
Ad
source: stackoverflow.com
Ad