From 97a66090533163deaab73088426b47725cc499a2 Mon Sep 17 00:00:00 2001 From: Kae Bartlett Date: Thu, 18 Jan 2024 03:12:52 +0000 Subject: [PATCH] Fix guild ID cache counting --- novus/api/_cache.py | 11 ++--------- novus/api/gateway/dispatch.py | 1 + novus/api/gateway/gateway.py | 18 ------------------ novus/ext/client/cli/__main__.py | 4 ++-- 4 files changed, 5 insertions(+), 29 deletions(-) diff --git a/novus/api/_cache.py b/novus/api/_cache.py index ac532243..26cca597 100644 --- a/novus/api/_cache.py +++ b/novus/api/_cache.py @@ -32,7 +32,7 @@ __all__ = ( 'APICache', - 'NothingAPICache', + 'GuildIDCache', ) log = logging.getLogger("novus.api.cache") @@ -84,40 +84,33 @@ def do_nothing(instance: Any, *items: Any) -> None: pass def add_guilds(self, *items: Guild) -> None: - # log.debug("Caching guilds %s", items) for i in items: self.guild_ids.add(i.id) self.guilds[i.id] = i def add_users(self, *items: User) -> None: - # log.debug("Caching users %s", items) for i in items: self.users[i.id] = i def add_channels(self, *items: Channel) -> None: - # log.debug("Caching channels %s", items) for i in items: self.channels[i.id] = i def add_emojis(self, *items: Emoji) -> None: - # log.debug("Caching emojis %s", items) for i in items: if i.id is None: continue self.emojis[i.id] = i def add_stickers(self, *items: Sticker) -> None: - # log.debug("Caching stickers %s", items) for i in items: self.stickers[i.id] = i def add_events(self, *items: ScheduledEvent) -> None: - # log.debug("Caching events %s", items) for i in items: self.events[i.id] = i def add_messages(self, *items: Message) -> None: - # log.debug("Caching messages %s", items) for i in items: self.messages[i.id] = i @@ -167,7 +160,7 @@ def clear(self) -> None: self.events.clear() -class NothingAPICache(APICache): +class GuildIDCache(APICache): def add_guilds(self, *items: Guild) -> None: for i in items: diff --git a/novus/api/gateway/dispatch.py b/novus/api/gateway/dispatch.py index 887ef5d4..97e05119 100644 --- a/novus/api/gateway/dispatch.py +++ b/novus/api/gateway/dispatch.py @@ -209,6 +209,7 @@ async def _handle_guild_create_id_only( The non-cache version of guild create that we just have so as to keep a guild count internally. """ + self.cache.guild_ids.add(int(data["id"])) async def _handle_guild_update( diff --git a/novus/api/gateway/gateway.py b/novus/api/gateway/gateway.py index 508d9c23..33d18600 100644 --- a/novus/api/gateway/gateway.py +++ b/novus/api/gateway/gateway.py @@ -520,24 +520,6 @@ async def _connect( await self.close(0) reconnect = True - # Cache the connect data - if reconnect is False: - - # This is our first connection; let's update the cache based on the - # intents - if not self.intents.guilds: - self.cache.add_guilds = self.cache.do_nothing - self.cache.add_channels = self.cache.do_nothing - if not self.intents.guild_members: - self.cache.add_users = self.cache.do_nothing - if not self.intents.guild_scheduled_events: - self.cache.add_events = self.cache.do_nothing - if not self.intents.guild_emojis_and_stickers: - self.cache.add_emojis = self.cache.do_nothing - self.cache.add_stickers = self.cache.do_nothing - if not self.intents.message_content: - self.cache.add_messages = self.cache.do_nothing - # Open socket if reconnect is False: self.sequence = None diff --git a/novus/ext/client/cli/__main__.py b/novus/ext/client/cli/__main__.py index 15c54ab0..d2bf2de2 100644 --- a/novus/ext/client/cli/__main__.py +++ b/novus/ext/client/cli/__main__.py @@ -28,7 +28,7 @@ from aioconsole import AsynchronousCli import novus -from novus.api._cache import NothingAPICache +from novus.api._cache import GuildIDCache from novus.ext import client if TYPE_CHECKING: @@ -264,7 +264,7 @@ async def main(args: Namespace, unknown: list[str]) -> None: config.plugins = [] config.intents = novus.Intents(guilds=True) bot = client.Client(config) - bot.state.cache = NothingAPICache(bot.state) + bot.state.cache = GuildIDCache(bot.state) bot.state.gateway.guild_ids_only = True bot.add_plugin(GuildLogger) await bot.run(sync=False)