Home > Back-end >  Sorry I'm new to coding so it is probably a simple error: `TypeError: Cannot read properties of
Sorry I'm new to coding so it is probably a simple error: `TypeError: Cannot read properties of

Time:11-01

This is the part of the main file that I think is causing the issue

client.commands = new 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.data.name, command);
}

const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
    const event = require(`./events/${file}`);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args));
    } else {
        client.on(event.name, (...args) => event.execute(...args));
    }
}

This is the code in the Interaction Create script where the error is coming from

        if (!interaction.isCommand()) return;

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

        if (!command) return;

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

Forgot to mention the error is on the line defining 'command' in the interaction create script

FULL ERROR:

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

TypeError: Cannot read properties of undefined (reading 'commands')
    at Object.execute (C:\Users\willi\OneDrive\Desktop\New Bot\events\interactionCreate.js:6:26)
    at Client.emit (node:events:390:28)
    at InteractionCreateAction.handle (C:\Users\willi\OneDrive\Desktop\New Bot\node_modules\discord.js\src\client\actions\InteractionCreate.js:70:12)
:4:36)
    at WebSocketManager.handlePacket (C:\Users\willi\OneDrive\Desktop\New Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:350:31)
    at WebSocketShard.onPacket (C:\Users\willi\OneDrive\Desktop\New Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22)
    at WebSocketShard.onMessage (C:\Users\willi\OneDrive\Desktop\New Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:10)
    at WebSocket.onMessage (C:\Users\willi\OneDrive\Desktop\New Bot\node_modules\ws\lib\event-target.js:199:18)
    at WebSocket.emit (node:events:390:28)

CodePudding user response:

Try changing

client.commands = new Collection();

to

client.commands = new Discord.Collection();

CodePudding user response:

You have not defined the client

This is how you should start writing your bot:

// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');

// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

// Login to Discord with your client's token
client.login("yourBotToken");

source

  • Related