Home > Mobile >  discord.js check if user has permissions
discord.js check if user has permissions

Time:05-15

I want a command that can only be used by people who have the ban permission current code:

if(msg.member.guild.me.hasPermission('BAN_MEMBERS'))
    {
      msg.channel.send("hi")
    } else {
      msg.channel.send("You do not have permission to use this command")
    }

I get the error: msg.member.guild.me.hasPermission is not a function

What am I doing wrong?

CodePudding user response:

msg.member.guild.me is of type GuildMember and according to this https://discord.js.org/#/docs/discord.js/stable/class/GuildMember?scrollTo=permissionsIn GuildMember has no method called hasPermission. Pay attention that a lot of discord guides you see on the internet are based on the old versions.

CodePudding user response:

msg.member.guild.me.hasPermission('BAN_MEMBERS') looks to see if the client (bot) has the BAN_MEMBERS permission (back in old Discord versions anyways now it does nothing because .hasPermission has been replaced with permissions.has)

You need to check if the message sender has permission by using this code

const { Permissions } = require('discord.js'); // add this to the very top of your file

if (msg.member.permissions.has(Permissions.FLAGS.BAN_MEMBERS))
    {
      msg.channel.send("hi")
    } else {
      msg.channel.send("You do not have permission to use this command")
    }
  • Related