Skip to content

Commit 25823fd

Browse files
committed
refractor: Use A callback Parameter Of Interaction
1 parent 202a8db commit 25823fd

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

discord/interactions.py

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"MessageInteraction",
6161
"InteractionMetadata",
6262
"AuthorizingIntegrationOwners",
63+
"InteractionCallback"
6364
)
6465

6566
if TYPE_CHECKING:
@@ -83,6 +84,7 @@
8384
from .threads import Thread
8485
from .types.interactions import Interaction as InteractionPayload
8586
from .types.interactions import InteractionCallbackResponse, InteractionData
87+
from .types.interactions import InteractionCallback as InteractionCallbackPayload
8688
from .types.interactions import InteractionMetadata as InteractionMetadataPayload
8789
from .types.interactions import MessageInteraction as MessageInteractionPayload
8890
from .ui.modal import Modal
@@ -152,6 +154,11 @@ class Interaction:
152154
The context in which this command was executed.
153155
154156
.. versionadded:: 2.6
157+
callback: Optional[:class:`InteractionCallback`]
158+
The callback of the interaction. Contains information about the status of the interaction response.
159+
Will be `None` until the interaction is responded to.
160+
161+
.. versionadded:: 2.7
155162
"""
156163

157164
__slots__: tuple[str, ...] = (
@@ -172,6 +179,7 @@ class Interaction:
172179
"entitlements",
173180
"context",
174181
"authorizing_integration_owners",
182+
"callback",
175183
"_channel_data",
176184
"_message_data",
177185
"_guild_data",
@@ -185,16 +193,13 @@ class Interaction:
185193
"_cs_response",
186194
"_cs_followup",
187195
"_cs_channel",
188-
"_response_message_loading",
189-
"_response_message_ephemeral",
190196
)
191197

192198
def __init__(self, *, data: InteractionPayload, state: ConnectionState):
193199
self._state: ConnectionState = state
194200
self._session: ClientSession = state.http._HTTPClient__session
195201
self._original_response: InteractionMessage | None = None
196-
self._response_message_loading: bool = False
197-
self._response_message_ephemeral: bool = False
202+
self.callback: InteractionCallback | None = None
198203
self._from_data(data)
199204

200205
def _from_data(self, data: InteractionPayload):
@@ -312,22 +317,6 @@ def is_component(self) -> bool:
312317
"""Indicates whether the interaction is a message component."""
313318
return self.type == InteractionType.component
314319

315-
def is_loading(self) -> bool:
316-
"""Indicates whether the response message is in a loading state.
317-
318-
.. versionadded:: 2.7
319-
"""
320-
return self._response_message_loading
321-
322-
def is_ephemeral(self) -> bool:
323-
"""Indicates whether the response message is ephemeral.
324-
325-
This might be useful for determining if the message was forced to be ephemeral.
326-
327-
.. versionadded:: 2.7
328-
"""
329-
return self._response_message_ephemeral
330-
331320
@utils.cached_slot_property("_cs_channel")
332321
@utils.deprecated("Interaction.channel", "2.7", stacklevel=4)
333322
def cached_channel(self) -> InteractionChannel | None:
@@ -895,7 +884,7 @@ async def pong(self) -> None:
895884
)
896885
)
897886
self._responded = True
898-
self._process_callback_response(callback_response)
887+
await self._process_callback_response(callback_response)
899888

900889
async def _process_callback_response(
901890
self, callback_response: InteractionCallbackResponse
@@ -911,12 +900,7 @@ async def _process_callback_response(
911900
message = InteractionMessage(state=state, channel=channel, data=callback_response["resource"]["message"]) # type: ignore
912901
self._parent._original_response = message
913902

914-
self._parent._response_message_ephemeral = callback_response["interaction"].get(
915-
"response_message_ephemeral", False
916-
)
917-
self._parent._response_message_loading = callback_response["interaction"].get(
918-
"response_message_loading", False
919-
)
903+
self._parent.callback = InteractionCallback(callback_response["interaction"])
920904

921905
async def send_message(
922906
self,
@@ -1294,7 +1278,7 @@ async def send_autocomplete_result(
12941278
)
12951279

12961280
self._responded = True
1297-
self._process_callback_response(callback_response)
1281+
await self._process_callback_response(callback_response)
12981282

12991283
async def send_modal(self, modal: Modal) -> Interaction:
13001284
"""|coro|
@@ -1333,7 +1317,7 @@ async def send_modal(self, modal: Modal) -> Interaction:
13331317
)
13341318
)
13351319
self._responded = True
1336-
self._process_callback_response(callback_response)
1320+
await self._process_callback_response(callback_response)
13371321
self._parent._state.store_modal(modal, self._parent.user.id)
13381322
return self._parent
13391323

@@ -1372,7 +1356,7 @@ async def premium_required(self) -> Interaction:
13721356
)
13731357
)
13741358
self._responded = True
1375-
self._process_callback_response(callback_response)
1359+
await self._process_callback_response(callback_response)
13761360
return self._parent
13771361

13781362
async def _locked_response(self, coro: Coroutine[Any, Any, Any]) -> Any:
@@ -1718,3 +1702,26 @@ def guild(self) -> Guild | None:
17181702
if not self.guild_id:
17191703
return None
17201704
return self._state._get_guild(self.guild_id)
1705+
1706+
1707+
class InteractionCallback:
1708+
"""Information about the status of the interaction response.
1709+
1710+
.. versionadded:: 2.7
1711+
"""
1712+
def __init__(self, data: InteractionCallbackPayload):
1713+
self._response_message_loading: bool = data.get("response_message_loading", False)
1714+
self._response_message_ephemeral: bool = data.get("response_message_ephemeral", False)
1715+
1716+
def is_loading(self) -> bool:
1717+
"""Indicates whether the response message is in a loading state.
1718+
"""
1719+
return self._response_message_loading
1720+
1721+
def is_ephemeral(self) -> bool:
1722+
"""Indicates whether the response message is ephemeral.
1723+
1724+
This might be useful for determining if the message was forced to be ephemeral.
1725+
"""
1726+
return self._response_message_ephemeral
1727+

0 commit comments

Comments
 (0)