-
Notifications
You must be signed in to change notification settings - Fork 135
/
stage_instance.py
208 lines (163 loc) · 6.66 KB
/
stage_instance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# SPDX-License-Identifier: MIT
from __future__ import annotations
from typing import TYPE_CHECKING, Optional
from .enums import StagePrivacyLevel, try_enum
from .mixins import Hashable
from .utils import MISSING, _get_as_snowflake, cached_slot_property, warn_deprecated
__all__ = ("StageInstance",)
if TYPE_CHECKING:
from .channel import StageChannel
from .guild import Guild
from .guild_scheduled_event import GuildScheduledEvent
from .state import ConnectionState
from .types.channel import StageInstance as StageInstancePayload
class StageInstance(Hashable):
"""Represents a stage instance of a stage channel in a guild.
.. versionadded:: 2.0
.. container:: operations
.. describe:: x == y
Checks if two stage instances are equal.
.. describe:: x != y
Checks if two stage instances are not equal.
.. describe:: hash(x)
Returns the stage instance's hash.
Attributes
----------
id: :class:`int`
The stage instance's ID.
guild: :class:`Guild`
The guild that the stage instance is running in.
channel_id: :class:`int`
The ID of the channel that the stage instance is running in.
topic: :class:`str`
The topic of the stage instance.
privacy_level: :class:`StagePrivacyLevel`
The privacy level of the stage instance.
guild_scheduled_event_id: Optional[:class:`int`]
The ID of the stage instance's associated scheduled event, if applicable.
See also :attr:`.guild_scheduled_event`.
"""
__slots__ = (
"_state",
"id",
"guild",
"channel_id",
"topic",
"privacy_level",
"_discoverable_disabled",
"guild_scheduled_event_id",
"_cs_channel",
)
def __init__(self, *, state: ConnectionState, guild: Guild, data: StageInstancePayload) -> None:
self._state = state
self.guild = guild
self._update(data)
def _update(self, data: StageInstancePayload):
self.id: int = int(data["id"])
self.channel_id: int = int(data["channel_id"])
self.topic: str = data["topic"]
self.privacy_level: StagePrivacyLevel = try_enum(StagePrivacyLevel, data["privacy_level"])
self._discoverable_disabled: bool = data.get("discoverable_disabled", False)
self.guild_scheduled_event_id: Optional[int] = _get_as_snowflake(
data, "guild_scheduled_event_id"
)
def __repr__(self) -> str:
return f"<StageInstance id={self.id} guild={self.guild!r} channel_id={self.channel_id} topic={self.topic!r}>"
@cached_slot_property("_cs_channel")
def channel(self) -> Optional[StageChannel]:
"""Optional[:class:`StageChannel`]: The channel that stage instance is running in."""
# the returned channel will always be a StageChannel or None
return self._state.get_channel(self.channel_id) # type: ignore
@property
def discoverable_disabled(self) -> bool:
""":class:`bool`: Whether discoverability for the stage instance is disabled.
.. deprecated:: 2.5
Stages can no longer be discoverable.
"""
warn_deprecated(
"StageInstance.discoverable_disabled is deprecated and will be removed in a future version",
stacklevel=2,
)
return self._discoverable_disabled
def is_public(self) -> bool:
"""Whether the stage instance is public.
.. deprecated:: 2.5
Stages can no longer be public.
:return type: :class:`bool`
"""
warn_deprecated(
"StageInstance.is_public is deprecated and will be removed in a future version",
stacklevel=2,
)
return self.privacy_level is StagePrivacyLevel.public
@property
def guild_scheduled_event(self) -> Optional[GuildScheduledEvent]:
"""Optional[:class:`GuildScheduledEvent`]: The stage instance's scheduled event.
This is only set if this stage instance has an associated scheduled event,
and requires that event to be cached
(which requires the :attr:`~Intents.guild_scheduled_events` intent).
"""
if self.guild_scheduled_event_id is None:
return None
return self.guild.get_scheduled_event(self.guild_scheduled_event_id)
async def edit(
self,
*,
topic: str = MISSING,
privacy_level: StagePrivacyLevel = MISSING,
reason: Optional[str] = None,
) -> None:
"""|coro|
Edits the stage instance.
You must have the :attr:`~Permissions.manage_channels` permission to
use this.
.. versionchanged:: 2.6
Raises :exc:`TypeError` instead of ``InvalidArgument``.
Parameters
----------
topic: :class:`str`
The stage instance's new topic.
privacy_level: :class:`StagePrivacyLevel`
The stage instance's new privacy level.
reason: Optional[:class:`str`]
The reason the stage instance was edited. Shows up on the audit log.
Raises
------
TypeError
If the ``privacy_level`` parameter is not the proper type.
Forbidden
You do not have permissions to edit the stage instance.
HTTPException
Editing a stage instance failed.
"""
payload = {}
if topic is not MISSING:
payload["topic"] = topic
if privacy_level is not MISSING:
if not isinstance(privacy_level, StagePrivacyLevel):
raise TypeError("privacy_level field must be of type PrivacyLevel")
if privacy_level is StagePrivacyLevel.public:
warn_deprecated(
"Setting privacy_level to public is deprecated and will be removed in a future version.",
stacklevel=2,
)
payload["privacy_level"] = privacy_level.value
if payload:
await self._state.http.edit_stage_instance(self.channel_id, **payload, reason=reason)
async def delete(self, *, reason: Optional[str] = None) -> None:
"""|coro|
Deletes the stage instance.
You must have the :attr:`~Permissions.manage_channels` permission to
use this.
Parameters
----------
reason: Optional[:class:`str`]
The reason the stage instance was deleted. Shows up on the audit log.
Raises
------
Forbidden
You do not have permissions to delete the stage instance.
HTTPException
Deleting the stage instance failed.
"""
await self._state.http.delete_stage_instance(self.channel_id, reason=reason)