Improve component typing

This commit is contained in:
Lilly Rose Berner
2022-05-16 21:30:03 +02:00
committed by GitHub
parent b7e25645dc
commit 7267d18d9e
10 changed files with 131 additions and 69 deletions

View File

@@ -87,7 +87,7 @@ if TYPE_CHECKING:
from .types.gateway import MessageReactionRemoveEvent, MessageUpdateEvent
from .abc import Snowflake
from .abc import GuildChannel, MessageableChannel
from .components import Component
from .components import ActionRow, ActionRowChildComponentType
from .state import ConnectionState
from .channel import TextChannel
from .mentions import AllowedMentions
@@ -96,6 +96,7 @@ if TYPE_CHECKING:
from .ui.view import View
EmojiInputType = Union[Emoji, PartialEmoji, str]
MessageComponentType = Union[ActionRow, ActionRowChildComponentType]
__all__ = (
@@ -1340,7 +1341,7 @@ class Message(PartialMessage, Hashable):
A list of sticker items given to the message.
.. versionadded:: 1.6
components: List[:class:`Component`]
components: List[Union[:class:`ActionRow`, :class:`Button`, :class:`SelectMenu`]]
A list of components in the message.
.. versionadded:: 2.0
@@ -1392,6 +1393,7 @@ class Message(PartialMessage, Hashable):
mentions: List[Union[User, Member]]
author: Union[User, Member]
role_mentions: List[Role]
components: List[MessageComponentType]
def __init__(
self,
@@ -1418,7 +1420,6 @@ class Message(PartialMessage, Hashable):
self.content: str = data['content']
self.nonce: Optional[Union[int, str]] = data.get('nonce')
self.stickers: List[StickerItem] = [StickerItem(data=d, state=state) for d in data.get('sticker_items', [])]
self.components: List[Component] = [_component_factory(d) for d in data.get('components', [])]
try:
# if the channel doesn't have a guild attribute, we handle that
@@ -1460,7 +1461,7 @@ class Message(PartialMessage, Hashable):
# the channel will be the correct type here
ref.resolved = self.__class__(channel=chan, data=resolved, state=state) # type: ignore
for handler in ('author', 'member', 'mentions', 'mention_roles'):
for handler in ('author', 'member', 'mentions', 'mention_roles', 'components'):
try:
getattr(self, f'_handle_{handler}')(data[handler])
except KeyError:
@@ -1631,8 +1632,14 @@ class Message(PartialMessage, Hashable):
if role is not None:
self.role_mentions.append(role)
def _handle_components(self, components: List[ComponentPayload]):
self.components = [_component_factory(d) for d in components]
def _handle_components(self, data: List[ComponentPayload]) -> None:
self.components = []
for component_data in data:
component = _component_factory(component_data)
if component is not None:
self.components.append(component)
def _handle_interaction(self, data: MessageInteractionPayload):
self.interaction = MessageInteraction(state=self._state, guild=self.guild, data=data)