Completed
Push — main ( 3a3ea0...93fa25 )
by Yohann
23s queued 12s
created

pincer.objects.message.user_message   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 144
dl 0
loc 341
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A UserMessage.__post_init__() 0 43 1
1
# Copyright Pincer 2021-Present
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
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, Enum
8
from typing import List, Optional, Union, TYPE_CHECKING
9
10
from ..app.application import Application
11
from ..app.interaction_base import MessageInteraction
12
from ..guild.channel import Channel, ChannelMention
13
from ..guild.member import GuildMember
14
from ..guild.role import Role
15
from ..message.attachment import Attachment
16
from ..message.embed import Embed
17
from ..message.component import MessageComponent
18
from ..message.reference import MessageReference
19
from ..message.reaction import Reaction
20
from ..message.sticker import StickerItem
21
from ..user import User
22
from ..._config import GatewayConfig
23
from ...core.http import HTTPClient
24
from ...utils.api_object import APIObject
25
from ...utils.conversion import convert
26
from ...utils.snowflake import Snowflake
27
from ...utils.timestamp import Timestamp
0 ignored issues
show
Bug introduced by
The name timestamp does not seem to exist in module pincer.utils.
Loading history...
introduced by
Cannot import 'utils.timestamp' due to syntax error 'invalid syntax (<unknown>, line 70)'
Loading history...
28
from ...utils.types import MISSING
29
30
if TYPE_CHECKING:
31
    from ... import Client
32
    from ...utils import APINullable
33
34
35
class MessageActivityType(IntEnum):
36
    """
37
    The activity people can perform on a rich presence activity.
38
39
    Such an activity could for example be a spotify listen.
40
    """
41
    JOIN = 1
42
    SPECTATE = 2
43
    LISTEN = 3
44
    JOIN_REQUEST = 5
45
46
47
class MessageFlags(IntEnum):
48
    """
49
    Special message properties.
50
51
    :param CROSSPOSTED:
52
        the message has been published to subscribed
53
        channels (via Channel Following)
54
55
    :param IS_CROSSPOST:
56
        this message originated from a message
57
        in another channel (via Channel Following)
58
59
    :param SUPPRESS_EMBEDS:
60
        do not include any embeds when serializing this message
61
62
    :param SOURCE_MESSAGE_DELETED:
63
        the source message for this crosspost
64
        has been deleted (via Channel Following)
65
66
    :param URGENT:
67
        this message came from the urgent message system
68
69
    :param HAS_THREAD:
70
        this message has an associated thread,
71
        with the same id as the message
72
73
    :param EPHEMERAL:
74
        this message is only visible to the user
75
        who invoked the Interaction
76
77
    :param LOADING:
78
        this message is an Interaction
79
        Response and the bot is "thinking"
80
    """
81
    CROSSPOSTED = 1 << 0
82
    IS_CROSSPOST = 1 << 1
83
    SUPPRESS_EMBEDS = 1 << 2
84
    SOURCE_MESSAGE_DELETED = 1 << 3
85
    URGENT = 1 << 4
86
    HAS_THREAD = 1 << 5
87
    EPHEMERAL = 1 << 6
88
    LOADING = 1 << 7
89
90
91
class MessageType(IntEnum):
92
    """
93
    Represents the type of the message.
94
    """
95
    DEFAULT = 0
96
    RECIPIENT_ADD = 1
97
    RECIPIENT_REMOVE = 2
98
    CALL = 3
99
    CHANNEL_NAME_CHANGE = 4
100
    CHANNEL_ICON_CHANGE = 5
101
    CHANNEL_PINNED_MESSAGE = 6
102
    GUILD_MEMBER_JOIN = 7
103
    USER_PREMIUM_GUILD_SUBSCRIPTION = 8
104
    USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1 = 9
105
    USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2 = 10
106
    USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3 = 11
107
    CHANNEL_FOLLOW_ADD = 12
108
    GUILD_DISCOVERY_DISQUALIFIED = 14
109
    GUILD_DISCOVERY_REQUALIFIED = 15
110
    GUILD_DISCOVERY_GRACE_PERIOD_INITIAL_WARNING = 16
111
    GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING = 17
112
    THREAD_CREATED = 18
113
    REPLY = 19
114
    APPLICATION_COMMAND = 20
115
116
    if GatewayConfig.version < 8:
117
        REPLY = 0
118
        APPLICATION_COMMAND = 0
119
120
    if GatewayConfig.version >= 9:
121
        THREAD_STARTER_MESSAGE = 21
122
123
    GUILD_INVITE_REMINDER = 22
124
125
126
@dataclass
127
class MessageActivity(APIObject):
128
    """
129
    Represents a Discord Message Activity object
130
131
    :param type:
132
        type of message activity
133
134
    :param party_id:
135
        party_id from a Rich Presence event
136
    """
137
    type: MessageActivityType
138
    party_id: APINullable[str] = MISSING
139
140
141
class AllowedMentionTypes(str, Enum):
142
    """
143
    The allowed mentions.
144
145
    :param ROLES:
146
        Controls role mentions
147
148
    :param USERS:
149
        Controls user mentions
150
151
    :param EVERYONE:
152
        Controls @everyone and @here mentions
153
    """
154
    ROLES = "roles"
155
    USERS = "user"
156
    EVERYONE = "everyone"
