diff --git a/CHANGELOG.md b/CHANGELOG.md index e2b278dad9..e6ff2291e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#3152](https://github.com/Pycord-Development/pycord/pull/3152)) - Added `Embed.components` and `DesignerView.from_embed`. ([#3443](https://github.com/Pycord-Development/pycord/pull/3443)) +- Added support for spoiler channels. + ([#3252](https://github.com/Pycord-Development/pycord/pull/3252)) ### Changed diff --git a/discord/channel.py b/discord/channel.py index 69c2bc9865..13b0742754 100755 --- a/discord/channel.py +++ b/discord/channel.py @@ -26,10 +26,12 @@ from __future__ import annotations import datetime +import warnings from collections.abc import Callable, Iterable, Mapping, Sequence from typing import ( TYPE_CHECKING, Any, + Literal, NamedTuple, TypeVar, overload, @@ -317,6 +319,17 @@ def is_nsfw(self) -> bool: """Checks if the channel is NSFW.""" return self.nsfw + @property + def spoiler(self) -> bool: + """Checks if the channel is a spoiler channel. + + .. note:: + This is an alias for :attr:`ChannelFlags.is_spoiler_channel`. + + .. versionadded:: 2.9 + """ + return self.flags.is_spoiler_channel + @property def last_message(self) -> Message | None: """Fetches the last message from this channel in cache. @@ -809,6 +822,26 @@ async def edit( default_thread_slowmode_delay: int = ..., type: ChannelType = ..., overwrites: Mapping[Role | Member | Snowflake, PermissionOverwrite] = ..., + spoiler: Literal[False] = ..., + ) -> TextChannel | None: ... + + @overload + async def edit( + self, + *, + reason: str | None = ..., + name: str = ..., + topic: str = ..., + position: int = ..., + nsfw: Literal[False] = ..., + sync_permissions: bool = ..., + category: CategoryChannel | None = ..., + slowmode_delay: int = ..., + default_auto_archive_duration: ThreadArchiveDuration = ..., + default_thread_slowmode_delay: int = ..., + type: ChannelType = ..., + overwrites: Mapping[Role | Member | Snowflake, PermissionOverwrite] = ..., + spoiler: bool = ..., ) -> TextChannel | None: ... @overload @@ -841,6 +874,10 @@ async def edit(self, *, reason=None, **options): The new channel's position. nsfw: :class:`bool` Whether the channel is marked as NSFW. + + .. note:: + This setting is mutually exclusive with :attr:`spoiler`. Applying this to a spoiler channel will convert it to an NSFW channel. + Passing both as ``True`` will make the channel an NSFW channel. sync_permissions: :class:`bool` Whether to sync permissions with the channel's new or pre-existing category. Defaults to ``False``. @@ -865,6 +902,13 @@ async def edit(self, *, reason=None, **options): The new default slowmode delay in seconds for threads created in this channel. .. versionadded:: 2.3 + spoiler: :class:`bool` + Whether the channel should be a spoiler channel. Mutually exclusive with :attr:`nsfw`. + + .. versionadded:: 2.9 + + .. note:: + Passing both this and ``nsfw`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. Returns ------- @@ -882,6 +926,18 @@ async def edit(self, *, reason=None, **options): HTTPException Editing the channel failed. """ + if "spoiler" in options: + options["flags"] = ChannelFlags._from_value(self.flags.value) + options["flags"].is_spoiler_channel = options["spoiler"] + + if options.get("nsfw") and options.get("spoiler"): + warnings.warn( + "The NSFW setting is mutually exclusive with the spoiler setting. " + "The channel will become an NSFW channel.", + stacklevel=2, + ) + options.pop("spoiler") + payload = await self._edit(options, reason=reason) if payload is not None: # the payload will always be the proper channel payload @@ -1123,6 +1179,31 @@ async def edit( available_tags: list[ForumTag] = ..., require_tag: bool = ..., overwrites: Mapping[Role | Member | Snowflake, PermissionOverwrite] = ..., + spoiler: Literal[False] = ..., + ) -> ForumChannel | None: ... + + @overload + async def edit( + self, + *, + reason: str | None = ..., + name: str = ..., + topic: str = ..., + position: int = ..., + nsfw: Literal[False] = ..., + sync_permissions: bool = ..., + category: CategoryChannel | None = ..., + slowmode_delay: int = ..., + default_auto_archive_duration: ( + ThreadArchiveDuration | ThreadArchiveDurationEnum + ) = ..., + default_thread_slowmode_delay: int = ..., + default_sort_order: SortOrder = ..., + default_reaction_emoji: GuildEmoji | int | str | None = ..., + available_tags: list[ForumTag] = ..., + require_tag: bool = ..., + overwrites: Mapping[Role | Member | Snowflake, PermissionOverwrite] = ..., + spoiler: bool = ..., ) -> ForumChannel | None: ... @overload @@ -1145,7 +1226,10 @@ async def edit(self, *, reason=None, **options): position: :class:`int` The new channel's position. nsfw: :class:`bool` - Whether the channel is marked as NSFW. + Whether the channel is marked as NSFW. Mutually exclusive with :attr:`spoiler`. + + .. note:: + Passing both this and ``spoiler`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. sync_permissions: :class:`bool` Whether to sync permissions with the channel's new or pre-existing category. Defaults to ``False``. @@ -1185,6 +1269,13 @@ async def edit(self, *, reason=None, **options): Whether a tag should be required to be specified when creating a thread in this channel. .. versionadded:: 2.3 + spoiler: :class:`bool` + Whether the channel should be a spoiler channel. Mutually exclusive with :attr:`nsfw`. + + .. versionadded:: 2.9 + + .. note:: + Passing both this and ``nsfw`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. Returns ------- @@ -1205,6 +1296,19 @@ async def edit(self, *, reason=None, **options): if "require_tag" in options: options["flags"] = ChannelFlags._from_value(self.flags.value) options["flags"].require_tag = options.pop("require_tag") + if "spoiler" in options: + if "flags" not in options: + options["flags"] = ChannelFlags._from_value( + self.flags.value + ) # Reconstruct existing flags + options["flags"].is_spoiler_channel = options["spoiler"] + + if options.get("nsfw") and options.get("spoiler"): + warnings.warn( + "The NSFW setting is mutually exclusive with the spoiler setting. The channel will become an NSFW channel.", + stacklevel=2, + ) + options.pop("spoiler") payload = await self._edit(options, reason=reason) if payload is not None: @@ -1504,10 +1608,31 @@ async def edit( require_tag: bool = ..., hide_media_download_options: bool = ..., overwrites: Mapping[Role | Member | Snowflake, PermissionOverwrite] = ..., + spoiler: Literal[False] = ..., ) -> MediaChannel | None: ... @overload - async def edit(self) -> MediaChannel | None: ... + async def edit( + self, + *, + reason: str | None = ..., + name: str = ..., + topic: str = ..., + position: int = ..., + nsfw: Literal[False] = ..., + sync_permissions: bool = ..., + category: CategoryChannel | None = ..., + slowmode_delay: int = ..., + default_auto_archive_duration: ThreadArchiveDuration = ..., + default_thread_slowmode_delay: int = ..., + default_sort_order: SortOrder = ..., + default_reaction_emoji: GuildEmoji | int | str | None = ..., + available_tags: list[ForumTag] = ..., + require_tag: bool = ..., + hide_media_download_options: bool = ..., + overwrites: Mapping[Role | Member | Snowflake, PermissionOverwrite] = ..., + spoiler: bool = ..., + ) -> MediaChannel | None: ... async def edit(self, *, reason=None, **options): """|coro| @@ -1526,7 +1651,10 @@ async def edit(self, *, reason=None, **options): position: :class:`int` The new channel's position. nsfw: :class:`bool` - Whether the channel is marked as NSFW. + Whether the channel is marked as NSFW. Mutually exclusive with :attr:`spoiler`. + + .. note:: + Passing both this and ``spoiler`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. sync_permissions: :class:`bool` Whether to sync permissions with the channel's new or pre-existing category. Defaults to ``False``. @@ -1563,6 +1691,14 @@ async def edit(self, *, reason=None, **options): hide_media_download_options: :class:`bool` Whether media download options should be hidden in this media channel. + spoiler: :class:`bool` + Whether the channel should be a spoiler channel. Mutually exclusive with :attr:`nsfw`. + + .. versionadded:: 2.9 + + .. note:: + Passing both this and ``nsfw`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. + Returns ------- :class:`.MediaChannel` | :obj:`None` @@ -1580,14 +1716,20 @@ async def edit(self, *, reason=None, **options): Editing the channel failed. """ - if "require_tag" in options or "hide_media_download_options" in options: + if {"require_tag", "hide_media_download_options", "spoiler"} & options.keys(): flags = ChannelFlags._from_value(self.flags.value) flags.require_tag = options.pop("require_tag", flags.require_tag) flags.hide_media_download_options = options.pop( "hide_media_download_options", flags.hide_media_download_options ) + if options.get("nsfw") and options.get("spoiler"): + warnings.warn( + "The NSFW setting is mutually exclusive with the spoiler setting. " + "The channel will become an NSFW channel.", + stacklevel=2, + ) + flags.is_spoiler_channel = options.pop("spoiler", flags.is_spoiler_channel) options["flags"] = flags - payload = await self._edit(options, reason=reason) if payload is not None: # the payload will always be the proper channel payload @@ -1837,19 +1979,30 @@ def is_nsfw(self) -> bool: """Checks if the channel is NSFW.""" return self.nsfw + @property + def spoiler(self) -> bool: + """Checks if the channel is a spoiler channel. + + .. note:: + This is an alias for :attr:`ChannelFlags.is_spoiler_channel`. + + .. versionadded:: 2.9 + """ + return self.flags.is_spoiler_channel + @property def last_message(self) -> Message | None: """Fetches the last message from this channel in cache. The message might not be valid or point to an existing message. - .. admonition:: Reliable Fetching - :class: helpful + .. admonition:: Reliable Fetching + :class: helpful - For a slightly more reliable method of fetching the - last message, consider using either :meth:`history` - or :meth:`fetch_message` with the :attr:`last_message_id` - attribute. + For a slightly more reliable method of fetching the + last message, consider using either :meth:`history` + or :meth:`fetch_message` with the :attr:`last_message_id` + attribute. Returns ------- @@ -2118,6 +2271,26 @@ async def edit( slowmode_delay: int = ..., nsfw: bool = ..., reason: str | None = ..., + spoiler: Literal[False] = ..., + ) -> VoiceChannel | None: ... + + @overload + async def edit( + self, + *, + name: str = ..., + bitrate: int = ..., + user_limit: int = ..., + position: int = ..., + sync_permissions: int = ..., + category: CategoryChannel | None = ..., + overwrites: Mapping[Role | Member, PermissionOverwrite] = ..., + rtc_region: VoiceRegion | None = ..., + video_quality_mode: VideoQualityMode = ..., + slowmode_delay: int = ..., + nsfw: Literal[False] = ..., + reason: str | None = ..., + spoiler: bool = ..., ) -> VoiceChannel | None: ... @overload @@ -2176,10 +2349,21 @@ async def edit(self, *, reason=None, **options): A value of `0` disables slowmode. The maximum value possible is `21600`. nsfw: :class:`bool` - Whether the channel is marked as NSFW. + Whether the channel is marked as NSFW. Mutually exclusive with :attr:`spoiler`. .. versionadded:: 2.7 + .. note:: + Passing both this and ``spoiler`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. + + spoiler: :class:`bool` + Whether the channel should be a spoiler channel. Mutually exclusive with :attr:`nsfw`. + + .. versionadded:: 2.9 + + .. note:: + Passing both this and ``nsfw`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. + Returns ------- :class:`.VoiceChannel` | :obj:`None` @@ -2195,6 +2379,16 @@ async def edit(self, *, reason=None, **options): HTTPException Editing the channel failed. """ + if "spoiler" in options: + options["flags"] = ChannelFlags._from_value(self.flags.value) + options["flags"].is_spoiler_channel = options["spoiler"] + + if options.get("nsfw") and options.get("spoiler"): + warnings.warn( + "NSFW setting is mutually exclusive with spoiler setting. Channel will become an NSFW channel.", + stacklevel=2, + ) + options.pop("spoiler") payload = await self._edit(options, reason=reason) if payload is not None: diff --git a/discord/flags.py b/discord/flags.py index e64ff6b857..c69783c2b9 100644 --- a/discord/flags.py +++ b/discord/flags.py @@ -1628,6 +1628,14 @@ def channel_obfuscated(self): return 1 << 17 + @flag_value + def is_spoiler_channel(self): + """:class:`bool`: Returns ``True`` if the channel is a spoiler channel. + + .. versionadded:: 2.9 + """ + return 1 << 21 + @fill_with_flags() class AttachmentFlags(BaseFlags): diff --git a/discord/guild.py b/discord/guild.py index a112070d52..713bf70a9d 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -28,12 +28,14 @@ import copy import datetime import unicodedata +import warnings from collections.abc import Sequence from dataclasses import dataclass from typing import ( TYPE_CHECKING, Any, ClassVar, + Literal, NamedTuple, TypeVar, cast, @@ -70,7 +72,7 @@ ) from .errors import ClientException, HTTPException, InvalidArgument, InvalidData from .file import File -from .flags import SystemChannelFlags +from .flags import ChannelFlags, SystemChannelFlags from .incidents import IncidentsData from .integrations import Integration, _integration_factory from .invite import Invite @@ -1461,6 +1463,9 @@ def _create_channel( elif not isinstance(overwrites, dict): raise InvalidArgument("overwrites parameter expects a dict.") + if "flags" in options: + options["flags"] = options.pop("flags").value + perms = [] for target, perm in overwrites.items(): if not isinstance(perm, PermissionOverwrite): @@ -1492,6 +1497,40 @@ def _create_channel( **options, ) + @overload + async def create_text_channel( + self, + name: str, + *, + reason: str | None = ..., + category: CategoryChannel | None = ..., + position: int = ..., + topic: str = ..., + slowmode_delay: int = ..., + nsfw: bool = ..., + overwrites: dict[Role | Member, PermissionOverwrite] = ..., + default_thread_slowmode_delay: int | None = ..., + default_auto_archive_duration: int = ..., + spoiler: Literal[False] = ..., + ) -> TextChannel: ... + + @overload + async def create_text_channel( + self, + name: str, + *, + reason: str | None = ..., + category: CategoryChannel | None = ..., + position: int = ..., + topic: str = ..., + slowmode_delay: int = ..., + nsfw: Literal[False] = ..., + overwrites: dict[Role | Member, PermissionOverwrite] = ..., + default_thread_slowmode_delay: int | None = ..., + default_auto_archive_duration: int = ..., + spoiler: bool = ..., + ) -> TextChannel: ... + async def create_text_channel( self, name: str, @@ -1505,6 +1544,7 @@ async def create_text_channel( overwrites: dict[Role | Member, PermissionOverwrite] = MISSING, default_thread_slowmode_delay: int | None = MISSING, default_auto_archive_duration: int = MISSING, + spoiler: bool = MISSING, ) -> TextChannel: """|coro| @@ -1544,6 +1584,10 @@ async def create_text_channel( A value of `0` disables slowmode. The maximum value possible is `21600`. nsfw: :class:`bool` Whether the channel is marked as NSFW. + + .. note:: + Passing both this and ``spoiler`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. + reason: :class:`str` | :obj:`None` The reason for creating this channel. Shows up on the audit log. @@ -1556,6 +1600,13 @@ async def create_text_channel( The default auto archive duration in minutes for threads created in this channel. .. versionadded:: 2.7 + spoiler: :class:`bool` + Whether the channel is marked as a spoiler channel. + + .. versionadded:: 2.9 + + .. note:: + Passing both this and ``nsfw`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. Returns ------- @@ -1611,6 +1662,16 @@ async def create_text_channel( if default_auto_archive_duration is not MISSING: options["default_auto_archive_duration"] = default_auto_archive_duration + if spoiler is not MISSING: + options["flags"] = ChannelFlags(is_spoiler_channel=spoiler) + + if nsfw and spoiler: + warnings.warn( + "The NSFW setting is mutually exclusive with the spoiler setting. " + "The channel will be created as an NSFW channel.", + stacklevel=2, + ) + data = await self._create_channel( name, overwrites=overwrites, @@ -1625,6 +1686,42 @@ async def create_text_channel( self._channels[channel.id] = channel return channel + @overload + async def create_voice_channel( + self, + name: str, + *, + reason: str | None = ..., + category: CategoryChannel | None = ..., + position: int = ..., + bitrate: int = ..., + user_limit: int = ..., + rtc_region: VoiceRegion | None = ..., + video_quality_mode: VideoQualityMode = ..., + overwrites: dict[Role | Member, PermissionOverwrite] = ..., + slowmode_delay: int = ..., + nsfw: bool = ..., + spoiler: Literal[False] = ..., + ) -> VoiceChannel: ... + + @overload + async def create_voice_channel( + self, + name: str, + *, + reason: str | None = ..., + category: CategoryChannel | None = ..., + position: int = ..., + bitrate: int = ..., + user_limit: int = ..., + rtc_region: VoiceRegion | None = ..., + video_quality_mode: VideoQualityMode = ..., + overwrites: dict[Role | Member, PermissionOverwrite] = ..., + slowmode_delay: int = ..., + nsfw: Literal[False] = ..., + spoiler: bool = ..., + ) -> VoiceChannel: ... + async def create_voice_channel( self, name: str, @@ -1639,6 +1736,7 @@ async def create_voice_channel( overwrites: dict[Role | Member, PermissionOverwrite] = MISSING, slowmode_delay: int = MISSING, nsfw: bool = MISSING, + spoiler: bool = MISSING, ) -> VoiceChannel: """|coro| @@ -1688,6 +1786,17 @@ async def create_voice_channel( .. versionadded:: 2.7 + .. note:: + Passing both this and ``spoiler`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. + + spoiler: :class:`bool` + Whether the channel is marked as a spoiler channel. + + .. versionadded:: 2.9 + + .. note:: + Passing both this and ``nsfw`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. + Returns ------- :class:`VoiceChannel` @@ -1724,6 +1833,16 @@ async def create_voice_channel( if nsfw is not MISSING: options["nsfw"] = nsfw + if spoiler is not MISSING: + options["flags"] = ChannelFlags(is_spoiler_channel=spoiler) + + if nsfw and spoiler: + warnings.warn( + "The NSFW setting is mutually exclusive with the spoiler setting. " + "The channel will be created as an NSFW channel.", + stacklevel=2, + ) + data = await self._create_channel( name, overwrites=overwrites, @@ -1867,6 +1986,46 @@ async def create_stage_channel( self._channels[channel.id] = channel return channel + @overload + async def create_forum_channel( + self, + name: str, + *, + reason: str | None = ..., + category: CategoryChannel | None = ..., + position: int = ..., + topic: str = ..., + slowmode_delay: int = ..., + nsfw: bool = ..., + overwrites: dict[Role | Member, PermissionOverwrite] = ..., + default_reaction_emoji: GuildEmoji | int | str = ..., + available_tags: list[ForumTag] = ..., + default_sort_order: SortOrder | None = ..., + default_thread_slowmode_delay: int | None = ..., + default_auto_archive_duration: int = ..., + spoiler: Literal[False] = ..., + ) -> ForumChannel: ... + + @overload + async def create_forum_channel( + self, + name: str, + *, + reason: str | None = ..., + category: CategoryChannel | None = ..., + position: int = ..., + topic: str = ..., + slowmode_delay: int = ..., + nsfw: Literal[False] = ..., + overwrites: dict[Role | Member, PermissionOverwrite] = ..., + default_reaction_emoji: GuildEmoji | int | str = ..., + available_tags: list[ForumTag] = ..., + default_sort_order: SortOrder | None = ..., + default_thread_slowmode_delay: int | None = ..., + default_auto_archive_duration: int = ..., + spoiler: bool = ..., + ) -> ForumChannel: ... + async def create_forum_channel( self, name: str, @@ -1883,6 +2042,7 @@ async def create_forum_channel( default_sort_order: SortOrder | None = MISSING, default_thread_slowmode_delay: int | None = MISSING, default_auto_archive_duration: int = MISSING, + spoiler: bool = MISSING, ) -> ForumChannel: """|coro| @@ -1922,6 +2082,10 @@ async def create_forum_channel( A value of ``0`` disables slowmode. The maximum value possible is ``21600``. nsfw: :class:`bool` Whether the channel is marked as NSFW. + + .. note:: + Passing both this and ``spoiler`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. + reason: :class:`str` | :obj:`None` The reason for creating this channel. Shows up on the audit log. default_reaction_emoji: :class:`GuildEmoji` | :class:`int` | :class:`str` | :obj:`None` @@ -1950,6 +2114,13 @@ async def create_forum_channel( The default auto archive duration in minutes for threads created in this channel. .. versionadded:: 2.7 + spoiler: :class:`bool` + Whether the channel is marked as a spoiler channel. + + .. versionadded:: 2.9 + + .. note:: + Passing both this and ``nsfw`` as ``True`` will mark the channel as NSFW and ignore the spoiler flag. Returns ------- @@ -2013,6 +2184,16 @@ async def create_forum_channel( if default_auto_archive_duration is not MISSING: options["default_auto_archive_duration"] = default_auto_archive_duration + if spoiler is not MISSING: + options["flags"] = ChannelFlags(is_spoiler_channel=spoiler) + + if nsfw and spoiler: + warnings.warn( + "The NSFW setting is mutually exclusive with the spoiler setting. " + "The channel will be created as an NSFW channel.", + stacklevel=2, + ) + if default_reaction_emoji is not MISSING: if isinstance( default_reaction_emoji, _EmojiTag diff --git a/discord/http.py b/discord/http.py index 7cdafc0ccc..132cda4f38 100644 --- a/discord/http.py +++ b/discord/http.py @@ -1176,6 +1176,7 @@ def create_channel( "video_quality_mode", "auto_archive_duration", "default_reaction_emoji", + "flags", ) payload.update( {k: v for k, v in options.items() if k in valid_keys and v is not None}