Completed
Push — main ( 95099e...48f12d )
by Yohann
16s queued 12s
created

pincer.objects.channel.Channel.from_id()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nop 3
1
# -*- coding: utf-8 -*-
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# MIT License
3
#
4
# Copyright (c) 2021 Pincer
5
#
6
# Permission is hereby granted, free of charge, to any person obtaining
7
# a copy of this software and associated documentation files
8
# (the "Software"), to deal in the Software without restriction,
9
# including without limitation the rights to use, copy, modify, merge,
10
# publish, distribute, sublicense, and/or sell copies of the Software,
11
# and to permit persons to whom the Software is furnished to do so,
12
# subject to the following conditions:
13
#
14
# The above copyright notice and this permission notice shall be
15
# included in all copies or substantial portions of the Software.
16
#
17
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
from __future__ import annotations
25
from dataclasses import dataclass
26
from enum import IntEnum
27
from typing import Dict, Optional, List, TYPE_CHECKING
28
29
from .._config import GatewayConfig
30
from .guild_member import GuildMember
31
from .thread import ThreadMetadata
32
from .user import User
33
from .overwrite import Overwrite
34
from ..utils import APIObject, APINullable, MISSING, Snowflake, Timestamp
35
36
if TYPE_CHECKING:
37
    from pincer import Client
38
    from ..core.http import HTTPClient
39
40
41
42
43
class ChannelType(IntEnum):
44
    """Represents a channel its type."""
45
    GUILD_TEXT = 0
46
    DM = 1
47
    GUILD_VOICE = 2
48
    GROUP_DM = 3
49
    GUILD_CATEGORY = 4
50
    GUILD_NEWS = 5
51
    GUILD_STORE = 6
52
53
    if GatewayConfig.version >= 9:
54
        GUILD_NEWS_THREAD = 10
55
        GUILD_PUBLIC_THREAD = 11
56
        GUILD_PRIVATE_THREAD = 12
57
58
    GUILD_STAGE_VOICE = 13
59
60
61
@dataclass
0 ignored issues
show
best-practice introduced by
Too many instance attributes (28/7)
Loading history...
62
class Channel(APIObject):
63
    """
64
    Represents a Discord Channel Mention object
65
66
    :param id:
67
        the id of this channel
68
69
    :param type:
70
        the type of channel
71
72
    :param application_id:
73
        application id of the group DM creator if it is bot-created
74
75
    :param bitrate:
76
        the bitrate (in bits) of the voice channel
77
78
    :param default_auto_archive_duration:
79
        default duration for newly created threads, in minutes, to
80
        automatically archive the thread after recent activity, can be set to:
81
        60, 1440, 4320, 10080
82
83
    :param guild_id:
84
        the id of the guild (may be missing for some channel objects received
85
        over gateway guild dispatches)
86
87
    :param icon:
88
        icon hash
89
    :param last_message_id:
90
        the id of the last message sent in this channel (may not point to an
91
        existing or valid message)
92
93
    :param last_pin_timestamp:
94
        when the last pinned message was pinned. This may be null in events
95
        such as GUILD_CREATE when a message is not pinned.
96
97
    :param member:
98
        thread member object for the current user, if they have joined the
99
        thread, only included on certain API endpoints
100
101
    :param member_count:
102
        an approximate count of users in a thread, stops counting at 50
103
104
    :param message_count:
105
        an approximate count of messages in a thread, stops counting at 50
106
107
    :param name:
108
        the name of the channel (1-100 characters)
109
110
    :param nsfw:
111
        whether the channel is nsfw
112
113
    :param owner_id:
114
        id of the creator of the group DM or thread
115
116
    :param parent_id:
117
        for guild channels: id of the parent category for a channel (each
118
        parent category can contain up to 50 channels), for threads: id of the
119
        text channel this thread was created
120
121
    :param permissions:
122
        computed permissions for the invoking user in the channel, including
123
        overwrites, only included when part of the resolved data received on a
124
        slash command interaction
125
126
    :param permission_overwrites:
127
        explicit permission overwrites for members and roles
128
129
    :param position:
130
        sorting position of the channel
131
132
    :param rate_limit_per_user:
133
        amount of seconds a user has to wait before sending another message
134
        (0-21600); bots, as well as users with the permission manage_messages
135
        or manage_channel, are unaffected
136
137
    :param recipients:
138
        the recipients of the DM
139
140
    :param rtc_region:
141
        voice region id for the voice channel, automatic when set to null
142
143
    :param thread_metadata:
144
        thread-specific fields not needed by other channels
145
146
    :param topic:
147
        the channel topic (0-1024 characters)
148
149
    :param user_limit:
150
        the user limit of the voice channel
151
152
    :param video_quality_mode:
153
        the camera video quality mode of the voice channel, 1 when not present
154
    """
