简体   繁体   中英

Conditional Role Discord.js

Basically, what I'm trying to do is have it so that if a member in my server sends a message they get the "Active" role. I have another bot doing that already. The issue is, I want to assign an "Inactive" role to anyone who hasn't sent a message. The way I have it set up (I think) is that when a member's roles are changed, it checks to see if the member has the "Active" role and removes the "Inactive" role from them. Inversely, if the "Active" role is removed, it's supposed to add the "Inactive" role.

    client.on("guildMemberUpdate", (oldMember, newMember) => {
  //Has Acitve, remove Inactive.
  if (newMember.roles.cache.has(971885336680616007)) {
    newMember.roles.remove(971903053626241034);
  }
  //Doesn't have Active, add Inactive.
  elseif (!newMember.roles.cache.has(971885336680616007))
    newMember.roles.add(971903053626241034)
}) 

This is the code I'm using but I feel like I must've done something incorrectly because I can't get it to work

The reason why you might not be able to add the roles and why you might get an error is because, when checking if the member has a role, instead of passing a string, you are passing a number. One error you might get from doing it like this is: TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes. So to fix your error, all you have to do is pass all the role ids as strings so that your final code will look something like this:

client.on("guildMemberUpdate", (oldMember, newMember) => {
    // User has active role, remove inactive role
    if (newMember.roles.cache.has('971885336680616007')) {
        newMember.roles.remove('971903053626241034');
    }
    // User doesn't have active role, add inactive role
    else if (!newMember.roles.cache.has('971885336680616007')) {
        newMember.roles.add('971903053626241034')
    }
}) 

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