简体   繁体   中英

Having issue assigning roles on guildMemberUpdate discord.js

I'm currently working on a discord bot for a server with third-party age verification, after their update with the verified role being added.

I wanted to then apply another role depending on a separate identifying role. But I keep getting problems with parts of the code being undefined or not a function, so I'm really stuck.

代码图片

bot.on('guildMemberUpdate', (oldMember, newMember) => {
    if(oldMember.roles !== newMember.roles) {
      if(newMember.roles.cache.some(r => r.name === "Verified")) {
        if(newMember.roles.cache.some(r => r.name === "Workshop Devotee")) {
          let role1 = newMember.guild.roles.cache.some(role => role.name === "Verified Workshop Devotee");
          newMember.guild.cache.get(newMember.author.id).roles.add(role1);
        }
        else if (newMember.roles.cache.some(r => r.name === "Workshop Helpers")) {
          let role2 = newMember.guild.roles.cache.some(role => role.name === "Verified Workshop Helpers");
          newMember.guild.cache.get(newMember.author.id).roles.add(role2);
        }
        else if (newMember.roles.cache.some(r => r.name === "Workshop Supporters")) {
          let role3 = newMember.guild.roles.cache.some(role => role.name === "Verified Workshop Supporters");
          newMember.guild.cache.get(newMember.author.id).roles.add(role3);
        }
        else{
          return;
        }

There are two major errors with this code which is why it isn't working.

First of all, you are using the .some() method to fetch a role object, however .some() only checks to see if an item exists in a list, and returns true or false. Instead, you should use the .get() method to fetch the role.

let role1 = newMember.guild.roles.cache.get(role => role.name === "Verified Workshop Devotee");

The other error I can see, is where you are getting the member object to add the role. You are for some reason trying to fetch a guild object using newMember.author.id which is the ID of a member, so this will return an error. Instead you can write

newMember.guild.members.cache.get(newMember.author.id).roles.add(role1);

However, this code is also redundent as you already have the member object of the member you are adding roles to - its newMember . Therefore, you can instead write

newMember.roles.add(role1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM