From afda1ebecd6e757c3d74c520a3f2dad6840a99c7 Mon Sep 17 00:00:00 2001 From: dolfies Date: Sun, 26 Mar 2023 13:07:03 -0400 Subject: [PATCH] Add option to disable ban pagination --- discord/guild.py | 20 ++++++++++++++++++-- discord/http.py | 4 ++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/discord/guild.py b/discord/guild.py index 6f618430a..14a4fff52 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -2029,6 +2029,7 @@ class Guild(Hashable): limit: Optional[int] = 1000, before: Snowflake = MISSING, after: Snowflake = MISSING, + paginate: bool = True, ) -> AsyncIterator[BanEntry]: """Retrieves an :term:`asynchronous iterator` of the users that are banned from the guild as a :class:`BanEntry`. @@ -2063,6 +2064,11 @@ class Guild(Hashable): Retrieves bans before this user. after: :class:`.abc.Snowflake` Retrieve bans after this user. + paginate: :class:`bool` + Whether to paginate the results. If ``False``, all bans are fetched with a single request and yielded, + ``limit`` is ignored, and ``before`` and ``after`` must not be provided. + + .. versionadded:: 2.0 Raises ------- @@ -2083,7 +2089,17 @@ class Guild(Hashable): raise TypeError('bans pagination does not support both before and after') # This endpoint paginates in ascending order - endpoint = self._state.http.get_bans + _state = self._state + endpoint = _state.http.get_bans + + if not paginate: + # For user accounts, not providing a limit will return *every* ban, + # as they were too lazy to implement proper pagination in the client + # However, pagination may be wanted for guilds with massive ban lists + data = await endpoint(self.id) + for entry in data: + yield BanEntry(user=User(state=_state, data=entry['user']), reason=entry['reason']) + return async def _before_strategy(retrieve, before, limit): before_id = before.id if before else None @@ -2126,7 +2142,7 @@ class Guild(Hashable): limit = 0 for e in data: - yield BanEntry(user=User(state=self._state, data=e['user']), reason=e['reason']) + yield BanEntry(user=User(state=_state, data=e['user']), reason=e['reason']) async def prune_members( self, diff --git a/discord/http.py b/discord/http.py index d8f39d3cb..47d450db5 100644 --- a/discord/http.py +++ b/discord/http.py @@ -1462,12 +1462,12 @@ class HTTPClient: def get_bans( self, guild_id: Snowflake, - limit: int, + limit: Optional[int] = None, before: Optional[Snowflake] = None, after: Optional[Snowflake] = None, ) -> Response[List[guild.Ban]]: params: Dict[str, Any] = {} - if limit != 1000: + if limit is not None: params['limit'] = limit if before is not None: params['before'] = before