Search code examples
discorddiscord.js

discord.js v13 command handler of interactions failed


So I am trying to add interaction to my discord bot who already has a command and event handler. I followed the discord.js guide and adding the commands worked just fine. But now I am trying to do the command handling part, but it gives the error:

TypeError: command.execute is not a function

my code in index.js:

console.clear()

const fs = require('fs')

const { Collection } = require('discord.js');

const Client = require("./Structures/Client.js");

const config = require("./Data/config.json");

const client = new Client();

client.commands = new Collection();
const commandFiles = fs.readdirSync('./src/iCommands').filter(file => file.endsWith('.js'));

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

client.on("interactionCreate", async interaction => {
    if(!interaction.isCommand()) return;

    const command = client.commands.get(interaction.commandName);

    if(!command) return;

    try {
        await command.execute(interaction);
    } catch (error) {
        console.log(error);
        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});

client.start(config.token);

My code in deploy-commands.js:

const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { clientId, guildId, token } = require('./Data/config.json');

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

for (const file of commandFiles) {
    const command = require(`./iCommands/${file}`);
    commands.push(command.data.toJSON());
}
    
const rest = new REST({ version: '9' }).setToken(token);

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
    .then(() => console.log('Succesfully registerd application commands.'))
    .catch(console.error);

and then for the commands itself, just the standard code given by the guide. I also tried it in a blank piece of code, and there it worked fine


Solution

  • You've made some typo in the Directory name

    const commandFiles = fs.readdirSync('./src/iCommands').filter(file => file.endsWith('.js'));
    
    for (const file of commandFiles) {
        const command = require(`./iCommands/${file}`);
        commands.push(command.data.toJSON());
    }
    

    The folder that readed is in ./src/iCommands. But the file that want to be accessed is in ./iCommands/

    It should be like this

    const commandFiles = fs.readdirSync('./src/iCommands').filter(file => file.endsWith('.js'));
    
    for (const file of commandFiles) {
        const command = require(`./src/iCommands/${file}`);
        commands.push(command.data.toJSON());
    }
    

    Also make sure you have write the code (for example ping.js) in the ./src/iCommands/ping.js file. Avoid to leave it blank to prevent any error.

    Your bot directory should be look more like this

    dir1

    also this for disambiguation

    dir2