Kevin Verdoodt
Kevin Verdoodt

Reputation: 51

Discord.js version 12 I can't ban a user

I'm using discord.js version "12.1.1" and for this script 'banuser.js' I'm using a command handler.

For now I just want to ban the person, but I keep getting this error: (node:30800) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'banable' of undefined

Cool features if this would work would be unban + a timed ban -> that when I ban the person I can say 1/7 day ben or in hours... But for now I want to make it work so I can ban people.

const discord = require("discord.js");

module.exports.run = async (bot, message, args) => {

  if (message.member.hasPermission("BAN_MEMBERS")) {

      if (!message.mentions.users) return message.reply('You must tag 1 user.');

      else {

          const channel = message.guild.channels.cache.get("696692048543088691");
          const member = message.mentions.members.first();
          let reason = message.content.split(" ").slice(2).join(' ');

          if (member.banable== false) return message.channel.send("That user cannot be baned!");

          else {

              if (!reason) reason = (`No reason provided.`);

              await member.send(`You have been baned from **${message.guild.name}** with the reason: **${reason}**`)
                  .catch(err => message.channel.send(`⚠ Unable to contact **${member}**.`));

              await member.ban(reason);

              const banEmbed = new discord.MessageEmbed()
                  // .setAuthor(member.user.tag, member.user.iconURL()) -> kleine avatar
                  .setAuthor(member.user.avatarURL())
                  .setThumbnail(member.user.avatarURL())
                  .setAuthor(member.user.tag)
                  .setColor("#ee0000")
                  .setTimestamp()
                  .addField("Kicked By", message.author.tag)
                  .addField("Reason", reason);
                  // console.log(member.user.avatarURL('Heeft geen avatar'))

              await channel.send(banEmbed);

              console.log(`${message.author.tag} Baned ${member.user.tag} from '${message.guild.name}' with the reason: '${reason}'.`);

          }
      }
  } else {
      message.channel.send("You do not have permission to use the ban command.");
      return;
  }
}


module.exports.help = {
  name: "banuser"
}

/* module.exports.run = async (bot, message, args) => {

    if (!message.member.hasPermission("BAN_MEMBERS") || !message.member.hasPermission("ADMINISTRATOR")) return message.channel.send("You don't have a permissions to do this.");
    let user = message.mentions.users.first();

    let member = message.guild.member(user);
    let reason = args.slice(22).join(" ");

    if (!user) return message.channel.send("Please mention the user.");
    if (user.id === message.author.id) return message.channel.send("You can't ban yourself.");
    if (user.id === client.user.id) return message.channel.send("You can't ban me.");

    if (!reason) reason = "No reason provided";

    member.ban(reason).then(() => {
      message.channel.send(`Successfully banned **${user.tag}**`);
    }).catch(err => {
      message.reply("I was unable to ban the member.");
    })
}
module.exports.help = {
    name: "banuser"
} */

This is my code so far

Upvotes: 1

Views: 1002

Answers (1)

Syntle
Syntle

Reputation: 5174

You'll have to replace if (!message.mentions.users) return message.reply('You must tag 1 user.'); with if (!message.mentions.members) return message.reply('You must tag 1 user.');

Keep const member = message.mentions.members.first(); as it is

And replace member.banable with member.bannable

Upvotes: 1

Related Questions