From 38fd55a2ed5766473e73aa43172dc9f7cef4ed86 Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Mon, 18 Jul 2022 21:37:51 +0200 Subject: [PATCH 1/2] fix: Fix another bug --- interactions/api/gateway/client.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/interactions/api/gateway/client.py b/interactions/api/gateway/client.py index 546236829..15b512dbb 100644 --- a/interactions/api/gateway/client.py +++ b/interactions/api/gateway/client.py @@ -412,6 +412,8 @@ def __modify_guild_cache(): model_name = model.__name__.lower() if "guild" in model_name: model_name = model_name[5:] + elif model_name == "threadmembers": + return _obj = getattr(guild, f"{model_name}s", None) if _obj is not None and isinstance(_obj, list): for __obj in _obj: From 3a9db18f8a9c4e769b2a5b9df9073f81cb3f0b77 Mon Sep 17 00:00:00 2001 From: EdVraz <88881326+EdVraz@users.noreply.github.com> Date: Tue, 19 Jul 2022 21:03:24 +0200 Subject: [PATCH 2/2] fix: Fix bug --- interactions/api/gateway/client.py | 75 +++++++++++++++++++++++------- 1 file changed, 57 insertions(+), 18 deletions(-) diff --git a/interactions/api/gateway/client.py b/interactions/api/gateway/client.py index 15b512dbb..540d5f668 100644 --- a/interactions/api/gateway/client.py +++ b/interactions/api/gateway/client.py @@ -401,39 +401,76 @@ def _dispatch_event(self, event: str, data: dict) -> None: data["_client"] = self._http obj = model(**data) + _cache: "Storage" = self._http.cache[model] + + if isinstance(obj, Member): + id = (Snowflake(data["guild_id"]), obj.id) + else: + id = getattr(obj, "id", None) + + if id is None: + if model.__name__.startswith("Guild"): + if model.__name__ == "GuildScheduledEventUser": + id = model.guild_scheduled_event_id + elif model.__name__ in [ + "Invite", + "GuildBan", + "ChannelPins", + "MessageReaction", + "ReactionRemove", + # Extend this for everything that should not be cached + ]: + id = None + else: + model_name = model.__name__[5:] + if _data := getattr(obj, model_name, None): + id = ( + getattr(_data, "id") + if not isinstance(_data, dict) + else Snowflake(_data["id"]) + ) + elif hasattr(obj, f"{model_name}_id"): + id = getattr(obj, f"{model_name}_id") + else: + id = None + def __modify_guild_cache(): if not ( guild_id := data.get("guild_id") and not isinstance(obj, Guild) and "message" not in name + and id is not None ): return if guild := self._http.cache[Guild].get(Snowflake(guild_id)): - model_name = model.__name__.lower() + model_name: str = model.__name__ if "guild" in model_name: model_name = model_name[5:] elif model_name == "threadmembers": return - _obj = getattr(guild, f"{model_name}s", None) + _obj = getattr(guild, f"{model_name.lower()}s", None) if _obj is not None and isinstance(_obj, list): - for __obj in _obj: - if __obj.id == obj.id: - _obj.remove(__obj) + _data = getattr(obj, model_name, None) + + if "_create" in name or "_add" in name: + _obj.append(obj) + + for index, __obj in enumerate(_obj): + if __obj.id == id: + if "_remove" in name or "_delete" in name: + _obj.remove(__obj) + + elif "_update" in name and hasattr(obj, "id"): + _obj[index] = _data break setattr(guild, f"{model_name}s", _obj) self._http.cache[Guild].add(guild) - _cache: "Storage" = self._http.cache[model] - - if isinstance(obj, Member): - id = (Snowflake(data["guild_id"]), obj.id) - else: - id = getattr(obj, "id", None) - if "_create" in name or "_add" in name: - _cache.merge(obj, id) - __modify_guild_cache() + if id: + _cache.merge(obj, id) self._dispatch.dispatch(f"on_{name}", obj) + __modify_guild_cache() elif "_update" in name and hasattr(obj, "id"): old_obj = self._http.cache[model].get(id) @@ -445,12 +482,13 @@ def __modify_guild_cache(): before = model(**old_obj._json) old_obj.update(**obj._json) + + _cache.add(old_obj, id) else: before = None old_obj = obj - - _cache.add(old_obj, id) __modify_guild_cache() + self._dispatch.dispatch( f"on_{name}", before, old_obj ) # give previously stored and new one @@ -459,8 +497,9 @@ def __modify_guild_cache(): elif "_remove" in name or "_delete" in name: self._dispatch.dispatch(f"on_raw_{name}", obj) __modify_guild_cache() - old_obj = _cache.pop(id) - self._dispatch.dispatch(f"on_{name}", old_obj) + if id: + old_obj = _cache.pop(id) + self._dispatch.dispatch(f"on_{name}", old_obj) else: self._dispatch.dispatch(f"on_{name}", obj)