Passed
Push — main ( 66ff12...0b8b26 )
by
unknown
01:27
created

pincer.objects.interactions.Interaction.to_context()   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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