Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command target to MessageInteractionMetadata #10004

Merged
merged 4 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
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
40 changes: 36 additions & 4 deletions discord/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,14 @@
The ID of the message that containes the interactive components, if applicable.
modal_interaction: Optional[:class:`.MessageInteractionMetadata`]
The metadata of the modal submit interaction that triggered this interaction, if applicable.
target_user: Optional[:class:`User`]
The user the command was run on, only applicable to user context menus.

.. versionadded:: 2.5
target_message_id: Optional[:class:`int`]
The ID of the message the command was run on, only applicable to message context menus.

.. versionadded:: 2.5
"""

__slots__: Tuple[str, ...] = (
Expand All @@ -837,6 +845,8 @@
'original_response_message_id',
'interacted_message_id',
'modal_interaction',
'target_user',
'target_message_id',
'_integration_owners',
'_state',
'_guild',
Expand All @@ -848,31 +858,43 @@

self.id: int = int(data['id'])
self.type: InteractionType = try_enum(InteractionType, data['type'])
self.user = state.create_user(data['user'])
self.user: User = state.create_user(data['user'])
self._integration_owners: Dict[int, int] = {
int(key): int(value) for key, value in data.get('authorizing_integration_owners', {}).items()
}

self.original_response_message_id: Optional[int] = None
try:
self.original_response_message_id = int(data['original_response_message_id'])
self.original_response_message_id = int(data['original_response_message_id']) # type: ignore # EAFP

Check warning on line 868 in discord/message.py

View workflow job for this annotation

GitHub Actions / check 3.x

Unnecessary "# type: ignore" comment
except KeyError:
pass

self.interacted_message_id: Optional[int] = None
try:
self.interacted_message_id = int(data['interacted_message_id'])
self.interacted_message_id = int(data['interacted_message_id']) # type: ignore # EAFP
except KeyError:
pass

self.modal_interaction: Optional[MessageInteractionMetadata] = None
try:
self.modal_interaction = MessageInteractionMetadata(
state=state, guild=guild, data=data['triggering_interaction_metadata']
state=state, guild=guild, data=data['triggering_interaction_metadata'] # type: ignore # EAFP
)
except KeyError:
pass

self.target_user: Optional[User] = None
try:
self.target_user = state.create_user(data['target_user']) # type: ignore # EAFP
except KeyError:
pass

self.target_message_id: Optional[int] = None
try:
self.target_message_id = int(data['target_message_id']) # type: ignore # EAFP
except KeyError:
pass

def __repr__(self) -> str:
return f'<MessageInteraction id={self.id} type={self.type!r} user={self.user!r}>'

Expand All @@ -899,6 +921,16 @@
return self._state._get_message(self.interacted_message_id)
return None

@property
def target_message(self) -> Optional[Message]:
"""Optional[:class:`~discord.Message`]: The target message, if applicable and is found in cache.

.. versionadded:: 2.5
"""
if self.target_message_id:
return self._state._get_message(self.target_message_id)
return None

def is_guild_integration(self) -> bool:
""":class:`bool`: Returns ``True`` if the interaction is a guild integration."""
if self._guild:
Expand Down
46 changes: 42 additions & 4 deletions discord/types/interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,49 @@ class MessageInteraction(TypedDict):
member: NotRequired[Member]


class MessageInteractionMetadata(TypedDict):
class _MessageInteractionMetadata(TypedDict):
id: Snowflake
type: InteractionType
user: User
authorizing_integration_owners: Dict[Literal['0', '1'], Snowflake]
original_response_message_id: NotRequired[Snowflake]
interacted_message_id: NotRequired[Snowflake]
triggering_interaction_metadata: NotRequired[MessageInteractionMetadata]


class _ApplicationCommandMessageInteractionMetadata(_MessageInteractionMetadata):
type: Literal[2]
# command_type: Literal[1, 2, 3, 4]


class UserApplicationCommandMessageInteractionMetadata(_ApplicationCommandMessageInteractionMetadata):
# command_type: Literal[2]
target_user: User


class MessageApplicationCommandMessageInteractionMetadata(_ApplicationCommandMessageInteractionMetadata):
# command_type: Literal[3]
target_message_id: Snowflake


ApplicationCommandMessageInteractionMetadata = Union[
_ApplicationCommandMessageInteractionMetadata,
UserApplicationCommandMessageInteractionMetadata,
MessageApplicationCommandMessageInteractionMetadata,
]


class MessageComponentMessageInteractionMetadata(_MessageInteractionMetadata):
type: Literal[3]
interacted_message_id: Snowflake


class ModalSubmitMessageInteractionMetadata(_MessageInteractionMetadata):
type: Literal[5]
triggering_interaction_metadata: Union[
ApplicationCommandMessageInteractionMetadata, MessageComponentMessageInteractionMetadata
]


MessageInteractionMetadata = Union[
ApplicationCommandMessageInteractionMetadata,
MessageComponentMessageInteractionMetadata,
ModalSubmitMessageInteractionMetadata,
]
Loading