"Cannot read properties of undefined (reading 'guilds')

Hi there. I'm pretty new to discord.js and JavaScript (I'm working on Codecademy's course as I do this) and I'm trying to create a multipurpose bot for a community I own. So far, I've followed the guide to a tee with one exception. The code below is from my ready.js file. Once the bot is ready, I want it to change its activity to say "Watching over XX SRP members" where XX is the number of members in the guild. The issue is that I keep getting errors about things being undefined. Currently, I'm getting an error about 'guilds' being undefined. I have a feeling I'm doing this all wrong and am probably in way over my head. My guess is that this is a very simple issue to solve and due to my lack of knowledge and experience, I'm having a hard time figuring this stuff out. Thank you to anyone who is able to help.
const { Events, ActivityType, client } = require('discord.js');
const Guild = client.guilds.cache.get('1215384838526472212');

module.exports = {
name: Events.ClientReady,
once: true,
execute(client) {
client.user.setActivity({
name: `over ${Guild.memberCount} SRP members`,
type: ActivityType.Watching
})
console.log(`Ready! Logged in as ${client.user.tag}`);
},
};
const { Events, ActivityType, client } = require('discord.js');
const Guild = client.guilds.cache.get('1215384838526472212');

module.exports = {
name: Events.ClientReady,
once: true,
execute(client) {
client.user.setActivity({
name: `over ${Guild.memberCount} SRP members`,
type: ActivityType.Watching
})
console.log(`Ready! Logged in as ${client.user.tag}`);
},
};
/Users/XXXXX/Documents/SRP Helper/events/ready.js:2 const Guild = client.guilds.cache.get('1215384838526472212'); ^ TypeError: Cannot read properties of undefined (reading 'guilds') at Object.<anonymous> (/Users/XXXXX/Documents/SRP Helper/events/ready.js:2:22) at Module._compile (node:internal/modules/cjs/loader:1358:14) at Module._extensions..js (node:internal/modules/cjs/loader:1416:10) at Module.load (node:internal/modules/cjs/loader:1208:32) at Module._load (node:internal/modules/cjs/loader:1024:12) at Module.require (node:internal/modules/cjs/loader:1233:19) at require (node:internal/modules/helpers:179:18) at Object.<anonymous> (/Users/XXXXX/Documents/SRP Helper/index.js:31:16) at Module._compile (node:internal/modules/cjs/loader:1358:14) at Module._extensions..js (node:internal/modules/cjs/loader:1416:10) Node.js v20.13.1
5 Replies
d.js toolkit
d.js toolkit7mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
monbrey
monbrey7mo ago
You can't import your instance of a Client from Discord.js Use the client inside the ready event, dont do that outside it
The Specialist
The SpecialistOP7mo ago
Ok I removed that part. So now it's giving me this error: /Users/XXXXX/Documents/SRP Helper/events/ready.js:2 const Guild = client.guilds.cache.get('1215384838526472212'); ^ ReferenceError: client is not defined at Object.<anonymous> (/Users/XXXXX/Documents/SRP Helper/events/ready.js:2:15) at Module._compile (node:internal/modules/cjs/loader:1358:14) at Module._extensions..js (node:internal/modules/cjs/loader:1416:10) at Module.load (node:internal/modules/cjs/loader:1208:32) at Module._load (node:internal/modules/cjs/loader:1024:12) at Module.require (node:internal/modules/cjs/loader:1233:19) at require (node:internal/modules/helpers:179:18) at Object.<anonymous> (/Users/XXXXX/Documents/SRP Helper/index.js:31:16) at Module._compile (node:internal/modules/cjs/loader:1358:14) at Module._extensions..js (node:internal/modules/cjs/loader:1416:10) Node.js v20.13.1
monbrey
monbrey7mo ago
thats still outside your ready event, you didnt do anything
The Specialist
The SpecialistOP7mo ago
Ah I see what I did now. That makes complete sense. Thank you for putting up with my nonsense. 😂
Want results from more Discord servers?
Add your server