From bdb4ffc074076af0a979eb90ac02959ccc6d3d48 Mon Sep 17 00:00:00 2001 From: grey-cat-1908 Date: Fri, 15 Apr 2022 22:39:55 +0300 Subject: [PATCH] BREAKING CHANGE: new channels model parsing method --- README.md | 3 -- melisa/models/guild/channel.py | 59 +++++++++++++++++++++++++++++----- melisa/models/guild/guild.py | 1 - 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5607a4d..f21a201 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,6 @@
- -Scrutinizer - Documentation Status diff --git a/melisa/models/guild/channel.py b/melisa/models/guild/channel.py index 7f6e737..6077e22 100644 --- a/melisa/models/guild/channel.py +++ b/melisa/models/guild/channel.py @@ -28,6 +28,13 @@ if TYPE_CHECKING: from .thread import ThreadMember, ThreadMetadata +def _choose_channel_type(data): + data.update({"type": ChannelType(data.pop("type"))}) + + channel_cls = channel_types_for_converting.get(data["type"], NoneTypedChannel) + return channel_cls.from_dict(data) + + class ChannelType(IntEnum): """Channel Type NOTE: Type 10, 11 and 12 are only available in Discord API v9. @@ -97,6 +104,8 @@ class VideoQualityModes(IntEnum): class Channel(APIModelBase): """Represents a guild or DM channel within Discord + **It will be never returned!** + Attributes ---------- id: :class:`~melisa.utils.types.Snowflake` @@ -213,12 +222,9 @@ class Channel(APIModelBase): headers={"X-Audit-Log-Reason": reason}, ) - data.update({"type": ChannelType(data.pop("type"))}) + return _choose_channel_type(data) - channel_cls = channel_types_for_converting.get(data["type"], Channel) - return channel_cls.from_dict(data) - - async def delete(self, *, reason: Optional[str] = None) -> Dict[str, Any]: + async def delete(self, *, reason: Optional[str] = None): """|coro| Delete a channel, or close a private message. @@ -247,7 +253,7 @@ class Channel(APIModelBase): f"/channels/{self.id}", headers={"X-Audit-Log-Reason": reason} ) - return Channel.from_dict(data) + return _choose_channel_type(data) class MessageableChannel(Channel): @@ -602,7 +608,7 @@ class MessageableChannel(Channel): count = 0 async for message in iterator: - message_ids.append(message["id"]) + message_ids.append(message.id) count += 1 if count == 100: @@ -678,6 +684,40 @@ class MessageableChannel(Channel): ) +class NoneTypedChannel(Channel): + """It represents a channel, that is unknown, + so we don't know this type of the channel, + And also we can't convert it to something, but it has + every method, that Channel has. + + You can use ``raw`` attribute to access to the original data, + returned from the discord. + + Attributes + ---------- + id: :class:`~melisa.utils.snowflake.Snowflake` + Id of the channel + raw: Dict[:class:`str`, Any] + Raw value channel data (returned from the discord) + """ + + @classmethod + def from_dict(cls, data: Dict[str, Any]): + """Generate a channel with unknown type from the given data. + + Parameters + ---------- + data: :class:`dict` + The dictionary to convert into an unknown channel. + """ + self: NoneTypedChannel = super().__new__(cls) + + self.id = data["id"] + self.raw = data + + return self + + class TextChannel(MessageableChannel): """A subclass of ``Channel`` representing text channels with all the same attributes.""" @@ -960,5 +1000,8 @@ class ThreadsList(APIModelBase): # noinspection PyTypeChecker channel_types_for_converting: Dict[ChannelType, Channel] = { - ChannelType.GUILD_TEXT: TextChannel + ChannelType.GUILD_TEXT: TextChannel, + ChannelType.GUILD_NEWS_THREAD: Thread, + ChannelType.GUILD_PUBLIC_THREAD: Thread, + ChannelType.GUILD_PRIVATE_THREAD: Thread } diff --git a/melisa/models/guild/guild.py b/melisa/models/guild/guild.py index 6e0da42..55da1ee 100644 --- a/melisa/models/guild/guild.py +++ b/melisa/models/guild/guild.py @@ -320,7 +320,6 @@ class Guild(APIModelBase): system_channel_flags: APINullable[int] = UNDEFINED rules_channel_id: APINullable[Snowflake] = UNDEFINED joined_at: APINullable[Timestamp] = UNDEFINED - # TODO: Deal with joined_at large: APINullable[bool] = UNDEFINED unavailable: APINullable[bool] = UNDEFINED