Skip to content

Commit

Permalink
Fix guild sync not working properly
Browse files Browse the repository at this point in the history
  • Loading branch information
4Kaylum committed Sep 3, 2023
1 parent da5bdce commit 2565c89
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
10 changes: 10 additions & 0 deletions novus/api/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from __future__ import annotations

import logging
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, overload

Expand All @@ -34,6 +35,8 @@
'NothingAPICache',
)

log = logging.getLogger("novus.api.cache")


class MaxLenDict(OrderedDict):

Expand Down Expand Up @@ -81,33 +84,40 @@ 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
24 changes: 16 additions & 8 deletions novus/models/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -2071,56 +2071,64 @@ async def _sync(self, data: payloads.GuildSyncable) -> Self:
if "emojis" in data:
new_cache = {}
for d in data["emojis"]:
self._add_emoji(d, new_cache)
new = self._add_emoji(d, new_cache)
new_cache[new.id] = new
await asyncio.sleep(0)
self._emojis = new_cache

if "stickers" in data:
new_cache = {}
for d in data["stickers"]:
self._add_sticker(d, new_cache)
new = self._add_sticker(d, new_cache)
new_cache[new.id] = new
await asyncio.sleep(0)
self._stickers = new_cache

if "roles" in data:
new_cache = {}
for d in data["roles"]:
self._add_role(d, new_cache)
new = self._add_role(d, new_cache)
new_cache[new.id] = new
await asyncio.sleep(0)
self._roles = new_cache

if "members" in data:
new_cache = {}
for d in data["members"]:
self._add_member(d, new_cache)
new = self._add_member(d, new_cache)
new_cache[new.id] = new
await asyncio.sleep(0)
self._members = new_cache

if "guild_scheduled_events" in data:
new_cache = {}
for d in data["guild_scheduled_events"]:
self._add_guild_scheduled_event(d, new_cache)
new = self._add_guild_scheduled_event(d, new_cache)
new_cache[new.id] = new
await asyncio.sleep(0)
self._guild_scheduled_events = new_cache

if "threads" in data:
new_cache = {}
for d in data["threads"]:
self._add_thread(d, new_cache)
new = self._add_thread(d, new_cache)
new_cache[new.id] = new
await asyncio.sleep(0)
self._threads = new_cache

if "voice_states" in data:
new_cache = {}
for d in data["voice_states"]:
self._add_voice_state(d, new_cache)
new = self._add_voice_state(d, new_cache)
new_cache[new.id] = new
await asyncio.sleep(0)
self._voice_states = new_cache

if "channels" in data:
new_cache = {}
for d in data["channels"]:
self._add_channel(d, new_cache)
new = self._add_channel(d, new_cache)
new_cache[new.id] = new
await asyncio.sleep(0)
self._channels = new_cache

Expand Down

0 comments on commit 2565c89

Please sign in to comment.