Eighteen
Eighteen

Reputation: 1

TypeError: Cannot read properties of undefined (reading 'Collection')

Today I was coding a small discord.js music bot, but then i got an error and I don't really know how to fix it. Anyone know a fix?

This is the error

and this is my code

const fs = require('fs');
const { discord, Client, Intents } = require('discord.js');

const myIntents = new Intents();
myIntents.add(Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES);

const client = new Client({ intents: myIntents });
const { Player } = require('discord-player');

client.player = new Player(client);
client.config = require('./config/bot');
client.emotes = client.config.emojis;
client.filters = client.config.filters;
client.commands = new discord.Collection();

fs.readdirSync('./commands').forEach(dirs => {
    const commands = fs.readdirSync(`./commands/${dirs}`).filter(files => files.endsWith('.js'));

    for (const file of commands) {
        const command = require(`./commands/${dirs}/${file}`);
        console.log(`Loading command ${file}`);
        client.commands.set(command.name.toLowerCase(), command);
    };
});

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

for (const file of events) {
    console.log(`Loading discord.js event ${file}`);
    const event = require(`./events/${file}`);
    client.on(file.split(".")[0], event.bind(null, client));
};

for (const file of player) {
    console.log(`Loading discord-player event ${file}`);
    const event = require(`./player/${file}`);
    client.player.on(file.split(".")[0], event.bind(null, client));
};

client.login(client.config.discord.token);

Upvotes: 0

Views: 1203

Answers (1)

Zero
Zero

Reputation: 2286

Error originiates from this particular line

 client.commands.set(command.name.toLowerCase(), command);

Why? because of this:

const { discord, Client, Intents } = require('discord.js');

So you are trying to destructure discord from discord.js which is not available to the module, as you can see below the discord.js module has the following functions and properties: components of discord.js

Now if you want to destructure it you have to get the Collection itself instead of discord and then further discord.Collection. At the very top of your file you are destructuring some components from the module discord.js so you very well need to destructure the Collection too! like so:


    const { Client, Intents, DiscordAPIError, Collection } = require('discord.js');     
    // then you may define it like so    
    client.commands = new Collection()    

   // then you may further use 
    client.commands.set(command.name.toLowerCase(), command);

This would be absolutely valid as you can see here: display

Upvotes: 1

Related Questions