Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions discord/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
ApplicationCommandInteractionData,
InteractionCallback as InteractionCallbackPayload,
InteractionCallbackActivity as InteractionCallbackActivityPayload,
MessageComponentInteractionData,
ModalSubmitInteractionData,
)
from .types.webhook import (
Webhook as WebhookPayload,
Expand Down Expand Up @@ -191,6 +193,8 @@ class Interaction(Generic[ClientT]):
'channel',
'_cs_namespace',
'_cs_command',
'_cs_command_id',
'_cs_custom_id',
)

def __init__(self, *, data: InteractionPayload, state: ConnectionState[ClientT]):
Expand Down Expand Up @@ -376,6 +380,21 @@ def command(self) -> Optional[Union[Command[Any, ..., Any], ContextMenu]]:
else:
return tree._get_context_menu(data)

@utils.cached_slot_property('_cs_command_id')
def command_id(self) -> Optional[int]:
"""Optional[:class:`int`]: The ID of the command that triggered this interaction.

Only applicable if :attr:`type` is one of, :attr:`InteractionType.application_command` or
:attr:`InteractionType.autocomplete`.

.. versionadded:: 2.7
"""
if self.type not in (InteractionType.application_command, InteractionType.autocomplete):
return None

data: ApplicationCommandInteractionData = self.data # type: ignore
return int(data.get('id', 0))

@utils.cached_slot_property('_cs_response')
def response(self) -> InteractionResponse[ClientT]:
""":class:`InteractionResponse`: Returns an object responsible for handling responding to the interaction.
Expand Down Expand Up @@ -405,6 +424,21 @@ def expires_at(self) -> datetime.datetime:
""":class:`datetime.datetime`: When the interaction expires."""
return self.created_at + datetime.timedelta(minutes=15)

@utils.cached_slot_property('_cs_custom_id')
def custom_id(self) -> Optional[str]:
"""Optional[:class:`str`]: The custom ID of the component that triggered this interaction.

Only applicable if :attr:`type` is one of, :attr:`InteractionType.component` or
:attr:`InteractionType.modal_submit`.

.. versionadded:: 2.7
"""
if self.type not in (InteractionType.component, InteractionType.modal_submit):
return None

data: Union[MessageComponentInteractionData, ModalSubmitInteractionData] = self.data # type: ignore
return data.get('custom_id')

def is_expired(self) -> bool:
""":class:`bool`: Returns ``True`` if the interaction is expired."""
return utils.utcnow() >= self.expires_at
Expand Down
Loading