Skip to content

Commit 19a2eb6

Browse files
authored
fix: Fix another bug with cache (#944)
* fix: Fix another bug * fix: Fix bug
1 parent 228697e commit 19a2eb6

File tree

1 file changed

+59
-18
lines changed

1 file changed

+59
-18
lines changed

interactions/api/gateway/client.py

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -402,37 +402,76 @@ def _dispatch_event(self, event: str, data: dict) -> None:
402402
data["_client"] = self._http
403403
obj = model(**data)
404404

405+
_cache: "Storage" = self._http.cache[model]
406+
407+
if isinstance(obj, Member):
408+
id = (Snowflake(data["guild_id"]), obj.id)
409+
else:
410+
id = getattr(obj, "id", None)
411+
412+
if id is None:
413+
if model.__name__.startswith("Guild"):
414+
if model.__name__ == "GuildScheduledEventUser":
415+
id = model.guild_scheduled_event_id
416+
elif model.__name__ in [
417+
"Invite",
418+
"GuildBan",
419+
"ChannelPins",
420+
"MessageReaction",
421+
"ReactionRemove",
422+
# Extend this for everything that should not be cached
423+
]:
424+
id = None
425+
else:
426+
model_name = model.__name__[5:]
427+
if _data := getattr(obj, model_name, None):
428+
id = (
429+
getattr(_data, "id")
430+
if not isinstance(_data, dict)
431+
else Snowflake(_data["id"])
432+
)
433+
elif hasattr(obj, f"{model_name}_id"):
434+
id = getattr(obj, f"{model_name}_id")
435+
else:
436+
id = None
437+
405438
def __modify_guild_cache():
406439
if not (
407440
(guild_id := data.get("guild_id"))
408441
and not isinstance(obj, Guild)
409442
and "message" not in name
443+
and id is not None
410444
):
411445
return
412446
if guild := self._http.cache[Guild].get(Snowflake(guild_id)):
413-
model_name = model.__name__.lower()
447+
model_name: str = model.__name__
414448
if "guild" in model_name:
415449
model_name = model_name[5:]
416-
_obj = getattr(guild, f"{model_name}s", None)
450+
elif model_name == "threadmembers":
451+
return
452+
_obj = getattr(guild, f"{model_name.lower()}s", None)
417453
if _obj is not None and isinstance(_obj, list):
418-
for __obj in _obj:
419-
if __obj.id == obj.id:
420-
_obj.remove(__obj)
454+
_data = getattr(obj, model_name, None)
455+
456+
if "_create" in name or "_add" in name:
457+
_obj.append(obj)
458+
459+
for index, __obj in enumerate(_obj):
460+
if __obj.id == id:
461+
if "_remove" in name or "_delete" in name:
462+
_obj.remove(__obj)
463+
464+
elif "_update" in name and hasattr(obj, "id"):
465+
_obj[index] = _data
421466
break
422467
setattr(guild, f"{model_name}s", _obj)
423468
self._http.cache[Guild].add(guild)
424469

425-
_cache: "Storage" = self._http.cache[model]
426-
427-
if isinstance(obj, Member):
428-
id = (Snowflake(data["guild_id"]), obj.id)
429-
else:
430-
id = getattr(obj, "id", None)
431-
432470
if "_create" in name or "_add" in name:
433-
_cache.merge(obj, id)
434-
__modify_guild_cache()
471+
if id:
472+
_cache.merge(obj, id)
435473
self._dispatch.dispatch(f"on_{name}", obj)
474+
__modify_guild_cache()
436475

437476
elif "_update" in name and hasattr(obj, "id"):
438477
old_obj = self._http.cache[model].get(id)
@@ -448,12 +487,13 @@ def __modify_guild_cache():
448487

449488
before = model(**old_obj._json)
450489
old_obj.update(**obj._json)
490+
491+
_cache.add(old_obj, id)
451492
else:
452493
before = None
453494
old_obj = obj
454-
455-
_cache.add(old_obj, id)
456495
__modify_guild_cache()
496+
457497
self._dispatch.dispatch(
458498
f"on_{name}", before, old_obj
459499
) # give previously stored and new one
@@ -462,8 +502,9 @@ def __modify_guild_cache():
462502
elif "_remove" in name or "_delete" in name:
463503
self._dispatch.dispatch(f"on_raw_{name}", obj)
464504
__modify_guild_cache()
465-
old_obj = _cache.pop(id)
466-
self._dispatch.dispatch(f"on_{name}", old_obj)
505+
if id:
506+
old_obj = _cache.pop(id)
507+
self._dispatch.dispatch(f"on_{name}", old_obj)
467508

468509
else:
469510
self._dispatch.dispatch(f"on_{name}", obj)

0 commit comments

Comments
 (0)