From 08844985cc2ee78b470db800f52f7e9c67f09726 Mon Sep 17 00:00:00 2001 From: Lilly Rose Berner Date: Sat, 23 Apr 2022 04:31:33 +0200 Subject: [PATCH] Fix Client.fetch_guilds not working correctly, increase default limit --- discord/client.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/discord/client.py b/discord/client.py index e91b4dd35..aee47af78 100644 --- a/discord/client.py +++ b/discord/client.py @@ -1194,7 +1194,7 @@ class Client: async def fetch_guilds( self, *, - limit: Optional[int] = 100, + limit: Optional[int] = 200, before: Optional[SnowflakeTime] = None, after: Optional[SnowflakeTime] = None, ) -> AsyncIterator[Guild]: @@ -1230,7 +1230,12 @@ class Client: The number of guilds to retrieve. If ``None``, it retrieves every guild you have access to. Note, however, that this would make it a slow operation. - Defaults to ``100``. + Defaults to ``200``. + + .. versionchanged:: 2.0 + + The default has been changed to 200. + before: Union[:class:`.abc.Snowflake`, :class:`datetime.datetime`] Retrieves guilds before this date or object. If a datetime is provided, it is recommended to use a UTC aware datetime. @@ -1259,7 +1264,7 @@ class Client: if limit is not None: limit -= len(data) - before = Object(id=int(data[-1]['id'])) + before = Object(id=int(data[0]['id'])) return data, before, limit @@ -1271,7 +1276,7 @@ class Client: if limit is not None: limit -= len(data) - after = Object(id=int(data[0]['id'])) + after = Object(id=int(data[-1]['id'])) return data, after, limit @@ -1281,22 +1286,23 @@ class Client: after = Object(id=time_snowflake(after, high=True)) predicate: Optional[Callable[[GuildPayload], bool]] = None - strategy, state = _before_strategy, before + strategy, state = _after_strategy, after + + if before: + strategy, state = _before_strategy, before if before and after: predicate = lambda m: int(m['id']) > after.id - elif after: - strategy, state = _after_strategy, after while True: - retrieve = min(100 if limit is None else limit, 100) + retrieve = min(200 if limit is None else limit, 200) if retrieve < 1: return data, state, limit = await strategy(retrieve, state, limit) # Terminate loop on next iteration; there's no data left after this - if len(data) < 100: + if len(data) < 200: limit = 0 if predicate: