Passed
Pull Request — main (#167)
by
unknown
01:46
created

Interaction.__post_sent()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 1
nop 2
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 asyncio import gather, iscoroutine, sleep, ensure_future
7
from dataclasses import dataclass
8
from typing import Dict, TYPE_CHECKING, Union, Optional
9
10
from .command_types import AppCommandOptionType
11
from .interaction_base import InteractionType, CallbackType
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 ...exceptions import InteractionDoesNotExist, UseFollowup, \
18
    InteractionAlreadyAcknowledged, NotFoundError, InteractionTimedOut
19
from ...utils import APIObject, convert
20
from ...utils.convert_message import convert_message
21
from ...utils.snowflake import Snowflake
22
from ...utils.types import MISSING
23
24
if TYPE_CHECKING:
25
    from .interaction_flags import InteractionFlags
26
    from ...utils.convert_message import MessageConvertable
27
    from .command import AppCommandInteractionDataOption
28
    from ..message.message import Message
29
    from ..guild.channel import Channel
0 ignored issues
show
Bug introduced by
The name channel does not seem to exist in module pincer.objects.guild.
Loading history...
introduced by
Cannot import 'guild.channel' due to syntax error 'invalid syntax (<unknown>, line 235)'
Loading history...
30
    from ..guild.role import Role
31
    from ...utils import APINullable
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
105
@dataclass
0 ignored issues
show
best-practice introduced by
Too many instance attributes (14/7)
Loading history...
106
class Interaction(APIObject):
107
    """
108
    Represents a Discord Interaction object
109
110
    :param id:
111
        id of the interaction
112
113
    :param application_id:
114
        id of the application this interaction is for
115
116
    :param type:
117
        the type of interaction
118
119
    :param data:
120
        the command data payload
121
122
    :param guild_id:
123
        the guild it was sent from
124
125
    :param channel_id:
126
        the channel it was sent from
127
128
    :param member:
129
        guild member data for the invoking user, including permissions
130
131
    :param user:
132
        user object for the invoking user, if invoked in a DM
133
134
    :param token:
135
        a continuation token for responding to the interaction
136
137
    :param version:
138
        read-only property, always `1`
139
140
    :param message:
141
        for components, the message they were attached to
142
    """
143
    id: Snowflake
144
    application_id: Snowflake
145
    type: InteractionType
146
    token: str
147
148
    version: int = 1
149
    data: APINullable[InteractionData] = MISSING
150
    guild_id: APINullable[Snowflake] = MISSING
151
    channel_id: APINullable[Snowflake] = MISSING
152
    member: APINullable[GuildMember] = MISSING
153
    user: APINullable[User] = MISSING
154
    message: APINullable[UserMessage] = MISSING
155
    has_replied: bool = False
156
    has_acknowledged: bool = False
157
158
    def __post_init__(self):
159
        self.id = convert(self.id, Snowflake.from_string)
160
        self.application_id = convert(
161
            self.application_id, Snowflake.from_string
162
        )
163
        self.type = convert(self.type, InteractionType)
164
        self.data = convert(
165
            self.data,
166
            InteractionData.from_dict,
167
            InteractionData
168
        )
169
        self.guild_id = convert(self.guild_id, Snowflake.from_string)
170
        self.channel_id = convert(self.channel_id, Snowflake.from_string)
171
172
        self.member = convert(
173
            self.member,
174
            GuildMember.from_dict,
175
            GuildMember,
176
            client=self._client
177
        )
178
179
        self.user = convert(
180
            self.user,
181
            User.from_dict,
182
            User,
183
            client=self._client
184
        )
185
186
        self.message = convert(
187
            self.message,
188
            UserMessage.from_dict,
189
            UserMessage,
190
            client=self._client
191
        )
192
193
        self._convert_functions = {
194
            AppCommandOptionType.SUB_COMMAND: None,
195
            AppCommandOptionType.SUB_COMMAND_GROUP: None,
196
197
            AppCommandOptionType.STRING: str,
198
            AppCommandOptionType.INTEGER: int,
199
            AppCommandOptionType.BOOLEAN: bool,
200
            AppCommandOptionType.NUMBER: float,
201
202
            AppCommandOptionType.USER: lambda value:
203
            self._client.get_user(
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 27 spaces).
Loading history...
204
                convert(value, Snowflake.from_string)
205
            ),
206
            AppCommandOptionType.CHANNEL: lambda value:
207
            self._client.get_channel(
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 30 spaces).
Loading history...
208
                convert(value, Snowflake.from_string)
209
            ),
210
            AppCommandOptionType.ROLE: lambda value:
211
            self._client.get_role(
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation (add 27 spaces).
Loading history...
212
                convert(self.guild_id, Snowflake.from_string),
213
                convert(value, Snowflake.from_string)
214
            ),
215
            AppCommandOptionType.MENTIONABLE: None
216
        }
217
218
    async def build(self):
219
        """|coro|
220
221
        Sets the parameters in the interaction that need information
222
        from the discord API.
223
        """
224
225
        if not self.data.options:
226
            return
