aboutsummaryrefslogtreecommitdiffstats
path: root/bot/bot.py
diff options
context:
space:
mode:
authorGravatar kwzrd <[email protected]>2020-04-04 15:44:17 +0200
committerGravatar kwzrd <[email protected]>2020-04-04 15:44:17 +0200
commitdf17366c5d9023c4c4ed6c690479411a671fef34 (patch)
tree43efe81272bdb89df7a228cc44878889b9648668 /bot/bot.py
parentMerge pull request #392 from python-discord/fetch-devlog-channel (diff)
Add `wait_until_guild_available` coroutine
See docstring for explanation & comparison to the `on_ready` event. This is taken from the Python bot repository. Originally implemented by Mark. The `on_guild_available` callback was adapted and simplified from the original implementation, as with Sentry in place, it is believed that dispatching an error webhook is unnecessary. Co-authored-by: MarkKoz <[email protected]>
Diffstat (limited to 'bot/bot.py')
-rw-r--r--bot/bot.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/bot/bot.py b/bot/bot.py
index 4f238df8..e460d9ab 100644
--- a/bot/bot.py
+++ b/bot/bot.py
@@ -44,6 +44,8 @@ class SeasonalBot(commands.Bot):
self.http_session = ClientSession(
connector=TCPConnector(resolver=AsyncResolver(), family=socket.AF_INET)
)
+ self._guild_available = asyncio.Event()
+
self.loop.create_task(self.send_log("SeasonalBot", "Connected!"))
@property
@@ -168,5 +170,37 @@ class SeasonalBot(commands.Bot):
await devlog.send(embed=embed)
+ async def on_guild_available(self, guild: discord.Guild) -> None:
+ """
+ Set the internal `_guild_available` event when PyDis guild becomes available.
+
+ If the cache appears to still be empty (no members, no channels, or no roles), the event
+ will not be set.
+ """
+ if guild.id != Client.guild:
+ return
+
+ if not guild.roles or not guild.members or not guild.channels:
+ log.warning("Guild available event was dispatched but the cache appears to still be empty!")
+ return
+
+ self._guild_available.set()
+
+ async def on_guild_unavailable(self, guild: discord.Guild) -> None:
+ """Clear the internal `_guild_available` event when PyDis guild becomes unavailable."""
+ if guild.id != Client.guild:
+ return
+
+ self._guild_available.clear()
+
+ async def wait_until_guild_available(self) -> None:
+ """
+ Wait until the PyDis guild becomes available (and the cache is ready).
+
+ The on_ready event is inadequate because it only waits 2 seconds for a GUILD_CREATE
+ gateway event before giving up and thus not populating the cache for unavailable guilds.
+ """
+ await self._guild_available.wait()
+
bot = SeasonalBot(command_prefix=Client.prefix)