Issue with Channel Filtering and Counting Logic

Hi everyone,
I'm facing an issue with my Discord.js code where I'm trying to implement a system that counts user messages only in specific allowed channels. However, even when the channel is not in the list of allowedChannels, it still increments the count for the user.
Here's the relevant code snippet:
client.on('messageCreate', async (message) => {
try {
if (message.author.bot) return;

const guildId = message.guild.id;

const allowedChannels = (await db.get(`allowedChannels_${guildId}`)) || [];

if (allowedChannels.includes(message.channel.id)) {
const userId = message.author.id;

const messageCountKey = `messageCount_${guildId}_${userId}`;
let messageCount = (await db.get(messageCountKey)) || 0;
messageCount = parseInt(messageCount, 10);

await db.set(messageCountKey, messageCount + 1);

if ((messageCount + 1) % goalMessages === 0) {
const pointsKey = `points_${guildId}_${userId}`;
let points = (await db.get(pointsKey)) || 0;
points = parseInt(points, 10);

await db.set(pointsKey, points + 1);
}
} else {
console.log(`Channel ${message.channel.id} is not allowed for counting.`);
}
} catch (error) {
console.error('err:', error);
}
});
client.on('messageCreate', async (message) => {
try {
if (message.author.bot) return;

const guildId = message.guild.id;

const allowedChannels = (await db.get(`allowedChannels_${guildId}`)) || [];

if (allowedChannels.includes(message.channel.id)) {
const userId = message.author.id;

const messageCountKey = `messageCount_${guildId}_${userId}`;
let messageCount = (await db.get(messageCountKey)) || 0;
messageCount = parseInt(messageCount, 10);

await db.set(messageCountKey, messageCount + 1);

if ((messageCount + 1) % goalMessages === 0) {
const pointsKey = `points_${guildId}_${userId}`;
let points = (await db.get(pointsKey)) || 0;
points = parseInt(points, 10);

await db.set(pointsKey, points + 1);
}
} else {
console.log(`Channel ${message.channel.id} is not allowed for counting.`);
}
} catch (error) {
console.error('err:', error);
}
});
The problem:
Even when the message comes from a channel that isn't in the allowedChannels list, the code still increments the count for the user.
Could someone help me figure out what might be going wrong?
Thanks in advance! 😊
27 Replies
d.js toolkit
d.js toolkit2mo 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
Retro
Retro2mo ago
can you console.log allowedChannels
فيصل
فيصلOP2mo ago
Allowed Channels: [ '1320870580039450686' ] Message Channel ID: 1321080806419988531 Skipping channel: 1321080806419988531 (no skipping)
Syjalo
Syjalo2mo ago
Are you sure the code is executed? Try to add a console log inside the condition
فيصل
فيصلOP2mo ago
No description
Retro
Retro2mo ago
then it worked? it skipped
فيصل
فيصلOP2mo ago
no
Retro
Retro2mo ago
unless you have another counting somewhere else
NyR
NyR2mo ago
Issue is ! The code will execute if it doesn't include the channel
فيصل
فيصلOP2mo ago
this channel not allowed
No description
Retro
Retro2mo ago
i dont think so? he wants to skip it if its not in the list
NyR
NyR2mo ago
Oh, right. Well got confused from the first code
Syjalo
Syjalo2mo ago
Do you see the Processing channel log?
Retro
Retro2mo ago
well do you have any other pieces of code that might be running outside of that try ... catch?
فيصل
فيصلOP2mo ago
i receive a lot of messages, but I'm sure it says they've been skipped no
Syjalo
Syjalo2mo ago
So it definitely not that part of the code increments the counter
فيصل
فيصلOP2mo ago
There are no other parts to increase the counter
Syjalo
Syjalo2mo ago
Do you have another process running? Try to turn your bot off and send the command
فيصل
فيصلOP2mo ago
Pastebin
const { Client, GatewayIntentBits, SlashCommandBuilder, Collection,...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
فيصل
فيصلOP2mo ago
This is the whole code
Retro
Retro2mo ago
no what he meant was do you have another instance of the bot running
فيصل
فيصلOP2mo ago
no just this one
NyR
NyR2mo ago
Line 535
const messageCount = (await db.get(`messageCount_${guildId}_${userId}`)) || 0;
await db.set(`messageCount_${guildId}_${userId}`, messageCount + 1);

const messageCount = (await db.get(`messageCount_${guildId}_${userId}`)) || 0;
await db.set(`messageCount_${guildId}_${userId}`, messageCount + 1);

You do increase the count without any checks in you second messageCreate listener That's why we don't recommend duplicating listeners, it's easier to duplicate codes that way
Syjalo
Syjalo2mo ago
*we don't
NyR
NyR2mo ago
typo lol, yeah
فيصل
فيصلOP2mo ago
Thank you, I remembered why I separated them. I separated the listeners because I didn't want to apply the (count) conditions to the commands, and that's how I figured out the solution and the problem. Thank you all! :1980_11:
فيصل
فيصلOP2mo ago
No description

Did you find this page helpful?