首页 > 解决方案 > guildMemberAdd 函数很奇怪,每次加入都会多发送 1 次 dm

问题描述

我正在尝试为我的机器人创建一个 guildMemberAdd 功能,但是当第一个成员加入时,它只会给他们发消息一次,当第二个成员加入时给他们发消息两次,依此类推。

所以我做了一些测试,当我把它放在我的主文件中时它工作得很好,当我把它放在自己的文件中时它就开始起作用了。

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));
    });
};

标签: javascriptnode.jsdiscorddiscord.js

解决方案


我假设每次有新成员加入时,您都在执行模块中的代码。

guildMemberAdd发生这种情况是因为每次 aGuildMember加入 a时,您都在为事件创建一个新的侦听器Guild

相反,您应该传递GuildMember旁边的Discordand client,而不是每次都创建一个新的侦听器。


推荐阅读