【问题标题】:Make it so only people with certain role can use command. In discord.js v12+使其只有具有特定角色的人才能使用命令。在 discord.js v12+
【发布时间】:2021-03-16 19:56:08
【问题描述】:

如果您在非命令通道中使用命令,我有一个机器人会发送消息,它会告诉您仅在正确的通道中使用命令,但我希望它不会影响具有员工角色的人这是我的代码我收到此错误TypeError: Cannot read property 'roles' of null 我的代码:

client.on('message', message => {

if(!message.member.roles.cache.has(784236433975541771)) {

  if (message.content.startsWith("-")) {
  if (message.channel.id === '759066524605612108') return;
  if (message.channel.id === '775035651640918067') return;
  if (message.channel.id === '777287305580511262') return;
    message.channel.send('You have been pinged in the <#759066524605612108> channel with the results to your command. Please only use commands there.');
  }else
  if (message.content.startsWith("!")) {
  if (message.channel.id === '759066524605612108') return;
  if (message.channel.id === '775035651640918067') return;
  if (message.channel.id === '777287305580511262') return;
    message.channel.send('Rank has been disabled in this channel. Please only use commands in the <#759066524605612108> channel.');
  }
}
});

我正在尝试这样做是 discord.js v12

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    您可以尝试这样的方法来允许具有特定角色的成员使用您的命令:

    let allowedRole = message.guild.roles.find("name", "Staff");
    if (message.member.roles.has(allowedRole.id) {
        // allowed access to command
    } else { //aka members who arent staff 
       // not allowed access
    }
    

    【讨论】:

    • 现在我得到TypeError: message.guild.roles.find is not a function
    • 应该是message.guild.roles.cache.find()
    • 是的,我忘了缓存,我的 b
    • 好的,谢谢它使用这个let allowedRole = message.guild.roles.cache.find(r =&gt; r.name === "Staff");
    【解决方案2】:

    如果有人发现这个并正在寻找我使用的答案 let allowedRole = message.guild.roles.cache.find(r =&gt; r.name === "Staff"); 这对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-19
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 2020-08-19
      • 2020-12-25
      相关资源
      最近更新 更多