1
|
|
|
# Copyright Pincer 2021-Present |
|
|
|
|
2
|
|
|
# Full MIT License can be found in `LICENSE` at the project root. |
3
|
|
|
|
4
|
|
|
from __future__ import annotations |
5
|
|
|
|
6
|
|
|
from dataclasses import dataclass |
7
|
|
|
from enum import IntEnum |
8
|
|
|
from typing import Dict, Optional, List, TYPE_CHECKING, Union, overload |
9
|
|
|
|
10
|
|
|
from ..._config import GatewayConfig |
11
|
|
|
from ...utils.api_object import APIObject |
12
|
|
|
from ...utils.types import MISSING |
13
|
|
|
|
14
|
|
|
if TYPE_CHECKING: |
15
|
|
|
from ..guild.overwrite import Overwrite |
16
|
|
|
from ..guild.thread import ThreadMetadata |
17
|
|
|
from ..guild.member import GuildMember |
18
|
|
|
from ..user import User |
19
|
|
|
from ... import Client |
20
|
|
|
from ...core.http import HTTPClient |
21
|
|
|
from ...utils import APINullable, Snowflake, Timestamp |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
class ChannelType(IntEnum): |
25
|
|
|
"""Represents a channel its type.""" |
26
|
|
|
GUILD_TEXT = 0 |
27
|
|
|
DM = 1 |
28
|
|
|
GUILD_VOICE = 2 |
29
|
|
|
GROUP_DM = 3 |
30
|
|
|
GUILD_CATEGORY = 4 |
31
|
|
|
GUILD_NEWS = 5 |
32
|
|
|
GUILD_STORE = 6 |
33
|
|
|
|
34
|
|
|
if GatewayConfig.version >= 9: |
35
|
|
|
GUILD_NEWS_THREAD = 10 |
36
|
|
|
GUILD_PUBLIC_THREAD = 11 |
37
|
|
|
GUILD_PRIVATE_THREAD = 12 |
38
|
|
|
|
39
|
|
|
GUILD_STAGE_VOICE = 13 |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
@dataclass |
|
|
|
|
43
|
|
|
class Channel(APIObject): |
44
|
|
|
""" |
45
|
|
|
Represents a Discord Channel Mention object |
46
|
|
|
|
47
|
|
|
:param _client: |
48
|
|
|
reference to the Client |
49
|
|
|
|
50
|
|
|
:param _http: |
51
|
|
|
reference to the HTTPClient |
52
|
|
|
|
53
|
|
|
:param id: |
54
|
|
|
the id of this channel |
55
|
|
|
|
56
|
|
|
:param type: |
57
|
|
|
the type of channel |
58
|
|
|
|
59
|
|
|
:param application_id: |
60
|
|
|
application id of the group DM creator if it is bot-created |
61
|
|
|
|
62
|
|
|
:param bitrate: |
63
|
|
|
the bitrate (in bits) of the voice channel |
64
|
|
|
|
65
|
|
|
:param default_auto_archive_duration: |
66
|
|
|
default duration for newly created threads, in minutes, to |
67
|
|
|
automatically archive the thread after recent activity, can be set to: |
68
|
|
|
60, 1440, 4320, 10080 |
69
|
|
|
|
70
|
|
|
:param guild_id: |
71
|
|
|
the id of the guild (may be missing for some channel objects received |
72
|
|
|
over gateway guild dispatches) |
73
|
|
|
|
74
|
|
|
:param icon: |
75
|
|
|
icon hash |
76
|
|
|
:param last_message_id: |
77
|
|
|
the id of the last message sent in this channel (may not point to an |
78
|
|
|
existing or valid message) |
79
|
|
|
|
80
|
|
|
:param last_pin_timestamp: |
81
|
|
|
when the last pinned message was pinned. This may be null in events |
82
|
|
|
such as GUILD_CREATE when a message is not pinned. |
83
|
|
|
|
84
|
|
|
:param member: |
85
|
|
|
thread member object for the current user, if they have joined the |
86
|
|
|
thread, only included on certain API endpoints |
87
|
|
|
|
88
|
|
|
:param member_count: |
89
|
|
|
an approximate count of users in a thread, stops counting at 50 |
90
|
|
|
|
91
|
|
|
:param message_count: |
92
|
|
|
an approximate count of messages in a thread, stops counting at 50 |
93
|
|
|
|
94
|
|
|
:param name: |
95
|
|
|
the name of the channel (1-100 characters) |
96
|
|
|
|
97
|
|
|
:param nsfw: |
98
|
|
|
whether the channel is nsfw |
99
|
|
|
|
100
|
|
|
:param owner_id: |
101
|
|
|
id of the creator of the group DM or thread |
102
|
|
|
|
103
|
|
|
:param parent_id: |
104
|
|
|
for guild channels: id of the parent category for a channel (each |
105
|
|
|
parent category can contain up to 50 channels), for threads: id of the |
106
|
|
|
text channel this thread was created |
107
|
|
|
|
108
|
|
|
:param permissions: |
109
|
|
|
computed permissions for the invoking user in the channel, including |
110
|
|
|
overwrites, only included when part of the resolved data received on a |
111
|
|
|
slash command interaction |
112
|
|
|
|
113
|
|
|
:param permission_overwrites: |
114
|
|
|
explicit permission overwrites for members and roles |
115
|
|
|
|
116
|
|
|
:param position: |
117
|
|
|
sorting position of the channel |
118
|
|
|
|
119
|
|
|
:param rate_limit_per_user: |
120
|
|
|
amount of seconds a user has to wait before sending another message |
121
|
|
|
(0-21600); bots, as well as users with the permission manage_messages |
122
|
|
|
or manage_channel, are unaffected |
123
|
|
|
|
124
|
|
|
:param recipients: |
125
|
|
|
the recipients of the DM |
126
|
|
|
|
127
|
|
|
:param rtc_region: |
128
|
|
|
voice region id for the voice channel, automatic when set to null |
129
|
|
|
|
130
|
|
|
:param thread_metadata: |
131
|
|
|
thread-specific fields not needed by other channels |
132
|
|
|
|
133
|
|
|
:param topic: |
134
|
|
|
the channel topic (0-1024 characters) |
135
|
|
|
|
136
|
|
|
:param user_limit: |
137
|
|
|
the user limit of the voice channel |
138
|
|
|
|
139
|
|
|
:param video_quality_mode: |
140
|
|
|
the camera video quality mode of the voice channel, 1 when not present |
141
|
|
|
""" |
142
|
|
|
|
143
|
|
|
_client: Client |
|
|
|
|
144
|
|
|
_http: HTTPClient |
|
|
|
|
145
|
|
|
|
146
|
|
|
id: Snowflake |
|
|
|
|
147
|
|
|
type: ChannelType |
148
|
|
|
|
149
|
|
|
application_id: APINullable[Snowflake] = MISSING |
|
|
|
|
150
|
|
|
bitrate: APINullable[int] = MISSING |
151
|
|
|
default_auto_archive_duration: APINullable[int] = MISSING |
152
|
|
|
guild_id: APINullable[Snowflake] = MISSING |
153
|
|
|
icon: APINullable[Optional[str]] = MISSING |
154
|
|
|
|
155
|
|
|
last_message_id: APINullable[Optional[Snowflake]] = MISSING |
156
|
|
|
last_pin_timestamp: APINullable[Optional[Timestamp]] = MISSING |
|
|
|
|
157
|
|
|
member: APINullable[GuildMember] = MISSING |
|
|
|
|
158
|
|
|
|
159
|
|
|
member_count: APINullable[int] = MISSING |
160
|
|
|
|
161
|
|
|
message_count: APINullable[int] = MISSING |
162
|
|
|
|
163
|
|
|
name: APINullable[str] = MISSING |
164
|
|
|
nsfw: APINullable[bool] = MISSING |
165
|
|
|
owner_id: APINullable[Snowflake] = MISSING |
166
|
|
|
parent_id: APINullable[Optional[Snowflake]] = MISSING |
167
|
|
|
permissions: APINullable[str] = MISSING |
168
|
|
|
permission_overwrites: APINullable[List[Overwrite]] = MISSING |
|
|
|
|
169
|
|
|
position: APINullable[int] = MISSING |
170
|
|
|
rate_limit_per_user: APINullable[int] = MISSING |
171
|
|
|
recipients: APINullable[List[User]] = MISSING |
|
|
|
|
172
|
|
|
rtc_region: APINullable[Optional[str]] = MISSING |
173
|
|
|
thread_metadata: APINullable[ThreadMetadata] = MISSING |
|
|
|
|
174
|
|
|
topic: APINullable[Optional[str]] = MISSING |
175
|
|
|
user_limit: APINullable[int] = MISSING |
176
|
|
|
video_quality_mode: APINullable[int] = MISSING |
177
|
|
|
|
178
|
|
|
@classmethod |
179
|
|
|
async def from_id(cls, client: Client, id: int) -> Channel: |
|
|
|
|
180
|
|
|
data = (await client.http.get(f"channels/{id}")) or {} |
181
|
|
|
data.update( |
182
|
|
|
{ |
183
|
|
|
"_client": client, |
184
|
|
|
"_http": client.http, |
185
|
|
|
"type": ChannelType(data.pop("type")) |
186
|
|
|
} |
187
|
|
|
) |
188
|
|
|
|
189
|
|
|
channel_cls = _channel_type_map.get(data["type"], Channel) |
190
|
|
|
return channel_cls.from_dict(data) |
191
|
|
|
|
192
|
|
|
@overload |
193
|
|
|
async def edit( |
194
|
|
|
self, *, name: str = None, |
195
|
|
|
type: ChannelType = None, |
|
|
|
|
196
|
|
|
position: int = None, topic: str = None, nsfw: bool = None, |
197
|
|
|
rate_limit_per_user: int = None, bitrate: int = None, |
198
|
|
|
user_limit: int = None, |
199
|
|
|
permissions_overwrites: List[Overwrite] = None, |
200
|
|
|
parent_id: Snowflake = None, rtc_region: str = None, |
201
|
|
|
video_quality_mod: int = None, |
202
|
|
|
default_auto_archive_duration: int = None |
203
|
|
|
) -> Channel: |
204
|
|
|
... |
205
|
|
|
|
206
|
|
|
async def edit(self, **kwargs): |
|
|
|
|
207
|
|
|
data = await self._http.patch(f"channels/{self.id}", kwargs) |
208
|
|
|
data.update( |
209
|
|
|
{ |
210
|
|
|
"_client": self._client, |
211
|
|
|
"_http": self._http, |
212
|
|
|
"type": ChannelType(data.pop("type")) |
213
|
|
|
} |
214
|
|
|
) |
215
|
|
|
channel_cls = _channel_type_map.get(data["type"], Channel) |
216
|
|
|
return channel_cls.from_dict(data) |
217
|
|
|
|
218
|
|
|
def __str__(self): |
219
|
|
|
"""return the discord tag when object gets used as a string.""" |
220
|
|
|
return self.name or str(self.id) |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
class TextChannel(Channel): |
|
|
|
|
224
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
|
225
|
|
|
super().__init__(*args, **kwargs) |
226
|
|
|
|
227
|
|
|
@overload |
228
|
|
|
async def edit( |
|
|
|
|
229
|
|
|
self, name: str = None, type: ChannelType = None, |
|
|
|
|
230
|
|
|
position: int = None, topic: str = None, nsfw: bool = None, |
231
|
|
|
rate_limit_per_user: int = None, |
232
|
|
|
permissions_overwrites: List[Overwrite] = None, |
233
|
|
|
parent_id: Snowflake = None, |
234
|
|
|
default_auto_archive_duration: int = None |
235
|
|
|
) -> Union[TextChannel, NewsChannel]: |
236
|
|
|
... |
237
|
|
|
|
238
|
|
|
async def edit(self, **kwargs): |
239
|
|
|
return await super().edit(**kwargs) |
240
|
|
|
|
241
|
|
|
@property |
242
|
|
|
def mention(self) -> str: |
243
|
|
|
"""Return a channel mention.""" |
244
|
|
|
return f"<#{self.id}>" |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
class VoiceChannel(Channel): |
|
|
|
|
248
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
|
249
|
|
|
super().__init__(*args, **kwargs) |
250
|
|
|
|
251
|
|
|
@overload |
252
|
|
|
async def edit( |
|
|
|
|
253
|
|
|
self, name: str = None, position: int = None, bitrate: int = None, |
254
|
|
|
user_limit: int = None, |
255
|
|
|
permissions_overwrites: List[Overwrite] = None, |
256
|
|
|
rtc_region: str = None, video_quality_mod: int = None |
257
|
|
|
) -> VoiceChannel: |
258
|
|
|
... |
259
|
|
|
|
260
|
|
|
async def edit(self, **kwargs): |
261
|
|
|
return await super().edit(**kwargs) |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
class CategoryChannel(Channel): |
|
|
|
|
265
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
|
266
|
|
|
super().__init__(*args, **kwargs) |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
class NewsChannel(Channel): |
|
|
|
|
270
|
|
|
def __init__(self, *args, **kwargs): |
|
|
|
|
271
|
|
|
super().__init__(*args, **kwargs) |
272
|
|
|
|
273
|
|
|
@overload |
274
|
|
|
async def edit( |
|
|
|
|
275
|
|
|
self, name: str = None, type: ChannelType = None, |
|
|
|
|
276
|
|
|
position: int = None, topic: str = None, nsfw: bool = None, |
277
|
|
|
permissions_overwrites: List[Overwrite] = None, |
278
|
|
|
parent_id: Snowflake = None, |
279
|
|
|
default_auto_archive_duration: int = None |
280
|
|
|
) -> Union[TextChannel, NewsChannel]: |
281
|
|
|
... |
282
|
|
|
|
283
|
|
|
async def edit(self, **kwargs): |
284
|
|
|
return await super().edit(**kwargs) |
285
|
|
|
|
286
|
|
|
|
287
|
|
|
@dataclass |
288
|
|
|
class ChannelMention(APIObject): |
289
|
|
|
""" |
290
|
|
|
Represents a Discord Channel Mention object |
291
|
|
|
|
292
|
|
|
:param id: |
293
|
|
|
id of the channel |
294
|
|
|
|
295
|
|
|
:param guild_id: |
296
|
|
|
id of the guild containing the channel |
297
|
|
|
|
298
|
|
|
:param type: |
299
|
|
|
the type of channel |
300
|
|
|
|
301
|
|
|
:param name: |
302
|
|
|
the name of the channel |
303
|
|
|
""" |
304
|
|
|
id: Snowflake |
|
|
|
|
305
|
|
|
guild_id: Snowflake |
306
|
|
|
type: ChannelType |
307
|
|
|
name: str |
308
|
|
|
|
309
|
|
|
|
310
|
|
|
_channel_type_map: Dict[ChannelType, Channel] = { |
311
|
|
|
ChannelType.GUILD_TEXT: TextChannel, |
312
|
|
|
ChannelType.GUILD_VOICE: VoiceChannel, |
313
|
|
|
ChannelType.GUILD_CATEGORY: CategoryChannel, |
314
|
|
|
ChannelType.GUILD_NEWS: NewsChannel |
315
|
|
|
} |
316
|
|
|
|