机器人未在原始事件中添加角色

时间:2018-12-25 23:14:00

标签: javascript node.js bots discord.js

我无法给做出任何反应的用户。

机器人应该给正在做出特定角色的人,但是每次我做出反应时,它都会引发错误:

TypeError: Cannot read property 'addRole' of undefined

这是代码:

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

1 个答案:

答案 0 :(得分:0)

git使用此代码:

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