mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-05-10 07:49:48 +00:00
Add Forum.archived_threads
This commit is contained in:
parent
8aaeb6acfa
commit
686071814b
@ -2643,6 +2643,73 @@ class ForumChannel(discord.abc.GuildChannel, Hashable):
|
||||
data = await self._state.http.create_webhook(self.id, name=str(name), avatar=avatar, reason=reason)
|
||||
return Webhook.from_state(data, state=self._state)
|
||||
|
||||
async def archived_threads(
|
||||
self,
|
||||
*,
|
||||
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 this forum
|
||||
in order of decreasing :attr:`Thread.archive_timestamp`.
|
||||
|
||||
You must have :attr:`~Permissions.read_message_history` to do this.
|
||||
|
||||
.. versionadded:: 2.0
|
||||
|
||||
Parameters
|
||||
-----------
|
||||
limit: Optional[:class:`bool`]
|
||||
The number of threads to retrieve.
|
||||
If ``None``, retrieves every archived thread in the channel. Note, however,
|
||||
that this would make it a slow operation.
|
||||
before: Optional[Union[:class:`abc.Snowflake`, :class:`datetime.datetime`]]
|
||||
Retrieve archived channels before the given date or ID.
|
||||
|
||||
Raises
|
||||
------
|
||||
Forbidden
|
||||
You do not have permissions to get archived threads.
|
||||
HTTPException
|
||||
The request to get the archived threads failed.
|
||||
|
||||
Yields
|
||||
-------
|
||||
:class:`Thread`
|
||||
The archived threads.
|
||||
"""
|
||||
before_timestamp = None
|
||||
|
||||
if isinstance(before, datetime.datetime):
|
||||
before_timestamp = before.isoformat()
|
||||
elif before is not None:
|
||||
before_timestamp = utils.snowflake_time(before.id).isoformat()
|
||||
|
||||
update_before = lambda data: data['thread_metadata']['archive_timestamp']
|
||||
|
||||
while True:
|
||||
retrieve = 100
|
||||
if limit is not None:
|
||||
if limit <= 0:
|
||||
return
|
||||
retrieve = max(2, min(retrieve, limit))
|
||||
|
||||
data = await self.guild._state.http.get_public_archived_threads(self.id, before=before_timestamp, limit=retrieve)
|
||||
|
||||
threads = data.get('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
|
||||
|
||||
before_timestamp = update_before(threads[-1])
|
||||
|
||||
|
||||
class DMChannel(discord.abc.Messageable, discord.abc.PrivateChannel, Hashable):
|
||||
"""Represents a Discord direct message channel.
|
||||
|
Loading…
x
Reference in New Issue
Block a user