Lyasm
Lyasm

Reputation: 19

Ban outside server users in Discord.js 12.5.1

I am trying to make a ban command that can ban someone using his ID and it works great, the problem is that when I try to ban a user that isn't on the server (using his ID) or only use 1 digit, the command doesn't work and the bot crashes. I think the problem is caused by the if statement but I'm not sure.

Here's my code :

  let args = messageArray.slice(1);

  if(message.content.startsWith(prefix + 'ban')) {
      let toBan = message.guild.members.cache.get(args[0]) || message.mentions.members.first() || message.guild.members.cache.find(x => x.user.username.toLowerCase() === args.slice(0).join(" ") || x.user.username === args[0]);
      if(!args) return message.channel.send('You need to mention a user!')
      if (!message.member.hasPermission("VIEW_AUDIT_LOG")) return message.channel.send("You don't have enough permissions.")
      if (!message.guild.me.hasPermission("BAN_MEMBERS")) return message.channel.send("I don't have enought permissions.")
             if(!toBan.bannable) return message.channel.send("I can't ban that user!")
      if(toBan === message.author) return message.channel.send("No.")
      if(toBan.roles.highest.position >= message.member.roles.highest.position) return message.channel.send("You can't ban anyone that have a high or same role that you have.")

      const reason = args[1] || "Undefined";

      member.ban(`${toBan}`, {
        reason: reason
      })
           message.channel.send(`${toBan} Has been banned of the server by ${message.author}!`)
       }

Upvotes: 1

Views: 336

Answers (1)

Wylie Fowler
Wylie Fowler

Reputation: 409

You can use GuildMemberManager.ban(), which takes a UserResolvable. It can be a User, a GuildMember, or even just a Snowflake (ID).


Simple Example:

message.guild.members.ban("12345678901234567");

Upvotes: 1

Related Questions