Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 59 additions & 18 deletions interactions/api/gateway/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,37 +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:]
_obj = getattr(guild, f"{model_name}s", None)
elif model_name == "threadmembers":
return
_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)
Expand All @@ -443,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
Expand All @@ -457,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)
Expand Down