简体   繁体   中英

TypeError: message.member.hasPermissions is not a function

I want to make an unban command, but I still get some errors along the way and I don't know what to do.

TypeError: message.member.hasPermissions is not a function that is my error that i get

Please help me. I am a beginner at and this is my unban.js code:

const Discord = require('discord.js');
const client = new Discord.Client();

const embed = new Discord.MessageEmbed()
.setColor('#0B15A3')

module.exports = {
  name: 'unban',
  description: "This unbans a user",
  execute(message, args, client, Discord) {
      
      if (message.member.hasPermissions("BAN_MEMBERS") ){
          if (!isNaN(args[0])) {
            const bannedMember = message.guild.members.cache.get(args[0]) // Get the `member` property instead to recall later.
            var reason = args.slice(1).join(" ");
            if(!reason) {
              reason = "No reason given!"
            }
            if (bannedMember) {
              bannedMember
                message.guild.members.unban(bannedMember.id, reason)
                .then(() => {
                  embed.setDescription(`Successfully unbanned **${bannedMember.user.tag}**`); // `user` is undefined.
                  message.channel.send(embed);
                })
                .catch(err => {
                  embed.setDescription('I was unable to unban the member');
                  message.channel.send(embed);
                  console.error(err);
                });
            } else {
              embed.setDescription("That user isn't in this guild!");
              message.channel.send(embed);
            }
          } else {
            embed.setDescription("You need to provide an user ID to unban");
            message.channel.send(embed);
          }
      } else {
        embed.setDescription("You do not have `BAN_MEMBERS` permissions to unban this member");
        message.channel.send(embed);
      }
  }
}

and this is my index.js code:

const Discord = require('discord.js');

const client = new Discord.Client({ partials: ["MESSAGE", "CHANNEL", "REACTION" ]});

const prefix = '-';

const fs = require('fs');

client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))
for(const file of commandFiles){
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command)
}



client.once('ready', () => {
    console.log(`${client.user.tag} is online!`);


    client.user.setPresence({
     status: 'online',
     activity: {
         name: "Prefix: -",
         type: "PLAYING"
     }
 });
});

client.on('message', message =>{
   if(!message.content.startsWith(prefix) || message.author.bot) return;

   const args = message.content.slice(prefix.length).split(/ +/);
   const command = args.shift().toLowerCase();

   if(command === 'ping'){
        client.commands.get('ping').execute(message, args);
   } else if(command === 'invite'){
        client.commands.get('invite').execute(message, args, Discord);
   } else if(command === 'help'){
        client.commands.get('help').execute(message, args, Discord);
   } else if(command === 'clear'){
        client.commands.get('clear').execute(message, args);
   } else if(command === 'nuke'){
        client.commands.get('nuke').execute(message, args, Discord);
   } else if(command === 'supersecretrules'){
        client.commands.get('supersecretrules').execute(message, args, Discord);
   } else if(command === 'support'){
        client.commands.get('support').execute(message, args, Discord);
   } else if(command === 'kick'){
        client.commands.get('kick').execute(message, args);
   } else if(command === 'ban'){
        client.commands.get('ban').execute(message, args);
   } else if(command === 'unban'){
        client.commands.get('unban').execute(message, args);
   }
     
});

client.login('token');

Can you help me? Thanks in advance!

You made a typo mistake. It is hasPermission() and not hasPermissions() :

const Discord = require('discord.js');
const client = new Discord.Client();

const embed = new Discord.MessageEmbed()
.setColor('#0B15A3')

module.exports = {
  name: 'unban',
  description: "This unbans a user",
  execute(message, args, client, Discord) {
      
      if (message.member.hasPermission("BAN_MEMBERS") ){
          if (!isNaN(args[0])) {
            const bannedMember = message.guild.members.cache.get(args[0]) // Get the `member` property instead to recall later.
            var reason = args.slice(1).join(" ");
            if(!reason) {
              reason = "No reason given!"
            }
            if (bannedMember) {
              bannedMember
                message.guild.members.unban(bannedMember.id, reason)
                .then(() => {
                  embed.setDescription(`Successfully unbanned **${bannedMember.user.tag}**`); // `user` is undefined.
                  message.channel.send(embed);
                })
                .catch(err => {
                  embed.setDescription('I was unable to unban the member');
                  message.channel.send(embed);
                  console.error(err);
                });
            } else {
              embed.setDescription("That user isn't in this guild!");
              message.channel.send(embed);
            }

You have made a simple typography error. The correct function name is GuildMember#hasPermission .

Change:

message.member.hasPermissions("BAN_MEMBERS")

To:

message.member.hasPermission("BAN_MEMBERS")

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