Channel not found!

Dear everyone, i'm learning the basics and for learning purpose i wanted to create a fivem server status script. I got stucked at querying the channel id of the target "status" channel, it always says channel not found. All the intents, permission are set for the bot. code:
const { EmbedBuilder, Client, Intents } = require('discord.js');
const axios = require('axios');

const config = {
statusChannelId: mychannelid,
updateInterval: 60000 // 1 minute
};

async function getServerStatus() {
try {
const response = await axios.get(`myendpoint/players.json`);
const players = response.data.length;

return {
status: 'Online',
players: players
};
} catch (error) {
console.error('Error fetching server status:', error);
return {
status: 'Offline',
players: 0
};
}
}

async function updateStatusChannel(client) {
const status = await getServerStatus();
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('FiveM Server Status')
.addFields(
{ name: 'Status', value: status.status, inline: true },
{ name: 'Players', value: status.players.toString(), inline: true }
)
.setTimestamp();

try {
const channel = await client.channels.cache.get(config.statusChannelId);
if (channel) {
const messages = await channel.messages.fetch({ limit: 1 });
const lastMessage = messages.first();

if (lastMessage && lastMessage.embeds.length > 0) {
await lastMessage.edit({ embeds: [embed] });
} else {
await channel.send({ embeds: [embed] });
}
} else {
console.error('Channel not found!');
}
} catch (error) {
console.error('Error fetching channel:', error);
}
}

module.exports = async (client) => {
updateStatusChannel(client);
setInterval(() => updateStatusChannel(client), config.updateInterval);

client.on('messageCreate', async (message) => {
if (message.content === '!status') {
const status = await getServerStatus();

const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('FiveM Server Status')
.addFields(
{ name: 'Status', value: status.status, inline: true },
{ name: 'Players', value: status.players.toString(), inline: true }
)
.setTimestamp();

message.channel.send({ embeds: [embed] });
}
});
};
const { EmbedBuilder, Client, Intents } = require('discord.js');
const axios = require('axios');

const config = {
statusChannelId: mychannelid,
updateInterval: 60000 // 1 minute
};

async function getServerStatus() {
try {
const response = await axios.get(`myendpoint/players.json`);
const players = response.data.length;

return {
status: 'Online',
players: players
};
} catch (error) {
console.error('Error fetching server status:', error);
return {
status: 'Offline',
players: 0
};
}
}

async function updateStatusChannel(client) {
const status = await getServerStatus();
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('FiveM Server Status')
.addFields(
{ name: 'Status', value: status.status, inline: true },
{ name: 'Players', value: status.players.toString(), inline: true }
)
.setTimestamp();

try {
const channel = await client.channels.cache.get(config.statusChannelId);
if (channel) {
const messages = await channel.messages.fetch({ limit: 1 });
const lastMessage = messages.first();

if (lastMessage && lastMessage.embeds.length > 0) {
await lastMessage.edit({ embeds: [embed] });
} else {
await channel.send({ embeds: [embed] });
}
} else {
console.error('Channel not found!');
}
} catch (error) {
console.error('Error fetching channel:', error);
}
}