157
158
159
@dataclass
0 ignored issues
show
best-practice introduced by
Too many instance attributes (32/7)
Loading history...
160
class UserMessage(APIObject):
161
    """
162
    Represents a message sent in a channel within Discord.
163
164
    :param _client:
165
166
    :param _http:
167
168
    :param id:
169
        id of the message
170
171
    :param channel_id:
172
        id of the channel the message was sent in
173
174
    :param guild_id:
175
        id of the guild the message was sent in
176
177
    :param author:
178
        the author of this message (not guaranteed to be a valid user)
179
180
    :param member:
181
        member properties for this message's author
182
183
    :param content:
184
        contents of the message
185
186
    :param timestamp:
187
        when this message was sent
188
189
    :param edited_timestamp:
190
        when this message was edited (or null if never)
191
192
    :param tts:
193
        whether this was a TTS message
194
195
    :param mention_everyone:
196
        whether this message mentions everyone
197
198
    :param mentions:
199
        users specifically mentioned in the message
200
201
    :param mention_roles:
202
        roles specifically mentioned in this message
203
204
    :param mention_channels:
205
        channels specifically mentioned in this message
206
207
    :param attachments:
208
        any attached files
209
210
    :param embeds:
211
        any embedded content
212
213
    :param reactions:
214
        reactions to the message
215
216
    :param nonce:
217
        user for validating a message was sent
218
219
    :param pinned:
220
        whether this message is pinned
221
222
    :param webhook_id:
223
        if the message is generated by a webhook,
224
        this is the webhook's id
225
226
    :param type:
227
        type of message
228
229
    :param activity:
230
        sent with Rich Presence-related chat embeds
231
232
    :param application:
233
        sent with Rich Presence-related chat embeds
234
235
    :param application_id:
236
        if the message is a response to an Interaction,
237
        this is the id of the interaction's application
238
239
    :param message_reference:
240
        data showing the source of a crosspost,
241
        channel follow add, pin, or reply message
242
243
    :param flags:
244
        message flags combined as a bitfield
245
246
    :param referenced_message:
247
        the message associated with the message_reference
248
249
    :param interaction:
250
        sent if the message is a response to an Interaction
251
252
    :param thread:
253
        the thread that was started from this message,
254
        includes thread member object
255
256
    :param components:
257
        sent if the message contains components like buttons,
258
        action rows, or other interactive components
259
260
    :param sticker_items:
261
        sent if the message contains stickers
262
    """
263
    _client: Client
264
    _http: HTTPClient
265
266
    id: Snowflake
267
    channel_id: Snowflake
268
    author: User
269
    content: str
270
    timestamp: Timestamp
271
    edited_timestamp: Optional[Timestamp]
272
    tts: bool
273
    mention_everyone: bool
274
    mentions: List[User]
275
    mention_roles: List[Role]
276
    attachments: List[Attachment]
277
    embeds: List[Embed]
278
    pinned: bool
279
    type: MessageType
280
281
    mention_channels: APINullable[List[ChannelMention]] = MISSING
282
    guild_id: APINullable[Snowflake] = MISSING
283
    member: APINullable[GuildMember] = MISSING
284
    reactions: APINullable[List[Reaction]] = MISSING
285
    nonce: APINullable[Union[int, str]] = MISSING
286
    webhook_id: APINullable[Snowflake] = MISSING
287
    activity: APINullable[MessageActivity] = MISSING
288
    application: APINullable[Application] = MISSING
289
    application_id: APINullable[Snowflake] = MISSING
290
    message_reference: APINullable[MessageReference] = MISSING
291
    flags: APINullable[MessageFlags] = MISSING
292
    referenced_message: APINullable[Optional[UserMessage]] = MISSING
293
    interaction: APINullable[MessageInteraction] = MISSING
294
    thread: APINullable[Channel] = MISSING
295
    components: APINullable[List[MessageComponent]] = MISSING
296
    sticker_items: APINullable[List[StickerItem]] = MISSING
297
298
    def __post_init__(self):
299
        self.id = convert(self.id, Snowflake.from_string)
300
        self.channel_id = convert(self.channel_id, Snowflake.from_string)
301
        self.author = convert(self.author, User.from_dict, client=self._client)
302
        self.timestamp = convert(self.timestamp, Timestamp)
303
        self.edited_timestamp = convert(self.edited_timestamp, Timestamp)
304
        self.mentions = convert(self.mentions, User.from_dict,
305
                                client=self._client)
306
        self.mention_roles = convert(self.mention_roles, Role.from_dict)
307
        self.attachments = convert(self.attachments, Attachment.from_dict)
308
        self.embeds = convert(self.embeds, Embed.from_dict)
309
        self.mention_channels = convert(
310
            self.mention_channels,
311
            ChannelMention.from_dict
312
        )
313
        self.guild_id = convert(self.guild_id, Snowflake.from_string)
314
        self.member = convert(self.member, GuildMember.from_dict,
315
                              client=self._client)
316
        self.reactions = convert(self.reactions, Reaction.from_dict)
317
        self.webhook_id = convert(self.webhook_id, Snowflake.from_string)
318
        self.activity = convert(self.activity, MessageActivity.from_dict)
319
        self.application = convert(self.application, Application.from_dict)
320
        self.application_id = convert(
321
            self.application_id,
322
            Snowflake.from_string
323
        )
324
        self.message_reference = convert(
325
            self.message_reference,
326
            MessageReference.from_dict
327
        )
328
        # self.flags = convert(self.flags, MessageFlags.from_bytes)
329
        # self.referenced_message = convert(
330
        #     self.referenced_message,
331
        #     Message.from_dict
332
        # )
333
        self.interaction = convert(
334
            self.interaction,
335
            MessageInteraction.from_dict
336
        )
337
        self.thread = convert(self.thread, Channel.from_dict,
338
                              client=self._client)
339
        self.components = convert(self.components, MessageComponent.from_dict)
340
        self.sticker_items = convert(self.sticker_items, StickerItem.from_dict)
341