BREAKING CHANGE: new channels model parsing method

This commit is contained in:
grey-cat-1908 2022-04-15 22:39:55 +03:00
parent d192833508
commit bdb4ffc074
3 changed files with 51 additions and 12 deletions

View file

@ -6,9 +6,6 @@
<hr>
<a class="github-badge" href="https://scrutinizer-ci.com/g/MelisaDev/melisa/" tabindex="-1">
<img src="https://scrutinizer-ci.com/g/MelisaDev/melisa/badges/quality-score.png?b=master" alt="Scrutinizer" />
</a>
<a class="github-badge" href="https://melisa.readthedocs.io/en/latest/?badge=latest" tabindex="-1">
<img src="https://readthedocs.org/projects/melisa/badge/?version=latest" alt="Documentation Status"/>
</a>

View file

@ -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
}

View file

@ -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