“TypeError: Client.Collection is not a Constructor” error with Discord.js

This is my first time using Discord.js as I’m trying to create a personal music bot for my friend’s server. First, I’m going through a tutorial by CodeLyon but I’ve encountered an error and I’m not sure how to fix it since the video was released before the Gateway Intentions were added to Discord.js

The error I’m currently getting states:

TypeError: Client.Collection is not a constructor at Object.< anonymous> (C:UsersJewel WildmoonDownloadsMy Discord BotsMusic BotsBotmain.js:9:19) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) at node:internal/main/run_main_module:17:47

This is my code so far in my main.js file:

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

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

const prefix = '!';

const fs = require('fs');

client.commands = new Client.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);
}

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

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

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

    if(command === 'ping'){
        client.commands.get('ping').execute(message, args);
    } else if(command == 'youtube'){
        message.channel.send('LINK');
    }
});


client.login('TOKEN');

And this is my current code in the ping.js file:

module.exports = {
    name: 'ping',
    description: "This is a ping command",
    execute(message, args){
        message.channel.send('pong!');
    }
}

Not quite sure what to do here as again, I haven’t use Discord.js and am not familiar with Gateway Intentions. That and I’m still new to JavaScript.

Answer

Collection is exported by the library, its not a client property. You need to deconstruct it at the top along with Client and Intents.

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


client.commands = new Collection();