Issues with channel ID fetched from MySQL database.

I have been wracking my brain on this one. I'm trying to create a very command that shows me what channel it is assigned to. I have it connected to a MySQL database and the command to set the channel works perfectly. It inserts the correct ID into the same table as the server ID, but when I try to use the command to verify the channel, I keep getting errors. For example: in my database, it's showing me "1209590565503635470" for the channel ID. This is the correct one and matches when I right click and copy ID. However, I get this error in my console from node when I use the command:
Error fetching the channel with ID 1209590565503635500: DiscordAPIError[10003]: Unknown Channel
Error fetching the channel with ID 1209590565503635500: DiscordAPIError[10003]: Unknown Channel
For some reason, it's showing the wrong channel ID. I originally suspected a caching issue, but after going off to do another part of the same app, it still shows that. Bear with me, I've rewritten this a few million times now, so it may look a little funky now, but this is these are the commands to set the channel and showing the channel that was set:
case 'setchannel':
await interaction.deferReply();
const setChannel = interaction.options.getChannel('channel');
if (setChannel && (setChannel.type === ChannelType.GuildText || setChannel.type === ChannelType.GuildNews)) {
const dbConnection = await createDatabaseConnection();
await dbConnection.execute(
'REPLACE INTO channels (channel_id, server_id) VALUES (?, ?)',
[setChannel.id.toString(), interaction.guild.id.toString()] // Ensuring IDs are treated as strings
);
dbConnection.end();
await interaction.editReply(`The news channel has been successfully set to: ${setChannel.name}`);
} else {
await interaction.editReply('Invalid channel type. Please select a text or news channel.');
}
break;

case 'currentchannel':
await interaction.deferReply();
const dbConnectionCurrentChannel = await createDatabaseConnection();
const [channelRows] = await dbConnectionCurrentChannel.execute(
'SELECT channel_id FROM channels WHERE server_id = ? LIMIT 1',
[interaction.guild.id.toString()] // Ensuring server ID is treated as a string
);
dbConnectionCurrentChannel.end();

if (channelRows.length > 0 && channelRows[0].channel_id) {
try {
const fetchedChannel = await interaction.guild.channels.fetch(channelRows[0].channel_id.toString()); // Fetch using string ID
if (fetchedChannel) {
await interaction.editReply(`The current news channel is set to: ${fetchedChannel.toString()}`);
} else {
await interaction.editReply('The news channel previously set no longer exists or the bot lacks permission to view it.');
}
} catch (error) {
console.error(`Error fetching the channel with ID ${channelRows[0].channel_id}: ${error}`);
await interaction.editReply('There was an error while fetching the channel. It might not exist, or the bot may lack the necessary permissions.');
}
} else {
await interaction.editReply('No news channel has been set.');
}
break;
case 'setchannel':
await interaction.deferReply();
const setChannel = interaction.options.getChannel('channel');
if (setChannel && (setChannel.type === ChannelType.GuildText || setChannel.type === ChannelType.GuildNews)) {
const dbConnection = await createDatabaseConnection();
await dbConnection.execute(
'REPLACE INTO channels (channel_id, server_id) VALUES (?, ?)',
[setChannel.id.toString(), interaction.guild.id.toString()] // Ensuring IDs are treated as strings
);
dbConnection.end();
await interaction.editReply(`The news channel has been successfully set to: ${setChannel.name}`);
} else {
await interaction.editReply('Invalid channel type. Please select a text or news channel.');
}
break;

case 'currentchannel':
await interaction.deferReply();
const dbConnectionCurrentChannel = await createDatabaseConnection();
const [channelRows] = await dbConnectionCurrentChannel.execute(
'SELECT channel_id FROM channels WHERE server_id = ? LIMIT 1',
[interaction.guild.id.toString()] // Ensuring server ID is treated as a string
);
dbConnectionCurrentChannel.end();

if (channelRows.length > 0 && channelRows[0].channel_id) {
try {
const fetchedChannel = await interaction.guild.channels.fetch(channelRows[0].channel_id.toString()); // Fetch using string ID
if (fetchedChannel) {
await interaction.editReply(`The current news channel is set to: ${fetchedChannel.toString()}`);
} else {
await interaction.editReply('The news channel previously set no longer exists or the bot lacks permission to view it.');
}
} catch (error) {
console.error(`Error fetching the channel with ID ${channelRows[0].channel_id}: ${error}`);
await interaction.editReply('There was an error while fetching the channel. It might not exist, or the bot may lack the necessary permissions.');
}
} else {
await interaction.editReply('No news channel has been set.');
}
break;
Anybody have any insight to this? It's driving me crazy. 😵‍💫
6 Replies
d.js toolkit
d.js toolkit9mo 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
Sythan
SythanOP9mo ago
discord.js version - 14.14.1 node version 21.7.1
Amgelo
Amgelo9mo ago
make sure your model isn't saving ids as numbers just in case
Sythan
SythanOP9mo ago
I had switched them to strings, but I was still getting it
Kode
Kode9mo ago
They can be integers, save the channel I'd as BigInt. Ensure supportBigNumbers=true Console out the returned details from the DB ensure your calling it correctly.
Sythan
SythanOP9mo ago
Thank you. This actually helped and fixed my issue!
Want results from more Discord servers?
Add your server