Passed
Pull Request — main (#253)
by
unknown
01:47
created

Interaction.edit_followup()   A

Complexity

Conditions 1

Size

Total Lines 32
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 32
rs 9.75
c 0
b 0
f 0
cc 1
nop 3
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 sleep, ensure_future
7
from dataclasses import dataclass
8
from typing import Any, Dict, TYPE_CHECKING, Union, Optional, List
9
10
from .command_types import AppCommandOptionType
11
from .interaction_base import InteractionType, CallbackType
12
from .mentionable import Mentionable
13
from ..app.select_menu import SelectOption
14
from ..guild.member import GuildMember
15
from ..message.context import MessageContext
16
from ..message.message import Message
17
from ..message.user_message import UserMessage
18
from ..user import User
19
from ...exceptions import InteractionDoesNotExist, UseFollowup, \
20
    InteractionAlreadyAcknowledged, NotFoundError, InteractionTimedOut
21
from ...utils import APIObject
22
from ...utils.convert_message import convert_message
23
from ...utils.snowflake import Snowflake
24
from ...utils.types import MISSING
25
26
if TYPE_CHECKING:
27
    from .interaction_flags import InteractionFlags
28
    from ...utils.convert_message import MessageConvertable
29
    from .command import AppCommandInteractionDataOption
30
    from ..guild.channel import Channel
31
    from ..guild.role import Role
32
    from ...utils import APINullable
33
34
35
@dataclass
36
class ResolvedData(APIObject):
37
    """Represents a Discord Resolved Data structure
38
39
    Attributes
40
    ----------
41
    users: APINullable[Dict[:class:`~pincer.utils.snowflake.Snowflake`, :class:`~pincer.objects.user.user.User`]]
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (113/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
42
        Map of Snowflakes to user objects
43
    members: APINullable[Dict[:class:`~pincer.utils.snowflake.Snowflake`, :class:`~pincer.objects.guild.member.GuildMember`]]
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (125/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
44
        Map of Snowflakes to partial member objects
45
    roles: APINullable[Dict[:class:`~pincer.utils.snowflake.Snowflake`, :class:`~pincer.objects.guild.role.Role`]]
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (114/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
46
        Map of Snowflakes to role objects
47
    channels: APINullable[Dict[:class:`~pincer.utils.snowflake.Snowflake`, :class:`~pincer.objects.guild.channel.Channel`]]
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (123/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
48
        Map of Snowflakes to partial channel objects
49
    messages: APINullable[Dict[:class:`~pincer.utils.snowflake.Snowflake`, :class:`~pincer.objects.message.user_message.UserMessage`]]
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (134/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
50
        Map of Snowflakes to partial message objects
51
    """
52
    # noqa: E501
53
    users: APINullable[Dict[Snowflake, User]] = MISSING
54
    members: APINullable[Dict[Snowflake, GuildMember]] = MISSING
55
    roles: APINullable[Dict[Snowflake, Role]] = MISSING
56
    channels: APINullable[Dict[Snowflake, Channel]] = MISSING
57
    messages: APINullable[Dict[Snowflake, UserMessage]] = MISSING
58
59
60
@dataclass
61
class InteractionData(APIObject):
0 ignored issues
show
best-practice introduced by
Too many instance attributes (9/7)
Loading history...
62
    """Represents a Discord Interaction Data structure
63
64
    Attributes
65
    ----------
66
    id: :class:`~pincer.utils.snowflake.Snowflake`
67
        The `ID` of the invoked command
68
    name: :class:`str`
69
        The `name` of the invoked command
70
    type: :class:`int`
71
        The `type` of the invoked command
72
    resolved: APINullable[:class:`~pincer.objects.app.interactions.ResolvedData`]
73
        Converted users + roles + channels
74
    options: APINullable[:class:`~pincer.objects.app.command.AppCommandInteractionDataOption`]
75
        The params + values from the user
76
    custom_id: APINullable[:class:`str`]
77
        The `custom_id` of the component
78
    component_type: APINullable[:class:`int`]
79
        The type of the component
80
    values: APINullable[:class:`~pincer.objects.app.select_menu.SelectOption`]
81
        The values the user selected
82
    target_id: APINullable[:class:`~pincer.utils.snowflake.Snowflake`]
83
        Id of the user or message targeted by a user or message command
84
    """
85
    # noqa: E501
86
    id: Snowflake
87
    name: str
88
    type: int
89
90
    resolved: APINullable[ResolvedData] = MISSING
91
    options: APINullable[List[AppCommandInteractionDataOption]] = MISSING
92
    custom_id: APINullable[str] = MISSING
93
    component_type: APINullable[int] = MISSING
94
    values: APINullable[SelectOption] = MISSING
95
    target_id: APINullable[Snowflake] = MISSING
96
97
98
@dataclass
99
class Interaction(APIObject):
0 ignored issues
show
best-practice introduced by
Too many instance attributes (13/7)
Loading history...
100
    """Represents a Discord Interaction object
101
102
    Attributes
103
    ----------
104
    id: :class:`~pincer.utils.snowflake.Snowflake`
105
        Id of the interaction
106
    application_id: :class:`~pincer.utils.snowflake.Snowflake`
107
        Id of the application this interaction is for
108
    type: :class:`~pincer.objects.app.interaction_base.InteractionType`
109
        The type of interaction
110
    token: :class:`str`
111
        A continuation token for responding to the interaction
112
    version: :class:`int`
113
        Read-only property, always ``1``
114
    data: APINullable[:class:`~pincer.objects.app.interactions.InteractionData`]
115
        The command data payload
116
    guild_id: APINullable[:class:`~pincer.utils.snowflake.Snowflake`]
117
        The guild it was sent from
118
    channel_id: APINullable[:class:`~pincer.utils.snowflake.Snowflake`]
119
        The channel it was sent from
120
    member: APINullable[:class:`~pincer.objects.guild.member.GuildMember`]
121
        Guild member data for the invoking user, including permissions
122
    user: APINullable[:class:`~pincer.objects.user.user.User`]
123
        User object for the invoking user, if invoked in a DM
124
    message: APINullable[:class:`~pincer.objects.message.user_message.UserMessage`]
125
        For components, the message they were attached to
126
    """
127
    # noqa: E501
128
    id: Snowflake
129
    application_id: Snowflake
130
    type: InteractionType
131
    token: str
132
133
    version: int = 1
134
    data: APINullable[InteractionData] = MISSING
135
    guild_id: APINullable[Snowflake] = MISSING
136
    channel_id: APINullable[Snowflake] = MISSING
137
    member: APINullable[GuildMember] = MISSING
138
    user: APINullable[User] = MISSING
139
    message: APINullable[UserMessage] = MISSING
140
    has_replied: bool = False
141
    has_acknowledged: bool = False
142
143
    def __post_init__(self):
144
        super().__post_init__()
145
146
        for option in self.data.options:
147
148
            if option.type is AppCommandOptionType.STRING:
149
                option.value = str(option.value)
150
            elif option.type is AppCommandOptionType.INTEGER:
151
                option.value = int(option.value)
152
            elif option.type is AppCommandOptionType.BOOLEAN:
153
                option.value = bool(option.value)
154
            elif option.type is AppCommandOptionType.NUMBER:
155
                option.value = float(option.value)
156
157
            elif option.type is AppCommandOptionType.USER:
158
                user = self.return_type(option, self.data.resolved.members)
159
                user.set_user_data(
160
                    self.return_type(option, self.data.resolved.users)
161
                )
162
                option.value = user
163
164
            elif option.type is AppCommandOptionType.CHANNEL:
165
                option.value = self.return_type(
166
                    option, self.data.resolved.channels
167
                )
168
169
            elif option.type is AppCommandOptionType.ROLE:
170
                option.value = self.return_type(
171
                    option, self.data.resolved.roles
172
                )
173
174
            elif option.type is AppCommandOptionType.MENTIONABLE:
175
                user = self.return_type(option, self.data.resolved.members)
176
                if user:
177
                    user.set_user_data(self.return_type(
178
                        option, self.data.resolved.users)
179
                    )
0 ignored issues
show
Coding Style introduced by
Wrong continued indentation.
Loading history...
180
181
                option.value = Mentionable(
182
                    user,
183
                    self.return_type(
184
                        option, self.data.resolved.roles
185
                    )
186
                )
187
188
    @staticmethod
189
    def return_type(
190
        option: Snowflake,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
191
        data: Dict[Snowflake, Any]
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
192
    ) -> Optional[APIObject]:
193
        """
194
        Returns a value from the option or None if it doesn't exist.
195
196
        option : :class:`~pincer.utils.types.Snowflake`
197
            Snowflake to search ``data`` for.
198
        data : Dict[:class:`~pincer.utils.types.Snowflake`, Any]
199
            Resolved data to search through.
200
        """
201
        if data:
202
            return data[option.value]
203
204
        return None
205
206
    def convert_to_message_context(self, command):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
207
        return MessageContext(
208
            self.member or self.user,
209
            command,
210
            self,
211
            self.guild_id,
212
            self.channel_id
213
        )
214
215
    async def response(self) -> UserMessage:
216
        """|coro|
217
218
        Gets the original response for an interaction.
219
220
        Returns
221
        -------
222
        :class:`~pincer.objects.message.user_message.UserMessage`
223
            The fetched response!
224
        """
225
        if not self.has_replied:
226
            raise InteractionDoesNotExist(
227
                "No interaction reply has been sent yet!"
228
            )
229
230
        resp = await self._http.get(
231
            f"/webhooks/{self._client.bot.id}/{self.token}/messages/@original"
232
        )
233
        return UserMessage.from_dict(resp)
234
235
    async def ack(self, flags: Optional[InteractionFlags] = None):
236
        """|coro|
237
238
        Acknowledge an interaction, any flags here are applied to the reply.
239
240
        Parameters
241
        ----------
242
        flags: :class:`~pincer.objects.app.interaction_flags.InteractionFlags`
243
            The flags which must be applied to the reply.
244
245
        Raises
246
        ------
247
        :class:`~pincer.exceptions.InteractionAlreadyAcknowledged`
248
            The interaction was already acknowledged, this can be
249
            because a reply or ack was already sent.
250
        """
251
        if self.has_replied or self.has_acknowledged:
252
            raise InteractionAlreadyAcknowledged(
253
                "The interaction you are trying to acknowledge has already "
254
                "been acknowledged"
255
            )
256
257
        self.has_acknowledged = True
258
        await self._http.post(
259
            f"interactions/{self.id}/{self.token}/callback",
260
            {
261
                "type": CallbackType.DEFERRED_MESSAGE,
262
                "data": {
263
                    "flags": flags
264
                }
265
            }
266
        )
267
268
    async def __post_send_handler(self, message: Message):
269
        """Process the interaction after it was sent.
270
271
        Parameters
272
        ----------
273
        message :class:`~pincer.objects.message.message.Message`
274
            The interaction message.
275
        """
276
277
        if message.delete_after:
278
            await sleep(message.delete_after)
279
            await self.delete()
280
281
    def __post_sent(self, message: Message):
282
        """Ensure the `__post_send_handler` method its future.
283
284
        Parameters
285
        ----------
286
        message :class:`~pincer.objects.message.message.Message`
287
            The interaction message.
288
        """
289
        self.has_replied = True
290
        ensure_future(self.__post_send_handler(message))
291
292
    async def reply(self, message: MessageConvertable):
293
        """|coro|
294
295
        Initial reply, only works if no ACK has been sent yet.
296
297
        Parameters
298
        ----------
299
        message :class:`~pincer.utils.convert_message.MessageConvertable`
300
            The response message!
301
302
        Raises
303
        ------
304
        :class:`~.pincer.errors.UseFollowup`
305
            Exception raised when a reply has already been sent so a
306
            :func:`~pincer.objects.app.interactions.Interaction.followup`
307
            should be used instead.
308
        :class:`~.pincer.errors.InteractionTimedOut`
309
            Exception raised when discord had to wait too long for a reply.
310
            You can extend the discord wait time by using the
311
            :func:`~pincer.objects.app.interaction.Interaction.ack`
312
            function.
313
        """
314
        if self.has_replied:
0 ignored issues
show
Unused Code introduced by
Unnecessary "elif" after "raise"
Loading history...
315
            raise UseFollowup(
316
                "A response has already been sent to the interaction. "
317
                "Please use a followup instead!"
318
            )
319
        elif self.has_acknowledged:
320
            self.has_replied = True
321
            await self.edit(message)
322
            return
323
324
        message = convert_message(self._client, message)
325
        content_type, data = message.serialize(
326
            message_type=CallbackType.MESSAGE
327
        )
328
329
        try:
330
            await self._http.post(
331
                f"interactions/{self.id}/{self.token}/callback",
332
                data,
333
                content_type=content_type
334
            )
335
        except NotFoundError:
336
            raise InteractionTimedOut(
337
                "Discord had to wait too long for the interaction reply, "
338
                "you can extend the time it takes for discord to timeout by "
339
                "acknowledging the interaction. (using interaction.ack)"
340
            )
341
342
        self.__post_sent(message)
343
344
    async def edit(self, message: MessageConvertable) -> UserMessage:
345
        """|coro|
346
347
        Edit an interaction. This is also the way to reply to
348
        interactions whom have been acknowledged.
349
350
        Parameters
351
        ----------
352
        message :class:`~pincer.utils.convert_message.MessageConvertable`
353
            The new message!
354
355
        Returns
356
        -------
357
        :class:`~pincer.objects.message.user_message.UserMessage`
358
            The updated message object.
359
360
        Raises
361
        ------
362
        :class:`~.pincer.errors.InteractionDoesNotExist`
363
            Exception raised when no reply has been sent.
364
        """
365
366
        if not self.has_replied:
367
            raise InteractionDoesNotExist(
368
                "The interaction whom you are trying to edit has not "
369
                "been sent yet!"
370
            )
371
372
        message = convert_message(self._client, message)
373
        content_type, data = message.serialize()
374
375
        resp = await self._http.patch(
376
            f"webhooks/{self._client.bot.id}/{self.token}/messages/@original",
377
            data,
378
            content_type=content_type
379
        )
380
        self.__post_sent(message)
381
        return UserMessage.from_dict(resp)
382
383
    async def delete(self):
384
        """|coro|
385
386
        Delete the interaction.
387
388
        Raises
389
        ------
390
        :class:`~pincer.errors.InteractionDoesNotExist`
391
            Exception raised when no reply has been sent.
392
        """
393
        if not self.has_replied:
394
            raise InteractionDoesNotExist(
395
                "The interaction whom you are trying to delete has not "
396
                "been sent yet!"
397
            )
398
399
        await self._http.delete(
400
            f"webhooks/{self._client.bot.id}/{self.token}/messages/@original"
401
        )
402
403
    async def __post_followup_send_handler(
404
            self,
405
            followup: UserMessage,
406
            message: Message
407
    ):
408
        """Process a followup after it was sent.
409
410
        Parameters
411
        ----------
412
        followup :class:`~pincer.objects.message.user_message.UserMessage`
413
            The followup message that is being post processed.
414
        message :class:`~pincer.objects.message.message.Message`
415
            The followup message.
416
        """
417
418
        if message.delete_after:
419
            await sleep(message.delete_after)
420
            await self.delete_followup(followup.id)
421
422
    def __post_followup_sent(
423
            self,
424
            followup: UserMessage,
425
            message: Message
426
    ):
427
        """Ensure the `__post_followup_send_handler` method its future.
428
429
        Parameters
430
        ----------
431
        followup :class:`~pincer.objects.message.user_message.UserMessage`
432
            The followup message that is being post processed.
433
        message :class:`~pincer.objects.message.message.Message`
434
            The followup message.
435
        """
436
        ensure_future(self.__post_followup_send_handler(followup, message))
437
438
    async def followup(self, message: MessageConvertable) -> UserMessage:
439
        """|coro|
440
441
        Create a follow up message for the interaction.
442
        This allows you to respond with multiple messages.
443
444
        Parameters
445
        ----------
446
        message :class:`~pincer.utils.convert_message.MessageConvertable`
447
            The message to sent.
448
449
        Returns
450
        -------
451
        :class:`~pincer.objects.message.user_message.UserMessage`
452
            The message that has been sent.
453
        """
454
        message = convert_message(self._client, message)
455
        content_type, data = message.serialize()
456
457
        resp = await self._http.post(
458
            f"webhooks/{self._client.bot.id}/{self.token}",
459
            data,
460
            content_type=content_type
461
        )
462
        msg = UserMessage.from_dict(resp)
463
        self.__post_followup_sent(msg, message)
464
        return msg
465
466
    async def edit_followup(
467
            self,
468
            message_id: int,
469
            message: MessageConvertable
470
    ) -> UserMessage:
471
        """|coro|
472
473
        Edit a followup message.
474
475
        Parameters
476
        ----------
477
        message_id :class:`int`
478
            The id of the original followup message.
479
        message :class:`~pincer.utils.convert_message.MessageConvertable`
480
            The message new message.
481
482
        Returns
483
        -------
484
        :class:`~pincer.objects.message.user_message.UserMessage`
485
            The updated message object.
486
        """
487
        message = convert_message(self._client, message)
488
        content_type, data = message.serialize()
489
490
        resp = await self._http.patch(
491
            f"webhooks/{self._client.bot.id}/{self.token}/messages/{message_id}",
492
            data,
493
            content_type=content_type
494
        )
495
        msg = UserMessage.from_dict(resp)
496
        self.__post_followup_sent(msg, message)
497
        return msg
498
499
    async def get_followup(self, message_id: int) -> UserMessage:
500
        """|coro|
501
502
        Get a followup message by id.
503
504
        Parameters
505
        ----------
506
        message_id :class:`int`
507
            The id of the original followup message that must be fetched.
508
509
        Returns
510
        -------
511
        :class:`~pincer.objects.message.user_message.UserMessage`
512
            The fetched message object.
513
        """
514
515
        resp = await self._http.get(
516
            f"webhooks/{self._client.bot.id}/{self.token}/messages/{message_id}",
517
        )
518
        return UserMessage.from_dict(resp)
519
520
    async def delete_followup(self, message: Union[UserMessage, int]):
521
        """|coro|
522
523
        Remove a followup message by id.
524
525
        Parameters
526
        ----------
527
        message Union[:class:`~pincer.objects.user_message.UserMessage`, :class:`int`]
528
            The id/followup object of the followup message that must be deleted.
529
        """
530
        message_id = message if isinstance(message, int) else message.id
531
532
        await self._http.delete(
533
            f"webhooks/{self._client.bot.id}/{self.token}/messages/{message_id}",
534
        )
535