227
228
        await gather(
229
            *map(self.convert, self.data.options)
230
        )
231
232
    async def convert(self, option: AppCommandInteractionDataOption):
233
        """|coro|
234
235
        Sets an AppCommandInteractionDataOption value paramater to
236
        the payload type
237
        """
238
239
        converter = self._convert_functions.get(option.type)
240
241
        if not converter:
242
            raise NotImplementedError(
243
                f"Handling for AppCommandOptionType {option.type} is not "
244
                "implemented"
245
            )
246
247
        res = converter(option.value)
248
249
        option.value = (await res) if iscoroutine(res) else res
250
251
    def convert_to_message_context(self, command):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
252
        return MessageContext(
253
            self.member or self.user,
254
            command,
255
            self,
256
            self.guild_id,
257
            self.channel_id
258
        )
259
260
    async def ack(self, flags: Optional[InteractionFlags] = None):
261
        """Acknowledge an interaction, any flags here are applied to the
262
        reply.
263
264
        Parameters
265
        ----------
266
        flags :class:`~pincer.objects.app.interaction_flags.InteractionFlags`
267
            The flags which must be applied to the reply.
268
269
        Raises
270
        ------
271
        :class:`~pincer.exceptions.InteractionAlreadyAcknowledged`
272
            The interaction was already acknowledged, this can be
273
            because a reply or ack was already sent.
274
        """
275
        if self.has_replied or self.has_acknowledged:
276
            raise InteractionAlreadyAcknowledged(
277
                "The interaction you are trying to acknowledge has already "
278
                "been acknowledged"
279
            )
280
281
        self.has_acknowledged = True
282
        await self._http.post(
283
            f"interactions/{self.id}/{self.token}/callback",
284
            {
285
                "type": CallbackType.DEFERRED_MESSAGE,
286
                "data": {
287
                    "flags": flags
288
                }
289
            }
290
        )
291
292
    async def __post_send_handler(self, message: Message):
293
        """Process the interaction after it was sent.
294
295
        Parameters
296
        ----------
297
        message :class:`~pincer.objects.message.message.Message`
298
            The interaction message.
299
        """
300
301
        if message.delete_after:
302
            await sleep(message.delete_after)
303
            await self.delete()
304
305
    def __post_sent(self, message: Message):
306
        """Ensure the `__post_send_handler` method its future.
307
308
        Parameters
309
        ----------
310
        message :class:`~pincer.objects.message.message.Message`
311
            The interaction message.
312
        """
313
        self.has_replied = True
314
        ensure_future(self.__post_send_handler(message))
315
316
    async def reply(self, message: MessageConvertable):
317
        """|coro|
318
319
        Initial reply, only works if no ACK has been sent yet.
320
321
        Parameters
322
        ----------
323
        message :class:`~pincer.utils.convert_message.MessageConvertable`
324
            The response message!
325
326
        Raises
327
        ------
328
        :class:`~.pincer.errors.UseFollowup`
329
            Exception raised when a reply has already been sent so a
330
            :func:`~pincer.objects.app.interactions.Interaction.followup`
331
            should be used instead.
332
        :class:`~.pincer.errors.InteractionTimedOut`
333
            Exception raised when discord had to wait too long for a reply.
334
            You can extend the discord wait time by using the
335
            :func:`~pincer.objects.app.interaction.Interaction.ack`
336
            function.
337
        """
338
        if self.has_acknowledged and not self.has_replied:
0 ignored issues
show
unused-code introduced by
Unnecessary "elif" after "return"
Loading history...
339
            self.has_replied = True
340
            return await self.edit(message)
341
        elif self.has_replied:
342
            raise UseFollowup(
343
                "A response has already been sent to the interaction. "
344
                "Please use a followup instead!"
345
            )
346
347
        message = convert_message(self._client, message)
348
        content_type, data = message.serialize()
349
350
        try:
351
            await self._http.post(
352
                f"interactions/{self.id}/{self.token}/callback",
353
                {
354
                    "type": CallbackType.MESSAGE,
355
                    "data": data
356
                },
357
                content_type=content_type
358
            )
359
        except NotFoundError:
360
            raise InteractionTimedOut(
361
                "Discord had to wait too long for the interaction reply, "
362
                "you can extend the time it takes for discord to timeout by "
363
                "acknowledging the interaction. (using interaction.ack)"
364
            )
365
366
        self.__post_sent(message)
367
368
    async def edit(self, message: MessageConvertable) -> UserMessage:
369
        """|coro|
370
371
        Edit an interaction. This is also the way to reply to
372
        interactions whom have been acknowledged.
373
374
        Parameters
375
        ----------
376
        message :class:`~pincer.utils.convert_message.MessageConvertable`
377
            The new message!
378
379
        Returns
380
        -------
381
        :class:`~pincer.objects.message.user_message.UserMessage`
382
            The updated message object.
383
384
        Raises
385
        ------
386
        :class:`~.pincer.errors.InteractionDoesNotExist`
387
            Exception raised when no reply has been sent.
388
        """
389
390
        if not self.has_replied:
