'discord.js check if user has permissions

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?



Solution 1:[1]

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.

Solution 2:[2]

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)

Use this link for reference to new permissions setup.

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")
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shahryar_Jahanshahloo
Solution 2