Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

187
Visualizações
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);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda