0

I was trying to make a ban command where you can ban a user with a reason.

Turns out user.ban is not a function in Discord.js V12 even though it should be.

Here is my code.

const { MessageEmbed } = require('discord.js');

module.exports = {
 name: 'ban',
 description: 'Bans a user.',
 category: 'Moderation',
 usage: '^ban <user> <reason>',
 run: async (bot, message, args) => {
  if (!message.member.hasPermission('BAN_MEMBERS')) {
   return message.channel.send('You do not have permission to do this! ❌');
  }

  if (!message.guild.me.hasPermission('BAN_MEMBERS')) {
   return message.channel.send('I do not have permission to do this! ❌');
  }

  const user = message.mentions.users.first();

  if (!user) {
   return message.channel.send('User was not specified. ❌');
  }

  if (user.id === message.author.id) {
   return message.channel.send('You cannot ban yourself! ❌');
  }

  let reason = message.content
   .split(' ')
   .slice(2)
   .join(' ');

  if (!reason) {
   reason = 'No reason provided.';
  }

  let Embed = new MessageEmbed()
   .setTitle(`Justice! | Ban Action`)
   .setDescription(`Banned \`${user}\` - Tag: \`${user.discriminator}\``)
   .setColor('ORANGE')
   .setThumbnail(user.avatarURL)
   .addField('Banned by', `\`${message.author.username}\``)
   .addField(`Reason?`, `\`${reason}\``)
   .setTimestamp();

  message.channel.send(Embed);

  user.ban(reason);
 },
};

Is there a way to fix this?

Lioness100
  • 8,260
  • 6
  • 18
  • 49
kodiak
  • 21
  • 1
  • 6
  • Does this answer your question? [What is the difference between a User and a GuildMember in discord.js?](https://stackoverflow.com/questions/63979076/what-is-the-difference-between-a-user-and-a-guildmember-in-discord-js) – Lioness100 Oct 11 '20 at 01:41

1 Answers1

1

You're getting a User instead of a GuildMember. A User represents a person on discord, while a GuildMember represents a member of a server. You can get a GuildMember instead of a User by using mentions.members instead of mentions.users ex:

    const user = message.mentions.members.first()
Evan Smith
  • 66
  • 3
  • I don't think that this is the problem, my ban command works fine with mentions.users – Apollo24 May 14 '20 at 15:32
  • In the current discord.js version (v12) `mentions.users` returns a collection of 'User', which don't have a `ban` method. See [the docs](https://discord.js.org/#/docs/main/stable/class/MessageMentions?scrollTo=users). – Evan Smith May 14 '20 at 15:42
  • I should have v12, if I'm not mistaken. Is there a way I can check this? – Apollo24 May 14 '20 at 15:58
  • @Apollo24 You can run `npm list --depth=0` in your bot's directory and it should print out all the packages you have with their versions listed. – Evan Smith May 14 '20 at 16:06
  • discord.js wasn't among them – Apollo24 May 14 '20 at 18:30
  • @Apollo24 If discord.js doesn't show up either you're not in the directory you have discord.js installed to, or it's a dependency for some reason. You can always navigate to /node_modules/discord.js/ and check the package.json file for the `_id` property, it'll say `discord.js@(version here)`. – Evan Smith May 14 '20 at 18:53
  • @Apollo24 Evan is correct, you can not ban `User`s you can only ban `GuildMember`s – Syntle May 14 '20 at 18:57
  • Yes I indeed do have Version 12.2.0. That's the part in my code we're talking about: `const bUser = message.guild.member(message.mentions.users.first());` – Apollo24 May 14 '20 at 19:13
  • @Apollo24 Your code is unnecessarily long. You're converting a `User` to a `GuildMember`, when you could just be getting a `GuildMember`. Ex: `const bUser = message.mentions.members.first();`. – Evan Smith May 14 '20 at 19:50