# -*- coding: utf-8 -*- """ The MIT License (MIT) Copyright (c) 2015-present Rapptz Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ from __future__ import annotations from discord.types.interactions import InteractionResponse from typing import Any, Dict, List, Optional, TYPE_CHECKING, Tuple, Union from . import utils from .enums import try_enum, InteractionType, InteractionResponseType from .user import User from .member import Member from .message import Message, Attachment from .object import Object from .webhook.async_ import async_context, Webhook __all__ = ( 'Interaction', 'InteractionResponse', ) if TYPE_CHECKING: from .types.interactions import ( Interaction as InteractionPayload, ) from .guild import Guild from .abc import GuildChannel from .state import ConnectionState from aiohttp import ClientSession from .embeds import Embed from .ui.view import View MISSING: Any = utils.MISSING class Interaction: """Represents a Discord interaction. An interaction happens when a user does an action that needs to be notified. Current examples are slash commands but future examples include forms and buttons. .. versionadded:: 2.0 Attributes ----------- id: :class:`int` The interaction's ID. type: :class:`InteractionType` The interaction type. guild_id: Optional[:class:`int`] The guild ID the interaction was sent from. channel_id: Optional[:class:`int`] The channel ID the interaction was sent from. application_id: :class:`int` The application ID that the interaction was for. user: Optional[Union[:class:`User`, :class:`Member`]] The user or member that sent the interaction. message: Optional[:class:`Message`] The message that sent this interaction. token: :class:`str` The token to continue the interaction. These are valid for 15 minutes. """ __slots__: Tuple[str, ...] = ( 'id', 'type', 'guild_id', 'channel_id', 'data', 'application_id', 'message', 'user', 'token', 'version', '_state', '_session', '_cs_response', '_cs_followup', ) def __init__(self, *, data: InteractionPayload, state: ConnectionState): self._state = state self._session: ClientSession = state.http._HTTPClient__session self._from_data(data) def _from_data(self, data: InteractionPayload): self.id = int(data['id']) self.type = try_enum(InteractionType, data['type']) self.data = data.get('data') self.token = data['token'] self.version = data['version'] self.channel_id = utils._get_as_snowflake(data, 'channel_id') self.guild_id = utils._get_as_snowflake(data, 'guild_id') self.application_id = utils._get_as_snowflake(data, 'application_id') channel = self.channel or Object(id=self.channel_id) try: self.message = Message(state=self._state, channel=channel, data=data['message']) except KeyError: self.message = None self.user: Optional[Union[User, Member]] = None # TODO: there's a potential data loss here if self.guild_id: guild = self.guild or Object(id=self.guild_id) try: self.user = Member(state=self._state, guild=guild, data=data['member']) except KeyError: pass else: try: self.user = User(state=self._state, data=data['user']) except KeyError: pass @property def guild(self) -> Optional[Guild]: """Optional[:class:`Guild`]: The guild the interaction was sent from.""" return self._state and self._state._get_guild(self.guild_id) @property def channel(self) -> Optional[GuildChannel]: """Optional[:class:`abc.GuildChannel`]: The channel the interaction was sent from. Note that due to a Discord limitation, DM channels are not resolved since there is no data to complete them. """ guild = self.guild return guild and guild.get_channel(self.channel_id) @utils.cached_slot_property('_cs_response') def response(self) -> InteractionResponse: """:class:`InteractionResponse`: Returns an object responsible for handling responding to the interaction.""" return InteractionResponse(self) @utils.cached_slot_property('_cs_followup') def followup(self) -> Webhook: """:class:`Webhook`: Returns the follow up webhook for follow up interactions.""" payload = { 'id': self.application_id, 'type': 3, 'token': self.token, } return Webhook.from_state(data=payload, state=self._state) class InteractionResponse: """Represents a Discord interaction response. This type can be accessed through :attr:`Interaction.response`. .. versionadded:: 2.0 """ __slots__: Tuple[str, ...] = ( '_responded', '_parent', ) def __init__(self, parent: Interaction): self._parent: Interaction = parent self._responded: bool = False async def defer(self, *, ephemeral: bool = False) -> None: """|coro| Defers the interaction response. This is typically used when the interaction is acknowledged and a secondary action will be done later. Parameters ----------- ephemeral: :class:`bool` Indicates whether the deferred message will eventually be ephemeral. This only applies for interactions of type :attr:`InteractionType.application_command`. Raises ------- HTTPException Deferring the interaction failed. """ if self._responded: return defer_type: int = 0 data: Optional[Dict[str, Any]] = None parent = self._parent if parent.type is InteractionType.component: defer_type = InteractionResponseType.deferred_message_update.value elif parent.type is InteractionType.application_command: defer_type = InteractionResponseType.deferred_channel_message.value if ephemeral: data = {'flags': 64} if defer_type: adapter = async_context.get() await adapter.create_interaction_response( parent.id, parent.token, session=parent._session, type=defer_type, data=data ) self._responded = True async def pong(self) -> None: """|coro| Pongs the ping interaction. This should rarely be used. Raises ------- HTTPException Ponging the interaction failed. """ if self._responded: return parent = self._parent if parent.type is InteractionType.ping: adapter = async_context.get() await adapter.create_interaction_response( parent.id, parent.token, session=parent._session, type=InteractionResponseType.pong.value ) self._responded = True async def send_message( self, content: Optional[Any] = None, *, embed: Embed = MISSING, embeds: List[Embed] = MISSING, view: View = MISSING, tts: bool = False, ephemeral: bool = False, ) -> None: """|coro| Responds to this interaction by sending a message. Parameters ----------- content: Optional[:class:`str`] The content of the message to send. embeds: List[:class:`Embed`] A list of embeds to send with the content. Maximum of 10. This cannot be mixed with the ``embed`` parameter. embed: :class:`Embed` The rich embed for the content to send. This cannot be mixed with ``embeds`` parameter. tts: :class:`bool` Indicates if the message should be sent using text-to-speech. view: :class:`discord.ui.View` The view to send with the message. ephemeral: :class:`bool` Indicates if the message should only be visible to the user who started the interaction. If a view is sent with an ephemeral message and it has no timeout set then the timeout is set to 15 minutes. Raises ------- HTTPException Sending the message failed. TypeError You specified both ``embed`` and ``embeds``. ValueError The length of ``embeds`` was invalid. """ if self._responded: return payload: Dict[str, Any] = { 'tts': tts, } if embed is not MISSING and embeds is not MISSING: raise TypeError('cannot mix embed and embeds keyword arguments') if embed is not MISSING: embeds = [embed] if embeds: if len(embeds) > 10: raise ValueError('embeds cannot exceed maximum of 10 elements') payload['embeds'] = [e.to_dict() for e in embeds] if content is not None: payload['content'] = str(content) if ephemeral: payload['flags'] = 64 if view is not MISSING: payload['components'] = view.to_components() parent = self._parent adapter = async_context.get() await adapter.create_interaction_response( parent.id, parent.token, session=parent._session, type=InteractionResponseType.channel_message.value, data=payload, ) if view is not MISSING: if ephemeral and view.timeout is None: view.timeout = 15 * 60.0 self._parent._state.store_view(view) self._responded = True async def edit_message( self, *, content: Optional[Any] = MISSING, embed: Optional[Embed] = MISSING, embeds: List[Embed] = MISSING, attachments: List[Attachment] = MISSING, view: Optional[View] = MISSING, ) -> None: """|coro| Responds to this interaction by editing the original message of a component interaction. Parameters ----------- content: Optional[:class:`str`] The new content to replace the message with. ``None`` removes the content. embeds: List[:class:`Embed`] A list of embeds to edit the message with. embed: Optional[:class:`Embed`] The embed to edit the message with. ``None`` suppresses the embeds. This should not be mixed with the ``embeds`` parameter. attachments: List[:class:`Attachment`] A list of attachments to keep in the message. If ``[]`` is passed then all attachments are removed. view: Optional[:class:`~discord.ui.View`] The updated view to update this message with. If ``None`` is passed then the view is removed. Raises ------- HTTPException Editing the message failed. TypeError You specified both ``embed`` and ``embeds``. """ if self._responded: return parent = self._parent msg = parent.message state = parent._state message_id = msg.id if msg else None if parent.type is not InteractionType.component: return # TODO: embeds: List[Embed]? payload = {} if content is not MISSING: if content is None: payload['content'] = None else: payload['content'] = str(content) if embed is not MISSING and embeds is not MISSING: raise TypeError('cannot mix both embed and embeds keyword arguments') if embed is not MISSING: if embed is None: embeds = [] else: embeds = [embed] if embeds is not MISSING: payload['embeds'] = [e.to_dict() for e in embeds] if attachments is not MISSING: payload['attachments'] = [a.to_dict() for a in attachments] if view is not MISSING: state.prevent_view_updates_for(message_id) if view is None: payload['components'] = [] else: payload['components'] = view.to_components() adapter = async_context.get() await adapter.create_interaction_response( parent.id, parent.token, session=parent._session, type=InteractionResponseType.message_update.value, data=payload, ) if view and not view.is_finished(): state.store_view(view, message_id) self._responded = True