Skip to content

Commit

Permalink
Fix guild ID cache counting
Browse files Browse the repository at this point in the history
  • Loading branch information
4Kaylum committed Jan 18, 2024
1 parent 2eeefda commit 97a6609
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 29 deletions.
11 changes: 2 additions & 9 deletions novus/api/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

__all__ = (
'APICache',
'NothingAPICache',
'GuildIDCache',
)

log = logging.getLogger("novus.api.cache")
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions novus/api/gateway/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 0 additions & 18 deletions novus/api/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions novus/ext/client/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 97a6609

Please sign in to comment.