guildMemberAdd function being weird and sending the dm 1 more time for every join

I am trying to make a guildMemberAdd function to my bot however when the first member joins, it just DMs them once, when the second person joins it DMs them twice, and so on.

So I did some testing and when I put it in my main file it works perfectly fine, it starts acting up when I put it in its own file.

module.exports = (Discord, client) => {
    client.on("guildMemberAdd", (guildMember) => {
        const colors = require("../../colors.json");
        const config = require("../../config.json");
        const guild = client.guilds.cache.get(config.serverID);
        const memberCount = guild.memberCount;

        const WelcomeEmbed = new Discord.MessageEmbed()

            .setTitle(`Welcome to the Official Lightyear's Mods Discord server!`)
            .setColor(colors.purple)
            .setDescription(
                `Please make sure to read the rules, they can be found in <#819913883828879361> and have a great time here in **${guild.name}**!nBe sure to check out <#819914570784309258> for some weet deals, and <#819914774446866442> for daily free money drops!`
            )
            .setFooter("©Lightyear's Mods", guild.iconURL({ dynamic: true }));
        guildMember.send(WelcomeEmbed).catch((err) => console.log(err));
    });
};

Answer

I’m going to assume that every time a new member joins you are executing the code within the module.

This is happening because you are creating a new listener to guildMemberAdd event every time a GuildMember joins a Guild.

Instead, you should pass the GuildMember alongside Discord and client, and not create a new listener each time.