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



我有一个机器人程序,如果你在非命令通道中使用命令,它会发送消息,它会告诉你只在正确的通道中使用指令,但我希望它不会影响Staff角色的人。这是我的代码,我得到了这个错误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.');
}
}
});

我正在尝试做这是不和谐.js v12

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

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

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
}

最新更新