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:\Users\Jewel Wildmoon\Downloads\My Discord Bots\Music Bots\Bot\main.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.
The Discord.js Collection function isn't a method of the Client [Client object].
Instead it's exported by the actual module, instead you should import it from the Discord.js module.
You can read the Discord.js guide on it here.
An example of how this should be used can be seen below.
const { Client, Intents, Collection } = require('discord.js');
client.commands = new Collection();
In the future, you could read the docs on Discord.js or you could read the guide on it.
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();
Thank you everyone for your help with the constructor. I finally got the bot working (for now) after fixing additional errors with depreciated commands and incorrect characters. The new code I got to work is as follows.
For main.js:
const Discord = require('discord.js');
const { Client, Intents } = require('discord.js');
const client = new Discord.Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const prefix = '!';
const fs = require('fs');
client.commands = new Discord.Collection();
const commandsFile = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandsFile){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.once('ready', () => {
console.log('Bot is Online!');
});
client.on('messageCreate', messageCreate => {
if(!messageCreate.content.startsWith(prefix) || messageCreate.author.bot) return;
const args = messageCreate.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(messageCreate, args);
} else if(command === 'youtube'){
client.commands.get('youtube').execute(messageCreate, args);
}
});
client.login('TOKEN');
For ping.js:
module.exports = {
name: 'ping',
description: "This is a ping command",
execute(messageCreate, args){
messageCreate.channel.send('pong!');
}
For youtube.js:
module.exports = {
name: 'youtube',
description: "Sends a link to a YouTube channel",
execute(messageCreate, args){
messageCreate.channel.send('LINK');
}
}