Home > front end >  discord bot ban/kick command is not working
discord bot ban/kick command is not working

Time:05-17

i made a ban and kick command but wenn i use then my bot craches with the error [ message.guild.member is not a function ]

this is my code for the ban command

  if (message.content.startsWith(prefix   'ban')) {
    const user = message.mentions.users.first();
    if (user) {
      if (message.member.hasPermission('BAN_MEMBERS')) {
        message.guild.member(user).ban('Vous avez été ban par un admin').then(() => {
          message.channel.send(`${user.username} a été ban !`);
        }
        ).catch(err => {
          console.log(err);
        }
        );
      } else {
        message.channel.send('Vous n\'avez pas la permission de ban !');
      }
    } else {
      message.channel.send('Vous devez mentionner un utilisateur !');
    }
  }
});

this is my code for the kick command

client.on('message', message => {
  if (message.content.startsWith(prefix   'kick')) {
    const user = message.mentions.users.first();
    if (user) {
      const member = message.guild.member(user);
      if (member) {
        if (message.member.hasPermission('KICK_MEMBERS')) {
          member.kick('Vous avez été kick par un admin').then(() => {
            message.reply(`${user.tag} a été kick !`);
          }).catch(err => {
            message.reply('Je ne peux pas kick cet utilisateur !');
            console.log(err);
          });
        } else {
          message.reply('Vous n\'avez pas la permission de kick !');
        }
      } else {
        message.reply('Cet utilisateur n\'est pas dans le serveur !');
      }
    } else {
      message.reply('Vous devez mentionner un utilisateur !');
    }
  }
});

CodePudding user response:

client.on('messageCreate', async (message) => { //since you're using v13, *message* is deprecated. So you need to change it to *messageCreate*
   if(message.content.startsWith(prefix   "kick")) { //You can do ${prefix}kick too.
      if(!message.member.permissions.has(Permissions.FLAGS.BAN_MEMBERS)) return message.reply("You can't use this command") //This line code is for when the member using this command. It will return when permissions isn't detected.
      if(!message.guild.me.permissions.has(Permissions.FLAGS.BAN_MEMBERS)) return message.reply("I can't use this command.") //This is for when the bot missing its permissions.
      const member = message.mentions.members.first(); //users is not going to work.
      if(!member) return message.reply("Who are you going to kick?") //This line code is for when the member didn't find or pinged.
      if(!member.bannable) return message.reply("I can't ban this person") // This is for the person that is not bannable.
      await member.ban({ reason: 'Your reply'}) //You need to await, so it can fetch the member.
   }
})

You can also use their ID to ban using

const member = message.mentions.members.first() || message.guild.members.fetch(args[0]) || message.guild.members.cache.get(args[0])

You can also add reason for your own using

const reason = args.slice(1).join(" ")
member.ban({
   reason: reason
})

And using an embed

const reason = args.slice(1).join(" ")
member.ban({
   reason: reason
})

const embed = new MessageEmbed()
.setDescription(`${reason}`)
.setColor('RANDOM')

message.channel.send({embeds: [embed]})

CodePudding user response:

change the permission line to this...

message.member.permissions.has(Permissions.FLAGS.BAN_MEMBERS)

change the ban line to this...

user.ban({ reason:'Vous avez été ban par un admin' })
.then(() => {
    message.channel.send(`${user.username} a été ban !`);
}

lmk if this helped ;)

CodePudding user response:

Do it like this:

message.guild.members.cache.get(user).ban()
message.guild.members.cache.get(user).kick()

  • Related