简体   繁体   English

Discord.js client.on guildMemberAdd 多次触发

[英]Discord.js client.on guildMemberAdd firing multiple times

As the title explain, i have a function on discord.js (v12) bot which takes care of welcoming users, and imposing a specific role on them, this:正如标题所解释的,我在 discord.js (v12) bot 上有一个函数,它负责欢迎用户,并为他们施加特定的角色,这个:

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 have a problem: every time someone join server it's like this event is triggered many times over and over again, flooding the channel.现在,我有一个问题:每次有人加入服务器时,就像这个事件被一遍又一遍地触发,淹没了频道。

Obviously this does not happen if I set "client.once" but in that case the message is given only at the first access of a user, the second does not receive it anymore.显然,如果我设置了“client.once”,则不会发生这种情况,但在这种情况下,该消息仅在用户第一次访问时提供,第二次不再接收。

I've been looking everywhere for answers, but I seem to be the only run into this issues, I hope someone can help me.我一直在到处寻找答案,但我似乎是唯一遇到这个问题的人,我希望有人能帮助我。

ps I want to clarify haven't other active instances of the bot, and that it is hosted on a small vultr's vps. ps 我想澄清没有其他活跃的 bot 实例,并且它托管在一个小型 vultr 的 vps 上。

This happens to me a lot go to the discord dev portal and generate a new token and paste the new token into your code.这发生在我身上很多去不和谐开发门户并生成一个新令牌并将新令牌粘贴到您的代码中。 For some reason its running multiple instances of your bot at the same time and generating a new token should fix that issue.出于某种原因,它同时运行机器人的多个实例并生成新令牌应该可以解决该问题。 I know you said you dont have multiple instances running but you will not be able to see the instances, its a weird bug.我知道您说过您没有运行多个实例,但您将无法看到这些实例,这是一个奇怪的错误。

Ok after a bit i found the problem... if you run into this check in entire project to find this method call:好的,过了一会儿我发现了问题......如果你在整个项目中遇到这个检查来找到这个方法调用:

client.login(<token>)

If it's called within a keepalive function, or in any case in a function called several times after/ouside the server startup, it creates a new instance of the bot and each of them will respond to events, effectively creating a flood of messages (in case of chat messages, but any operation inside the function is repeated for the number of instances created).如果在 keepalive 函数中调用它,或者在任何情况下在服务器启动后/外部多次调用的函数中调用它,它会创建一个新的机器人实例,每个实例都会响应事件,有效地创建大量消息(在聊天消息的情况,但函数内部的任何操作都会针对创建的实例数量重复)。

In short, the login() method only needs to be called once to avoid creating new instances.简而言之,login() 方法只需要调用一次就可以避免创建新实例。

And Yes, You can throw me all the rotten tomatoes you want now.是的,你现在可以把所有你想要的烂番茄都扔给我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM