Browse Source

chore: Update thins thongs

pull/9957/head
DA-344 5 months ago
parent
commit
115a817a23
  1. 215
      discord/interactions.py
  2. 4
      discord/types/interactions.py
  3. 2
      discord/webhook/async_.py
  4. 8
      docs/interactions/api.rst

215
discord/interactions.py

@ -55,6 +55,7 @@ __all__ = (
'InteractionMessage', 'InteractionMessage',
'InteractionResponse', 'InteractionResponse',
'InteractionCallback', 'InteractionCallback',
'InteractionCallbackResource',
'InteractionCallbackActivity', 'InteractionCallbackActivity',
) )
@ -63,8 +64,10 @@ if TYPE_CHECKING:
Interaction as InteractionPayload, Interaction as InteractionPayload,
InteractionData, InteractionData,
ApplicationCommandInteractionData, ApplicationCommandInteractionData,
InteractionCallbackResponse as InteractionCallbackResponsePayload, InteractionCallback as InteractionCallbackPayload,
InteractionCallbackActivity as InteractionCallbackActivityPayload, InteractionCallbackActivity as InteractionCallbackActivityPayload,
InteractionCallbackResponse as InteractionCallbackResponsePayload,
InteractionCallbackResource as InteractionCallbackResourcePayload,
) )
from .types.webhook import ( from .types.webhook import (
Webhook as WebhookPayload, Webhook as WebhookPayload,
@ -655,106 +658,150 @@ class InteractionCallbackActivity:
return f'<InteractionCallbackActivity id={self.id!r}>' return f'<InteractionCallbackActivity id={self.id!r}>'
class InteractionCallback(Generic[ClientT]): class InteractionCallback:
"""Represents a Discord response to an interaction. """Represents an interaction callback invoking interaction.
.. versionadded:: 2.5
Attributes Attribute
---------- ---------
id: :class:`int` id: :class:`int`
The interaction ID this callback responds to. The ID of the interaction.
interaction_type: :class:`InteractionType` type: :class:`InteractionType`
The interaction type.
callback_type: Optional[:class:`InteractionResponseType`]
The interaction response type. The interaction response type.
activity_instance_id: Optional[:class:`str`] activity_instance_id: Optional[:class:`str`]
The activity instance ID this interaction was invoked from. The ID of the activity that was launched as response of this interaction.
activity_instance: Optional[:class:`InteractionCallbackActivity`] message_id: Optional[:class:`int`]
The resolved activity instance this interaction was invoked from. The ID of the message that was sent as response of this interaction.
response_message_id: Optional[:class:`int`] """
The message ID of the interaction response.
response_message_loading: :class:`bool`
Whether the response message showed the application was thinking.
response_message_ephemeral: :class:`bool`
Whether the response message was ephemeral.
response_message: Optional[:class:`InteractionMessage`]
The resolved response message.
.. note:: __slots__ = (
'_state',
'id',
'_message',
'type',
'activity_instance_id',
'message_id',
)
def __init__(self, *, data: InteractionCallbackPayload, state: ConnectionState) -> None:
self._state: ConnectionState = state
self.id: int = int(data['id'])
self._message: Optional[Message] = None
self._update(data)
This is ``None`` if the interaction response type is not :attr:`InteractionResponseType.channel_message` def _update(self, data: InteractionCallbackResponsePayload) -> None:
or :attr:`InteractionResponseType.message_update`. self.type: InteractionType = try_enum(InteractionType, data['type'])
response_channel: Union[:class:`abc.GuildChannel`, :class:`abc.PrivateChannel`, :class:`Thread`] self.activity_instance_id: Optional[str] = data.get('activity_instance_id')
The channel this interaction was invoked from. self.message_id: Optional[int] = utils._get_as_snowflake(data, 'response_message_id')
@property
def message(self) -> Optional[Message]:
"""Optional[:class:`Message`]: Returns the cached message, or ``None``."""
return self._message or self._state._get_message(self.message_id)
class InteractionCallbackResource(Generic[ClientT]):
"""Represents an interaction callback's resource.
Attributes
----------
type: :class:`InteractionResponseType`
The interaction callback response type.
activity: Optional[:class:`InteractionCallbackActivity`]
The activity that was launched as a response to the interaction.
message: Optional[:class:`InteractionMessage`]
The message that was sent as a response to the interaction.
""" """
__slots__ = ( __slots__ = (
'id',
'_parent',
'_state', '_state',
'interaction_type', '_parent',
'activity_instance_id', 'activity',
'activity_instance', 'message',
'response_message_id', 'type',
'response_message_thinking',
'response_message_ephemeral',
'response_message',
'response_channel',
'callback_type',
) )
def __init__( def __init__(
self, self,
*, *,
parent: Interaction[ClientT], data: InteractionCallbackResourcePayload,
channel: InteractionChannel, state: ConnectionState,
data: InteractionCallbackResponsePayload, parent: InteractionCallbackResponse,
) -> None: ) -> None:
self._parent: Interaction[ClientT] = parent self._state: ConnectionState = state
self._state: ConnectionState = parent._state self._parent: InteractionCallbackResponse = parent
self.response_channel: InteractionChannel = channel self.activity: Optional[InteractionCallbackActivity] = None
self.message: Optional[InteractionMessage] = None
self._update(data) self._update(data)
parent._original_response = self.response_message
def __repr__(self) -> str:
return f'<InteractionCallback id={self.id} interaction_type={self.interaction_type!r} callback_type={self.callback_type!r}>'
def _update(self, data: InteractionCallbackResponsePayload) -> None:
interaction = data['interaction']
self.id: int = int(interaction['id'])
self.interaction_type: InteractionType = try_enum(InteractionType, interaction['type'])
self.activity_instance_id: Optional[str] = interaction.get('activity_instance_id')
self.response_message_id: Optional[int] = utils._get_as_snowflake(interaction, 'response_message_id')
self.response_message_thinking: bool = interaction.get('response_message_loading', False)
self.response_message_ephemeral: bool = interaction.get('response_message_ephemeral', False)
resource = data.get('resource', {})
self.callback_type: Optional[InteractionResponseType] = None
self.activity_instance: Optional[InteractionCallbackActivity] = None
self.response_message: Optional[InteractionMessage] = None
def _update(self, data: InteractionCallbackResourcePayload) -> None:
try: try:
self.callback_type = try_enum(InteractionResponseType, resource['type']) self.type: InteractionResponseType = try_enum(InteractionResponseType, data['type'])
except KeyError: except KeyError:
pass pass
try: try:
self.activity_instance = InteractionCallbackActivity( self.activity = InteractionCallbackActivity(data=data['activity_instance'])
data=resource['activity_instance'],
)
except KeyError: except KeyError:
pass pass
try: try:
self.response_message = InteractionMessage( self.message = InteractionMessage(
state=self._state, state=self._state,
channel=self.response_channel, # type: ignore channel=self._parent._parent.channel, # type: ignore
data=resource['message'], data=data['message'],
) )
except KeyError: except KeyError:
pass pass
self._parent.response._message = self.message
class InteractionCallbackResponse(Generic[ClientT]):
"""Represents a Discord response to an interaction.
.. versionadded:: 2.5
Attributes
----------
interaction: :class:`InteractionCallback`
The interaction callback response.
resource: :class:`InteractionCallbackResource`
The interaction callback resource.
"""
__slots__ = (
'_parent',
'_state',
'interaction',
'resource',
)
def __init__(
self,
*,
parent: Interaction[ClientT],
data: InteractionCallbackPayload,
) -> None:
self._parent: Interaction[ClientT] = parent
self._state: ConnectionState = parent._state
self.interaction: InteractionCallback = InteractionCallback(data=data['interaction'], state=self._state)
self.resource: Optional[InteractionCallbackResource] = None
def __repr__(self) -> str:
return f'<InteractionCallback id={self.id} interaction_type={self.interaction_type!r} callback_type={self.callback_type!r}>'
def _update(self, data: InteractionCallbackPayload) -> None:
interaction = data['interaction']
resource = data.get('resource', {})
self.interaction._update(interaction)
if self.resource:
self.resource._update(resource)
else:
self.resource = InteractionCallbackResource(data=resource, state=self._state, parent=self) # type: ignore
self._parent._original_response = self.interaction.message # type: ignore
class InteractionResponse(Generic[ClientT]): class InteractionResponse(Generic[ClientT]):
"""Represents a Discord interaction response. """Represents a Discord interaction response.
@ -792,7 +839,7 @@ class InteractionResponse(Generic[ClientT]):
ephemeral: bool = ..., ephemeral: bool = ...,
thinking: bool = ..., thinking: bool = ...,
with_response: Literal[True] = ..., with_response: Literal[True] = ...,
) -> InteractionCallback[ClientT]: ) -> InteractionCallbackResponse[ClientT]:
... ...
@overload @overload
@ -801,7 +848,7 @@ class InteractionResponse(Generic[ClientT]):
*, *,
ephemeral: bool = ..., ephemeral: bool = ...,
thinking: bool = ..., thinking: bool = ...,
with_response: Literal[False] = False, with_response: Literal[False] = ...,
) -> None: ) -> None:
... ...
@ -811,7 +858,7 @@ class InteractionResponse(Generic[ClientT]):
ephemeral: bool = False, ephemeral: bool = False,
thinking: bool = False, thinking: bool = False,
with_response: bool = True, with_response: bool = True,
) -> Optional[InteractionCallback[ClientT]]: ) -> Optional[InteractionCallbackResponse[ClientT]]:
"""|coro| """|coro|
Defers the interaction response. Defers the interaction response.
@ -887,7 +934,7 @@ class InteractionResponse(Generic[ClientT]):
) )
self._response_type = InteractionResponseType(defer_type) self._response_type = InteractionResponseType(defer_type)
if response: if response:
return InteractionCallback( return InteractionCallbackResponse(
parent=parent, parent=parent,
channel=parent.channel, # type: ignore channel=parent.channel, # type: ignore
data=response, data=response,
@ -943,7 +990,7 @@ class InteractionResponse(Generic[ClientT]):
delete_after: Optional[float] = ..., delete_after: Optional[float] = ...,
poll: Poll = ..., poll: Poll = ...,
with_response: Literal[True] = ..., with_response: Literal[True] = ...,
) -> InteractionCallback[ClientT]: ) -> InteractionCallbackResponse[ClientT]:
... ...
@overload @overload
@ -963,7 +1010,7 @@ class InteractionResponse(Generic[ClientT]):
silent: bool = ..., silent: bool = ...,
delete_after: Optional[float] = ..., delete_after: Optional[float] = ...,
poll: Poll = ..., poll: Poll = ...,
with_response: Literal[False] = False, with_response: Literal[False] = ...,
) -> None: ) -> None:
... ...
@ -984,7 +1031,7 @@ class InteractionResponse(Generic[ClientT]):
delete_after: Optional[float] = None, delete_after: Optional[float] = None,
poll: Poll = MISSING, poll: Poll = MISSING,
with_response: bool = True, with_response: bool = True,
) -> Optional[InteractionCallback[ClientT]]: ) -> Optional[InteractionCallbackResponse[ClientT]]:
"""|coro| """|coro|
Responds to this interaction by sending a message. Responds to this interaction by sending a message.
@ -1113,7 +1160,7 @@ class InteractionResponse(Generic[ClientT]):
asyncio.create_task(inner_call()) asyncio.create_task(inner_call())
if response: if response:
return InteractionCallback( return InteractionCallbackResponse(
parent=parent, parent=parent,
channel=parent.channel, # type: ignore channel=parent.channel, # type: ignore
data=response, data=response,
@ -1132,7 +1179,7 @@ class InteractionResponse(Generic[ClientT]):
delete_after: Optional[float] = ..., delete_after: Optional[float] = ...,
suppress_embeds: bool = ..., suppress_embeds: bool = ...,
with_response: Literal[True] = ..., with_response: Literal[True] = ...,
) -> InteractionCallback[ClientT]: ) -> InteractionCallbackResponse[ClientT]:
... ...
@overload @overload
@ -1147,7 +1194,7 @@ class InteractionResponse(Generic[ClientT]):
allowed_mentions: Optional[AllowedMentions] = ..., allowed_mentions: Optional[AllowedMentions] = ...,
delete_after: Optional[float] = ..., delete_after: Optional[float] = ...,
suppress_embeds: bool = ..., suppress_embeds: bool = ...,
with_response: Literal[False] = False, with_response: Literal[False] = ...,
) -> None: ) -> None:
... ...
@ -1292,21 +1339,21 @@ class InteractionResponse(Generic[ClientT]):
asyncio.create_task(inner_call()) asyncio.create_task(inner_call())
if response: if response:
return InteractionCallback( return InteractionCallbackResponse(
parent=parent, parent=parent,
channel=parent.channel, # type: ignore channel=parent.channel, # type: ignore
data=response, data=response,
) )
@overload @overload
async def send_modal(self, modal: Modal, /, *, with_response: Literal[True] = ...) -> InteractionCallback[ClientT]: async def send_modal(self, modal: Modal, /, *, with_response: Literal[True] = ...) -> InteractionCallbackResponse[ClientT]:
... ...
@overload @overload
async def send_modal(self, modal: Modal, /, *, with_response: Literal[False] = False) -> None: async def send_modal(self, modal: Modal, /, *, with_response: Literal[False] = ...) -> None:
... ...
async def send_modal(self, modal: Modal, /, *, with_response: bool = True) -> Optional[InteractionCallback[ClientT]]: async def send_modal(self, modal: Modal, /, *, with_response: bool = True) -> Optional[InteractionCallbackResponse[ClientT]]:
"""|coro| """|coro|
Responds to this interaction by sending a modal. Responds to this interaction by sending a modal.
@ -1355,7 +1402,7 @@ class InteractionResponse(Generic[ClientT]):
self._response_type = InteractionResponseType.modal self._response_type = InteractionResponseType.modal
if response: if response:
return InteractionCallback( return InteractionCallbackResponse(
parent=parent, parent=parent,
channel=parent.channel, # type: ignore channel=parent.channel, # type: ignore
data=response, data=response,

4
discord/types/interactions.py

@ -275,7 +275,7 @@ class MessageInteractionMetadata(TypedDict):
triggering_interaction_metadata: NotRequired[MessageInteractionMetadata] triggering_interaction_metadata: NotRequired[MessageInteractionMetadata]
class InteractionCallback(TypedDict): class InteractionCallbackResponse(TypedDict):
id: Snowflake id: Snowflake
type: InteractionType type: InteractionType
activity_instance_id: NotRequired[str] activity_instance_id: NotRequired[str]
@ -294,6 +294,6 @@ class InteractionCallbackResource(TypedDict):
message: NotRequired[Message] message: NotRequired[Message]
class InteractionCallbackResponse(TypedDict): class InteractionCallback(TypedDict):
interaction: InteractionCallback interaction: InteractionCallback
resource: NotRequired[InteractionCallbackResource] resource: NotRequired[InteractionCallbackResource]

2
discord/webhook/async_.py

@ -91,7 +91,7 @@ if TYPE_CHECKING:
from ..types.emoji import PartialEmoji as PartialEmojiPayload from ..types.emoji import PartialEmoji as PartialEmojiPayload
from ..types.snowflake import SnowflakeList from ..types.snowflake import SnowflakeList
from ..types.interactions import ( from ..types.interactions import (
InteractionCallbackResponse as InteractionCallbackResponsePayload, InteractionCallback as InteractionCallbackResponsePayload,
) )
BE = TypeVar('BE', bound=BaseException) BE = TypeVar('BE', bound=BaseException)

8
docs/interactions/api.rst

@ -28,6 +28,14 @@ InteractionResponse
.. autoclass:: InteractionResponse() .. autoclass:: InteractionResponse()
:members: :members:
InteractionCallbackResource
~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. attributetable:: InteractionCallbackResource
.. autoclass:: InteractionCallbackResource()
:members:
InteractionCallback InteractionCallback
~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~

Loading…
Cancel
Save