Fix category cache issue#900
Conversation
scragly
left a comment
There was a problem hiding this comment.
Alterations look good to me, thanks for your work!
SebastiaanZ
left a comment
There was a problem hiding this comment.
Yes, looks good.
Just to add some context to those wondering what was going on: The channels cache we accessed through the guild attribute of a reference to the Available category was completely out of sync with actual channel lay-out of the guild. This started happening after a disconnect/restart of the shard's websocket/gateway connection:
2020-04-21 01:18:47 | discord.gateway | WARNING | Shard ID None has stopped responding to the gateway. Closing and restarting.
Somehow, that channel cache registered all channels being moved to the available category, but not the moves of channels out of that category. That caused the effect we saw in the stats: All the channels appeared to be in the Available category.
We confirmed that this channels cache was wrong and different from the one access via bot.guild.channels using our internal eval. The latter, bot.guild.channels did reflect the actual state of the channels on our server.
Internal eval results:
# Using the `channels` accessed through the category reference:
In [4]: print(sum(1 for _ in bot.get_cog("HelpChannels").get_category_channels(bot.get_cog("HelpChannels").available_category)))
29
In [5]: print(sum(1 for _ in bot.get_cog("HelpChannels").get_category_channels(bot.get_cog("HelpChannels").dormant_category)))
0
# -------------------------------------
# Using other means:
In [10]: available = guild.get_channel(691405807388196926)
...: print(len(available.channels))
...:
2
In [14]: import discord
...: occupied = 696958401460043776
...: print(sum(isinstance(c, discord.TextChannel) and c.category_id == occupied for c in guild.channels))
...:
10
In [18]: hc = bot.get_cog("HelpChannels")
...: channels = hc.bot.get_guild(267624335836053506).channels
...: print(sum(isinstance(c, discord.TextChannel) and c.category_id == 696958401460043776 for c in channels))
...:
10
After experiencing a reconnect from Discord, the channel cache we were using in the help cog began to fall apart and claimed there were 29 channels in the available category and no channels elsewhere.
(Blue line marks disconnect)
This PR forces retrieval of the channels in the guild from the bot cache in the
get_category_channelsmethod through the usage ofbot.get_guildinstead ofcategory.guild.channels, since the latter fell apart after a reconnect.