Search code examples
discorddiscord.jsconstantsclient

"Collection is not defined" when trying to create a discord bot


I am new to discord.js and have been following a youtube tutorial (linked here: https://www.youtube.com/watch?v=22BFybCwSFU&list=PL-Ux9UcyEUaNxn_TbByomT-01UUKHOoD1&index=3&ab_channel=Exane) that uses an outdated version of discord.js (something that has given me some problems)

Their code:

const Discord = require('discord.js') 
const bot = new Discord.Client();
const fs = require("fs")
bot.commands = new Discord.Collection(); 

On the final line (line 5), when I try to input new Discord.Collection(); I am given the ReferenceError: Discord is not defined, and yet when I remove "Discord." from the line, I am given the ReferenceError: Collection is not defined.

I have changed my code according to comments from people also struggling when using a more updated version of discord.js and node.js, but so far have found no fix. My code will be put below this and I'm really sorry if it's a dumb/obvious fix, thank you so much for reading!

const { Client, Intents, DiscordAPIError, CommandInteractionOptionResolver } = require("discord.js");
const bot = new Client({ intents: ["DIRECT_MESSAGES", "GUILD_MESSAGES"] });
const fs = require("fs")
bot.Commands = new Collection();

( I think that I am supposed to define the first "const" variable with the Name "Discord" but when I try to do this I am greeted with the same errors )

edit: I forgot to mention before that before this I had to add (steal) code to the first line to allow me to add intents when creating a new client, as in older versions you previously didn't need to add intents here, yet my code would not function without it in discord.js version 13.3


Solution

  • YouTube video author you are telling us about is using Discord.Collection() because it's the way "you say" you want to create discord.js collection. If you don't want to use it and just use Collection(), you have to define it using const { Collection } = require('discord.js') first