'How do I unban user with commands?

I am trying to make a command that unbans someone.

I have tried member.unban, message.guild.unban and the current one, message.guild.members.unban.

I am not quite sure what to do.

Here is my current code:

    const Discord = require('discord.js');

    module.exports = {
        name: "unban",
        description: "unbans a member from the server",
        
    
        async run (client, message, args) {
    
            if(!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send('You can\'t use that!')
            if(!message.guild.me.hasPermission("BAN_MEMBERS")) return message.channel.send('I don\'t have the permissions.')
    
            const member = message.mentions.members.first();
    
            if(!args[0]) return message.channel.send('Please specify a user');
    
    
    
            
    
            let reason = args.slice(1).join(" ");
    
            if(!reason) reason = 'Unspecified';
    
            message.guild.members.unban(`${member}`, `${reason}`)
            .catch(err => {
                if(err) return message.channel.send('Something went wrong')
            })
    
            const banembed = new Discord.MessageEmbed()
            .setTitle('Member Unbanned')
            .addField('User Unbanned', member)
            .addField('Unbanned by', message.author)
            .addField('Reason', reason)
            .setFooter('Time Unbanned', client.user.displayAvatarURL())
            .setTimestamp()
    
            message.channel.send(banembed);
    
    
        }
    }

I don't get any error on my command prompt, but this is what shows up in discord: Image and the user remains unbanned.



Solution 1:[1]

You have to pass a UserResolvable to the .unban() method. Putting the member object inside a template string (${member}) will turn it into a string that mentions the user, and won't work.

The way you're doing it will also probably not work due to users not being mentionable as GuildMembers when they're banned from the server.

To go around that, you can change your command so it unbans people by their IDs instead (!unban 873478935468795467). Code would then look like this:

message.guild.members.unban(id)

(id being a string that contains the id)

I highly recommend that you check the docs out. They have great examples for these situations.

Solution 2:[2]

In discord.Is v13 you can use

Let target = args[1];
Let reason = args.slice(2).join(“ “);
If (!reason) reason = ‘N/A’;
Message.guild.bans.fetch().then(bans => {
    Bans.forEach(banned => {
        If (banned.user.username === target) {
            Message.guild.members.unban(banned.user.id, reason);
        }
    });
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pedro Fracassi
Solution 2 WilliamDragon10