简体   繁体   中英

Make it so only people with certain role can use command. In discord.js v12+

I have a bot that sends a message if you use a command in the non-command channel it tells you to only use commands in the correct channel, but I want it not to affect people with the Staff role here is my code i am getting this error TypeError: Cannot read property 'roles' of null My code:

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.');
  }
}
});

Thanks in advance! (BTW i am trying to do this is discord.js v12)

if someone finds this and is looking for the answer i used let allowedRole = message.guild.roles.cache.find(r => r.name === "Staff"); and that worked for me.

You can try something like this to allow members with certain roles to use your commands:

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
}

Please let me know if this helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM