From ab9356be83da8baab081c32a2dadb6d320e0c834 Mon Sep 17 00:00:00 2001 From: Eta <24918963+Eta0@users.noreply.github.com> Date: Sun, 27 Nov 2022 00:43:24 -0600 Subject: [PATCH] Fix async iterators requesting past their bounds This affects Messageable.history, ScheduledEvent.users, Client.fetch_guilds, and Guild.audit_logs. To illustrate the problem, Messageable.history counted returned messages to tell when to stop iteration, but did so before filtering away those past the before or after boundaries. When both oldest_first=False and an after boundary were provided, this led to the history iterator continuing to retrieve messages older than the after boundary, which would then all be filtered away, continuing until the message limit or the beginning of the entire channel was reached. A similar situation would also occur with oldest_first=True and a before boundary provided. This commit changes the logic in these methods to count items after filtering, so they stop requesting more as soon as the in-bounds items are exhausted. --- discord/abc.py | 14 ++++++++------ discord/guild.py | 16 +++++++++------- discord/scheduled_event.py | 12 +++++++----- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/discord/abc.py b/discord/abc.py index 2099e8e94..111afd5c0 100644 --- a/discord/abc.py +++ b/discord/abc.py @@ -1887,24 +1887,26 @@ class Messageable: channel = await self._get_channel() while True: - retrieve = min(100 if limit is None else limit, 100) + retrieve = 100 if limit is None else min(limit, 100) 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: - limit = 0 - if reverse: data = reversed(data) if predicate: data = filter(predicate, data) - for raw_message in data: + count = 0 + + for count, raw_message in enumerate(data, 1): yield self._state.create_message(channel=channel, data=raw_message) + if count < 100: + # There's no data left after this + break + def slash_commands( self, query: Optional[str] = None, diff --git a/discord/guild.py b/discord/guild.py index 950fffcb9..a32e469f5 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -2314,7 +2314,7 @@ class Guild(Hashable): strategy, state = _after_strategy, after while True: - retrieve = min(1000 if limit is None else limit, 1000) + retrieve = 1000 if limit is None else min(limit, 1000) if retrieve < 1: return @@ -3626,16 +3626,12 @@ class Guild(Hashable): predicate = lambda m: int(m['id']) > after.id while True: - retrieve = min(100 if limit is None else limit, 100) + retrieve = 100 if limit is None else min(limit, 100) if retrieve < 1: return data, raw_entries, state, limit = await strategy(retrieve, state, limit) - # Terminate loop on next iteration; there's no data left after this - if len(raw_entries) < 100: - limit = 0 - if reverse: raw_entries = reversed(raw_entries) if predicate: @@ -3650,7 +3646,9 @@ class Guild(Hashable): ) automod_rule_map = {rule.id: rule for rule in automod_rules} - for raw_entry in raw_entries: + count = 0 + + for count, raw_entry in enumerate(raw_entries, 1): # Weird Discord quirk if raw_entry['action_type'] is None: continue @@ -3662,6 +3660,10 @@ class Guild(Hashable): guild=self, ) + if count < 100: + # There's no data left after this + break + async def ack(self) -> None: """|coro| diff --git a/discord/scheduled_event.py b/discord/scheduled_event.py index 0f6911d22..04c40c10a 100644 --- a/discord/scheduled_event.py +++ b/discord/scheduled_event.py @@ -549,25 +549,27 @@ class ScheduledEvent(Hashable): predicate = lambda u: u['user']['id'] > after.id while True: - retrieve = min(100 if limit is None else limit, 100) + retrieve = 100 if limit is None else min(limit, 100) if retrieve < 1: return data, state, limit = await strategy(retrieve, state, limit) - if len(data) < 100: - limit = 0 - if reverse: data = reversed(data) if predicate: data = filter(predicate, data) users = (self._state.store_user(raw_user) for raw_user in data) + count = 0 - for user in users: + for count, user in enumerate(users, 1): yield user + if count < 100: + # There's no data left after this + break + def _add_user(self, user: User) -> None: self._users[user.id] = user