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

Interaction.__post_init__()   A

Complexity

Conditions 1

Size

Total Lines 33
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 33
rs 9.256
c 0
b 0
f 0
cc 1
nop 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
8
from typing import Dict, TYPE_CHECKING
9
10
from .command import AppCommandInteractionDataOption
11
from .interaction_base import InteractionType
12
from ..app.select_menu import SelectOption
13
from ..guild.member import GuildMember
14
from ..message.context import MessageContext
15
from ..message.user_message import UserMessage
16
from ..user import User
17
from ...core.http import HTTPClient
18
from ...utils import APIObject, MISSING, Snowflake, convert
19
20
if TYPE_CHECKING:
21
    from ..guild.channel import Channel
22
    from ..guild.role import Role
23
    from ... import Client
24
    from ...utils import APINullable
25
26
27
class InteractionFlags(IntEnum):
28
    """
29
    :param EPHEMERAL:
30
        only the user receiving the message can see it
31
    """
32
    EPHEMERAL = 1 << 6
33
34
35
@dataclass
36
class ResolvedData(APIObject):
37
    """
38
    Represents a Discord Resolved Data structure
39
40
    :param users:
41
        Map of Snowflakes to user objects
42
43
    :param members:
44
        Map of Snowflakes to partial member objects
45
46
    :param roles:
47
        Map of Snowflakes to role objects
48
49
    :param channels:
50
        Map of Snowflakes to partial channel objects
51
52
    :param messages:
53
        Map of Snowflakes to partial message objects
54
    """
55
    users: APINullable[Dict[Snowflake, User]] = MISSING
0 ignored issues
show
introduced by
The variable APINullable does not seem to be defined in case TYPE_CHECKING on line 20 is False. Are you sure this can never be the case?
Loading history...
56
    members: APINullable[Dict[Snowflake, GuildMember]] = MISSING
57
    roles: APINullable[Dict[Snowflake, Role]] = MISSING
0 ignored issues
show
introduced by
The variable Role does not seem to be defined in case TYPE_CHECKING on line 20 is False. Are you sure this can never be the case?
Loading history...
58
    channels: APINullable[Dict[Snowflake, Channel]] = MISSING
0 ignored issues
show
introduced by
The variable Channel does not seem to be defined in case TYPE_CHECKING on line 20 is False. Are you sure this can never be the case?
Loading history...
59
    messages: APINullable[Dict[Snowflake, UserMessage]] = MISSING
60
61
62
@dataclass
0 ignored issues
show
best-practice introduced by
Too many instance attributes (9/7)
Loading history...
63
class InteractionData(APIObject):
64
    """
65
    Represents a Discord Interaction Data structure
66
67
    :param id:
68
        the `ID` of the invoked command
69
70
    :param name:
71
        the `name` of the invoked command
72
73
    :param type:
74
        the `type` of the invoked command
75
76
    :param resolved:
77
        converted users + roles + channels
78
79
    :param options:
80
        the params + values from the user
81
82
    :param custom_id:
83
        the `custom_id` of the component
84
85
    :param component_type:
86
        the type of the component
87
88
    :param values:
89
        the values the user selected
90
91
    :param target_id:
92
        id of the user or message targeted by a user or message command
93
    """
94
    id: Snowflake
95
    name: str
96
    type: int
97
98
    resolved: APINullable[ResolvedData] = MISSING
0 ignored issues
show
introduced by
The variable APINullable does not seem to be defined in case TYPE_CHECKING on line 20 is False. Are you sure this can never be the case?
Loading history...
99
    options: APINullable[AppCommandInteractionDataOption] = MISSING
100
    custom_id: APINullable[str] = MISSING
101
    component_type: APINullable[int] = MISSING
102
    values: APINullable[SelectOption] = MISSING
103
    target_id: APINullable[Snowflake] = MISSING
104
105
    def __post_init__(self):
106
        self.id = convert(self.id, Snowflake.from_string)
107
        self.resolved = convert(
108
            self.resolved,
109
            ResolvedData.from_dict,
110
            ResolvedData
111
        )
112
        self.options = convert(
113
            self.options,
114
            AppCommandInteractionDataOption.from_dict,
115
            AppCommandInteractionDataOption
116
        )
117
        self.values = convert(self.values, SelectOption.from_dict, SelectOption)
118
        self.target_id = convert(self.target_id, Snowflake.from_string)
119
120
121
@dataclass
0 ignored issues
show
best-practice introduced by
Too many instance attributes (13/7)
Loading history...
122
class Interaction(APIObject):
123
    """
124
    Represents a Discord Interaction object
125
126
    :param _client:
127
128
    :param _http:
129
130
    :param id:
131
        id of the interaction
132
133
    :param application_id:
134
        id of the application this interaction is for
135
136
    :param type:
137
        the type of interaction
138
139
    :param data:
140
        the command data payload
141
142
    :param guild_id:
143
        the guild it was sent from
144
145
    :param channel_id:
146
        the channel it was sent from
147
148
    :param member:
149
        guild member data for the invoking user, including permissions
150
151
    :param user:
152
        user object for the invoking user, if invoked in a DM
153
154
    :param token:
155
        a continuation token for responding to the interaction
156
157
    :param version:
158
        read-only property, always `1`
159
160
    :param message:
161
        for components, the message they were attached to
162
    """
163
164
    _client: Client
0 ignored issues
show
introduced by
The variable Client does not seem to be defined in case TYPE_CHECKING on line 20 is False. Are you sure this can never be the case?
Loading history...
165
    _http: HTTPClient
166
167
    id: Snowflake
168
    application_id: Snowflake
169
    type: InteractionType
170
    token: str
171
172
    version: int = 1
173
    data: APINullable[InteractionData] = MISSING
0 ignored issues
show
introduced by
The variable APINullable does not seem to be defined in case TYPE_CHECKING on line 20 is False. Are you sure this can never be the case?
Loading history...
174
    guild_id: APINullable[Snowflake] = MISSING
175
    channel_id: APINullable[Snowflake] = MISSING
176
    member: APINullable[GuildMember] = MISSING
177
    user: APINullable[User] = MISSING
178
    message: APINullable[UserMessage] = MISSING
179
180
    def __post_init__(self):
181
        self.id = convert(self.id, Snowflake.from_string)
182
        self.application_id = convert(
183
            self.application_id, Snowflake.from_string
184
        )
185
        self.type = convert(self.type, InteractionType)
186
        self.data = convert(
187
            self.data,
188
            InteractionData.from_dict,
189
            InteractionData
190
        )
191
        self.guild_id = convert(self.guild_id, Snowflake.from_string)
192
        self.channel_id = convert(self.channel_id, Snowflake.from_string)
193
194
        self.member = convert(
195
            self.member,
196
            GuildMember.from_dict,
197
            GuildMember,
198
            client=self._client
199
        )
200
201
        self.user = convert(
202
            self.user,
203
            User.from_dict,
204
            User,
205
            client=self._client
206
        )
207
208
        self.message = convert(
209
            self.message,
210
            UserMessage.from_dict,
211
            UserMessage,
212
            client=self._client
213
        )
214
215
    def convert_to_message_context(self, command):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
216
        return MessageContext(
217
            self.id,
218
            self.member or self.user,
219
            command,
220
            self.guild_id,
221
            self.channel_id
222
        )
223