diff --git a/discord/interactions.py b/discord/interactions.py index a235a0a6f..ccdac7926 100644 --- a/discord/interactions.py +++ b/discord/interactions.py @@ -25,6 +25,7 @@ DEALINGS IN THE SOFTWARE. """ from __future__ import annotations +from typing import Optional, TYPE_CHECKING from . import utils from .enums import try_enum, InteractionType @@ -33,6 +34,14 @@ __all__ = ( 'Interaction', ) +if TYPE_CHECKING: + from .types.interactions import ( + Interaction as InteractionPayload, + ) + from .guild import Guild + from .abc import GuildChannel + + class Interaction: """Represents a Discord interaction. @@ -60,6 +69,7 @@ class Interaction: The token to continue the interaction. These are valid for 15 minutes. """ + __slots__ = ( 'id', 'type', @@ -73,11 +83,11 @@ class Interaction: '_state', ) - def __init__(self, *, data, state=None): + def __init__(self, *, data: InteractionPayload, state=None): self._state = state self._from_data(data) - def _from_data(self, data): + def _from_data(self, data: InteractionPayload): self.id = int(data['id']) self.type = try_enum(InteractionType, data['type']) self.data = data.get('data') @@ -88,12 +98,12 @@ class Interaction: self.application_id = utils._get_as_snowflake(data, 'application_id') @property - def guild(self): + 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): + 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 @@ -101,4 +111,3 @@ class Interaction: """ guild = self.guild return guild and guild.get_channel(self.channel_id) - diff --git a/discord/types/interactions.py b/discord/types/interactions.py new file mode 100644 index 000000000..6c7c26546 --- /dev/null +++ b/discord/types/interactions.py @@ -0,0 +1,126 @@ +""" +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 typing import TypedDict, Union, List, Literal +from .snowflake import Snowflake +from .message import AllowedMentions +from .embed import Embed +from .member import Member +from .user import User + + +class _ApplicationCommandOptional(TypedDict, total=False): + options: List[ApplicationCommandOption] + + +class ApplicationCommand(_ApplicationCommandOptional): + id: Snowflake + application_id: Snowflake + name: str + description: str + + +class _ApplicationCommandOptionOptional(TypedDict, total=False): + choices: List[ApplicationCommandOptionChoice] + options: List[ApplicationCommandOption] + + +ApplicationCommandOptionType = Literal[1, 2, 3, 4, 5, 6, 7, 8] + + +class ApplicationCommandOption(_ApplicationCommandOptionOptional): + type: ApplicationCommandOptionType + name: str + description: str + required: bool + + +class ApplicationCommandOptionChoice(TypedDict): + name: str + value: Union[str, int] + + +InteractionType = Literal[1, 2] + + +class _ApplicationCommandInteractionDataOptionOptional(TypedDict, total=False): + value: ApplicationCommandOptionType + options: List[ApplicationCommandInteractionDataOption] + + +class ApplicationCommandInteractionDataOption(_ApplicationCommandInteractionDataOptionOptional): + name: str + + +class _ApplicationCommandInteractionDataOptional(TypedDict, total=False): + options: List[ApplicationCommandInteractionDataOption] + + +class ApplicationCommandInteractionData(_ApplicationCommandInteractionDataOptional): + id: Snowflake + name: str + + +class _InteractionOptional(TypedDict, total=False): + data: ApplicationCommandInteractionData + guild_id: Snowflake + channel_id: Snowflake + member: Member + user: User + + +class Interaction(_InteractionOptional): + id: Snowflake + application_id: Snowflake + type: InteractionType + token: str + version: int + + +class InteractionApplicationCommandCallbackData(TypedDict, total=False): + tts: bool + content: str + embeds: List[Embed] + allowed_mentions: AllowedMentions + flags: int + + +InteractionResponseType = Literal[1, 2, 3, 4, 5] + + +class _InteractionResponseOptional(TypedDict, total=False): + data: InteractionApplicationCommandCallbackData + + +class InteractionResponse(_InteractionResponseOptional): + type: InteractionResponseType + + +class MessageInteraction(TypedDict): + id: Snowflake + type: InteractionType + name: str + user: User