mirror of
https://github.com/MelisaDev/melisa.git
synced 2024-09-22 19:22:01 +03:00
feat(cache): some new methods (set_guild_channel_last_message_id, remove_guild_channel) and some new things with listeners
This commit is contained in:
parent
40281d016c
commit
cf9f926698
5 changed files with 101 additions and 11 deletions
|
@ -3,13 +3,17 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from ..models.guild.channel import _choose_channel_type
|
||||
from ..models.guild.channel import _choose_channel_type, ChannelType
|
||||
from ..utils.types import Coro
|
||||
|
||||
|
||||
async def channel_create_listener(self, gateway, payload: dict):
|
||||
channel = _choose_channel_type(payload)
|
||||
|
||||
# i am not sure that it is needed, but why not
|
||||
if channel.type not in [ChannelType.DM, ChannelType.GROUP_DM]:
|
||||
self.cache.set_guild_channel(channel)
|
||||
|
||||
await self.dispatch("on_channel_create", (channel,))
|
||||
|
||||
return
|
||||
|
|
|
@ -8,10 +8,10 @@ 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 = _choose_channel_type(payload)
|
||||
|
||||
self.cache.remove_guild_channel(channel.id)
|
||||
|
||||
await self.dispatch("on_channel_delete", (channel,))
|
||||
|
||||
return
|
||||
|
|
|
@ -3,19 +3,20 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from ..models.guild.channel import ChannelType, _choose_channel_type
|
||||
from ..utils.types import Coro
|
||||
from ..models.guild import Channel, ChannelType, channel_types_for_converting
|
||||
|
||||
|
||||
async def channel_update_listener(self, gateway, payload: dict):
|
||||
# ToDo: Replace None to the old channel object (so it requires cache manager)
|
||||
payload.update({"type": ChannelType(payload.pop("type"))})
|
||||
channel = _choose_channel_type(payload)
|
||||
old_channel = self.cache.get_guild_channel(channel.id)
|
||||
|
||||
channel_cls = channel_types_for_converting.get(payload["type"], Channel)
|
||||
# i am not sure that it is needed, but why not
|
||||
if channel.type not in [ChannelType.DM, ChannelType.GROUP_DM]:
|
||||
self.cache.set_guild_channel(channel)
|
||||
|
||||
channel = channel_cls.from_dict(payload)
|
||||
|
||||
await self.dispatch("on_channel_update", (None, channel))
|
||||
await self.dispatch("on_channel_update", (old_channel, channel))
|
||||
|
||||
return
|
||||
|
||||
|
|
|
@ -9,6 +9,11 @@ from ..utils.types import Coro
|
|||
|
||||
async def message_create_listener(self, gateway, payload: dict):
|
||||
message = Message.from_dict(payload)
|
||||
|
||||
self.cache.set_guild_channel_last_message_id(
|
||||
message.channel_id, message.guild_id, message.id
|
||||
)
|
||||
|
||||
await self.dispatch("on_message_create", (message,))
|
||||
|
||||
return
|
||||
|
|
|
@ -137,7 +137,9 @@ class CacheManager:
|
|||
if channel is None:
|
||||
return None
|
||||
|
||||
channel = self.__remove_unused_attributes(channel, Channel) # ToDo: add channel type
|
||||
channel = self.__remove_unused_attributes(
|
||||
channel, Channel
|
||||
) # ToDo: add channel type
|
||||
|
||||
guild = self._raw_guilds.get(channel.guild_id, UNDEFINED)
|
||||
|
||||
|
@ -211,7 +213,10 @@ class CacheManager:
|
|||
return
|
||||
|
||||
guilds_dict = dict(
|
||||
map(lambda i: (Snowflake(int(i["id"])), UnavailableGuild.from_dict(i)), guilds)
|
||||
map(
|
||||
lambda i: (Snowflake(int(i["id"])), UnavailableGuild.from_dict(i)),
|
||||
guilds,
|
||||
)
|
||||
)
|
||||
|
||||
self._raw_guilds.update(guilds_dict)
|
||||
|
@ -234,3 +239,78 @@ class CacheManager:
|
|||
guild_id = Snowflake(int(guild_id))
|
||||
|
||||
return self._raw_guilds.pop(guild_id, None)
|
||||
|
||||
def remove_guild_channel(self, channel_id: Union[Snowflake, str, int]):
|
||||
"""
|
||||
Remove guild channel from cache
|
||||
|
||||
Parameters
|
||||
----------
|
||||
channel_id: Optional[:class:`~melisa.utils.snowflake.Snowflake`, `str`, `int`]
|
||||
ID of guild channel to remove from cache.
|
||||
"""
|
||||
|
||||
if self._disabled:
|
||||
return
|
||||
|
||||
if not isinstance(channel_id, Snowflake):
|
||||
channel_id = Snowflake(int(channel_id))
|
||||
|
||||
guild_id = self._channel_symlinks.pop(channel_id, None)
|
||||
|
||||
if guild_id is None:
|
||||
return None
|
||||
|
||||
guild = self.get_guild(guild_id)
|
||||
|
||||
if guild is None:
|
||||
return None
|
||||
|
||||
if hasattr(guild, "channels") is False:
|
||||
return None
|
||||
|
||||
return guild.channels.pop(channel_id, None)
|
||||
|
||||
def set_guild_channel_last_message_id(
|
||||
self,
|
||||
channel_id: Union[Snowflake, str, int],
|
||||
guild_id: Union[Snowflake, str, int],
|
||||
message_id: Union[Snowflake, str, int],
|
||||
):
|
||||
"""
|
||||
Set guild channel last message id
|
||||
|
||||
Parameters
|
||||
----------
|
||||
channel_id: Optional[:class:`~melisa.utils.snowflake.Snowflake`, `str`, `int`]
|
||||
ID of channel to set.
|
||||
guild_id: Optional[:class:`~melisa.utils.snowflake.Snowflake`, `str`, `int`]
|
||||
ID of guild to set.
|
||||
message_id: Optional[:class:`~melisa.utils.snowflake.Snowflake`, `str`, `int`]
|
||||
ID of message to set.
|
||||
"""
|
||||
if not isinstance(channel_id, Snowflake):
|
||||
channel_id = Snowflake(int(channel_id))
|
||||
|
||||
if not isinstance(guild_id, Snowflake):
|
||||
guild_id = Snowflake(int(guild_id))
|
||||
|
||||
if not isinstance(message_id, Snowflake):
|
||||
message_id = Snowflake(int(message_id))
|
||||
|
||||
guild = self.get_guild(guild_id)
|
||||
|
||||
if guild is None:
|
||||
return None
|
||||
|
||||
if hasattr(guild, "channels") is False:
|
||||
return None
|
||||
|
||||
if guild.channels.get(channel_id) is None:
|
||||
return None
|
||||
|
||||
channel = self._raw_guilds[guild_id].channels[channel_id]
|
||||
|
||||
channel.last_message_id = message_id
|
||||
|
||||
self._raw_guilds[guild_id].channels[channel_id] = channel
|
||||
|
|
Loading…
Reference in a new issue