Sythan
Sythan
DIAdiscord.js - Imagine an app
Created by Sythan on 3/11/2024 in #djs-questions
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. 😵‍💫
7 replies