Can't send message to specific channels

In my ClientReady event. I can send a message to specific channel
const notiChannel = client.channels.cache.get('1379862671217791069');

notiChannel.send({
embeds: [notiEmbed]
});
const notiChannel = client.channels.cache.get('1379862671217791069');

notiChannel.send({
embeds: [notiEmbed]
});
but it seems not to work with this slash command
const notiChannel = client.channels.cache.get('1379778222169784431');
const publicNotiChannel = client.channels.cache.get('1374718357953183804');

try {
const notificationEmbed = new EmbedBuilder()
// ...

if (notiChannel) {
await notiChannel.send({ embeds: [notificationEmbed] });
}

if (publicNotiChannel) {
const publicMessage = await publicNotiChannel.send({ embeds: [notificationEmbed] });
await publicMessage.react('👋🏻');
}
} catch (error) {
console.error('Error : '+ error.message);
}
const notiChannel = client.channels.cache.get('1379778222169784431');
const publicNotiChannel = client.channels.cache.get('1374718357953183804');

try {
const notificationEmbed = new EmbedBuilder()
// ...

if (notiChannel) {
await notiChannel.send({ embeds: [notificationEmbed] });
}

if (publicNotiChannel) {
const publicMessage = await publicNotiChannel.send({ embeds: [notificationEmbed] });
await publicMessage.react('👋🏻');
}
} catch (error) {
console.error('Error : '+ error.message);
}
it throws: Cannot read properties of undefined (reading 'channels')
11 Replies
d.js toolkit
d.js toolkit6mo 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
Mark
Mark6mo ago
show the full code of your ready event listener because client is apparently undefined according to your error
qwrts
qwrtsOP6mo ago
here
const { Events, EmbedBuilder } = require('discord.js');

module.exports = {
name: Events.ClientReady,
once: true,
execute(client) {
console.log(`✅ Logged in : ${client.user.tag} (${client.user.id})`);

const notiChannel = client.channels.cache.get('1379862671217791069');

const notiEmbed = new EmbedBuilder()
.setDescription("## ✅ **บอทเริ่มทำงานแล้ว**")
.setColor("#84cc16")
.setFooter({
text: "Arena Breakout Assistance#3107",
iconURL: "https://media.discordapp.net/attachments/1376182983433781349/1380066711893442610/favicon-512x512.png?ex=684286c8&is=68413548&hm=bac15f1ac366e6b384187b5499d7e6dbe974b2f29340e68c326cb34db2807d31&=&format=webp&quality=lossless&width=576&height=576",
})
.setTimestamp();

notiChannel.send({
embeds: [notiEmbed]
});
},
};
const { Events, EmbedBuilder } = require('discord.js');

module.exports = {
name: Events.ClientReady,
once: true,
execute(client) {
console.log(`✅ Logged in : ${client.user.tag} (${client.user.id})`);

const notiChannel = client.channels.cache.get('1379862671217791069');

const notiEmbed = new EmbedBuilder()
.setDescription("## ✅ **บอทเริ่มทำงานแล้ว**")
.setColor("#84cc16")
.setFooter({
text: "Arena Breakout Assistance#3107",
iconURL: "https://media.discordapp.net/attachments/1376182983433781349/1380066711893442610/favicon-512x512.png?ex=684286c8&is=68413548&hm=bac15f1ac366e6b384187b5499d7e6dbe974b2f29340e68c326cb34db2807d31&=&format=webp&quality=lossless&width=576&height=576",
})
.setTimestamp();

notiChannel.send({
embeds: [notiEmbed]
});
},
};
Mark
Mark6mo ago
and how do you register your event listeners? what's the handler?
qwrts
qwrtsOP6mo ago
I'm using the one that on the document
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);

if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);

if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
Mark
Mark6mo ago
console.log(`:white_check_mark: Logged in : ${client.user.tag} (${client.user.id})`);
console.log(`:white_check_mark: Logged in : ${client.user.tag} (${client.user.id})`);
does that line show up in your console?
qwrts
qwrtsOP6mo ago
yes it does
Mark
Mark6mo ago
post the full error you receive including stack
qwrts
qwrtsOP6mo ago
Kick error: TypeError: Cannot read properties of undefined (reading 'channels')
at Object.execute (d:\Works\Discord Bots\abith\src\commands\moderation\kick.js:143:52)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async Object.execute (d:\Works\Discord Bots\abith\src\events\interactionCreate.js:16:13)
Kick error: TypeError: Cannot read properties of undefined (reading 'channels')
at Object.execute (d:\Works\Discord Bots\abith\src\commands\moderation\kick.js:143:52)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async Object.execute (d:\Works\Discord Bots\abith\src\events\interactionCreate.js:16:13)
Mark
Mark6mo ago
at Object.execute (d:\Works\Discord Bots\abith\src\commands\moderation\kick.js:143:52)
not anywhere close to your ready listener. please pay attention to your errors, they have a lot of useful information
qwrts
qwrtsOP6mo ago
thank you. I found the solution now. I just have to add interaction. before client

Did you find this page helpful?