Browse Source

Support spoiler flags for stage channels

pull/10487/head
harumaki4649 3 weeks ago
parent
commit
c2ec97aeab
  1. 28
      discord/channel.py
  2. 6
      discord/guild.py
  3. 46
      tests/test_channel_flags.py

28
discord/channel.py

@ -142,7 +142,7 @@ if TYPE_CHECKING:
spoiler: bool
overwrites: Mapping[Union[Role, Member, Object], PermissionOverwrite]
class _CreateStageChannelOptions(_BaseCreateChannelOptions, total=False):
class _CreateStageChannelOptions(_CreateVoiceChannelOptions, total=False):
bitrate: int
user_limit: int
rtc_region: Optional[str]
@ -1153,6 +1153,13 @@ class VocalGuildChannel(discord.abc.Messageable, discord.abc.Connectable, discor
"""
return self.nsfw
def is_spoiler(self) -> bool:
""":class:`bool`: Checks if members must opt in before viewing the channel's contents.
.. versionadded:: 2.8
"""
return self.flags.spoiler
@property
def members(self) -> List[Member]:
"""List[:class:`Member`]: Returns all members that are currently inside this voice channel."""
@ -1570,13 +1577,6 @@ class VoiceChannel(VocalGuildChannel):
""":class:`ChannelType`: The channel's Discord type."""
return ChannelType.voice
def is_spoiler(self) -> bool:
""":class:`bool`: Checks if members must opt in before viewing the channel's contents.
.. versionadded:: 2.8
"""
return self.flags.spoiler
@overload
async def edit(self) -> None: ...
@ -1963,6 +1963,7 @@ class StageChannel(VocalGuildChannel):
*,
name: str = ...,
nsfw: bool = ...,
spoiler: bool = ...,
bitrate: int = ...,
user_limit: int = ...,
position: int = ...,
@ -2005,6 +2006,8 @@ class StageChannel(VocalGuildChannel):
The new channel's position.
nsfw: :class:`bool`
To mark the channel as NSFW or not.
spoiler: :class:`bool`
Whether members must opt in before viewing the channel's contents.
user_limit: :class:`int`
The new channel's user limit.
sync_permissions: :class:`bool`
@ -2045,6 +2048,15 @@ class StageChannel(VocalGuildChannel):
then ``None`` is returned instead.
"""
try:
spoiler = options.pop('spoiler')
except KeyError:
pass
else:
flags = self.flags
flags.spoiler = spoiler
options['flags'] = flags.value
payload = await self._edit(options, reason=reason)
if payload is not None:
# the payload will always be the proper channel payload

6
discord/guild.py

@ -1668,6 +1668,7 @@ class Guild(Hashable):
video_quality_mode: VideoQualityMode = MISSING,
overwrites: Mapping[Union[Role, Member, Object], PermissionOverwrite] = MISSING,
nsfw: bool = MISSING,
spoiler: bool = MISSING,
) -> StageChannel:
"""|coro|
@ -1715,6 +1716,8 @@ class Guild(Hashable):
To mark the channel as NSFW or not.
.. versionadded:: 2.6
spoiler: :class:`bool`
Whether members must opt in before viewing the channel's contents.
reason: Optional[:class:`str`]
The reason for creating this channel. Shows up on the audit log.
@ -1754,6 +1757,9 @@ class Guild(Hashable):
if nsfw is not MISSING:
options['nsfw'] = nsfw
if spoiler is not MISSING:
options['flags'] = ChannelFlags(spoiler=spoiler).value
data = await self._create_channel(
name, overwrites=overwrites, channel_type=ChannelType.stage_voice, category=category, reason=reason, **options
)

46
tests/test_channel_flags.py

@ -1,6 +1,6 @@
import pytest
from discord.channel import TextChannel
from discord.channel import StageChannel, TextChannel
from discord.flags import ChannelFlags
@ -68,3 +68,47 @@ async def test_text_channel_edit_sets_spoiler_flag():
assert edited is not None
assert edited.flags.spoiler is True
assert edited.is_spoiler() is True
class _StageHTTP(_HTTP):
async def edit_channel(self, channel_id, *, reason, **options):
self.channel_id = channel_id
self.reason = reason
self.options = options
return {
'id': str(channel_id),
'type': 13,
'name': 'spoilers',
'position': 0,
'permission_overwrites': [],
'bitrate': 64000,
'user_limit': 0,
'flags': options['flags'],
}
@pytest.mark.asyncio
async def test_stage_channel_edit_sets_spoiler_flag():
state = _State()
state.http = _StageHTTP()
channel = StageChannel(
state=state,
guild=_Guild(),
data={
'id': '1',
'type': 13,
'name': 'spoilers',
'position': 0,
'permission_overwrites': [],
'bitrate': 64000,
'user_limit': 0,
'flags': 1 << 4,
},
)
edited = await channel.edit(spoiler=True)
assert state.http.options == {'flags': (1 << 4) | (1 << 21)}
assert edited is not None
assert edited.flags.spoiler is True
assert edited.is_spoiler() is True

Loading…
Cancel
Save