Channel is not found ?

hello i have a probleme a little probleme i want to send a message in a channel when someone rank up but the channel is not found im sure 100% i put the right ID so why he cant find the channel ?
8 Replies
d.js toolkit
d.js toolkit16mo 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!
Azarak58
Azarak58OP16mo ago
const Level = require("./level");

const rankInfo = [
{ range: [1, 9], rank: "1145704810138566708", xpRequired: 111, channelId: "1146385311421710346" }, // Common
{ range: [10, 29], rank: "1145704879520751687", xpRequired: 1111, channelId: "1146385311421710346" }, // Uncommon
{ range: [30, 49], rank: "1145704961699741748", xpRequired: 11111, channelId: "1146385311421710346" }, // Rare
{ range: [50, 69], rank: "1145705017886654534", xpRequired: 27777, channelId: "1146385311421710346" }, // Epic
{ range: [70, 89], rank: "1145705048882552982", xpRequired: 55555, channelId: "1146385334893019250" }, // Legendary
{ range: [90, 99], rank: "1145705091697999925", xpRequired: 111111, channelId: "1146385334893019250" }, // Mythical
{ range: [100, 100], rank: "1145705144579788860", xpRequired: 0, channelId: "1146385334893019250" }, // Divine
];

...

const updateUserXPInDatabase = async (userId, message, client) => {

...

try {
console.log(`Fetching channel with ID ${newRank.channelId}`);
const channel = await client.channels.fetch(newRank.channelId);
if (channel && channel.type === "text") {
const congratsMessage = `Congratulations! You are now ${role.name} tier!`;
await channel.send(congratsMessage);
console.log(
`Congratulation message sent to channel ${channel.name}`
);
} else {
console.error(
`Channel with ID ${newRank.channelId} not found or not a text channel.`
);
}
} catch (error) {
console.error("Error fetching or sending message:", error);
}
}
} else {
console.error(`Role with ID ${newRank.rank} not found.`);
}
}
}

...

module.exports = {
updateUserXPInDatabase,
};
const Level = require("./level");

const rankInfo = [
{ range: [1, 9], rank: "1145704810138566708", xpRequired: 111, channelId: "1146385311421710346" }, // Common
{ range: [10, 29], rank: "1145704879520751687", xpRequired: 1111, channelId: "1146385311421710346" }, // Uncommon
{ range: [30, 49], rank: "1145704961699741748", xpRequired: 11111, channelId: "1146385311421710346" }, // Rare
{ range: [50, 69], rank: "1145705017886654534", xpRequired: 27777, channelId: "1146385311421710346" }, // Epic
{ range: [70, 89], rank: "1145705048882552982", xpRequired: 55555, channelId: "1146385334893019250" }, // Legendary
{ range: [90, 99], rank: "1145705091697999925", xpRequired: 111111, channelId: "1146385334893019250" }, // Mythical
{ range: [100, 100], rank: "1145705144579788860", xpRequired: 0, channelId: "1146385334893019250" }, // Divine
];

...

const updateUserXPInDatabase = async (userId, message, client) => {

...

try {
console.log(`Fetching channel with ID ${newRank.channelId}`);
const channel = await client.channels.fetch(newRank.channelId);
if (channel && channel.type === "text") {
const congratsMessage = `Congratulations! You are now ${role.name} tier!`;
await channel.send(congratsMessage);
console.log(
`Congratulation message sent to channel ${channel.name}`
);
} else {
console.error(
`Channel with ID ${newRank.channelId} not found or not a text channel.`
);
}
} catch (error) {
console.error("Error fetching or sending message:", error);
}
}
} else {
console.error(`Role with ID ${newRank.rank} not found.`);
}
}
}

...

module.exports = {
updateUserXPInDatabase,
};
my terminal i have this
Fetching channel with ID 1146385311421710346
Channel with ID 1146385311421710346 not found or not a text channel.
Fetching channel with ID 1146385311421710346
Channel with ID 1146385311421710346 not found or not a text channel.
any idea where i messed up ?
ShompiFlen
ShompiFlen16mo ago
first of all if you have the guilds intent you shouldnt really need to fetch channels (at lesst text channels) because they will be cached on ready are you doing this inside an event? is you bot logged in when you try to fetch this channel? oh okay I see the issue you are comparing the channel type to a string but it has to be a number now OR I think ("GuildText") could work too, not entirely sure about the last one so my suggestion is: Import ChannelType from discord.js and check channel.type === ChannelType.GuildText instead yea no im pretty sure strings dont work here anymore
Azarak58
Azarak58OP16mo ago
try {
console.log(`Fetching channel with ID ${newRank.channelId}`);
const channel = await client.channels.fetch(newRank.channelId);
if (channel && channel.type === ChannelTypes.GUILD_TEXT) {
const congratsMessage = `Congratulations! You are now ${role.name} tier!`;
await channel.send(congratsMessage);
console.log(
`Congratulation message sent to channel ${channel.name}`
);
} else {
console.error(
`Channel with ID ${newRank.channelId} not found or not a text channel.`
);
}
} catch (error) {
console.error("Error fetching or sending message:", error);
}
try {
console.log(`Fetching channel with ID ${newRank.channelId}`);
const channel = await client.channels.fetch(newRank.channelId);
if (channel && channel.type === ChannelTypes.GUILD_TEXT) {
const congratsMessage = `Congratulations! You are now ${role.name} tier!`;
await channel.send(congratsMessage);
console.log(
`Congratulation message sent to channel ${channel.name}`
);
} else {
console.error(
`Channel with ID ${newRank.channelId} not found or not a text channel.`
);
}
} catch (error) {
console.error("Error fetching or sending message:", error);
}
like this ?
Fetching channel with ID 1146385311421710346
Error fetching or sending message: TypeError: Cannot read properties of undefined (reading 'GUILD_TEXT')
at updateUserXPInDatabase (C:\Users\Moham\OneDrive\Bureau\Project\Project Lina\models\updateXp.js:61:62)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Fetching channel with ID 1146385311421710346
Error fetching or sending message: TypeError: Cannot read properties of undefined (reading 'GUILD_TEXT')
at updateUserXPInDatabase (C:\Users\Moham\OneDrive\Bureau\Project\Project Lina\models\updateXp.js:61:62)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
maybe i didnt put "guildtext" Sweats
ShompiFlen
ShompiFlen16mo ago
i did tell you exactly how it was, its pascal case not screaming case now GuildText all good, just try again oh and also the enum is ChannelType not ChannelTypes
Azarak58
Azarak58OP16mo ago
oh yeah you were right its working now thx you so much
ShompiFlen
ShompiFlen16mo ago
glad i could nekoThumsup
Azarak58
Azarak58OP16mo ago
4638chocolaclap
Want results from more Discord servers?
Add your server