diff --git a/melisa/client.py b/melisa/client.py index 146c9f9..4bd846f 100644 --- a/melisa/client.py +++ b/melisa/client.py @@ -47,7 +47,7 @@ class Client: self._events: Dict[str, Coro] = {} # ToDo: Transfer guilds in to the cache manager - self.guilds = [] + self.guilds = {} self.user = None self._loop = asyncio.get_event_loop() @@ -59,6 +59,7 @@ class Client: self._activity = activity self._status = status + self._none_guilds_cached = False APIModelBase.set_client(self) diff --git a/melisa/listeners/guild_create.py b/melisa/listeners/guild_create.py index c0a7ade..f183a02 100644 --- a/melisa/listeners/guild_create.py +++ b/melisa/listeners/guild_create.py @@ -3,18 +3,28 @@ from __future__ import annotations +import asyncio + from ..utils.types import Coro +from ..models.guild import Guild async def guild_create_listener(self, gateway, payload: dict): gateway.session_id = payload.get("session_id") - self.guilds[payload["id"]] = payload + guild_was_cached_as_none = False + + guild = Guild.from_dict(payload) + + if self.guilds.get(guild.id, "empty") != "empty": + guild_was_cached_as_none = True + + self.guilds[guild.id] = guild custom_listener = self._events.get("on_guild_create") - if custom_listener is not None: - await custom_listener(payload) # ToDo: Guild Model + if custom_listener is not None and guild_was_cached_as_none is False: + asyncio.ensure_future(custom_listener(guild)) return diff --git a/melisa/listeners/ready.py b/melisa/listeners/ready.py index e582f47..7398e3b 100644 --- a/melisa/listeners/ready.py +++ b/melisa/listeners/ready.py @@ -14,7 +14,10 @@ async def on_ready_listener(self, gateway, payload: dict): guilds = payload.get("guilds") - self.guilds = dict(map(lambda i: (i["id"], None), guilds)) + if self._none_guilds_cached is False: + self.guilds = dict(map(lambda i: (i["id"], None), guilds)) + self._none_guilds_cached = True + self.user = User.from_dict(payload.get("user")) custom_listener = self._events.get("on_shard_ready")