How has this line changed from v13?

const testGuild = client.guilds.cache.get('1006932090102747238'); It is saying TypeError: Cannot read properties of undefined (reading 'guilds')
23 Replies
d.js docs
d.js docs3y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
Erin
ErinOP3y ago
14.2.0 & 16.15.0
node:events:505
throw er; // Unhandled 'error' event
^

TypeError: Cannot read properties of undefined (reading 'guilds')
at Object.execute (/root/sakura-moon/events/guildCreate.js:10:34)
at Client.<anonymous> (/root/sakura-moon/index.js:109:49)
at Client.emit (node:events:527:28)
at Object.module.exports [as GUILD_CREATE] (/root/sakura-moon/node_modules/discord.js/src/client/websocket/handlers/GUILD_CREATE.js:23:14)
at WebSocketManager.handlePacket (/root/sakura-moon/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
at WebSocketShard.onPacket (/root/sakura-moon/node_modules/discord.js/src/client/websocket/WebSocketShard.js:481:22)
at WebSocketShard.onMessage (/root/sakura-moon/node_modules/discord.js/src/client/websocket/WebSocketShard.js:321:10)
at WebSocket.onMessage (/root/sakura-moon/node_modules/ws/lib/event-target.js:199:18)
at WebSocket.emit (node:events:527:28)
at Receiver.receiverOnMessage (/root/sakura-moon/node_modules/ws/lib/websocket.js:1178:20)
node:events:505
throw er; // Unhandled 'error' event
^

TypeError: Cannot read properties of undefined (reading 'guilds')
at Object.execute (/root/sakura-moon/events/guildCreate.js:10:34)
at Client.<anonymous> (/root/sakura-moon/index.js:109:49)
at Client.emit (node:events:527:28)
at Object.module.exports [as GUILD_CREATE] (/root/sakura-moon/node_modules/discord.js/src/client/websocket/handlers/GUILD_CREATE.js:23:14)
at WebSocketManager.handlePacket (/root/sakura-moon/node_modules/discord.js/src/client/websocket/WebSocketManager.js:352:31)
at WebSocketShard.onPacket (/root/sakura-moon/node_modules/discord.js/src/client/websocket/WebSocketShard.js:481:22)
at WebSocketShard.onMessage (/root/sakura-moon/node_modules/discord.js/src/client/websocket/WebSocketShard.js:321:10)
at WebSocket.onMessage (/root/sakura-moon/node_modules/ws/lib/event-target.js:199:18)
at WebSocket.emit (node:events:527:28)
at Receiver.receiverOnMessage (/root/sakura-moon/node_modules/ws/lib/websocket.js:1178:20)
Mozzy
Mozzy3y ago
client is undefined (It has not changed)
Erin
ErinOP3y ago
Alright, any ideas why that is @mozzy ? now that you say that I do see another error on the console.log here:
module.exports = {
name: 'guildCreate',
async execute(guild, message, client) {
const testGuild = client.guilds.cache.get('1006932090102747238');


console.log('client?', client)
module.exports = {
name: 'guildCreate',
async execute(guild, message, client) {
const testGuild = client.guilds.cache.get('1006932090102747238');


console.log('client?', client)
Mozzy
Mozzy3y ago
guildCreate only emits guild. You will need to pass client another way. Or you can probably do guild.client
Erin
ErinOP3y ago
Doesn't work. So how do people get bots to send messages when they first get added to a guild?
Mozzy
Mozzy3y ago
guild.channels is a thing And some additional channel properties https://discord.js.org/#/docs/discord.js/stable/class/Guild
Discord.js
Discord.js is a powerful node.js module that allows you to interact with the Discord API very easily. It takes a much more object-oriented approach than most other JS Discord libraries, making your bot's code significantly tidier and easier to comprehend.
Mozzy
Mozzy3y ago
Where are you trying to send the message?
Erin
ErinOP3y ago
In a test server using the variable testGuild in a specific channel that only I can see.
Mozzy
Mozzy3y ago
And why can't you use guild.client.guilds.cache.get()?
Erin
ErinOP3y ago
This is why
ReferenceError: client is not defined
at Object.execute (/root/sakura-moon/events/guildCreate.js:11:32)
at Client.<anonymous> (/root/sakura-moon/index.js:109:49)
ReferenceError: client is not defined
at Object.execute (/root/sakura-moon/events/guildCreate.js:11:32)
at Client.<anonymous> (/root/sakura-moon/index.js:109:49)
Mozzy
Mozzy3y ago
Read my message again
Erin
ErinOP3y ago
line 11 is the last line here:
module.exports = {
name: 'guildCreate',
async execute(guild, message, ) {
const testGuild = guild.client.guilds.cache.get('1006932090102747238');
console.log('client?', client)
module.exports = {
name: 'guildCreate',
async execute(guild, message, ) {
const testGuild = guild.client.guilds.cache.get('1006932090102747238');
console.log('client?', client)
Don't I still need to get the ID that the channel is in?
Mozzy
Mozzy3y ago
Well, client isn't defined guild.client probably is
Erin
ErinOP3y ago
Okay, now it is saying channels isn't defined... entire command:
const config = require('../config/config.json');
const bot = require('../config/bot.json');
const me = require('../config/dev.json');
const Discord = require('discord.js');
const connection = require('../database.js')

module.exports = {
name: 'guildCreate',
async execute(guild, message, ) {
const testGuild = guild.client.guilds.cache.get('1006932090102747238');
console.log('client?', guild.client)

connection.query(`INSERT INTO guildConfig (guildId) VALUES(?);`, [guild.id]);

const added = {
name: 'I was just added to a new guild!',
thumbnail: {
url: guild.iconURL
},
addFields:[
{
name: 'Guild Name:',
value: guild.name
},
{
name: 'Guild ID:',
value: `\`${guild.id}\``
},
{
name: 'Owner\'s Info:',
value: guild.owner
}
],
timestamp: new Date(),
};

if(guild) {
channel = testGuild.channels.cache.get(`1006932090102747238`);
if(channel) setInterval(() => { channel.send({embeds: [added]})})
}
}
}
const config = require('../config/config.json');
const bot = require('../config/bot.json');
const me = require('../config/dev.json');
const Discord = require('discord.js');
const connection = require('../database.js')

module.exports = {
name: 'guildCreate',
async execute(guild, message, ) {
const testGuild = guild.client.guilds.cache.get('1006932090102747238');
console.log('client?', guild.client)

connection.query(`INSERT INTO guildConfig (guildId) VALUES(?);`, [guild.id]);

const added = {
name: 'I was just added to a new guild!',
thumbnail: {
url: guild.iconURL
},
addFields:[
{
name: 'Guild Name:',
value: guild.name
},
{
name: 'Guild ID:',
value: `\`${guild.id}\``
},
{
name: 'Owner\'s Info:',
value: guild.owner
}
],
timestamp: new Date(),
};

if(guild) {
channel = testGuild.channels.cache.get(`1006932090102747238`);
if(channel) setInterval(() => { channel.send({embeds: [added]})})
}
}
}
TypeError: Cannot read properties of undefined (reading 'channels')
at Object.execute (/root/sakura-moon/events/guildCreate.js:38:33)
at Client.<anonymous> (/root/sakura-moon/index.js:109:49)
TypeError: Cannot read properties of undefined (reading 'channels')
at Object.execute (/root/sakura-moon/events/guildCreate.js:38:33)
at Client.<anonymous> (/root/sakura-moon/index.js:109:49)
this line is causing it... channel = testGuild.channels.cache.get('1006932090102747238');
Dagan
Dagan3y ago
Valid guildId?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Erin
ErinOP3y ago
Alright will do. Ty!
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Erin
ErinOP3y ago
It doesn’t need it for time stamp
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Erin
ErinOP3y ago
Nope
Mozzy
Mozzy3y ago
But why use it?
Want results from more Discord servers?
Add your server