cronokirby / alchemy

A discord library for Elixir
MIT License
152 stars 34 forks source link

FIX: Cache crashes if guild not present when handling event #116

Closed Zarathustra2 closed 3 years ago

Zarathustra2 commented 3 years ago

If the bot is running and someones add the bot to another guild the cache can crash and become unresponsive if at the same time another event occurs in the guild.

For instance. if the bot FooBar joins a guild where another bot assigns all new members a role, then discord would send the signals GUILD_MEMBER_UPDATE & GUILD_CREATE simultaneously. It can happen that the GUILD_MEMBER_UPDATE is being processed before the GUILD_CREATE signal.

But as the guild may not exist yet, this can crash here: https://github.com/cronokirby/alchemy/blob/edb04294b10dfe16e2b40dcc31a4937f9382c0b6/lib/Cache/guilds.ex#L43

To prevent that, we can just handle all events that come in and check if a GenServer with the associated guild_id is already running.


Unfortunately, as the scenario sounds quite uncommon it happens quite often as a lot of larger guilds have bots that assign roles when a new member joins or have a welcome message posted in a channel.

cronokirby commented 3 years ago

Very nice!