Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Commit

Permalink
fix: handle resume-ready guild create events
Browse files Browse the repository at this point in the history
do not view the horrors that lay within, simply know they exist, and know they protect you. A necessary evil to defend the weak and protect the holy. To smite the miscreants whom aim to damage your cache
  • Loading branch information
LordOfPolls committed Aug 10, 2022
1 parent 59fc704 commit 9b86f7f
Showing 1 changed file with 58 additions and 37 deletions.
95 changes: 58 additions & 37 deletions naff/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,47 +725,68 @@ async def _on_websocket_ready(self, event: events.RawGatewayEvent) -> None:
expected_guilds = {to_snowflake(guild["id"]) for guild in data["guilds"]}
self._user._add_guilds(expected_guilds)

while True:
try: # wait to let guilds cache
await asyncio.wait_for(self._guild_event.wait(), self.guild_event_timeout)
if self.fetch_members:
# ensure all guilds have completed chunking
for guild in self.guilds:
if guild and not guild.chunked.is_set():
logger.debug(f"Waiting for {guild.id} to chunk")
await guild.chunked.wait()

except asyncio.TimeoutError:
logger.warning("Timeout waiting for guilds cache: Not all guilds will be in cache")
break
self._guild_event.clear()

if len(self.cache.guild_cache) == len(expected_guilds):
# all guilds cached
break

if self.fetch_members:
# ensure all guilds have completed chunking
for guild in self.guilds:
if guild and not guild.chunked.is_set():
logger.debug(f"Waiting for {guild.id} to chunk")
await guild.chunked.wait()

# run any pending startup tasks
if self.async_startup_tasks:
try:
await asyncio.gather(*self.async_startup_tasks)
except Exception as e:
self.dispatch(events.Error("async-extension-loader", e))

# cache slash commands
if not self._startup:
await self._init_interactions()
while True:
try: # wait to let guilds cache
await asyncio.wait_for(self._guild_event.wait(), self.guild_event_timeout)
if self.fetch_members:
# ensure all guilds have completed chunking
for guild in self.guilds:
if guild and not guild.chunked.is_set():
logger.debug(f"Waiting for {guild.id} to chunk")
await guild.chunked.wait()

except asyncio.TimeoutError:
logger.warning("Timeout waiting for guilds cache: Not all guilds will be in cache")
break
self._guild_event.clear()

if len(self.cache.guild_cache) == len(expected_guilds):
# all guilds cached
break

if self.fetch_members:
# ensure all guilds have completed chunking
for guild in self.guilds:
if guild and not guild.chunked.is_set():
logger.debug(f"Waiting for {guild.id} to chunk")
await guild.chunked.wait()

# run any pending startup tasks
if self.async_startup_tasks:
try:
await asyncio.gather(*self.async_startup_tasks)
except Exception as e:
self.dispatch(events.Error("async-extension-loader", e))

# cache slash commands
if not self._startup:
await self._init_interactions()

self._ready.set()
if not self._startup:
self._startup = True
self.dispatch(events.Startup())

else:
# reconnect ready
ready_guilds = set()

async def _temp_listener(_event: events.RawGatewayEvent) -> None:
ready_guilds.add(_event.data["id"])

listener = Listener.create("_on_raw_guild_create")(_temp_listener)
self.add_listener(listener)

while True:
try:
await asyncio.wait_for(self._guild_event.wait(), self.guild_event_timeout)
if len(ready_guilds) == len(expected_guilds):
break
except asyncio.TimeoutError:
break

self.listeners["raw_guild_create"].remove(listener)

self._ready.set()
self.dispatch(events.Ready())

async def login(self, token) -> None:
Expand Down

0 comments on commit 9b86f7f

Please sign in to comment.