Passed
Push — main ( 515ac2...5218dd )
by
unknown
01:49
created

pincer.objects.user_message   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 344
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
eloc 132
dl 0
loc 344
rs 10
c 0
b 0
f 0

1 Method

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