|
1
|
|
|
# Copyright Pincer 2021-Present |
|
|
|
|
|
|
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 asyncio import gather, iscoroutine |
|
11
|
|
|
|
|
12
|
|
|
from .command import AppCommandInteractionDataOption, AppCommandOptionType |
|
13
|
|
|
from .interaction_base import InteractionType |
|
14
|
|
|
from ..app.select_menu import SelectOption |
|
15
|
|
|
from ..guild.member import GuildMember |
|
16
|
|
|
from ..message.context import MessageContext |
|
17
|
|
|
from ..message.user_message import UserMessage |
|
18
|
|
|
from ..user import User |
|
19
|
|
|
from ...core.http import HTTPClient |
|
20
|
|
|
from ...utils import APIObject, MISSING, Snowflake, convert |
|
21
|
|
|
|
|
22
|
|
|
if TYPE_CHECKING: |
|
23
|
|
|
from ..guild.channel import Channel |
|
24
|
|
|
from ..guild.role import Role |
|
25
|
|
|
from ... import Client |
|
26
|
|
|
from ...utils import APINullable |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
class InteractionFlags(IntEnum): |
|
30
|
|
|
""" |
|
31
|
|
|
:param EPHEMERAL: |
|
32
|
|
|
only the user receiving the message can see it |
|
33
|
|
|
""" |
|
34
|
|
|
EPHEMERAL = 1 << 6 |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
@dataclass |
|
38
|
|
|
class ResolvedData(APIObject): |
|
39
|
|
|
""" |
|
40
|
|
|
Represents a Discord Resolved Data structure |
|
41
|
|
|
|
|
42
|
|
|
:param users: |
|
43
|
|
|
Map of Snowflakes to user objects |
|
44
|
|
|
|
|
45
|
|
|
:param members: |
|
46
|
|
|
Map of Snowflakes to partial member objects |
|
47
|
|
|
|
|
48
|
|
|
:param roles: |
|
49
|
|
|
Map of Snowflakes to role objects |
|
50
|
|
|
|
|
51
|
|
|
:param channels: |
|
52
|
|
|
Map of Snowflakes to partial channel objects |
|
53
|
|
|
|
|
54
|
|
|
:param messages: |
|
55
|
|
|
Map of Snowflakes to partial message objects |
|
56
|
|
|
""" |
|
57
|
|
|
users: APINullable[Dict[Snowflake, User]] = MISSING |
|
58
|
|
|
members: APINullable[Dict[Snowflake, GuildMember]] = MISSING |
|
59
|
|
|
roles: APINullable[Dict[Snowflake, Role]] = MISSING |
|
60
|
|
|
channels: APINullable[Dict[Snowflake, Channel]] = MISSING |
|
61
|
|
|
messages: APINullable[Dict[Snowflake, UserMessage]] = MISSING |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
@dataclass |
|
|
|
|
|
|
65
|
|
|
class InteractionData(APIObject): |
|
66
|
|
|
""" |
|
67
|
|
|
Represents a Discord Interaction Data structure |
|
68
|
|
|
|
|
69
|
|
|
:param id: |
|
70
|
|
|
the `ID` of the invoked command |
|
71
|
|
|
|
|
72
|
|
|
:param name: |
|
73
|
|
|
the `name` of the invoked command |
|
74
|
|
|
|
|
75
|
|
|
:param type: |
|
76
|
|
|
the `type` of the invoked command |
|
77
|
|
|
|
|
78
|
|
|
:param resolved: |
|
79
|
|
|
converted users + roles + channels |
|
80
|
|
|
|
|
81
|
|
|
:param options: |
|
82
|
|
|
the params + values from the user |
|
83
|
|
|
|
|
84
|
|
|
:param custom_id: |
|
85
|
|
|
the `custom_id` of the component |
|
86
|
|
|
|
|
87
|
|
|
:param component_type: |
|
88
|
|
|
the type of the component |
|
89
|
|
|
|
|
90
|
|
|
:param values: |
|
91
|
|
|
the values the user selected |
|
92
|
|
|
|
|
93
|
|
|
:param target_id: |
|
94
|
|
|
id of the user or message targeted by a user or message command |
|
95
|
|
|
""" |
|
96
|
|
|
id: Snowflake |
|
97
|
|
|
name: str |
|
98
|
|
|
type: int |
|
99
|
|
|
|
|
100
|
|
|
resolved: APINullable[ResolvedData] = MISSING |
|
101
|
|
|
options: APINullable[AppCommandInteractionDataOption] = MISSING |
|
102
|
|
|
custom_id: APINullable[str] = MISSING |
|
103
|
|
|
component_type: APINullable[int] = MISSING |
|
104
|
|
|
values: APINullable[SelectOption] = MISSING |
|
105
|
|
|
target_id: APINullable[Snowflake] = MISSING |
|
106
|
|
|
|
|
107
|
|
|
def __post_init__(self): |
|
108
|
|
|
self.id = convert(self.id, Snowflake.from_string) |
|
109
|
|
|
self.resolved = convert( |
|
110
|
|
|
self.resolved, |
|
111
|
|
|
ResolvedData.from_dict, |
|
112
|
|
|
ResolvedData |
|
113
|
|
|
) |
|
114
|
|
|
self.options = convert( |
|
115
|
|
|
self.options, |
|
116
|
|
|
AppCommandInteractionDataOption.from_dict, |
|
117
|
|
|
AppCommandInteractionDataOption |
|
118
|
|
|
) |
|
119
|
|
|
self.values = convert( |
|
120
|
|
|
self.values, |
|
121
|
|
|
SelectOption.from_dict, |
|
122
|
|
|
SelectOption |
|
123
|
|
|
) |
|
124
|
|
|
self.target_id = convert(self.target_id, Snowflake.from_string) |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
@dataclass |
|
|
|
|
|
|
128
|
|
|
class Interaction(APIObject): |
|
129
|
|
|
""" |
|
130
|
|
|
Represents a Discord Interaction object |
|
131
|
|
|
|
|
132
|
|
|
:param _client: |
|
133
|
|
|
|
|
134
|
|
|
:param _http: |
|
135
|
|
|
|
|
136
|
|
|
:param id: |
|
137
|
|
|
id of the interaction |
|
138
|
|
|
|
|
139
|
|
|
:param application_id: |
|
140
|
|
|
id of the application this interaction is for |
|
141
|
|
|
|
|
142
|
|
|
:param type: |
|
143
|
|
|
the type of interaction |
|
144
|
|
|
|
|
145
|
|
|
:param data: |
|
146
|
|
|
the command data payload |
|
147
|
|
|
|
|
148
|
|
|
:param guild_id: |
|
149
|
|
|
the guild it was sent from |
|
150
|
|
|
|
|
151
|
|
|
:param channel_id: |
|
152
|
|
|
the channel it was sent from |
|
153
|
|
|
|
|
154
|
|
|
:param member: |
|
155
|
|
|
guild member data for the invoking user, including permissions |
|
156
|
|
|
|
|
157
|
|
|
:param user: |
|
158
|
|
|
user object for the invoking user, if invoked in a DM |
|
159
|
|
|
|
|
160
|
|
|
:param token: |
|
161
|
|
|
a continuation token for responding to the interaction |
|
162
|
|
|
|
|
163
|
|
|
:param version: |
|
164
|
|
|
read-only property, always `1` |
|
165
|
|
|
|
|
166
|
|
|
:param message: |
|
167
|
|
|
for components, the message they were attached to |
|
168
|
|
|
""" |
|
169
|
|
|
|
|
170
|
|
|
_client: Client |
|
171
|
|
|
_http: HTTPClient |
|
172
|
|
|
|
|
173
|
|
|
id: Snowflake |
|
174
|
|
|
application_id: Snowflake |
|
175
|
|
|
type: InteractionType |
|
176
|
|
|
token: str |
|
177
|
|
|
|
|
178
|
|
|
version: int = 1 |
|
179
|
|
|
data: APINullable[InteractionData] = MISSING |
|
180
|
|
|
guild_id: APINullable[Snowflake] = MISSING |
|
181
|
|
|
channel_id: APINullable[Snowflake] = MISSING |
|
182
|
|
|
member: APINullable[GuildMember] = MISSING |
|
183
|
|
|
user: APINullable[User] = MISSING |
|
184
|
|
|
message: APINullable[UserMessage] = MISSING |
|
185
|
|
|
|
|
186
|
|
|
def __post_init__(self): |
|
187
|
|
|
self.id = convert(self.id, Snowflake.from_string) |
|
188
|
|
|
self.application_id = convert( |
|
189
|
|
|
self.application_id, Snowflake.from_string |
|
190
|
|
|
) |
|
191
|
|
|
self.type = convert(self.type, InteractionType) |
|
192
|
|
|
self.data = convert( |
|
193
|
|
|
self.data, |
|
194
|
|
|
InteractionData.from_dict, |
|
195
|
|
|
InteractionData |
|
196
|
|
|
) |
|
197
|
|
|
self.guild_id = convert(self.guild_id, Snowflake.from_string) |
|
198
|
|
|
self.channel_id = convert(self.channel_id, Snowflake.from_string) |
|
199
|
|
|
|
|
200
|
|
|
self.member = convert( |
|
201
|
|
|
self.member, |
|
202
|
|
|
GuildMember.from_dict, |
|
203
|
|
|
GuildMember, |
|
204
|
|
|
client=self._client |
|
205
|
|
|
) |
|
206
|
|
|
|
|
207
|
|
|
self.user = convert( |
|
208
|
|
|
self.user, |
|
209
|
|
|
User.from_dict, |
|
210
|
|
|
User, |
|
211
|
|
|
client=self._client |
|
212
|
|
|
) |
|
213
|
|
|
|
|
214
|
|
|
self.message = convert( |
|
215
|
|
|
self.message, |
|
216
|
|
|
UserMessage.from_dict, |
|
217
|
|
|
UserMessage, |
|
218
|
|
|
client=self._client |
|
219
|
|
|
) |
|
220
|
|
|
|
|
221
|
|
|
self._convert_functions = { |
|
222
|
|
|
AppCommandOptionType.SUB_COMMAND: None, |
|
223
|
|
|
AppCommandOptionType.SUB_COMMAND_GROUP: None, |
|
224
|
|
|
|
|
225
|
|
|
AppCommandOptionType.STRING: str, |
|
226
|
|
|
AppCommandOptionType.INTEGER: int, |
|
227
|
|
|
AppCommandOptionType.BOOLEAN: bool, |
|
228
|
|
|
AppCommandOptionType.NUMBER: float, |
|
229
|
|
|
|
|
230
|
|
|
AppCommandOptionType.USER: lambda value: |
|
231
|
|
|
self._client.get_user( |
|
|
|
|
|
|
232
|
|
|
convert(value, Snowflake.from_string) |
|
233
|
|
|
), |
|
234
|
|
|
AppCommandOptionType.CHANNEL: lambda value: |
|
235
|
|
|
self._client.get_channel( |
|
|
|
|
|
|
236
|
|
|
convert(value, Snowflake.from_string) |
|
237
|
|
|
), |
|
238
|
|
|
AppCommandOptionType.ROLE: lambda value: |
|
239
|
|
|
self._client.get_role( |
|
|
|
|
|
|
240
|
|
|
convert(self.guild_id, Snowflake.from_string), |
|
241
|
|
|
convert(value, Snowflake.from_string) |
|
242
|
|
|
), |
|
243
|
|
|
AppCommandOptionType.MENTIONABLE: None |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
async def build(self): |
|
247
|
|
|
""" |
|
248
|
|
|
Sets the parameters in the interaction that need information from the |
|
249
|
|
|
discord API. |
|
250
|
|
|
""" |
|
251
|
|
|
|
|
252
|
|
|
if not self.data.options: |
|
253
|
|
|
return |
|
254
|
|
|
|
|
255
|
|
|
await gather( |
|
256
|
|
|
*map(self.convert, self.data.options) |
|
257
|
|
|
) |
|
258
|
|
|
|
|
259
|
|
|
async def convert(self, option: AppCommandInteractionDataOption): |
|
260
|
|
|
""" |
|
261
|
|
|
Sets an AppCommandInteractionDataOption value paramater to the payload |
|
262
|
|
|
type |
|
263
|
|
|
""" |
|
264
|
|
|
|
|
265
|
|
|
converter = self._convert_functions.get(option.type) |
|
266
|
|
|
|
|
267
|
|
|
if not converter: |
|
268
|
|
|
raise NotImplementedError( |
|
269
|
|
|
f"Handling for AppCommandOptionType {option.type} is not " |
|
270
|
|
|
"implemented" |
|
271
|
|
|
) |
|
272
|
|
|
|
|
273
|
|
|
res = converter(option.value) |
|
274
|
|
|
|
|
275
|
|
|
if iscoroutine(res): |
|
276
|
|
|
option.value = await res |
|
277
|
|
|
return |
|
278
|
|
|
|
|
279
|
|
|
option.value = res |
|
280
|
|
|
|
|
281
|
|
|
def convert_to_message_context(self, command): |
|
|
|
|
|
|
282
|
|
|
return MessageContext( |
|
283
|
|
|
self.id, |
|
284
|
|
|
self.member or self.user, |
|
285
|
|
|
command, |
|
286
|
|
|
self.guild_id, |
|
287
|
|
|
self.channel_id |
|
288
|
|
|
) |
|
289
|
|
|
|