Home > Back-end >  Discord.js (V12) | Ban Command
Discord.js (V12) | Ban Command

Time:06-18

I am making a discord bot and I want to make a ban command. When I try to use the command though this error pops up in my terminal: TypeError: client.commands.get(...).execute is not a function at Client.<anonymous> (C:\Users\Lucas\Desktop\Discord BOT\index.js:29:32) What did I do wrong? I have tried multiple solutions I found on StackOverflow or v12.discordjs.guide But nothing I tried worked. Thanks for helping out!

This is the index.js file:

const fs = require('fs');
const Discord = require('Discord.js');
const { prefix, token } = require('./config.json');

const client = new Discord.Client();
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command, Discord);
}

client.once('ready', () => {
    console.log('Ready!');
});

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/  /);
    const command = args.shift().toLowerCase();


    if (!client.commands.has(command)) return;

    try {
        client.commands.get(command).execute(message, args, Discord);
    } catch (error) {
        console.error(error);
        message.reply('there was an error trying to execute that command!');
    }

    if (command.permissions) {
        const authorPerms = message.channel.permissionsFor(message.author);
        if (!authorPerms || !authorPerms.has(command.permissions)) {
            return message.reply('You can not do this!');
        }
    }

});

client.login(token);

This is the ban command file (ban.js):

const Discord = require("Discord.js");

module.exports = {
    name: 'ban',
    description: 'Banish someone',
    permissions: 'ADMINISTRATOR',
    usage: 'ban @user <reason>',
    

    run: async (message, client, args) => {

        if (!target) {
            return message.channel.send(`${message.author.username} USAGE: ban @user reason`)
        }

        if(target.id === message.author.id) {
            return message.channel.send(`${message.author.username}, You can't ban yourself. YOU FOOL!`)
        }

        if(!args[1]) {
            return message.channel.send(`${message.author.username}, Please give a reason to ban that member`)
        }

        let embed = new Discord.MessageEmbed()
        .setTitle('Action: BAN')
        .setDescription(`Banned ${target} (${target.id})`)
        .setColor("0ff")
        .setFooter(`Banned by ${message.author.tag}`);

        message.channel.send(embed)
        target.ban(args[1])

    }
}

CodePudding user response:

In your index.js file, you run your commands by using client.commands.get(command).execute(message, args, Discord);

however, in your ban.js file, you have a run keyword not execute, try using that instead

execute: async (message, client, args) => {

  • Related