简体   繁体   中英

How do I only allow a command to be toggled by users with certain permissions?

I have coded a bot that can format text - I need it so that not just randoms can access the command. Here is my code - it throws no errors

try {
    if(message.member.guild.me.hasPermission('ADMINISTRATOR')) {
      if (args[0] === 'off') {
        christian[message.guild.id] = false
      } else if (args[0] === 'on') {
        christian[message.guild.id] = true
      }
      message.channel.send(`Christian Mode ${christian[message.guild.id] ? 'Active' : 'Inactive'}`)
      return christian
    } else {
      message.channel.send('You do not have permission to use that command!')
    }
  } catch (error) {
    console.log('Permission Error')
  }

note: the command is toggled back to the index.js (main) by saying =Christian on/off this function works.

message.member.guild.me.hasPermission('ADMINISTRATOR') is checking the bot's permissions, not the command executor's. Call GuildMember#hasPermission() on message.member (see Message#member ).

For example...

if (!message.member.hasPermission('ADMINISTRATOR')) {
  // return an error to the user (and actually return)
}

// continue with command code

NOTE: The Discord.js documentation hyperlinked is for recently released v12. If your Discord.js isn't up to date, switch to the correct version at the top of the page for accurate info.

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