From 22b06e7bf87082222796688ad7b745e5a8024cf4 Mon Sep 17 00:00:00 2001 From: mniip Date: Tue, 22 Feb 2022 05:53:40 +0300 Subject: [PATCH] Fix TextChannel.archived_threads Threads are now returned in a consistent order if multiple requests are made. The server-side limit is [2, 100] threads per request. --- discord/channel.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/discord/channel.py b/discord/channel.py index fc50810a8..5e09e6f39 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -760,10 +760,11 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable): *, private: bool = False, joined: bool = False, - limit: Optional[int] = 50, + limit: Optional[int] = 100, before: Optional[Union[Snowflake, datetime.datetime]] = None, ) -> AsyncIterator[Thread]: - """Returns an :term:`asynchronous iterator` that iterates over all archived threads in the guild. + """Returns an :term:`asynchronous iterator` that iterates over all archived threads in the guild, + in order of decreasing ID for joined threads, and decreasing :attr:`Thread.archive_timestamp` otherwise. You must have :attr:`~Permissions.read_message_history` to use this. If iterating over private threads then :attr:`~Permissions.manage_threads` is also required. @@ -825,22 +826,28 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable): endpoint = self.guild._state.http.get_private_archived_threads while True: - retrieve = 50 if limit is None else max(limit, 50) + retrieve = 100 + if limit is not None: + if limit <= 0: + return + retrieve = max(2, min(retrieve, limit)) + data = await endpoint(self.id, before=before_timestamp, limit=retrieve) threads = data.get('threads', []) - for raw_thread in reversed(threads): + for raw_thread in threads: yield Thread(guild=self.guild, state=self.guild._state, data=raw_thread) + # Currently the API doesn't let you request less than 2 threads. + # Bail out early if we had to retrieve more than what the limit was. + if limit is not None: + limit -= 1 + if limit <= 0: + return if not data.get('has_more', False): return - if limit is not None: - limit -= len(threads) - if limit <= 0: - return - - before = update_before(threads[-1]) + before_timestamp = update_before(threads[-1]) class VocalGuildChannel(discord.abc.Connectable, discord.abc.GuildChannel, Hashable):