client.channels.cache.get(channelId) returning undefined

Hello, I'm unsure why a specific message is returning undefined from a channel. Could use another set of eyes since I've been staring for so long.
const { Client, Events, GatewayIntentBits, Partials, ReactionManager, SlashCommandBuilder,} = require("discord.js");
const clientInstance = require("../../bot.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("verify")
.setDescription("assign and remove the verify role for the rules."),


async execute(interaction, client) {
const botMessage = await interaction.deferReply({
fetchReply: true,
});


const message = client.channels.cache
.get(1103869557849997403)
.messages.cache.get(1164272120994463744); //message id;
console.log(message);


if (!message) {
console.error('Message not found.');
return;
}
const { Client, Events, GatewayIntentBits, Partials, ReactionManager, SlashCommandBuilder,} = require("discord.js");
const clientInstance = require("../../bot.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("verify")
.setDescription("assign and remove the verify role for the rules."),


async execute(interaction, client) {
const botMessage = await interaction.deferReply({
fetchReply: true,
});


const message = client.channels.cache
.get(1103869557849997403)
.messages.cache.get(1164272120994463744); //message id;
console.log(message);


if (!message) {
console.error('Message not found.');
return;
}
25 Replies
d.js toolkit
d.js toolkit14mo 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!
-Carlos👑
-Carlos👑14mo ago
message ids are strings
Snapper
SnapperOP14mo ago
I'll give that a try, thank you!
-Carlos👑
-Carlos👑14mo ago
np
Snapper
SnapperOP14mo ago
Still coming up as undefined
-Carlos👑
-Carlos👑14mo ago
send your code again
Snapper
SnapperOP14mo ago
const { Client, Events, GatewayIntentBits, Partials, ReactionManager, SlashCommandBuilder,} = require("discord.js");
const clientInstance = require("../../bot.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("verify")
.setDescription("assign and remove the verify role for the rules."),


async execute(interaction, client) {
const botMessage = await interaction.deferReply({
fetchReply: true,
});


const message = client.channels.cache
.get(1103869557849997403)
.messages.cache.get(1164272120994463744); //message id;
console.log(message);


if (!message) {
console.error('Message not found.');

return;
}
const { Client, Events, GatewayIntentBits, Partials, ReactionManager, SlashCommandBuilder,} = require("discord.js");
const clientInstance = require("../../bot.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("verify")
.setDescription("assign and remove the verify role for the rules."),


async execute(interaction, client) {
const botMessage = await interaction.deferReply({
fetchReply: true,
});


const message = client.channels.cache
.get(1103869557849997403)
.messages.cache.get(1164272120994463744); //message id;
console.log(message);


if (!message) {
console.error('Message not found.');

return;
}
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOP14mo ago
oh sorry I copy pasted above yeah they're strings now
-Carlos👑
-Carlos👑14mo ago
can you send the updated code
Snapper
SnapperOP14mo ago
const { Client, Events, GatewayIntentBits, Partials, ReactionManager, SlashCommandBuilder,} = require("discord.js");
const clientInstance = require("../../bot.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("verify")
.setDescription("assign and remove the verify role for the rules."),


async execute(interaction, client) {
const botMessage = await interaction.deferReply({
fetchReply: true,
});


const message = client.channels.cache
.get("1103869557849997403")
.messages.cache.get("1164272120994463744"); //message id;
console.log(message);


if (!message) {
console.error('Message not found.');
return;
}
const { Client, Events, GatewayIntentBits, Partials, ReactionManager, SlashCommandBuilder,} = require("discord.js");
const clientInstance = require("../../bot.js");

module.exports = {
data: new SlashCommandBuilder()
.setName("verify")
.setDescription("assign and remove the verify role for the rules."),


async execute(interaction, client) {
const botMessage = await interaction.deferReply({
fetchReply: true,
});


const message = client.channels.cache
.get("1103869557849997403")
.messages.cache.get("1164272120994463744"); //message id;
console.log(message);


if (!message) {
console.error('Message not found.');
return;
}
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOP14mo ago
the variable for message is returning as undefined, so it's not able to find the message with the specific id
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
d.js docs
d.js docs14mo ago
method MessageManager#fetch() Fetches message(s) from a channel. (more...)
Snapper
SnapperOP14mo ago
I'll give that a try 🙂

module.exports = {
data: new SlashCommandBuilder()
.setName("verify")
.setDescription("assign and remove the verify role for the rules."),


async execute(interaction, client) {
const botMessage = await interaction.deferReply({
fetchReply: true,
});

const message = await client.channels.get('1103869557849997403').messages.fetch('1164272120994463744');
console.log(message);

module.exports = {
data: new SlashCommandBuilder()
.setName("verify")
.setDescription("assign and remove the verify role for the rules."),


async execute(interaction, client) {
const botMessage = await interaction.deferReply({
fetchReply: true,
});

const message = await client.channels.get('1103869557849997403').messages.fetch('1164272120994463744');
console.log(message);
am I using this right? I'm still getting undefined oh hang on this is a different error
TypeError: Cannot read properties of undefined (reading 'get')
at Object.execute (C:\Users\Owner\Desktop\BartleBot\src\commands\tools\verifyParse.js:16:36)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.execute (C:\Users\Owner\Desktop\BartleBot\src\events\client\interactionCreate.js:12:13)
TypeError: Cannot read properties of undefined (reading 'get')
at Object.execute (C:\Users\Owner\Desktop\BartleBot\src\commands\tools\verifyParse.js:16:36)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.execute (C:\Users\Owner\Desktop\BartleBot\src\events\client\interactionCreate.js:12:13)
Does this mean the client channel is reading as undefined?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOP14mo ago
okay so I was able to get that working using fetch. For some reason I'm having a similar issue using it later down the line as well trying to get the members.
const members = await interaction.client.members.fetch()
.then(console.log)
.catch(error);
const members = await interaction.client.members.fetch()
.then(console.log)
.catch(error);
Throws the error:
TypeError: Cannot read properties of undefined (reading 'fetch')
TypeError: Cannot read properties of undefined (reading 'fetch')
Snapper
SnapperOP14mo ago
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.
Snapper
SnapperOP14mo ago
I should also mention that this method is failing as well
const members = await interaction.client.members.cache.array();
const members = await interaction.client.members.cache.array();
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOP14mo ago
oh okay that actually makes a lot more sense. What's a good way to access the guild from the discord client? I would assume client.guild?
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Snapper
SnapperOP14mo ago
I appreciate you Jo! You were helpful when I was first learning JS and you're still amazing today. Thanks homie
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server