Search code examples
javascriptnode.jsdiscorddiscord.js

The ban command doesn't do what it's supposed to do


When I try to run my command, instead of the errors I specified, I get the errors from the console and the result is: Failed to ban the user and the console says DiscordAPIError: Interaction has already been acknowledged. and/or missing permissions. But if I try to ban myself with an account that has no rights, I get banned myself. So if I do tryfyfu /ban user: @tryfyfu with my acc (the account without rights) I can still ban myself but the bot should actually say: "❌ | You can't ban a user from the guild!" answer and so it is with the other functions like invalid memberor ❌ | You can't ban yourself and so on Here's the code. Would be nice if someone could help me.

const {
    Client,
    CommandInteraction,
    Message,
    MessageEmbed,
    MessageActionRow,
    MessageButton
} = require('discord.js');

module.exports = {
    name: 'ban',
    description: 'Ban a user',
    options: [
        {
            name: "user",
            description: "Choose the user to ban.",
            type: "USER",
            required: true
        },
        {
            name: "reason",
            description: "reason for punishment",
            type: "STRING",
            required: false
        }
    ],
    run: async (client, interaction, options) => {
        const member = interaction.options.getMember("user")
        const reason = interaction.options.getString("reason") || "No reason given"


        if(!interaction.member.permissions.has("BAN_MEMBERS")) {
        const buttons = new MessageActionRow()
        .addComponents(new MessageButton()
        .setLabel("Permissions")
        .setEmoji("⚙️")
        .setStyle("LINK")
        .setURL("https://discord.com/developers/docs/topics/permissions"))

        const embed = new MessageEmbed()
        .setColor("RED")
        .setDescription("❌ | You can't ban a user from the guild!")

        interaction.reply({
            embeds: [embed],
            components: [buttons],
            ephemeral: true
        })
    }
     if(member === interaction.member) {
            const embed = new MessageEmbed()
            .setColor("RED")
            .setDescription("❌ | You can't ban yourself")

            interaction.reply({
                embeds: [embed]
            })
        }
     if (!member) {
        
        const embed = new MessageEmbed()
        .setColor("RED")
        .setDescription("❌ | Invalid Member")

        interaction.reply({
            embeds: [embed],
            ephemeral: true
        })
        }
    
        try {
            await interaction.guild.bans.create(member, {
                reason
            })
            const embed = new MessageEmbed()
            .setColor("GREEN")
            .setDescription(`✅ | ${member.user.tag} was banned for: ${reason}`)
            
            return interaction.reply({
                embeds: [embed]
            })
        }
        catch(err){
            if (err){
                console.error(err)
                const embed = new MessageEmbed()
                .setColor("RED")
                .setDescription(`❌ | Failed to ban ${member.user.tag}`)

                return interaction.reply({
                    embeds: [embed]
                })
            }
        }
}
}

Solution

  • Changed two things below (different method of banning and corrected one mistake). They are notated below.

    const {
        MessageEmbed,
        MessageActionRow,
        MessageButton
    } = require('discord.js');
    
    module.exports = {
        name: 'ban',
        description: 'Ban a user',
        options: [{
                name: "user",
                description: "Choose the user to ban.",
                type: "USER",
                required: true
            },
            {
                name: "reason",
                description: "reason for punishment",
                type: "STRING",
                required: false
            }
        ],
        run: async (client, interaction, options) => {
            const member = interaction.options.getUser("user")
            // .getMember is not a valid function as the type specified above is "USER"
            const reason = interaction.options.getString("reason") || "No reason given"
    
    
            if (!interaction.member.permissions.has("BAN_MEMBERS")) {
                const buttons = new MessageActionRow()
                    .addComponents(new MessageButton()
                        .setLabel("Permissions")
                        .setEmoji("⚙️")
                        .setStyle("LINK")
                        .setURL("https://discord.com/developers/docs/topics/permissions"))
    
                const embed = new MessageEmbed()
                    .setColor("RED")
                    .setDescription("❌ | You can't ban a user from the guild!")
    
                return interaction.reply({
                    embeds: [embed],
                    components: [buttons],
                    ephemeral: true
                })
            }
            if (member === interaction.member) {
                const embed = new MessageEmbed()
                    .setColor("RED")
                    .setDescription("❌ | You can't ban yourself")
    
                return interaction.reply({
                    embeds: [embed]
                })
            }
            if (!member) {
                const embed = new MessageEmbed()
                    .setColor("RED")
                    .setDescription("❌ | Invalid Member")
    
                return interaction.reply({
                    embeds: [embed],
                    ephemeral: true
                })
            }
    // changed up the part below
            try {
                const embed = new MessageEmbed()
                    .setColor("GREEN")
                    .setDescription(`✅ | ${member.user.tag} was banned for: ${reason}`)
    
                interaction.reply({
                    embeds: [embed]
                })
                return member.ban({
                    reason: reason
                })
            } catch (err) {
                if (err) {
                    console.error(err)
                    const embed = new MessageEmbed()
                        .setColor("RED")
                        .setDescription(`❌ | Failed to ban ${member.user.tag}`)
    
                    return interaction.reply({
                        embeds: [embed]
                    })
                }
            }
        }
    }