javascript - 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));
});
};
解决方案
我假设每次有新成员加入时,您都在执行模块中的代码。
guildMemberAdd
发生这种情况是因为每次 aGuildMember
加入 a时,您都在为事件创建一个新的侦听器Guild
。
相反,您应该传递GuildMember
旁边的Discord
and client
,而不是每次都创建一个新的侦听器。
推荐阅读
- xargs - 使用xargs时如何解决没有这样的文件或目录
- python - MariaDB 连接器 python 获取执行的查询
- aws-codepipeline - 用于 CodePipeline 的云开发工具包失败,没有为 cdk.out 找到匹配的基本目录路径
- android - NanoHTTPD - 写入套接字而不是复制字符串来传递页面
- amazon-web-services - 有没有办法在 Github Actions 中运行 Boto3 (AWS SDK) 命令?
- api - 颤振错误:列表
不是 Map 类型的子类型 - dart - 我如何转换为双 .dart
- reactjs - React 测试库:测试中的更新未包含在 act(...) 中,并且无法对未安装的组件执行 React 状态更新
- c# - 我可以从 UWP 应用的进程外后台任务中启动 FullTrust Win32 应用吗?
- xaml - CollectionView 中的扩展器未正确更新 xamarin 中的屏幕