155
156
    _client: Client
0 ignored issues
show
introduced by
The variable Client does not seem to be defined in case TYPE_CHECKING on line 36 is False. Are you sure this can never be the case?
Loading history...
157
    _http: HTTPClient
0 ignored issues
show
introduced by
The variable HTTPClient does not seem to be defined in case TYPE_CHECKING on line 36 is False. Are you sure this can never be the case?
Loading history...
158
159
    id: Snowflake
160
    type: ChannelType
161
162
    application_id: APINullable[Snowflake] = MISSING
163
    bitrate: APINullable[int] = MISSING
164
    default_auto_archive_duration: APINullable[int] = MISSING
165
    guild_id: APINullable[Snowflake] = MISSING
166
    icon: APINullable[Optional[str]] = MISSING
167
168
    last_message_id: APINullable[Optional[Snowflake]] = MISSING
169
    last_pin_timestamp: APINullable[Optional[Timestamp]] = MISSING
170
    member: APINullable[GuildMember] = MISSING
171
172
    member_count: APINullable[int] = MISSING
173
174
    message_count: APINullable[int] = MISSING
175
176
    name: APINullable[str] = MISSING
177
    nsfw: APINullable[bool] = MISSING
178
    owner_id: APINullable[Snowflake] = MISSING
179
    parent_id: APINullable[Optional[Snowflake]] = MISSING
180
    permissions: APINullable[str] = MISSING
181
    permission_overwrites: APINullable[List[Overwrite]] = MISSING
182
    position: APINullable[int] = MISSING
183
    rate_limit_per_user: APINullable[int] = MISSING
184
    recipients: APINullable[List[User]] = MISSING
185
    rtc_region: APINullable[Optional[str]] = MISSING
186
    thread_metadata: APINullable[ThreadMetadata] = MISSING
187
    topic: APINullable[Optional[str]] = MISSING
188
    user_limit: APINullable[int] = MISSING
189
    video_quality_mode: APINullable[int] = MISSING
190
191
    @classmethod
192
    async def from_id(cls, client: Client, id: int) -> Channel:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Bug Best Practice introduced by
This seems to re-define the built-in id.

It is generally discouraged to redefine built-ins as this makes code very hard to read.

Loading history...
193
        data = (await client.http.get(f"/guilds/{id}")) or {}
194
        data.update({"_client": client, "_http": client.http, "type": ChannelType(data.pop("type"))})
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
195
        channel_cls = _channel_type_map.get(data["type"], Channel)
196
        return channel_cls.from_dict(data)
197
198
199
def __str__(self):
200
    """return the discord tag when object gets used as a string."""
201
    return self.name or str(self.id)
202
203
204
@dataclass
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
205
class TextChannel(Channel):
206
    pass
207
208
209
@dataclass
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
210
class VoiceChannel(Channel):
211
    pass
212
213
214
@dataclass
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
215
class CategoryChannel(Channel):
216
    pass
217
218
219
@dataclass
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
220
class NewsChannel(Channel):
221
    pass
222
223
224
225
@dataclass
226
class ChannelMention(APIObject):
227
    """
228
    Represents a Discord Channel Mention object
229
230
    :param id:
231
        id of the channel
232
233
    :param guild_id:
234
        id of the guild containing the channel
235
236
    :param type:
237
        the type of channel
238
239
    :param name:
240
        the name of the channel
241
    """
242
    id: Snowflake
243
    guild_id: Snowflake
244
    type: ChannelType
245
    name: str
246
247
248
_channel_type_map: Dict[ChannelType, Channel] = {
249
    ChannelType.GUILD_TEXT: TextChannel,
250
    ChannelType.GUILD_VOICE: VoiceChannel,
251
    ChannelType.GUILD_CATEGORY: CategoryChannel,
252
    ChannelType.GUILD_NEWS: NewsChannel
253
}
254