如何将上述角色添加到成员?

时间:2021-04-27 06:41:28

标签: javascript discord.js

在我的不和谐机器人中,我试图制作一个 add command,因此它添加了 member 自我角色。

我知道如何将 roles 添加到 member 但我知道如何添加 memberrolementioned

这是我目前的代码:

if (msg.content.startsWith(prefix + 'add')) {
    let rolename = msg.content.slice(5).trim()
    let role = msg.guild.roles.cache.find(r => r.name === rolename)

    //The code will work if the member only types the name of the role without mention
    //so it is also a solution if someone knows how to change the mention to a name
    msg.member.roles.add(role)
}

1 个答案:

答案 0 :(得分:1)

使用 message 对象的 mentions 属性并检查 mention 中是否包含 role

如果是,则存储该值,然后将其添加member。而 <MessageMentions>#roles#first() 会为您提供 first mentioned role 集合中的 roles