为了账号安全,请及时绑定邮箱和手机立即绑定

使只有具有特定角色的人才能使用命令。在discord.js v12+ 中

使只有具有特定角色的人才能使用命令。在discord.js v12+ 中

慕雪6442864 2023-10-14 09:48:36
我有一个机器人,如果您在非命令通道中使用命令,它会发送一条消息,它会告诉您仅在正确的通道中使用命令,但我希望它不会影响具有员工角色的人员,这是我得到的代码这个错误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
查看完整描述

2 回答

?
qq_笑_17

TA贡献1818条经验 获得超7个赞

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



查看完整回答
反对 回复 2023-10-14
?
江户川乱折腾

TA贡献1851条经验 获得超5个赞

您可以尝试这样的操作,以允许具有特定角色的成员使用您的命令:


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

}


查看完整回答
反对 回复 2023-10-14
  • 2 回答
  • 0 关注
  • 64 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信