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

pincer.objects.app.interactions.Interaction.ack()   A

Complexity

Conditions 3

Size

Total Lines 29
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 29
rs 9.85
c 0
b 0
f 0
cc 3
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 response(self) -> UserMessage:
261
        """|coro|
262
263
        Gets the original response for an interaction.
264
265
        Returns
266
        -------
267
        :class:`~pincer.objects.message.user_message.UserMessage`
268
            The fetched response!
269
        """
270
        if not self.has_replied:
271
            raise InteractionDoesNotExist(
272
                "No interaction reply has been sent yet!"
273
            )
274
275
        resp = await self._http.get(
276
            f"/webhooks/{self._client.bot.id}/{self.token}/messages/@original"
277
        )
278
        return UserMessage.from_dict(resp)
279
280
    async def ack(self, flags: Optional[InteractionFlags] = None):
281
        """|coro|
282
283
        Acknowledge an interaction, any flags here are applied to the reply.
284
285
        Parameters
286
        ----------
287
        flags :class:`~pincer.objects.app.interaction_flags.InteractionFlags`
288
            The flags which must be applied to the reply.
289
290
        Raises
291
        ------
292
        :class:`~pincer.exceptions.InteractionAlreadyAcknowledged`
293
            The interaction was already acknowledged, this can be
294
            because a reply or ack was already sent.
295
        """
296
        if self.has_replied or self.has_acknowledged:
297
            raise InteractionAlreadyAcknowledged(
298
                "The interaction you are trying to acknowledge has already "
299
                "been acknowledged"
300
            )
301
302
        self.has_acknowledged = True
303
        await self._http.post(
304
            f"interactions/{self.id}/{self.token}/callback",
305
            {
306
                "type": CallbackType.DEFERRED_MESSAGE,
307
                "data": {
308
                    "flags": flags
309
                }
310
            }
311
        )
312
313
    async def __post_send_handler(self, message: Message):
314
        """Process the interaction after it was sent.
315
316
        Parameters
317
        ----------
318
        message :class:`~pincer.objects.message.message.Message`
319
            The interaction message.
320
        """
321
322
        if message.delete_after:
323
            await sleep(message.delete_after)
324
            await self.delete()
325
326
    def __post_sent(self, message: Message):
327
        """Ensure the `__post_send_handler` method its future.
328
329
        Parameters
330
        ----------
331
        message :class:`~pincer.objects.message.message.Message`
332
            The interaction message.
333
        """
334
        self.has_replied = True
335
        ensure_future(self.__post_send_handler(message))
336
337
    async def reply(self, message: MessageConvertable):
338
        """|coro|
339
340
        Initial reply, only works if no ACK has been sent yet.
341
342
        Parameters
343
        ----------
344
        message :class:`~pincer.utils.convert_message.MessageConvertable`
345
            The response message!
346
347
        Raises
348
        ------
349
        :class:`~.pincer.errors.UseFollowup`
350
            Exception raised when a reply has already been sent so a
351
            :func:`~pincer.objects.app.interactions.Interaction.followup`
352
            should be used instead.
353
        :class:`~.pincer.errors.InteractionTimedOut`
354
            Exception raised when discord had to wait too long for a reply.
355
            You can extend the discord wait time by using the
356
            :func:`~pincer.objects.app.interaction.Interaction.ack`
357
            function.
358
        """
359
        if self.has_replied:
0 ignored issues
show
Unused Code introduced by
Unnecessary "elif" after "raise"
Loading history...
360
            raise UseFollowup(
361
                "A response has already been sent to the interaction. "
362
                "Please use a followup instead!"
363
            )
364
        elif self.has_acknowledged:
365
            self.has_replied = True
366
            await self.edit(message)
367
            return
368
369
        message = convert_message(self._client, message)
370
        content_type, data = message.serialize()
371
372
        try:
373
            await self._http.post(
374
                f"interactions/{self.id}/{self.token}/callback",
375
                {
376
                    "type": CallbackType.MESSAGE,
377
                    "data": data
378
                },
379
                content_type=content_type
380
            )
381
        except NotFoundError:
382
            raise InteractionTimedOut(
383
                "Discord had to wait too long for the interaction reply, "
384
                "you can extend the time it takes for discord to timeout by "
385
                "acknowledging the interaction. (using interaction.ack)"
386
            )
387
388
        self.__post_sent(message)
389
390
    async def edit(self, message: MessageConvertable) -> UserMessage:
391
        """|coro|
392
393
        Edit an interaction. This is also the way to reply to
394
        interactions whom have been acknowledged.
395
396
        Parameters
397
        ----------
398
        message :class:`~pincer.utils.convert_message.MessageConvertable`
399
            The new message!
400
401
        Returns
402
        -------
403
        :class:`~pincer.objects.message.user_message.UserMessage`
404
            The updated message object.
405
406
        Raises
407
        ------
408
        :class:`~.pincer.errors.InteractionDoesNotExist`
409
            Exception raised when no reply has been sent.
410
        """
411
412
        if not self.has_replied:
