/
__init__.py
381 lines (330 loc) · 12.4 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import datetime
import typing
from copy import deepcopy
from os import urandom
import interactions
from interactions import (
UPLOADABLE_TYPE,
AllowedMentions,
Attachment,
BaseComponent,
Client,
Embed,
Message,
MessageFlags,
MessageReference,
SlashContext,
Snowflake_Type,
Sticker,
models,
process_message_payload,
to_snowflake,
)
from interactions.api.http.http_client import HTTPClient
def random_snowflake() -> int:
timestamp = int(
(datetime.datetime.now() - datetime.datetime(2015, 1, 1)).total_seconds() * 1000
)
worker = int("0x" + urandom(5).hex(), 0)
process = int("0x" + urandom(5).hex(), 0)
increment = int("0x" + urandom(12).hex(), 0)
return int((timestamp << 22) | (worker << 17) | (process << 12) | increment)
class FakeSlashContext(SlashContext):
"""
A fake SlashContext class for testing
This class is used to simulate a SlashContext object for testing purposes.
It will avoid calling the Discord API and instead store the actions that would be taken in a list.
It is meant to be used with the other fake classes in this module.
"""
__slots__ = ("actions", "_fake_cache", "http")
def __init__(self, client: "interactions.Client"):
self.actions = client.actions
self._fake_cache = client._fake_cache
super().__init__(client)
self.http = self
async def defer(self, *, ephemeral: bool = False) -> None:
"""
Defer the interaction.
Args:
ephemeral: Whether the interaction response should be ephemeral.
"""
self.deferred = True
self.ephemeral = ephemeral
self.actions += ({"action": "defer", "ephemeral": ephemeral},)
async def send(
self,
content: typing.Optional[str] = None,
*,
embeds: typing.Optional[
typing.Union[
typing.Iterable[typing.Union["Embed", dict]],
typing.Union["Embed", dict],
]
] = None,
embed: typing.Optional[typing.Union["Embed", dict]] = None,
components: typing.Optional[
typing.Union[
typing.Iterable[typing.Iterable[typing.Union["BaseComponent", dict]]],
typing.Iterable[typing.Union["BaseComponent", dict]],
"BaseComponent",
dict,
]
] = None,
stickers: typing.Optional[
typing.Union[
typing.Iterable[typing.Union["Sticker", "Snowflake_Type"]],
"Sticker",
"Snowflake_Type",
]
] = None,
allowed_mentions: typing.Optional[typing.Union["AllowedMentions", dict]] = None,
reply_to: typing.Optional[
typing.Union["MessageReference", "Message", dict, "Snowflake_Type"]
] = None,
files: typing.Optional[
typing.Union["UPLOADABLE_TYPE", typing.Iterable["UPLOADABLE_TYPE"]]
] = None,
file: typing.Optional["UPLOADABLE_TYPE"] = None,
tts: bool = False,
suppress_embeds: bool = False,
silent: bool = False,
flags: typing.Optional[typing.Union[int, "MessageFlags"]] = None,
delete_after: typing.Optional[float] = None,
ephemeral: bool = False,
**kwargs: typing.Any,
) -> "Message":
"""
Send a message.
Args:
content: Message text content.
embeds: Embedded rich content (up to 6000 characters).
embed: Embedded rich content (up to 6000 characters).
components: The components to include with the message.
stickers: IDs of up to 3 stickers in the server to send in the message.
allowed_mentions: Allowed mentions for the message.
reply_to: Message to reference, must be from the same channel.
files: Files to send, the path, bytes or File() instance,
defaults to None. You may have up to 10 files.
file: Files to send, the path, bytes or File() instance,
defaults to None. You may have up to 10 files.
tts: Should this message use Text To Speech.
suppress_embeds: Should embeds be suppressed on this send
silent: Should this message be sent without triggering a notification.
flags: Message flags to apply.
delete_after: Delete message after this many seconds.
ephemeral: Whether the response should be ephemeral
Returns:
New message object that was sent.
"""
flags = MessageFlags(flags or 0)
if ephemeral or self.ephemeral:
flags |= MessageFlags.EPHEMERAL
self.ephemeral = True
if suppress_embeds:
flags |= MessageFlags.SUPPRESS_EMBEDS
if silent:
flags |= MessageFlags.SILENT
if not flags:
flags = MessageFlags(0)
if suppress_embeds:
if isinstance(flags, int):
flags = MessageFlags(flags)
flags = flags | MessageFlags.SUPPRESS_EMBEDS
if silent:
if isinstance(flags, int):
flags = MessageFlags(flags)
flags = flags | MessageFlags.SILENT
if (
files
and (
isinstance(files, typing.Iterable)
and any(
isinstance(file, interactions.models.discord.message.Attachment)
for file in files
)
)
or isinstance(files, interactions.models.discord.message.Attachment)
):
raise ValueError(
"Attachments are not files. "
"Attachments only contain metadata about the file, "
"not the file itself - to send an attachment, "
"you need to download it first. Check Attachment.url"
)
message_payload = models.discord.message.process_message_payload(
content=content,
embeds=embeds or embed,
components=components,
stickers=stickers,
allowed_mentions=allowed_mentions,
reply_to=reply_to,
tts=tts,
flags=flags,
**kwargs,
)
message_data = deepcopy(message_payload)
message_data["id"] = random_snowflake()
if "embeds" in message_data:
message_data["embeds"] = [
embed.to_dict() if isinstance(embed, Embed) else embed
for embed in message_data["embeds"]
]
if message_data:
self.actions += ({"action": "send", "message": message_data},)
message = Message.from_dict(deepcopy(message_data), self.client)
self._fake_cache[message.id] = message
return message
respond = send
async def delete(self, message: "Snowflake_Type" = "@original") -> None:
"""
Delete a message sent in response to this interaction.
Args:
message: The message id to delete.
"""
self.actions += (
{
"action": "delete",
"message_id": (
to_snowflake(message) if message != "@original" else message
),
},
)
del self._fake_cache[
to_snowflake(message) if message != "@original" else message
]
async def edit(
self,
message: "Snowflake_Type" = "@original",
*,
content: typing.Optional[str] = None,
embeds: typing.Optional[
typing.Union[
typing.Iterable[typing.Union["Embed", dict]],
typing.Union["Embed", dict],
]
] = None,
embed: typing.Optional[typing.Union["Embed", dict]] = None,
components: typing.Optional[
typing.Union[
typing.Iterable[typing.Iterable[typing.Union["BaseComponent", dict]]],
typing.Iterable[typing.Union["BaseComponent", dict]],
"BaseComponent",
dict,
]
] = None,
attachments: typing.Optional[typing.Sequence[Attachment | dict]] = None,
allowed_mentions: typing.Optional[typing.Union["AllowedMentions", dict]] = None,
files: typing.Optional[
typing.Union["UPLOADABLE_TYPE", typing.Iterable["UPLOADABLE_TYPE"]]
] = None,
file: typing.Optional["UPLOADABLE_TYPE"] = None,
tts: bool = False,
) -> "interactions.Message":
message_payload = process_message_payload(
content=content,
embeds=embeds or embed,
components=components,
allowed_mentions=allowed_mentions,
attachments=attachments,
tts=tts,
)
self._fake_cache[
to_snowflake(message) if message != "@original" else message
].update_from_dict(message_payload)
message_data = deepcopy(
self._fake_cache[
to_snowflake(message) if message != "@original" else message
].to_dict()
)
message_data["id"] = (
to_snowflake(message) if message != "@original" else message
)
if "embeds" in message_data:
message_data["embeds"] = [
embed.to_dict() if isinstance(embed, Embed) else embed
for embed in message_data["embeds"]
]
if message_data:
self.actions += ({"action": "edit", "message": message_data},)
self._fake_cache[message_data["id"]] = Message.from_dict(
deepcopy(message_data), self.client
)
return Message.from_dict(deepcopy(message_data), self.client)
class FakeClient(Client):
"""
A fake Client class for testing
This class is used to simulate a Client object for testing purposes.
It will override the HTTPClient object with a FakeHttp object
to bypass the discord api and store the actions that would be taken in a list.
"""
__slots__ = ("_fake_cache", "actions")
def __init__(self, *args, **kwargs):
self._fake_cache = {}
self.actions = ()
super().__init__(*args, **kwargs)
self.http = FakeHttp(client=self)
class FakeHttp(HTTPClient):
"""
A fake HTTPClient class for testing
This class will simulate any calls made to the discord api.
"""
def __init__(self, *args, client: FakeClient, **kwargs):
self.client = client
self.actions = self.client.actions
self._fake_cache = self.client._fake_cache
super().__init__(*args, **kwargs)
async def delete_message(
self,
channel_id: "Snowflake_Type",
message_id: "Snowflake_Type",
reason: str | None = None,
) -> None:
self.actions += ({"action": "delete", "message_id": to_snowflake(message_id)},)
del self._fake_cache[to_snowflake(message_id)]
async def edit_message(
self,
payload: dict,
channel_id: "Snowflake_Type",
message_id: "Snowflake_Type",
files: list["UPLOADABLE_TYPE"] | None = None,
) -> "Message":
message = self._fake_cache[to_snowflake(message_id)]
message.update_from_dict(payload)
self._fake_cache[to_snowflake(message_id)] = message
self.actions += ({"action": "edit", "message": message.to_dict()},)
return message
async def create_reaction(
self, channel_id: "Snowflake_Type", message_id: "Snowflake_Type", emoji: str
) -> None:
self._fake_cache[to_snowflake(message_id)].reactions.append(emoji)
self.actions += (
{
"action": "create_reaction",
"message_id": to_snowflake(message_id),
"emoji": emoji,
},
)
async def call_slash(
func: typing.Callable, *args, _client: FakeClient = None, **kwargs
):
"""
Call a slash command function with the given arguments.
:param func: The function to call.
:param _client: A FakeClient instance to use.
:param args: The positional arguments to pass to the function.
:param kwargs: The keyword arguments to pass to the function.
:return:
"""
# default_kwargs
client = FakeClient() or _client
client.add_interaction(func)
ctx = FakeSlashContext(client)
ctx.args = args
ctx.kwargs = kwargs
await func(ctx, *args, **kwargs)
return deepcopy(ctx.actions)
def get_client() -> FakeClient:
"""Returns a FakeClient instance.
:return: A FakeClient instance.
"""
return FakeClient()