As the title explains, I have a function in my Discord.js (v12) bot that takes care of welcoming users and assigning them a specific role. Here's the code:
client.on("guildMemberAdd", member => {
member.roles.add('604250195001081859');
member.guild.channels.cache.get("707323130523418686").send(`Hi ${member.user} blabla`);
console.log(member.user.id + ' is in da house');
});
Now, I'm facing a problem: every time someone joins the server, it seems like this event is triggered multiple times, flooding the channel.
Obviously, this doesn't happen if I use "client.once," but in that case, the message is only sent to the user on their first access, and subsequent users don't receive it.
I've searched everywhere for answers, but it seems like I'm the only one experiencing this issue. I hope someone can help me.
P.S. I want to clarify that I don't have any other active instances of the bot, and it is hosted on a small Vultr VPS.
After some time, I managed to find the problem. If you encounter this issue, please check your entire project to locate this method call:
client.login(<token>)
If it's called within a keepalive function or, in any case, within a function that is called multiple times after or outside the server startup, it creates a new instance of the bot. Each instance will respond to events, resulting in a flood of messages (in the case of chat messages) or the repetition of any operation inside the function for each created instance.
In summary, the login()
method only needs to be called once to prevent the creation of new instances.
And yes, you can now throw all the rotten tomatoes at me if you want.