On ban listener - Discord Bot

I am working on anti-nuke bot, to prevent server from getting nuked by bots. However the discord js documentation is very confusing, so how would I able to listen for ban event using 12v Discord JS. Something like this:

client.on("onBan", ban => {
  console.log(ban.theUserWhoDidTheBan);
  console.log(ban.reason);
  console.log(ban.theUserWhoGotTheBan);
});

So I can print this:

console.log(`${userA} banned ${userB} because ${reason}`);

userA is the user that committed the ban
userB is the user that got the ban
reason is an ban reason

This will allow me to check how many members and how fast they got banned by who.

the event is not onBan its guildBanAdd

I’d look here first or Google, then here, then the D.JS Discord if you can’t find the answer.(not to be mean)

Hey @JavaScript,

You need to use the guildBanAdd event of the Client object. It accepts two parameters, guild and user which you can use like this:

client.on("guildBanAdd", async function (guild, user) {
   // guild is the guild where the ban took place
   // if you need to send a message or anything, you can reference it from guild
   // the 'guild' object methods and properties: https://discord.js.org/#/docs/main/stable/class/Guild
   
   // user is the user object which is banned
   // user object: https://discord.js.org/#/docs/main/stable/class/User

   // following code will send a message in the #general channel
   const channel = guild.channels.cache.find(channel => channel.name === "general");
   channel.send(`${user} has been banned!`);
}); 

As for finding the reason why the user was banned, see https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=fetchBans.

You can find more events like guildBanAdd at https://discord.js.org/#/docs/main/stable/class/Client in the Events section.

Hope this helps!

2 Likes