How do I check if a user is banned or not in DiscordJS V14?

Advertisements

So I want to fetch banned members in DiscordJS V14, but the old method doesn’t work.

My code:

const { EmbedBuilder } = require("discord.js");
const { colors } = require("discordjs-colors-bundle");

module.exports = {
  config: {
    name: "unban",
    description: "Unban a member from the guild",
    usage: "unban [id]",
  },
  permissions: ["BanMembers"],
  owner: false,
  run: async (client, message, args, prefix, config, db) => {
    const userId = args.join(" ");

    if (!userId) {
      return message.reply({ content: `:x: | Please provide a ID to ban! ` });
    }

    const banList = await message.guild.fetchBans();
    const bannedUser = banList.find((user) => user.id === userId);

    const UnbannedEmbed = new EmbedBuilder()
      .setColor(colors.SeaBlue)
      .setTitle(`Unbanned`)
      .setDescription(`The user is unbanned!`)
      .setTimestamp()
      .setFooter({ text: `${message.guild.name}` });

    if (bannedUser) {
      message.guild.members.unban(userId);
      message.reply({ embeds: [UnbannedEmbed] });
    } else {
      message.reply({ content: `:x: | The mentioned user is not banned!` });
    }
  },
};

The error I get:

TypeError: message.guild.fetchBans is not a function Promise { <rejected> TypeError: message.guild.fetchBans is not a function

I was expecting that if the user is banned then it’ll unban the user, else it’ll return an error message to the Discord user

>Solution :

Check the docs.

The function you can use to fetch the bans is:

const banList = await message.guild.bans.fetch();

Leave a ReplyCancel reply