首页 > 解决方案 > 如何将提到的角色添加到成员?

问题描述

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

我知道如何添加rolesmember但我知道如何添加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)
}

标签: javascriptdiscord.js

解决方案


使用对象的mentions属性message并检查其中是否mention有a role

如果是,则存储该值,然后其添加到member. 并且<MessageMentions>#roles#first()会给你first mentioned role来自集合的roles


推荐阅读