Ad

How To Add "reason" Thing To Ban Command

- 1 answer

I am programming a discord bot in node.js (not master) and I an working on a kick and ban command. I'm trying to make the BOT write the ban log of the user. like +ban @user reason. I did +ban @user but i can't make reason thing.

  if (!message.member.hasPermission("BAN_MEMBERS")) return;

  if (message.content.startsWith('+ban')) {
    const user = message.mentions.users.first();
    if (user) {
      const member = message.guild.member(user);
      if (member) {
        member.ban({
          reason: 'reason',
        }).then(() => {
          message.channel.send(`${user.tag} BAN!`);
        }).catch(err => {
          message.channel.send('Bu çar çok güçlü, banlayamıyorum! ');
          console.error(err);
        });
      } else {
        message.channel.send('Kullanıcı sunucuda değil.');
      }
    } else {
      message.channel.send('Adını ver banlayayım, sahip.');
    }
  }
});```
Ad

Answer

Simply use member.ban('reason here'). Use an object if you need to delete previous messages and supply a reason, like so:

member.ban({days: 2, reason: 'bad'});

Now, just use this setup with the user's reason. Use a variable for the reason as a sliced version of the arguments array, joined with spaces.

Edit: Showing context...

if (message.content.toLowerCase().startsWith('+ban')) { // changed to case insensitive command
  const member = message.mentions.members.first(); // keep in mind it isn't the best practice to use message.mentions to retrieve an argument
  if (!member) return message.channel.send('no member mentioned');
  let reason = args.slice(2).join(' '); // arguments should already be defined
  member.ban(reason)
  .then(message.channel.send('success'))
  .catch(err => {
    message.channel.send('something went wrong');
    console.error();
  });
}
Ad
source: stackoverflow.com
Ad