diff --git a/discord/raw_models.py b/discord/raw_models.py index 0fdae813e..cda754d1b 100644 --- a/discord/raw_models.py +++ b/discord/raw_models.py @@ -22,6 +22,25 @@ 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 TYPE_CHECKING, Optional, Set, List + +if TYPE_CHECKING: + from .types.raw_models import ( + MessageDeleteEvent, + BulkMessageDeleteEvent, + ReactionActionEvent, + MessageUpdateEvent, + ReactionClearEvent, + ReactionClearEmojiEvent, + IntegrationDeleteEvent + ) + from .message import Message + from .partial_emoji import PartialEmoji + from .member import Member + + __all__ = ( 'RawMessageDeleteEvent', 'RawBulkMessageDeleteEvent', @@ -32,11 +51,13 @@ __all__ = ( 'RawIntegrationDeleteEvent', ) + class _RawReprMixin: - def __repr__(self): + def __repr__(self) -> str: value = ' '.join(f'{attr}={getattr(self, attr)!r}' for attr in self.__slots__) return f'<{self.__class__.__name__} {value}>' + class RawMessageDeleteEvent(_RawReprMixin): """Represents the event payload for a :func:`on_raw_message_delete` event. @@ -54,14 +75,15 @@ class RawMessageDeleteEvent(_RawReprMixin): __slots__ = ('message_id', 'channel_id', 'guild_id', 'cached_message') - def __init__(self, data): - self.message_id = int(data['id']) - self.channel_id = int(data['channel_id']) - self.cached_message = None + def __init__(self, data: MessageDeleteEvent) -> None: + self.message_id: int = int(data['id']) + self.channel_id: int = int(data['channel_id']) + self.cached_message: Optional[Message] = None try: - self.guild_id = int(data['guild_id']) + self.guild_id: Optional[int] = int(data['guild_id']) except KeyError: - self.guild_id = None + self.guild_id: Optional[int] = None + class RawBulkMessageDeleteEvent(_RawReprMixin): """Represents the event payload for a :func:`on_raw_bulk_message_delete` event. @@ -80,15 +102,16 @@ class RawBulkMessageDeleteEvent(_RawReprMixin): __slots__ = ('message_ids', 'channel_id', 'guild_id', 'cached_messages') - def __init__(self, data): - self.message_ids = {int(x) for x in data.get('ids', [])} - self.channel_id = int(data['channel_id']) - self.cached_messages = [] + def __init__(self, data: BulkMessageDeleteEvent) -> None: + self.message_ids: Set[int] = {int(x) for x in data.get('ids', [])} + self.channel_id: int = int(data['channel_id']) + self.cached_messages: List[Message] = [] try: - self.guild_id = int(data['guild_id']) + self.guild_id: Optional[int] = int(data['guild_id']) except KeyError: - self.guild_id = None + self.guild_id: Optional[int] = None + class RawMessageUpdateEvent(_RawReprMixin): """Represents the payload for a :func:`on_raw_message_edit` event. @@ -115,16 +138,17 @@ class RawMessageUpdateEvent(_RawReprMixin): __slots__ = ('message_id', 'channel_id', 'guild_id', 'data', 'cached_message') - def __init__(self, data): - self.message_id = int(data['id']) - self.channel_id = int(data['channel_id']) - self.data = data - self.cached_message = None + def __init__(self, data: MessageUpdateEvent) -> None: + self.message_id: int = int(data['id']) + self.channel_id: int = int(data['channel_id']) + self.data: MessageUpdateEvent = data + self.cached_message: Optional[Message] = None try: - self.guild_id = int(data['guild_id']) + self.guild_id: Optional[int] = int(data['guild_id']) except KeyError: - self.guild_id = None + self.guild_id: Optional[int] = None + class RawReactionActionEvent(_RawReprMixin): """Represents the payload for a :func:`on_raw_reaction_add` or @@ -158,18 +182,19 @@ class RawReactionActionEvent(_RawReprMixin): __slots__ = ('message_id', 'user_id', 'channel_id', 'guild_id', 'emoji', 'event_type', 'member') - def __init__(self, data, emoji, event_type): - self.message_id = int(data['message_id']) - self.channel_id = int(data['channel_id']) - self.user_id = int(data['user_id']) - self.emoji = emoji - self.event_type = event_type - self.member = None + def __init__(self, data: ReactionActionEvent, emoji: PartialEmoji, event_type: str) -> None: + self.message_id: int = int(data['message_id']) + self.channel_id: int = int(data['channel_id']) + self.user_id: int = int(data['user_id']) + self.emoji: PartialEmoji = emoji + self.event_type: str = event_type + self.member: Optional[Member] = None try: - self.guild_id = int(data['guild_id']) + self.guild_id: Optional[int] = int(data['guild_id']) except KeyError: - self.guild_id = None + self.guild_id: Optional[int] = None + class RawReactionClearEvent(_RawReprMixin): """Represents the payload for a :func:`on_raw_reaction_clear` event. @@ -186,14 +211,15 @@ class RawReactionClearEvent(_RawReprMixin): __slots__ = ('message_id', 'channel_id', 'guild_id') - def __init__(self, data): - self.message_id = int(data['message_id']) - self.channel_id = int(data['channel_id']) + def __init__(self, data: ReactionClearEvent) -> None: + self.message_id: int = int(data['message_id']) + self.channel_id: int = int(data['channel_id']) try: - self.guild_id = int(data['guild_id']) + self.guild_id: Optional[int] = int(data['guild_id']) except KeyError: - self.guild_id = None + self.guild_id: Optional[int] = None + class RawReactionClearEmojiEvent(_RawReprMixin): """Represents the payload for a :func:`on_raw_reaction_clear_emoji` event. @@ -214,15 +240,16 @@ class RawReactionClearEmojiEvent(_RawReprMixin): __slots__ = ('message_id', 'channel_id', 'guild_id', 'emoji') - def __init__(self, data, emoji): - self.emoji = emoji - self.message_id = int(data['message_id']) - self.channel_id = int(data['channel_id']) + def __init__(self, data: ReactionClearEmojiEvent, emoji: PartialEmoji) -> None: + self.emoji: PartialEmoji = emoji + self.message_id: int = int(data['message_id']) + self.channel_id: int = int(data['channel_id']) try: - self.guild_id = int(data['guild_id']) + self.guild_id: Optional[int] = int(data['guild_id']) except KeyError: - self.guild_id = None + self.guild_id: Optional[int] = None + class RawIntegrationDeleteEvent(_RawReprMixin): """Represents the payload for a :func:`on_raw_integration_delete` event. @@ -241,11 +268,11 @@ class RawIntegrationDeleteEvent(_RawReprMixin): __slots__ = ('integration_id', 'application_id', 'guild_id') - def __init__(self, data): - self.integration_id = int(data['id']) - self.guild_id = int(data['guild_id']) + def __init__(self, data: IntegrationDeleteEvent) -> None: + self.integration_id: int = int(data['id']) + self.guild_id: int = int(data['guild_id']) try: - self.application_id = int(data['application_id']) + self.application_id: Optional[int] = int(data['application_id']) except KeyError: - self.application_id = None + self.application_id: Optional[int] = None diff --git a/discord/types/raw_models.py b/discord/types/raw_models.py new file mode 100644 index 000000000..3c45b299c --- /dev/null +++ b/discord/types/raw_models.py @@ -0,0 +1,87 @@ +""" +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 typing import TypedDict, List +from .snowflake import Snowflake +from .member import Member +from .emoji import PartialEmoji + + +class _MessageEventOptional(TypedDict, total=False): + guild_id: Snowflake + + +class MessageDeleteEvent(_MessageEventOptional): + id: Snowflake + channel_id: Snowflake + + +class BulkMessageDeleteEvent(_MessageEventOptional): + ids: List[Snowflake] + channel_id: Snowflake + + +class _ReactionActionEventOptional(TypedDict, total=False): + guild_id: Snowflake + member: Member + + +class MessageUpdateEvent(_MessageEventOptional): + id: Snowflake + channel_id: Snowflake + + +class ReactionActionEvent(_ReactionActionEventOptional): + user_id: Snowflake + channel_id: Snowflake + message_id: Snowflake + emoji: PartialEmoji + + +class _ReactionClearEventOptional(TypedDict, total=False): + guild_id: Snowflake + + +class ReactionClearEvent(_ReactionClearEventOptional): + channel_id: Snowflake + message_id: Snowflake + + +class _ReactionClearEmojiEventOptional(TypedDict, total=False): + guild_id: Snowflake + + +class ReactionClearEmojiEvent(_ReactionClearEmojiEventOptional): + channel_id: int + message_id: int + emoji: PartialEmoji + + +class _IntegrationDeleteEventOptional(TypedDict, total=False): + application_id: Snowflake + + +class IntegrationDeleteEvent(_IntegrationDeleteEventOptional): + id: Snowflake + guild_id: Snowflake