MessageContext.update()   A
last analyzed

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 TYPE_CHECKING
7
8
if TYPE_CHECKING:
9
10
    from typing import Optional, Union
11
    from .user_message import UserMessage
12
    from ..app import Interaction
13
    from ..app.interaction_flags import InteractionFlags
14
    from ..guild.member import GuildMember
15
    from ..user.user import User
16
    from ...client import Client
17
    from ...utils.convert_message import MessageConvertable
18
    from ...utils.snowflake import Snowflake
19
20
21
@dataclass(repr=False)
22
class MessageContext:
23
    """Represents the context of a message interaction.
24
25
    Attributes
26
    ----------
27
    id: :class:`~pincer.utils.snowflake.Snowflake`
28
        The ID of the interaction.
29
    author: Union[:class:`~pincer.objects.guild.member.GuildMember`, :class:`~pincer.objects.user.user.User`]
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (109/100).

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

Loading history...
30
31
        The user whom invoked the interaction.
32
    command: :class:`~pincer.objects.app.command.InteractableStructure`
33
        The local command object for the command to whom this context
34
        belongs.
35
36
    guild_id: Optional[:class:`~pincer.utils.snowflake.Snowflake`]
37
        The ID of the guild the interaction was invoked in.
38
        Can be None if it wasn't invoked in a guild.
39
    channel_id: Optional[:class:`~pincer.utils.snowflake.Snowflake`]
40
        The ID of the channel the interaction was invoked in.
41
        Can be None if it wasn't invoked in a channel.
42
    """  # noqa: E501
43
44
    _client: Client
45
46
    author: Union[GuildMember, User]
47
    interaction: Interaction
48
49
    guild_id: Optional[Snowflake] = None
50
    channel_id: Optional[Snowflake] = None
51
52
    # Properties do not use ChannelProperty and GuildProperty because MessageContext is
53
    # not an APIObject.
54
    @property
55
    def channel(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
56
        return self._client.channels[self.channel_id]
57
58
    @property
59
    def guild(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
60
        return self._client.guilds[self.channel_id]
61
62
    async def ack(self, flags: InteractionFlags = None):
63
        """|coro|
64
65
        Alias for :func:`~pincer.objects.app.interactions.Interaction.ack`.
66
67
        Parameters
68
        ----------
69
        flags :class:`~pincer.objects.app.interaction_flags.InteractionFlags`
70
            The flags which must be applied to the reply. |default| :data:`None`
71
        """
72
        await self.interaction.ack(flags)
73
74
    async def deferred_update_ack(self, flags: InteractionFlags = None):
75
        """|coro|
76
77
        Alias for :func:`~pincer.objects.app.interactions.Interaction.deferred_update_ack`.
78
79
        Parameters
80
        ----------
81
        flags :class:`~pincer.objects.app.interaction_flags.InteractionFlags`
82
            The flags which must be applied to the reply. |default| :data:`None`
83
        """  # noqa: E501
84
        await self.interaction.deferred_update_ack(flags)
85
86
    async def reply(self, message: MessageConvertable):
87
        """|coro|
88
89
        Alias for :func:`~pincer.objects.app.interactions.Interaction.reply`.
90
91
        Parameters
92
        ----------
93
        message :class:`~pincer.utils.convert_message.MessageConvertable`
94
            The response message!
95
        """
96
        await self.interaction.reply(message)
97
98
    async def update(self, message: MessageConvertable):
99
        """|coro|
100
101
        Alias for :func:`~pincer.objects.app.interactions.Interaction.update`.
102
103
        Parameters
104
        ----------
105
        message :class:`~pincer.utils.convert_message.MessageConvertable`
106
            The parts of the message to edit.
107
        """
108
        await self.interaction.update(message)
109
110
    async def followup(self, message: MessageConvertable) -> UserMessage:
111
        """|coro|
112
113
        Alias for :func:`~pincer.objects.app.interactions.Interaction.followup`.
114
115
        Parameters
116
        ----------
117
        message :class:`~pincer.utils.convert_message.MessageConvertable`
118
            The message to send.
119
        """
120
        return await self.interaction.followup(message)
121
122
    async def send(self, message: MessageConvertable) -> UserMessage:
123
        """|coro|
124
125
        Send a response for an interaction.
126
        This object returns the sent object and may be used several
127
        times after each other. (first one will always be the main
128
        interaction response)
129
130
        Uses
131
        ----
132
        :func:`reply() <reply>`
133
            Method gets called for initial send.
134
        :func:`response() <Interaction.response>`
135
            Method gets called for initial send to get response.
136
        :func:`followup() <MessageContext.followup>`
137
            Method gets called for second message and onwards.
138
139
        Returns
140
        -------
141
        :class:`~pincer.objects.message.user_message.UserMessage`
142
            The message that was sent.
143
        """
144
        if self.interaction.has_replied:
145
            return await self.followup(message)
146
147
        await self.reply(message)
148
        return await self.interaction.response()
149