Skip to content

Commit 5bdf254

Browse files
DamegoEepyElvyra
andauthored
fix: fix some bugs with cache (#970)
* fix: fix some bugs with cache * you already is None lol * Update interactions/api/gateway/client.py Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com> * add .lower() to model_name & add new model to ignore * revert last commit * oops Co-authored-by: EdVraz <88881326+EdVraz@users.noreply.github.com>
1 parent d1fbb39 commit 5bdf254

File tree

2 files changed

+28
-35
lines changed

2 files changed

+28
-35
lines changed

interactions/api/gateway/client.py

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -409,30 +409,27 @@ def _dispatch_event(self, event: str, data: dict) -> None:
409409
id = getattr(obj, "id", None)
410410

411411
if id is None:
412-
if model.__name__.startswith("Guild"):
413-
if model.__name__ == "GuildScheduledEventUser":
414-
id = model.guild_scheduled_event_id
415-
elif model.__name__ in [
416-
"Invite",
417-
"GuildBan",
418-
"ChannelPins",
419-
"MessageReaction",
420-
"ReactionRemove",
421-
# Extend this for everything that should not be cached
422-
]:
423-
id = None
424-
else:
425-
model_name = model.__name__[5:]
426-
if _data := getattr(obj, model_name, None):
427-
id = (
428-
getattr(_data, "id")
429-
if not isinstance(_data, dict)
430-
else Snowflake(_data["id"])
431-
)
432-
elif hasattr(obj, f"{model_name}_id"):
433-
id = getattr(obj, f"{model_name}_id")
434-
else:
435-
id = None
412+
if model.__name__ == "GuildScheduledEventUser":
413+
id = model.guild_scheduled_event_id
414+
elif model.__name__ in [
415+
"Invite",
416+
"GuildBan",
417+
"ChannelPins",
418+
"MessageReaction",
419+
"MessageReactionRemove",
420+
# Extend this for everything that should not be cached
421+
]:
422+
id = None
423+
elif model.__name__.startswith("Guild"):
424+
model_name = model.__name__[5:]
425+
if _data := getattr(obj, model_name, None):
426+
id = (
427+
getattr(_data, "id")
428+
if not isinstance(_data, dict)
429+
else Snowflake(_data["id"])
430+
)
431+
elif hasattr(obj, f"{model_name}_id"):
432+
id = getattr(obj, f"{model_name}_id", None)
436433

437434
def __modify_guild_cache():
438435
if not (
@@ -450,18 +447,15 @@ def __modify_guild_cache():
450447
return
451448
_obj = getattr(guild, f"{model_name.lower()}s", None)
452449
if _obj is not None and isinstance(_obj, list):
453-
_data = getattr(obj, model_name, None)
454-
455450
if "_create" in name or "_add" in name:
456451
_obj.append(obj)
457-
458452
for index, __obj in enumerate(_obj):
459453
if __obj.id == id:
460454
if "_remove" in name or "_delete" in name:
461455
_obj.remove(__obj)
462456

463457
elif "_update" in name and hasattr(obj, "id"):
464-
_obj[index] = _data
458+
_obj[index] = obj
465459
break
466460
setattr(guild, f"{model_name}s", _obj)
467461
self._http.cache[Guild].add(guild)
@@ -474,15 +468,14 @@ def __modify_guild_cache():
474468

475469
elif "_update" in name and hasattr(obj, "id"):
476470
old_obj = self._http.cache[model].get(id)
477-
478471
if old_obj:
479472
before = model(**old_obj._json)
480473
old_obj.update(**obj._json)
481-
482-
_cache.add(old_obj, id)
483474
else:
484475
before = None
485476
old_obj = obj
477+
478+
_cache.add(old_obj, id)
486479
__modify_guild_cache()
487480

488481
self._dispatch.dispatch(

interactions/api/models/gw.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"ChannelPins",
3939
"ThreadMembers",
4040
"ThreadList",
41-
"ReactionRemove",
41+
"MessageReactionRemove",
4242
"MessageReaction",
4343
"GuildIntegrations",
4444
"GuildBan",
@@ -762,7 +762,7 @@ class Presence(ClientSerializerMixin):
762762
@define()
763763
class MessageReaction(DictSerializerMixin):
764764
"""
765-
A class object representing the gateway event ``MESSAGE_REACTION_ADD``.
765+
A class object representing the gateway event ``MESSAGE_REACTION_ADD`` and ``MESSAGE_REACTION_REMOVE``.
766766
767767
:ivar Optional[Snowflake] user_id?: The user ID of the event.
768768
:ivar Snowflake channel_id: The channel ID of the event.
@@ -780,9 +780,9 @@ class MessageReaction(DictSerializerMixin):
780780
emoji: Optional[Emoji] = field(converter=Emoji, default=None)
781781

782782

783-
class ReactionRemove(MessageReaction):
783+
class MessageReactionRemove(MessageReaction):
784784
"""
785-
A class object representing the gateway events ``MESSAGE_REACTION_REMOVE``, ``MESSAGE_REACTION_REMOVE_ALL`` and ``MESSAGE_REACTION_REMOVE_EMOJI``.
785+
A class object representing the gateway events ``MESSAGE_REACTION_REMOVE_ALL`` and ``MESSAGE_REACTION_REMOVE_EMOJI``.
786786
787787
.. note::
788788
This class inherits the already existing attributes of :class:`interactions.api.models.gw.Reaction`.

0 commit comments

Comments
 (0)