diff --git a/discord/channel.py b/discord/channel.py index 4e955a6ae..9636bc229 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -47,7 +47,7 @@ import datetime import discord.abc from .scheduled_event import ScheduledEvent from .permissions import PermissionOverwrite, Permissions -from .enums import ChannelType, ForumLayoutType, PrivacyLevel, try_enum, VideoQualityMode, EntityType +from .enums import ChannelType, ForumLayoutType, ForumOrderType, PrivacyLevel, try_enum, VideoQualityMode, EntityType from .mixins import Hashable from . import utils from .utils import MISSING @@ -2154,6 +2154,10 @@ class ForumChannel(discord.abc.GuildChannel, Hashable): Defaults to :attr:`ForumLayoutType.not_set`. .. versionadded:: 2.2 + default_sort_order: Optional[:class:`ForumOrderType`] + The default sort order for posts in this forum channel. + + .. versionadded:: 2.3 """ __slots__ = ( @@ -2173,6 +2177,7 @@ class ForumChannel(discord.abc.GuildChannel, Hashable): 'default_thread_slowmode_delay', 'default_reaction_emoji', 'default_layout', + 'default_sort_order', '_available_tags', '_flags', ) @@ -2218,6 +2223,11 @@ class ForumChannel(discord.abc.GuildChannel, Hashable): name=default_reaction_emoji.get('emoji_name') or '', ) + self.default_sort_order: Optional[ForumOrderType] = None + default_sort_order = data.get('default_sort_order') + if default_sort_order is not None: + self.default_sort_order = try_enum(ForumOrderType, default_sort_order) + self._flags: int = data.get('flags', 0) self._fill_overwrites(data) @@ -2344,6 +2354,7 @@ class ForumChannel(discord.abc.GuildChannel, Hashable): default_thread_slowmode_delay: int = ..., default_reaction_emoji: Optional[EmojiInputType] = ..., default_layout: ForumLayoutType = ..., + default_sort_order: ForumOrderType = ..., require_tag: bool = ..., ) -> ForumChannel: ... @@ -2402,6 +2413,10 @@ class ForumChannel(discord.abc.GuildChannel, Hashable): The new default layout for posts in this forum. .. versionadded:: 2.2 + default_sort_order: Optional[:class:`ForumOrderType`] + The new default sort order for posts in this forum. + + .. versionadded:: 2.3 require_tag: :class:`bool` Whether to require a tag for threads in this channel or not. @@ -2464,6 +2479,21 @@ class ForumChannel(discord.abc.GuildChannel, Hashable): options['default_forum_layout'] = layout.value + try: + sort_order = options.pop('default_sort_order') + except KeyError: + pass + else: + if sort_order is None: + options['default_sort_order'] = None + else: + if not isinstance(sort_order, ForumOrderType): + raise TypeError( + f'default_sort_order parameter must be a ForumOrderType not {sort_order.__class__.__name__}' + ) + + options['default_sort_order'] = sort_order.value + payload = await self._edit(options, reason=reason) if payload is not None: # the payload will always be the proper channel payload diff --git a/discord/enums.py b/discord/enums.py index 21a38caf8..94ca8c726 100644 --- a/discord/enums.py +++ b/discord/enums.py @@ -67,6 +67,7 @@ __all__ = ( 'AutoModRuleEventType', 'AutoModRuleActionType', 'ForumLayoutType', + 'ForumOrderType', ) if TYPE_CHECKING: @@ -751,6 +752,11 @@ class ForumLayoutType(Enum): gallery_view = 2 +class ForumOrderType(Enum): + latest_activity = 0 + creation_date = 1 + + def create_unknown_value(cls: Type[E], val: Any) -> E: value_cls = cls._enum_value_cls_ # type: ignore # This is narrowed below name = f'unknown_{val}' diff --git a/discord/guild.py b/discord/guild.py index 4861333d5..e1cc7814e 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -73,6 +73,7 @@ from .enums import ( MFALevel, Locale, AutoModRuleEventType, + ForumOrderType, ) from .mixins import Hashable from .user import User @@ -1576,6 +1577,7 @@ class Guild(Hashable): reason: Optional[str] = None, default_auto_archive_duration: int = MISSING, default_thread_slowmode_delay: int = MISSING, + default_sort_order: Optional[ForumOrderType] = None, available_tags: Sequence[ForumTag] = MISSING, ) -> ForumChannel: """|coro| @@ -1620,6 +1622,10 @@ class Guild(Hashable): The default slowmode delay in seconds for threads created in this forum. .. versionadded:: 2.1 + default_sort_order: Optional[:class:`ForumOrderType`] + The default sort order for posts in this forum channel. + + .. versionadded:: 2.3 available_tags: Sequence[:class:`ForumTag`] The available tags for this forum channel. @@ -1659,6 +1665,16 @@ class Guild(Hashable): if default_thread_slowmode_delay is not MISSING: options['default_thread_rate_limit_per_user'] = default_thread_slowmode_delay + if default_sort_order is None: + options['default_sort_order'] = None + else: + if not isinstance(default_sort_order, ForumOrderType): + raise TypeError( + f'default_sort_order parameter must be a ForumOrderType not {default_sort_order.__class__.__name__}' + ) + + options['default_sort_order'] = default_sort_order.value + if available_tags is not MISSING: options['available_tags'] = [t.to_dict() for t in available_tags] diff --git a/discord/http.py b/discord/http.py index 3fa622967..5e8d32dcd 100644 --- a/discord/http.py +++ b/discord/http.py @@ -1148,6 +1148,7 @@ class HTTPClient: 'available_tags', 'applied_tags', 'default_forum_layout', + 'default_sort_order', ) payload = {k: v for k, v in options.items() if k in valid_keys} @@ -1189,6 +1190,7 @@ class HTTPClient: 'video_quality_mode', 'default_auto_archive_duration', 'default_thread_rate_limit_per_user', + 'default_sort_order', 'available_tags', ) payload.update({k: v for k, v in options.items() if k in valid_keys and v is not None}) diff --git a/discord/types/channel.py b/discord/types/channel.py index ad17af689..421232b45 100644 --- a/discord/types/channel.py +++ b/discord/types/channel.py @@ -134,6 +134,7 @@ class ForumTag(TypedDict): emoji_name: Optional[str] +ForumOrderType = Literal[0, 1] ForumLayoutType = Literal[0, 1, 2] @@ -141,6 +142,7 @@ class ForumChannel(_BaseTextChannel): type: Literal[15] available_tags: List[ForumTag] default_reaction_emoji: Optional[DefaultReaction] + default_sort_order: Optional[ForumOrderType] default_forum_layout: NotRequired[ForumLayoutType] flags: NotRequired[int] diff --git a/docs/api.rst b/docs/api.rst index 5dd452af0..316dbda14 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -3310,6 +3310,21 @@ of :class:`enum.Enum`. Displays posts as a collection of tiles. +.. class:: ForumOrderType + + Represents how a forum's posts are sorted in the client. + + .. versionadded:: 2.3 + + .. attribute:: latest_activity + + Sort forum posts by activity. + + .. attribute:: creation_date + + Sort forum posts by creation time (from most recent to oldest). + + .. _discord-api-audit-logs: Audit Log Data