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

MessageContext.ack()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 11
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
from __future__ import annotations
4
5
from dataclasses import dataclass
6
from typing import Optional, Union, TYPE_CHECKING
7
8
9
if TYPE_CHECKING:
10
    from ..app.interaction_flags import InteractionFlags
11
    from ...utils.convert_message import MessageConvertable
12
    from .user_message import UserMessage
13
    from ..app import ClientCommandStructure, Interaction
14
    from ..guild.member import GuildMember
15
    from ..user import User
16
    from ...utils.snowflake import Snowflake
17
18
19
@dataclass
20
class MessageContext:
21
    """
22
    Represents the context of a message interaction.
23
24
    :param author:
25
        The user whom invoked the interaction.
26
27
    :param command:
28
        The local command object for the command to whom this context
29
        belongs.
30
31
    interaction :class:`~pincer.objects.app.interaction.Interaction`
32
        The interaction this command belongs to.
33
34
    :param guild_id:
35
        The ID of the guild the interaction was invoked in.
36
        Can be None if it wasn't invoked in a guild.
37
38
    :param channel_id:
39
        The ID of the channel the interaction was invoked in.
40
        Can be None if it wasn't invoked in a channel.
41
    """
42
    author: Union[GuildMember, User]
0 ignored issues
show
introduced by
The variable User does not seem to be defined in case TYPE_CHECKING on line 9 is False. Are you sure this can never be the case?
Loading history...
introduced by
The variable GuildMember does not seem to be defined in case TYPE_CHECKING on line 9 is False. Are you sure this can never be the case?
Loading history...
43
    command: ClientCommandStructure
0 ignored issues
show
introduced by
The variable ClientCommandStructure does not seem to be defined in case TYPE_CHECKING on line 9 is False. Are you sure this can never be the case?
Loading history...
44
    interaction: Interaction
0 ignored issues
show
introduced by
The variable Interaction does not seem to be defined in case TYPE_CHECKING on line 9 is False. Are you sure this can never be the case?
Loading history...
45
46
    guild_id: Optional[Snowflake] = None
0 ignored issues
show
introduced by
The variable Snowflake does not seem to be defined in case TYPE_CHECKING on line 9 is False. Are you sure this can never be the case?
Loading history...
47
    channel_id: Optional[Snowflake] = None
48
49
    async def ack(self, flags: InteractionFlags):
50
        """|coro|
51
52
        Alias for :func:`~pincer.objects.app.interactions.Interaction.ack`.
53
54
        Parameters
55
        ----------
56
        flags :class:`~pincer.objects.app.interaction_flags.InteractionFlags`
57
            The flags which must be applied to the reply.
58
        """
59
        await self.interaction.ack(flags)
60
61
    async def reply(self, message: MessageConvertable):
62
        """|coro|
63
64
        Alias for :func:`~pincer.objects.app.interactions.Interaction.reply`.
65
66
        Parameters
67
        ----------
68
        message :class:`~pincer.utils.convert_message.MessageConvertable`
69
            The response message!
70
        """
71
        await self.interaction.reply(message)
72
73
    async def followup(self, message: MessageConvertable) -> UserMessage:
74
        """|coro|
75
76
        Alias for :func:`~pincer.objects.app.interactions.Interaction.followup`.
77
78
        Parameters
79
        ----------
80
        message :class:`~pincer.utils.convert_message.MessageConvertable`
81
            The message to sent.
82
        """
83
        return await self.interaction.followup(message)
84
85
    async def send(self, message: MessageConvertable) -> UserMessage:
86
        """|coro|
87
88
        Send a response for an interaction.
89
        This object returns the sent object and may be used several
90
        times after eachoter. (first one will always be the main
91
        interaction response)
92
93
        Uses
94
        ----
95
        :func:`~pincer.objects.message.context.MessageContext.reply`
96
            Method gets called for initial send.
97
        :func:`~pincer.objects.app.interactions.Interaction.response`
98
            Method gets called for initial send to get response.
99
        :func:`~pincer.objects.message.context.MessageContext.followup`
100
            Method gets called for second message and onwards.
101
102
         Returns
103
         -------
104
         :class:`~pincer.objects.message.user_message.UserMessage`
105
            The message that was sent.
106
        """
107
        if self.interaction.has_replied:
108
            return await self.followup(message)
109
110
        await self.reply(message)
111
        return await self.interaction.response()
112