Message sending multiple times

Sounds like a really basic issue - and it may be so. However, I've been unable to come up with a solution as to what is exactly causing the issue & how I would resolve it. You will see from the code attached below that I am attempting to send a welcome message ONCE. If you also look at the attached image, you'll see that there are multiple of these messages being sent at one time.
import { TextChannel } from 'discord.js';
import keys from '../keys';
import { event } from '../utils';
import { CustomEmbeds } from '../config/embeds';

const handled_this_session = new Set();

export default event('guildMemberAdd', async ({ client }, member) => {
if (!client.user) return;
if (member.guild.id !== keys.GUILD_ID_MAIN) return;
if (handled_this_session.has(member.id)) return;

const welcome_channel = await client.channels.fetch(keys.CHANNEL_ID_WELCOME) as TextChannel;

const messages = await welcome_channel.messages.fetch()
const isExisting = messages.filter(msg => { return msg.content.includes(`<@${member.id}>`) });
if (isExisting.size > 0) return;

await welcome_channel.send({
content: member.toString(),
embeds: [CustomEmbeds.general.welcome_message(member)]
})

handled_this_session.add(member.id)
})
import { TextChannel } from 'discord.js';
import keys from '../keys';
import { event } from '../utils';
import { CustomEmbeds } from '../config/embeds';

const handled_this_session = new Set();

export default event('guildMemberAdd', async ({ client }, member) => {
if (!client.user) return;
if (member.guild.id !== keys.GUILD_ID_MAIN) return;
if (handled_this_session.has(member.id)) return;

const welcome_channel = await client.channels.fetch(keys.CHANNEL_ID_WELCOME) as TextChannel;

const messages = await welcome_channel.messages.fetch()
const isExisting = messages.filter(msg => { return msg.content.includes(`<@${member.id}>`) });
if (isExisting.size > 0) return;

await welcome_channel.send({
content: member.toString(),
embeds: [CustomEmbeds.general.welcome_message(member)]
})

handled_this_session.add(member.id)
})
No description
7 Replies
d.js toolkit
d.js toolkit3w 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!
oxi
oxiOP3w ago
the handled_this_session set should still include the user's ID as the bot has NOT been reset in the past 22 days.
No description
zevnda
zevnda3w ago
Why check if a message exists with the member's ID if you're already referencing a set? Can't say for sure, but if either one of those async methods takes a while to execute, and other people join before they complete, that could lead to duplicate messages being sent. You're better off just referencing the set alone Plus you're just making redundant API calls at that point
oxi
oxiOP3w ago
another attempt to resolve the issue - neither one was successful at stopping the messages being duplicated this is for 1 user, this isn't multiple users
zevnda
zevnda3w ago
Are you unknowingly running multiple instances of your bot? Are you listening to the guildMemberAdd event anywhere else? And I get that it's only for 1 user, but if multiple users joined at the same time it might cause duplicates if any of those async operations didn't finish before the event was fired again, this is unlikely but possible I think the likely issue is multiple instances, or your event handler is triggering the event multiple time
treble/luna
treble/luna3w ago
You probably need to use enforceNonce and nonce in your send options, or provide the enforceNonce option in the client options and let d.js handle it for you if you're on the latest version
oxi
oxiOP3w ago
thank you will try this
Want results from more Discord servers?
Add your server