【问题标题】:Trying to get the number of roles a user has (Discord.js)尝试获取用户拥有的角色数量(Discord.js)
【发布时间】:2021-05-05 04:46:24
【问题描述】:

我正在尝试获取提到的用户拥有的许多角色,但是,除了执行命令的成员之外,我无法找到让他带来职位数量的方法,
也就是说,BOT 只是忽略提及并将角色数量仅提供给命令的作者。我的代码:

const user = message.mentions.users.first() || message.author;

const roles  = message.member.roles.cache;
//just an embed
  {
        "name": "????️ Roles:",
        "value": `${roles ? roles.map(c => roles.length) : 'None'}`
  },

message.channel.send({ embed })

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    当然,它只是获取发送消息的用户的角色。您正在创建一个包含上述用户的 user 变量,但您根本不会在代码中使用它。您只使用message.member.roles.cache,它只获取消息作者的角色。

    使用guild.member()user 转换为GuildMember,然后使用它。

    const user = message.mentions.users.first() || message.author;
    
    const roles  = message.guild.member(user).roles.cache;
    //just an embed
      {
            "name": "?️ Roles:",
            "value": `${roles ? roles.array().length - 1 : 'None'}`
      },
    
    message.channel.send({ embed })
    

    相关资源:
    https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=member

    【讨论】:

    • 现在出现了一个新的错误,它不返回角色的数量,它只返回一个逗号
    • 我已经编辑了答案。现在应该修好了。这可能是由roles.map(c => roles.length) 返回一个数组而不是一个数字以及没有roles.length 这样的属性引起的。我现在还要从角色计数中减去 1,因为默认情况下,[@]everyone 提到的每个用户都算作一个角色。如果修改后问题仍然存在,请告诉我。
    猜你喜欢
    • 2019-02-04
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2021-08-10
    • 2023-03-21
    • 2011-07-09
    • 1970-01-01
    相关资源
    最近更新 更多