繁体   English   中英

如何为用户添加角色

[英]How to add role to a user

我在网上看了几个指南,他们大多使用这个命令

let role = message.guild.roles.cache.find(r => r.id === "Role ID");

// The member you want to add the role to
let member = message.mentions.members.first();

// Add role to the member
member.roles.add(role);

// Or add it to yourself
message.author.roles.add(role);

我尝试了很多,这是我对该代码的实现

if (message.member.roles.cache.some(role => role.name === "Co-Owner" || role.name === "Operator")) {
    if (command == "birthday") {
      const mention = message.mentions.users.first() || client.users.get(args[0]);
      var role = message.guild.roles.cache.find(role => role.name === "BIRTHDAY POG");

      if (mention.roles.cache.some(role => role.name === "BIRTHDAY POG")) {
        message.reply("The user has already the role, the role will now be removed");
        mention.roles.remove(role);
      }
      else {
        mention.roles.add(role);
        message.reply("The role has been added to `" + mention.username + "`");
        client.guilds.cache.get('731368965963513936').channels.cache.get('778124805135663104').send(message.author.username + " has used the command Birthday to " + mention.username);
        message.reply("Use the same command with the person to remove the role");
      }

    }
  }

此命令应该向提到的用户添加生日的角色,但我无法使其工作,它给出了这个错误

      if (mention.roles.cache.some(role => role.name === "BIRTHDAY POG")) {
                        ^

TypeError: Cannot read property 'cache' of undefined

有人可以帮我吗?

看起来您的mention.roles 只是未定义,否则您的代码看起来不错。

你能做一个 console.log(mention) 并发送你得到的吗?

看看这个https://stackoverflow.com/a/63979077/15107810

但基本上,你需要改变

message.mentions.users.first() || client.users.get(args[0]);

message.mentions.members.first() || message.guild.members.cache.get(args[0])

暂无
暂无

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

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