فيصل
فيصل
Explore posts from servers
DIAdiscord.js - Imagine an app
Created by فيصل on 12/26/2024 in #djs-questions
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! 😊
33 replies
DIAdiscord.js - Imagine an app
Created by فيصل on 10/22/2024 in #djs-questions
Context Commands Registering
No description
16 replies
VVALORANT
Created by فيصل on 8/25/2024 in #community-help
Purchase error
No description
2 replies
DIAdiscord.js - Imagine an app
Created by فيصل on 7/16/2024 in #djs-questions
vanity link
Is there a function in discord.js v13? If I change the vanity link, I can do special actions to change the vanity link.
5 replies
DIAdiscord.js - Imagine an app
Created by فيصل on 5/31/2024 in #djs-questions
Embeds Style
No description
16 replies
DIAdiscord.js - Imagine an app
Created by فيصل on 5/26/2024 in #djs-questions
Change Role icon (Emoji)
No description
11 replies