Browse Source

Type hint instance variables in interactions

pull/7160/head
Rapptz 4 years ago
parent
commit
12e90f9c6d
  1. 28
      discord/interactions.py
  2. 25
      discord/types/interactions.py

28
discord/interactions.py

@ -46,6 +46,7 @@ __all__ = (
if TYPE_CHECKING:
from .types.interactions import (
Interaction as InteractionPayload,
InteractionData,
)
from .guild import Guild
from .state import ConnectionState
@ -107,23 +108,24 @@ class Interaction:
)
def __init__(self, *, data: InteractionPayload, state: ConnectionState):
self._state = state
self._state: ConnectionState = 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)
self.id: int = int(data['id'])
self.type: InteractionType = try_enum(InteractionType, data['type'])
self.data: Optional[InteractionData] = data.get('data')
self.token: str = data['token']
self.version: int = data['version']
self.channel_id: Optional[int] = utils._get_as_snowflake(data, 'channel_id')
self.guild_id: Optional[int] = utils._get_as_snowflake(data, 'guild_id')
self.application_id: Optional[int] = utils._get_as_snowflake(data, 'application_id')
channel = self.channel or Object(id=self.channel_id) # type: ignore
self.message: Optional[Message]
try:
self.message = Message(state=self._state, channel=channel, data=data['message'])
self.message = Message(state=self._state, channel=channel, data=data['message']) # type: ignore
except KeyError:
self.message = None
@ -133,7 +135,7 @@ class Interaction:
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'])
self.user = Member(state=self._state, guild=guild, data=data['member']) # type: ignore
except KeyError:
pass
else:

25
discord/types/interactions.py

@ -97,37 +97,27 @@ class _ApplicationCommandInteractionDataOption(TypedDict):
name: str
class _ApplicationCommandInteractionDataOptionSubcommand(
_ApplicationCommandInteractionDataOption
):
class _ApplicationCommandInteractionDataOptionSubcommand(_ApplicationCommandInteractionDataOption):
type: Literal[1, 2]
options: List[ApplicationCommandInteractionDataOption]
class _ApplicationCommandInteractionDataOptionString(
_ApplicationCommandInteractionDataOption
):
class _ApplicationCommandInteractionDataOptionString(_ApplicationCommandInteractionDataOption):
type: Literal[3]
value: str
class _ApplicationCommandInteractionDataOptionInteger(
_ApplicationCommandInteractionDataOption
):
class _ApplicationCommandInteractionDataOptionInteger(_ApplicationCommandInteractionDataOption):
type: Literal[4]
value: int
class _ApplicationCommandInteractionDataOptionBoolean(
_ApplicationCommandInteractionDataOption
):
class _ApplicationCommandInteractionDataOptionBoolean(_ApplicationCommandInteractionDataOption):
type: Literal[5]
value: bool
class _ApplicationCommandInteractionDataOptionSnowflake(
_ApplicationCommandInteractionDataOption
):
class _ApplicationCommandInteractionDataOptionSnowflake(_ApplicationCommandInteractionDataOption):
type: Literal[6, 7, 8, 9]
value: Snowflake
@ -174,8 +164,11 @@ class ComponentInteractionData(_ComponentInteractionDataOptional):
component_type: ComponentType
InteractionData = Union[ApplicationCommandInteractionData, ComponentInteractionData]
class _InteractionOptional(TypedDict, total=False):
data: Union[ApplicationCommandInteractionData, ComponentInteractionData]
data: InteractionData
guild_id: Snowflake
channel_id: Snowflake
member: Member

Loading…
Cancel
Save