Repository: Rapptz/discord.py
File: discord/interactions.py (Python)

Code before:
        self,
        *,
        data: InteractionCallbackPayload,
        parent: Interaction[ClientT],
        state: ConnectionState,
        type: InteractionResponseType,
    ) -> None:
        self._state: ConnectionState = state
        self._parent: Interaction[ClientT] = parent
        self.type: InteractionResponseType = type
        self._update(data)

    def _update(self, data: InteractionCallbackPayload) -> None:
        interaction = data['interaction']

        self.id: int = int(interaction['id'])
        self._thinking: bool = interaction.get('response_message_loading', False)
        self._ephemeral: bool = interaction.get('response_message_ephemeral', False)

        self.message_id: Optional[int] = utils._get_as_snowflake(interaction, 'response_message_id')
        self.activity_id: Optional[str] = interaction.get('activity_instance_id')

        self.resource: Optional[Union[InteractionMessage, InteractionCallbackActivityInstance]] = None

        if 'resource' in data:
            resource = data['resource']

            self.type = try_enum(InteractionResponseType, resource['type'])

            if 'message' in resource and resource['message']:
                self.resource = InteractionMessage(
                    state=self._state,
                    channel=self._parent.channel,  # type: ignore # channel should be the correct type here
                    data=resource['message'],
                )
            elif 'activity_instance' in resource and resource['activity_instance']:
                self.resource = InteractionCallbackActivityInstance(
                    resource['activity_instance'],
                )

    def is_thinking(self) -> bool:
        """:class:`bool`: Whether the response was a thinking defer."""
        return self._thinking

    def is_ephemeral(self) -> bool:
        """:class:`bool`: Whether the response was ephemeral."""
        return self._ephemeral


class InteractionResponse(Generic[ClientT]):
    """Represents a Discord interaction response.

Review comment (suggestion):
```suggestion
        resource = data.get('resource')
        if resource is not None:
```

Avoid doing "double checking" such as `'key' in dict` if you can help it.

Code after:
        The activity ID of the resource. Only available if the resource is a :class:`InteractionCallbackActivityInstance`.
    """

    __slots__ = (
        '_state',
        '_parent',
        'type',
        'id',
        '_thinking',
        '_ephemeral',
        'message_id',
        'activity_id',
        'resource',
    )

    def __init__(
        self,
        *,
        data: InteractionCallbackPayload,
        parent: Interaction[ClientT],
        state: ConnectionState,
        type: InteractionResponseType,
    ) -> None:
        self._state: ConnectionState = state
        self._parent: Interaction[ClientT] = parent
        self.type: InteractionResponseType = type
        self._update(data)

    def _update(self, data: InteractionCallbackPayload) -> None:
        interaction = data['interaction']

        self.id: int = int(interaction['id'])
        self._thinking: bool = interaction.get('response_message_loading', False)
        self._ephemeral: bool = interaction.get('response_message_ephemeral', False)

        self.message_id: Optional[int] = utils._get_as_snowflake(interaction, 'response_message_id')
        self.activity_id: Optional[str] = interaction.get('activity_instance_id')

        self.resource: Optional[InteractionCallbackResource] = None

        resource = data.get('resource')
        if resource is not None:

            self.type = try_enum(InteractionResponseType, resource['type'])

            message = resource.get('message')
            activity_instance = resource.get('activity_instance')
            if message is not None:
                self.resource = InteractionMessage(
                    state=self._state,
                    channel=self._parent.channel,  # type: ignore # channel should be the correct type here