413
            raise InteractionDoesNotExist(
414
                "The interaction whom you are trying to edit has not "
415
                "been sent yet!"
416
            )
417
418
        message = convert_message(self._client, message)
419
        content_type, data = message.serialize()
420
421
        resp = await self._http.patch(
422
            f"webhooks/{self._client.bot.id}/{self.token}/messages/@original",
423
            data,
424
            content_type=content_type
425
        )
426
        self.__post_sent(message)
427
        return UserMessage.from_dict(resp)
428
429
    async def delete(self):
430
        """|coro|
431
432
        Delete the interaction.
433
434
        Raises
435
        ------
436
        :class:`~pincer.errors.InteractionDoesNotExist`
437
            Exception raised when no reply has been sent.
438
        """
439
        if not self.has_replied:
440
            raise InteractionDoesNotExist(
441
                "The interaction whom you are trying to delete has not "
442
                "been sent yet!"
443
            )
444
445
        await self._http.delete(
446
            f"webhooks/{self._client.bot.id}/{self.token}/messages/@original"
447
        )
448
449
    async def __post_followup_send_handler(
450
            self,
451
            followup: UserMessage,
452
            message: Message
453
    ):
454
        """Process a folloup after it was sent.
455
456
        Parameters
457
        ----------
458
        followup :class:`~pincer.objects.message.user_message.UserMessage`
459
            The followup message that is being post processed.
460
        message :class:`~pincer.objects.message.message.Message`
461
            The followup message.
462
        """
463
464
        if message.delete_after:
465
            await sleep(message.delete_after)
466
            await self.delete_followup(followup.id)
467
468
    def __post_followup_sent(
469
            self,
470
            followup: UserMessage,
471
            message: Message
472
    ):
473
        """Ensure the `__post_followup_send_handler` method its future.
474
475
        Parameters
476
        ----------
477
        followup :class:`~pincer.objects.message.user_message.UserMessage`
478
            The followup message that is being post processed.
479
        message :class:`~pincer.objects.message.message.Message`
480
            The followup message.
481
        """
482
        ensure_future(self.__post_followup_send_handler(followup, message))
483
484
    async def followup(self, message: MessageConvertable) -> UserMessage:
485
        """|coro|
486
487
        Create a follow up message for the interaction.
488
        This allows you to respond with multiple messages.
489
490
        Parameters
491
        ----------
492
        message :class:`~pincer.utils.convert_message.MessageConvertable`
493
            The message to sent.
494
495
        Returns
496
        -------
497
        :class:`~pincer.objects.message.user_message.UserMessage`
498
            The message that has been sent.
499
        """
500
        message = convert_message(self._client, message)
501
        content_type, data = message.serialize()
502
503
        resp = await self._http.post(
504
            f"webhooks/{self._client.bot.id}/{self.token}",
505
            data,
506
            content_type=content_type
507
        )
508
        msg = UserMessage.from_dict(resp)
509
        self.__post_followup_sent(msg, message)
510
        return msg
511
512
    async def edit_followup(
513
            self,
514
            message_id: int,
515
            message: MessageConvertable
516
    ) -> UserMessage:
517
        """|coro|
518
519
        Edit a followup message.
520
521
        Parameters
522
        ----------
523
        message_id :class:`int`
524
            The id of the original followup message.
525
        message :class:`~pincer.utils.convert_message.MessageConvertable`
526
            The message new message.
527
528
        Returns
529
        -------
530
        :class:`~pincer.objects.message.user_message.UserMessage`
531
            The updated message object.
532
        """
533
        message = convert_message(self._client, message)
534
        content_type, data = message.serialize()
535
536
        resp = await self._http.patch(
537
            f"webhooks/{self._client.bot.id}/{self.token}/messages/{message_id}",
538
            data,
539
            content_type=content_type
540
        )
541
        msg = UserMessage.from_dict(resp)
542
        self.__post_followup_sent(msg, message)
543
        return msg
544
545
    async def get_followup(self, message_id: int) -> UserMessage:
546
        """|coro|
547
548
        Get a followup message by id.
549
550
        Parameters
551
        ----------
552
        message_id :class:`int`
553
            The id of the original followup message that must be fetched.
554
555
        Returns
556
        -------
557
        :class:`~pincer.objects.message.user_message.UserMessage`
558
            The fetched message object.
559
        """
560
561
        resp = await self._http.get(
562
            f"webhooks/{self._client.bot.id}/{self.token}/messages/{message_id}",
563
        )
564
        return UserMessage.from_dict(resp)
565
566
    async def delete_followup(self, message: Union[UserMessage, int]):
567
        """|coro|
568
569
        Remove a followup message by id.
570
571
        Parameters
572
        ----------
573
        message Union[:class:`~pincer.objects.user_message.UserMessage`, :class:`int`]
574
            The id/followup object of the followup message that must be deleted.
575
        """
576
        message_id = message if isinstance(message, int) else message.id
577
578
        await self._http.delete(
579
            f"webhooks/{self._client.bot.id}/{self.token}/messages/{message_id}",
580
        )
581