Home > Back-end >  how to remove roles of guild member TypeError: Cannot read properties of undefined (reading 'ro
how to remove roles of guild member TypeError: Cannot read properties of undefined (reading 'ro

Time:12-13

if the member got any role of the roleIDs the bot removes it

when i use guild.members.cache.get() it says

TypeError: Cannot read properties of undefined (reading 'roles')

when i use guild.members.fetch() it says [i removed .cache when i used fetch]

TypeError: Cannot read properties of undefined (reading 'some')

the code:

 var roleIDs = ["1048196394688725053","1051123574955589632"]
 const guild = client.guilds.cache.get(guildId)
 const member = guild.members.cache.get(discordid)
          if(!result[0]){
          }else{
            
            if(JSON.stringify(e) =='-1'){
              
              if (member.roles.cache.some(role => roleIDs.includes(role.id))) {
                  await member.roles.remove(role => roleIDs.includes(role.id))
              }
              //member.setNickname(`[${result.id}]-${JSON.stringify(charname).replaceAll("_"," ")}`)
            }
           }

if the member got any role of the roleIDs the bot removes it

CodePudding user response:

"TypeError [InvalidType]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes." This is because you are attempting to remove a role with a filter function. This is not built in to the \<GuildMemberRoleManager>#remove method. Consider the following changes to remove all of the roles which also exist in the collection.

await member.roles.remove(member.roles.cache.filter(r => roleIDs.includes(r.id)));

CodePudding user response:

Consider fetching the GuildMember

const member = await guild.members.fetch(discordid);
  • Related