【问题标题】:(discord.js) add role to author(discord.js) 为作者添加角色
【发布时间】:2020-01-02 01:23:58
【问题描述】:

我正在寻求帮助,我的代码处理了一个“消息”事件,我正试图让它为评论的作者添加一个角色

我目前的尝试是

client.on('message', msg => {msg.member.addRole("[role id]").catch(function(){});})

但是,它似乎不适用于我所做的各种尝试。有什么帮助吗?

谢谢!

【问题讨论】:

  • 括号中的括号是否只是为了表明角色的 id 所在的位置,还是您也将它们保留在那里?
  • 我的问题已通过使用:msg.guild.members.get(msg.author.id) 选择用户得到解决

标签: node.js discord discord.js


【解决方案1】:

使用此代码。 先检查用户是否没有这个角色,然后给他这个角色

client.on('message', msg => {
    if (!msg.member.roles.some(role => role.id === 'YourROLEID')) {
            msg.member.addRole('YourROLEID')
            .then(console.log(`Succesfuly added role to member ${msg.author.tag}`))
            .catch(console.error)
    }
})

【讨论】:

  • 如果您要在谓词函数中按 ID 搜索 Collection.some(),则只需确保 msg.member.roles.get('YOUR ID') 的存在即可。
  • 嗯,是的,这也是正确的方法。使用更正确地获取它。 if (!msg.member.roles.some(role => role.id === 'YourROLEID')) { 可以替换为if (!msg.member.roles.get('YourROLEID') {
猜你喜欢
  • 2021-01-28
  • 2021-08-05
  • 2022-01-18
  • 2020-11-30
  • 2021-05-03
  • 2023-03-15
  • 2021-04-10
  • 2021-02-06
  • 1970-01-01
相关资源
最近更新 更多