automuteus / galactus

The All-Powerful Socket.io Connection Aggregator
MIT License
22 stars 11 forks source link

[v7] Galactus does not fire `GuildCreateHandler` on startup #10

Closed kurokobo closed 3 years ago

kurokobo commented 3 years ago

Describe the bug

With a low probability, the GuildCreateHandler will not be executed when Galactus starts while the bot is already a member of the guilds. This issue tends to occur when the load on the container host is high or running on the hardware with low performance.

Once this problem occurs, AutoMuteUs will not handle guilds properly after its startup, causing problems such as emoji not being displayed at the start of the game.

This seems to be caused by the order of the functions in initialization process of shard_manager. Currently all handlers are added AFTER dshardmanager.start(), but should be added BEFORE start().

To Reproduce

Steps to reproduce the behavior:

  1. Invite the bot to guild
  2. docker-compose down -v
  3. docker-compose up -d

To force to reproduce, simply add Sleep before AddHandlers in internal/galactus/server.go.

    manager := shard_manager.MakeShardManager(logger, botToken, numShards, DefaultIntents)
    time.Sleep(time.Millisecond * 100) 👈👈👈👈👈
    shard_manager.AddHandlers(logger, manager, rdb, botPrefix)
    shard_manager.AddRateLimitHandler(manager, RateLimitHandler(logger, rdb))

Expected behavior

AutoMuteUs and Galactus handle guilds properly on its startup.

$ docker-compose logs | grep -i guild
automuteus_1  | 2021/03/07 18:14:05 No EMOJI_GUILD_ID specified!!! Emojis will be added to a RANDOM guild, I recommend you specify which guild you'd like me to add emojis to by specifying EMOJI_GUILD_ID
automuteus_1  | {"level":"info","ts":1615140848.3485575,"caller":"client/client.go:155","msg":"discord message handler registered","type":"GuildDelete"}
automuteus_1  | {"level":"info","ts":1615140848.3486629,"caller":"client/client.go:155","msg":"discord message handler registered","type":"GuildCreate"}
automuteus_1  | 2021/03/07 18:14:08 Added to new Guild, id 8025**********6117, name kuro-sandbox 👈👈👈👈👈
automuteus_1  | 2021/03/07 18:14:08 Adding any missing emojis to guild 8025**********6117. On first startup, this can take a long time to complete (Discord's rate-limits on adding emojis)
galactus_1    | {"level":"info","ts":1615140848.1920583,"caller":"handler/guild_create.go:39","msg":"pushed discord message to Redis","type":"GuildCreate","guild_id":"8025**********6117","user_id":"483272567797579778","id":"8025**********6117"} 👈👈👈👈👈
galactus_1    | {"level":"info","ts":1615140848.5512645,"caller":"galactus/guild.go:166","msg":"fetched guild emojis","guildID":"8025**********6117"} 👈👈👈👈👈
postgres_1    | 2021-03-07 18:14:08.357 UTC [61] ERROR:  relation "guilds" does not exist at character 15
postgres_1    | 2021-03-07 18:14:08.357 UTC [61] STATEMENT:  SELECT * FROM guilds WHERE guild_id=$1
postgres_1    | 2021-03-07 18:14:08.357 UTC [61] ERROR:  relation "guilds" does not exist at character 13
postgres_1    | 2021-03-07 18:14:08.357 UTC [61] STATEMENT:  INSERT INTO guilds VALUES ($1, $2, 0); 

Screenshots

Once this issue occur, none of the guilds are detected and nothing happens.

$ docker-compose logs | grep -i guild
automuteus_1  | 2021/03/07 18:01:43 No EMOJI_GUILD_ID specified!!! Emojis will be added to a RANDOM guild, I recommend you specify which guild you'd like me to add emojis to by specifying EMOJI_GUILD_ID
automuteus_1  | {"level":"info","ts":1615140104.5785844,"caller":"client/client.go:155","msg":"discord message handler registered","type":"GuildDelete"}
automuteus_1  | {"level":"info","ts":1615140104.5786276,"caller":"client/client.go:155","msg":"discord message handler registered","type":"GuildCreate"}

Version of bot & capture

denverquane commented 3 years ago

Appreciate you filing the issue, and also the fix!