module.exports = async (client) => {
updateStatusChannel(client);
setInterval(() => updateStatusChannel(client), config.updateInterval);

client.on('messageCreate', async (message) => {
if (message.content === '!status') {
const status = await getServerStatus();

const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('FiveM Server Status')
.addFields(
{ name: 'Status', value: status.status, inline: true },
{ name: 'Players', value: status.players.toString(), inline: true }
)
.setTimestamp();

message.channel.send({ embeds: [embed] });
}
});
};
What could be the problem? Thank you for your help in advance. Have a great day!
16 Replies
d.js toolkit
d.js toolkit5mo 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
sigma
sigmaOP5mo ago
Discord JS version: [email protected] Node: v20.12.1
treble/luna
treble/luna5mo ago
How is config.statusChannelId defined And show your client constructor
sigma
sigmaOP5mo ago
const config = {
statusChannelId: mychannelid,
updateInterval: 60000 // 1 minute
};
const config = {
statusChannelId: mychannelid,
updateInterval: 60000 // 1 minute
};
client:
const client = new Discord.Client({
allowedMentions: {
parse: [
'users',
'roles'
],
repliedUser: true
},
autoReconnect: true,
disabledEvents: [
"TYPING_START"
],
partials: [
Discord.Partials.Channel,
Discord.Partials.GuildMember,
Discord.Partials.Message,
Discord.Partials.Reaction,
Discord.Partials.User,
Discord.Partials.GuildScheduledEvent
],
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.GuildBans,
Discord.GatewayIntentBits.GuildEmojisAndStickers,
Discord.GatewayIntentBits.GuildIntegrations,
Discord.GatewayIntentBits.GuildWebhooks,
Discord.GatewayIntentBits.GuildInvites,
Discord.GatewayIntentBits.GuildVoiceStates,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.GuildMessageReactions,
Discord.GatewayIntentBits.GuildMessageTyping,
Discord.GatewayIntentBits.DirectMessages,
Discord.GatewayIntentBits.DirectMessageReactions,
Discord.GatewayIntentBits.DirectMessageTyping,
Discord.GatewayIntentBits.GuildScheduledEvents,
Discord.GatewayIntentBits.MessageContent
],
restTimeOffset: 0
});
const client = new Discord.Client({
allowedMentions: {
parse: [
'users',
'roles'
],
repliedUser: true
},
autoReconnect: true,
disabledEvents: [
"TYPING_START"
],
partials: [
Discord.Partials.Channel,
Discord.Partials.GuildMember,
Discord.Partials.Message,
Discord.Partials.Reaction,
Discord.Partials.User,
Discord.Partials.GuildScheduledEvent
],
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.GuildBans,
Discord.GatewayIntentBits.GuildEmojisAndStickers,
Discord.GatewayIntentBits.GuildIntegrations,
Discord.GatewayIntentBits.GuildWebhooks,
Discord.GatewayIntentBits.GuildInvites,
Discord.GatewayIntentBits.GuildVoiceStates,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.GuildMessageReactions,
Discord.GatewayIntentBits.GuildMessageTyping,
Discord.GatewayIntentBits.DirectMessages,
Discord.GatewayIntentBits.DirectMessageReactions,
Discord.GatewayIntentBits.DirectMessageTyping,
Discord.GatewayIntentBits.GuildScheduledEvents,
Discord.GatewayIntentBits.MessageContent
],
restTimeOffset: 0
});
treble/luna
treble/luna5mo ago
this still doesnt tell me how the id actually is defined and do you really need all those intents
d.js docs
d.js docs5mo ago
We highly recommend only specifying the intents you actually need. - Note, that 98303, 32767 or whatever other magic number you read that represents "all intents", gets outdated as soon as new intents are introduced. - The number will always represent the same set of intents, and will not include new ones. There is no magic "all intents" bit.
sigma
sigmaOP5mo ago
what do you mean? its defined in config {}. for this small script, not really. but they are needed for other purpose
treble/luna
treble/luna5mo ago
ok how is mychannelid defined then I highly doubt one would ever need DirectMessageTyping
sigma
sigmaOP5mo ago
I've just replace the integer with mychannelid. it should be 1267435664815689808 of course.
treble/luna
treble/luna5mo ago
Not to mention this causes unnecessary resource usage They should not be integers They should be strings
sigma
sigmaOP5mo ago
i have tried with string but the result was the same. now i'm giving the channelid in string, but still "channel not found". is it possible to query all the channels my bot see? 😄
treble/luna
treble/luna5mo ago
Log the id right before you get from cache thats what client.channels.cache is
sigma
sigmaOP5mo ago
i understand 😄 Channel ID (debug): 1267435664815689808 Channel not found! for some reason, the channel is not in the cache or the bot can't see it at all. what do you think would be the best solution? I'll try to resolve it somehow.
treble/luna
treble/luna5mo ago
Where do you call that function
sigma
sigmaOP5mo ago
the bot runs this when it starts up, and then every minute using setInterval, all within the same file. however, it reports that it can't find the channel on the initial start, but it works during the minute-by-minute updates, and the data in the channel is refreshed correctly. I assume that after the bot has loaded, it caches the channels and then I can manage them?
treble/luna
treble/luna5mo ago
No The client just isnt ready the first time Call it in your ready event
Want results from more Discord servers?
Add your server