From 40281d016cde86aab42bf98a4e0ee6adc5402437 Mon Sep 17 00:00:00 2001 From: grey-cat-1908 Date: Fri, 20 May 2022 15:16:27 +0300 Subject: [PATCH] add parent properties --- melisa/listeners/channel_delete.py | 6 ++---- melisa/models/app/cache.py | 10 +++++----- melisa/models/guild/channel.py | 8 ++++++++ melisa/models/message/message.py | 19 +++++++++++++++++++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/melisa/listeners/channel_delete.py b/melisa/listeners/channel_delete.py index 28efe17..87d42f7 100644 --- a/melisa/listeners/channel_delete.py +++ b/melisa/listeners/channel_delete.py @@ -4,15 +4,13 @@ from __future__ import annotations from ..utils.types import Coro -from ..models.guild import Channel, ChannelType, channel_types_for_converting +from ..models.guild.channel import ChannelType, _choose_channel_type async def channel_delete_listener(self, gateway, payload: dict): payload.update({"type": ChannelType(payload.pop("type"))}) - channel_cls = channel_types_for_converting.get(payload["type"], Channel) - - channel = channel_cls.from_dict(payload) + channel = _choose_channel_type(payload) await self.dispatch("on_channel_delete", (channel,)) diff --git a/melisa/models/app/cache.py b/melisa/models/app/cache.py index 5a6ee13..b229fab 100644 --- a/melisa/models/app/cache.py +++ b/melisa/models/app/cache.py @@ -32,11 +32,11 @@ class CacheManager: self, *, disabled: bool = False, - auto_models: Optional[List[AutoCacheModels]] = None, + disabled_auto_models: Optional[List[AutoCacheModels]] = None, auto_unused_attributes: Optional[Dict[Any, List[str]]] = None, ): - self._auto_models: List[AutoCacheModels] = ( - [] if auto_models is None else auto_models + self._disabled_auto_models: List[AutoCacheModels] = ( + [] if disabled_auto_models is None else disabled_auto_models ) self.auto_unused_attributes: Dict[Any, List[str]] = ( {} if auto_unused_attributes is not None else auto_unused_attributes @@ -105,9 +105,9 @@ class CacheManager: if hasattr(guild, "channels"): channels = guild.channels.values() - if AutoCacheModels.TEXT_CHANNELS not in self._auto_models: + if AutoCacheModels.TEXT_CHANNELS not in self._disabled_auto_models: channels = filter( - lambda channel: channel.type != ChannelType.GUILD_TEXT, channels + lambda channel: channel.type == ChannelType.GUILD_TEXT, channels ) for sym in channels: diff --git a/melisa/models/guild/channel.py b/melisa/models/guild/channel.py index e68ef88..93cc0da 100644 --- a/melisa/models/guild/channel.py +++ b/melisa/models/guild/channel.py @@ -116,6 +116,8 @@ class Channel(APIModelBase): guild_id: :class:`~melisa.utils.types.Snowflake` The id of the guild (may be missing for some channel objects received over gateway guild dispatches) + guild: Optional[:class:`~melisa.models.guild.Guild`] + Object of guild where channel is position: :class:`int` Sorting position of the channel permission_overwrites: :class:`typing.Any` @@ -203,6 +205,12 @@ class Channel(APIModelBase): def mention(self): return f"<#{self.id}>" + @property + def guild(self): + if self.guild_id is not None: + return self._client.cache.get_guild(self.guild_id) + return None + async def edit(self, *, reason: Optional[str] = None, **kwargs): """|coro| Edit a channel with the specified keyword arguments. diff --git a/melisa/models/message/message.py b/melisa/models/message/message.py index 2761d36..919d4b2 100644 --- a/melisa/models/message/message.py +++ b/melisa/models/message/message.py @@ -184,6 +184,10 @@ class Message(APIModelBase): Id of the message channel_id: :class:`~melisa.utils.types.snowflake.Snowflake` Id of the channel the message was sent in + channel: :class:`~melisa.models.guild.Channel` + Object of channel where message was sent in + guild: :class:`~melisa.models.guild.Guild` + Object of guild where message was sent in guild_id: :class:`~melisa.utils.types.snowflake.Snowflake` Id of the guild the message was sent in author: :class:`typing.Any` @@ -351,6 +355,21 @@ class Message(APIModelBase): return self + @property + def guild(self): + if self.guild_id is not None: + return self._client.cache.get_guild(self.guild_id) + return None + + @property + def channel(self): + print(self.channel_id) + print(self._client.cache._channel_symlinks) + if self.channel_id is not None: + return self._client.cache.get_guild_channel(self.channel_id) + + return None + async def pin(self, *, reason: Optional[str] = None): """|coro|