391
            raise InteractionDoesNotExist(
392
                "The interaction whom you are trying to edit has not "
393
                "been sent yet!"
394
            )
395
396
        message = convert_message(self._client, message)
397
        content_type, data = message.serialize()
398
399
        resp = await self._http.patch(
400
            f"webhooks/{self._client.bot.id}/{self.token}/messages/@original",
401
            data,
402
            content_type=content_type
403
        )
404
        self.__post_sent(message)
405
        return UserMessage.from_dict(resp)
406
407
    async def delete(self):
408
        """|coro|
409
410
        Delete the interaction.
411
412
        Raises
413
        ------
414
        :class:`~pincer.errors.InteractionDoesNotExist`
415
            Exception raised when no reply has been sent.
416
        """
417
        if not self.has_replied:
418
            raise InteractionDoesNotExist(
419
                "The interaction whom you are trying to delete has not "
420
                "been sent yet!"
421
            )
422
423
        await self._http.delete(
424
            f"webhooks/{self._client.bot.id}/{self.token}/messages/@original"
425
        )
426
427
    async def __post_followup_send_handler(
428
            self,
429
            followup: UserMessage,
430
            message: Message
431
    ):
432
        """Process a folloup after it was sent.
433
434
        Parameters
435
        ----------
436
        followup :class:`~pincer.objects.message.user_message.UserMessage`
437
            The followup message that is being post processed.
438
        message :class:`~pincer.objects.message.message.Message`
439
            The followup message.
440
        """
441
442
        if message.delete_after:
443
            await sleep(message.delete_after)
444
            await self.delete_followup(followup.id)
445
446
    def __post_followup_sent(
447
            self,
448
            followup: UserMessage,
449
            message: Message
450
    ):
451
        """Ensure the `__post_followup_send_handler` method its future.
452
453
        Parameters
454
        ----------
455
        followup :class:`~pincer.objects.message.user_message.UserMessage`
456
            The followup message that is being post processed.
457
        message :class:`~pincer.objects.message.message.Message`
458
            The followup message.
459
        """
460
        ensure_future(self.__post_followup_send_handler(followup, message))
461
462
    async def followup(self, message: MessageConvertable) -> UserMessage:
463
        """|coro|
464
465
        Create a follow up message for the interaction.
466
        This allows you to respond with multiple messages.
467
468
        Parameters
469
        ----------
470
        message :class:`~pincer.utils.convert_message.MessageConvertable`
471
            The message to sent.
472
473
        Returns
474
        -------
475
        :class:`~pincer.objects.message.user_message.UserMessage`
476
            The message that has been sent.
477
        """
478
        message = convert_message(self._client, message)
479
        content_type, data = message.serialize()
480
481
        resp = await self._http.post(
482
            f"webhooks/{self._client.bot.id}/{self.token}",
483
            data,
484
            content_type=content_type
485
        )
486
        msg = UserMessage.from_dict(resp)
487
        self.__post_followup_sent(msg, message)
488
        return msg
489
490
    async def edit_followup(
491
            self,
492
            message_id: int,
493
            message: MessageConvertable
494
    ) -> UserMessage:
495
        """|coro|
496
497
        Edit a followup message.
498
499
        Parameters
500
        ----------
501
        message_id :class:`int`
502
            The id of the original followup message.
503
        message :class:`~pincer.utils.convert_message.MessageConvertable`
504
            The message new message.
505
506
        Returns
507
        -------
508
        :class:`~pincer.objects.message.user_message.UserMessage`
509
            The updated message object.
510
        """
511
        message = convert_message(self._client, message)
512
        content_type, data = message.serialize()
513
514
        resp = await self._http.patch(
515
            f"webhooks/{self._client.bot.id}/{self.token}/messages/{message_id}",
516
            data,
517
            content_type=content_type
518
        )
519
        msg = UserMessage.from_dict(resp)
520
        self.__post_followup_sent(msg, message)
521
        return msg
522
523
    async def get_followup(self, message_id: int) -> UserMessage:
524
        """|coro|
525
526
        Get a followup message by id.
527
528
        Parameters
529
        ----------
530
        message_id :class:`int`
531
            The id of the original followup message that must be fetched.
532
533
        Returns
534
        -------
535
        :class:`~pincer.objects.message.user_message.UserMessage`
536
            The fetched message object.
537
        """
538
539
        resp = await self._http.get(
540
            f"webhooks/{self._client.bot.id}/{self.token}/messages/{message_id}",
541
        )
542
        return UserMessage.from_dict(resp)
543
544
    async def delete_followup(self, message: Union[UserMessage, int]):
545
        """|coro|
546
547
        Remove a followup message by id.
548
549
        Parameters
550
        ----------
551
        message Union[:class:`~pincer.objects.user_message.UserMessage`, :class:`int`]
552
            The id/followup object of the followup message that must be deleted.
553
        """
554
        message_id = message if isinstance(message, int) else message.id
555
556
        await self._http.delete(
557
            f"webhooks/{self._client.bot.id}/{self.token}/messages/{message_id}",
558
        )
559