Skip to content

Commit

Permalink
Chunk on guild join
Browse files Browse the repository at this point in the history
  • Loading branch information
dolfies committed Feb 1, 2024
1 parent 110423d commit 141e2ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
1 change: 1 addition & 0 deletions discord/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ def member_list_id(self) -> Union[str, Literal["everyone"]]:
overwrites.append(f"allow:{overwrite.id}")
elif deny.read_messages:
overwrites.append(f"deny:{overwrite.id}")

return str(utils.murmurhash32(",".join(sorted(overwrites)), signed=False))

def _update(self, guild: Guild, data: Dict[str, Any]) -> None:
Expand Down
17 changes: 11 additions & 6 deletions discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -4864,7 +4864,7 @@ async def fetch_price_tier(self, price_tier: int, /) -> Dict[str, int]:
"""
return await self._state.http.get_price_tier(price_tier)

async def chunk(self, channel: Snowflake = MISSING) -> List[Member]:
async def chunk(self, *, cache: bool = True) -> List[Member]:
"""|coro|
Requests all members that belong to this guild.
Expand All @@ -4886,19 +4886,24 @@ async def chunk(self, channel: Snowflake = MISSING) -> List[Member]:
This guild cannot be chunked or chunking failed.
Guild is no longer available.
InvalidData
Did not receive a response from the gateway.
Did not receive a response from the Gateway.
Returns
--------
List[:class:`Member`]
The members that belong to this guild.
"""
if self._offline_members_hidden:
raise ClientException('This guild cannot be chunked')
if self._state.is_guild_evicted(self):
state = self._state
if state.is_guild_evicted(self):
raise ClientException('This guild is no longer available')

members = await self._state.chunk_guild(self, channels=[channel] if channel else [])
if await state._can_chunk_guild(self):
members = await state.chunk_guild(self, cache=cache)
elif not self._offline_members_hidden:
...
else:
raise ClientException('This guild cannot be chunked')

return members

Check failure on line 4907 in discord/guild.py

View workflow job for this annotation

GitHub Actions / check

"members" is possibly unbound (reportUnboundVariable)

async def fetch_members(
Expand Down
29 changes: 20 additions & 9 deletions discord/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2659,12 +2659,11 @@ def _get_create_guild(self, data: gw.GuildCreateEvent) -> Optional[Guild]:
guild = self._get_guild(int(data['id']))
unavailable = data.get('unavailable')

# Discord being Discord sometimes sends a GUILD_CREATE after an OPCode 14 is sent (a la bots)
# Discord being Discord sometimes sends a GUILD_CREATE after subscribing to a guild
# In this case, we just update it and return None to avoid a double dispatch
# However, if the guild became available, then we gotta go through the motions
if guild is not None:
guild._from_data(data)
if unavailable != False:
if unavailable is not False:
return

return guild or self._add_guild_from_data(data)
Expand Down Expand Up @@ -2831,12 +2830,24 @@ async def _chunk_and_dispatch(self, guild: Guild, chunk: bool, unavailable: Opti
timeout = self._chunk_timeout(guild)

if chunk:
try:
await asyncio.wait_for(self.chunk_guild(guild), timeout=timeout)
except asyncio.TimeoutError:
_log.info('Somehow timed out waiting for chunks for guild %s.', guild.id)
except (ClientException, InvalidData):
pass
coro = None
if await self._can_chunk_guild(guild):
coro = self.chunk_guild(guild)
elif not guild._offline_members_hidden:
self._scrape_requests[guild.id] = request = MemberSidebar(
guild, MISSING, chunk=True, cache=True, loop=self.loop, delay=0
)
if request.channels:
request.start()
coro = request.get_future()

if coro is not None:
try:
await asyncio.wait_for(coro, timeout=timeout)
except asyncio.TimeoutError:
_log.warning('Somehow timed out waiting for chunks for guild %s.', guild.id)
except (ClientException, InvalidData):
pass

if unavailable is False:
self.dispatch('guild_available', guild)
Expand Down

0 comments on commit 141e2ef

Please sign in to comment.