FrostedPancake
FrostedPancake

Reputation: 31

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.

Upvotes: 2

Views: 16932

Answers (2)

WilliamDragon10
WilliamDragon10

Reputation: 44

In discord.Is v13 you can use

let target = args[1];
let reason = args.slice(2).join(“ “) || "N/A";

message.guild.bans.fetch().then(bans => {
    bans.forEach(banned => {
        if (banned.user.username === target) {
            message.guild.members.unban(banned.user.id, reason);
        }
    });
});

Upvotes: 0

Pedro Fracassi
Pedro Fracassi

Reputation: 3905

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.

Upvotes: 3

Related Questions