I was working on my new discord bot and using discordjs.guide to set up my command manager and when I run the bot I get an error of
client.commands = new Discord.Collection();
^
TypeError: Cannot set property 'commands' of undefined
Everything is defined,
const { Client, MessageEmbed, Activity, RoleManager, Message, member, GuildChannel, Guild,} = require('discord.js');
const botinfo = require("./package.json");
const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const {client, bot} = new Discord.Client();
client.commands = new Discord.Collection();
So I have no idea whatâs wrong, or if itâs me or the code
Thanks for any help you can give - Gatorz
Not sure about this syntax, never seen variables declared like that in JS
Please could you try just:
const client = new Discord.Client();
Its like that because my old code uses bot, backwards compaaiblty
Just a quick reply to confirm what SteGriff has posted. This isnât a Glitch or Discord bot thing but rather pure JavaScript. If the error tells you a reference is undefined then (by definition) it is not defined. We can think so or try to insist but the JS interpreter pretty much knows.
Iâm not arguing with you but there is no âbackwards compatibilityâ excuse if the syntax is wrong. If you write it correctly it runs, if you donât it doesnât.
Just for example, if you open dev tools and try
const {x, y} = new Date()
Then try the values of x and y, theyâll both be undefined:
x
undefined
y
undefined
Iâd recommend that since the whole codebase is yours, and you can do whatever you want to it, just find+replace âbotâ with âclientâ
Or another approach: if you want bot and client to be pointers to the same client, use
const client = new Discord.Client();
const bot = client;
âŚand if you want them to be separate instances:
const client = new Discord.Client();
const bot = new Discord.Client();
Please try it and I think youâll see that it fixes your error.
Itâs the destructuring pattern.
function openLunchbox() {
return {
sandwich: "peanut butter",
fruit: [
"banana",
"banana",
"pear"
],
drink: "iced coffee"
};
}
const { drink } = openLunchbox();
Itâs likely that the OP is misusing it in this case.
1 Like
Yes, in your example, openLunchbox returns something which can be destructured.
Does the new Client()
call of discord.js return an object with two members, bot
and client
, which are available to assign into the target object?
Iâm guessing it is a mistake, but I understand where the mistake came from.
Thanks for the instructive example though
system
Closed
November 24, 2021, 8:52am
8
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.