Browse Source

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.
pull/7494/head
mniip 3 years ago
committed by GitHub
parent
commit
22b06e7bf8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      discord/channel.py

27
discord/channel.py

@ -760,10 +760,11 @@ class TextChannel(discord.abc.Messageable, discord.abc.GuildChannel, Hashable):
*, *,
private: bool = False, private: bool = False,
joined: bool = False, joined: bool = False,
limit: Optional[int] = 50, limit: Optional[int] = 100,
before: Optional[Union[Snowflake, datetime.datetime]] = None, before: Optional[Union[Snowflake, datetime.datetime]] = None,
) -> AsyncIterator[Thread]: ) -> 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 You must have :attr:`~Permissions.read_message_history` to use this. If iterating over private threads
then :attr:`~Permissions.manage_threads` is also required. 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 endpoint = self.guild._state.http.get_private_archived_threads
while True: 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) data = await endpoint(self.id, before=before_timestamp, limit=retrieve)
threads = data.get('threads', []) 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) 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): if not data.get('has_more', False):
return return
if limit is not None: before_timestamp = update_before(threads[-1])
limit -= len(threads)
if limit <= 0:
return
before = update_before(threads[-1])
class VocalGuildChannel(discord.abc.Connectable, discord.abc.GuildChannel, Hashable): class VocalGuildChannel(discord.abc.Connectable, discord.abc.GuildChannel, Hashable):

Loading…
Cancel
Save