From 0ec72660cf608c0b825a1a734261dbc44ad29845 Mon Sep 17 00:00:00 2001
From: Rapptz <rapptz@gmail.com>
Date: Sat, 5 Sep 2020 13:57:50 -0400
Subject: [PATCH] Speed up chunking for guilds with presence intent enabled

---
 discord/state.py | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/discord/state.py b/discord/state.py
index 7d4acbb9d..b565ae28d 100644
--- a/discord/state.py
+++ b/discord/state.py
@@ -303,6 +303,10 @@ class ConnectionState:
         self._add_guild(guild)
         return guild
 
+    def _guild_needs_chunking(self, guild):
+        # If presences are enabled then we get back the old guild.large behaviour
+        return self._fetch_offline and not guild.chunked and not (self._intents.presences and not guild.large)
+
     def _get_guild_channel(self, data):
         channel_id = int(data['channel_id'])
         try:
@@ -356,7 +360,8 @@ class ConnectionState:
                     except asyncio.TimeoutError:
                         break
                     else:
-                        if self._fetch_offline:
+
+                        if self._guild_needs_chunking(guild):
                             future = await self.chunk_guild(guild, wait=False)
                             states.append((guild, future))
                         else:
@@ -781,7 +786,7 @@ class ConnectionState:
             return
 
         # check if it requires chunking
-        if self._fetch_offline:
+        if self._guild_needs_chunking(guild):
             asyncio.ensure_future(self._chunk_and_dispatch(guild, unavailable), loop=self.loop)
             return
 
@@ -1053,7 +1058,7 @@ class AutoShardedConnectionState(ConnectionState):
             except asyncio.TimeoutError:
                 break
             else:
-                if self._fetch_offline:
+                if self._guild_needs_chunking(guild):
                     # Chunk the guild in the background while we wait for GUILD_CREATE streaming
                     future = asyncio.ensure_future(self.chunk_guild(guild))
                 else: