简体   繁体   English

条件角色 Discord.js

[英]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.这样做可能会导致一个错误: 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:因此,要修复您的错误,您所要做的就是将所有角色 ID 作为字符串传递,这样您的最终代码将如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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