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