mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-04-21 00:07:51 +00:00
Add interaction related typings
This commit is contained in:
parent
249d94a011
commit
a31c19563f
@ -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)
|
||||
|
||||
|
126
discord/types/interactions.py
Normal file
126
discord/types/interactions.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user