Browse Source

Add option to disable ban pagination

pull/10109/head
dolfies 2 years ago
parent
commit
afda1ebecd
  1. 20
      discord/guild.py
  2. 4
      discord/http.py

20
discord/guild.py

@ -2029,6 +2029,7 @@ class Guild(Hashable):
limit: Optional[int] = 1000, limit: Optional[int] = 1000,
before: Snowflake = MISSING, before: Snowflake = MISSING,
after: Snowflake = MISSING, after: Snowflake = MISSING,
paginate: bool = True,
) -> AsyncIterator[BanEntry]: ) -> AsyncIterator[BanEntry]:
"""Retrieves an :term:`asynchronous iterator` of the users that are banned from the guild as a :class:`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. Retrieves bans before this user.
after: :class:`.abc.Snowflake` after: :class:`.abc.Snowflake`
Retrieve bans after this user. 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 Raises
------- -------
@ -2083,7 +2089,17 @@ class Guild(Hashable):
raise TypeError('bans pagination does not support both before and after') raise TypeError('bans pagination does not support both before and after')
# This endpoint paginates in ascending order # 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): async def _before_strategy(retrieve, before, limit):
before_id = before.id if before else None before_id = before.id if before else None
@ -2126,7 +2142,7 @@ class Guild(Hashable):
limit = 0 limit = 0
for e in data: 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( async def prune_members(
self, self,

4
discord/http.py

@ -1462,12 +1462,12 @@ class HTTPClient:
def get_bans( def get_bans(
self, self,
guild_id: Snowflake, guild_id: Snowflake,
limit: int, limit: Optional[int] = None,
before: Optional[Snowflake] = None, before: Optional[Snowflake] = None,
after: Optional[Snowflake] = None, after: Optional[Snowflake] = None,
) -> Response[List[guild.Ban]]: ) -> Response[List[guild.Ban]]:
params: Dict[str, Any] = {} params: Dict[str, Any] = {}
if limit != 1000: if limit is not None:
params['limit'] = limit params['limit'] = limit
if before is not None: if before is not None:
params['before'] = before params['before'] = before

Loading…
Cancel
Save