Added keyerror suppression to deletion methods in case of invalid cache state
@@ -21,6 +21,7 @@ A basic type of registry that handles storing global state.
"""
from __future__ import annotations
+import contextlib
import datetime
import typing
import weakref
@@ -109,23 +110,27 @@ class StateRegistryImpl(state_registry.StateRegistry):
elif channel_obj in self._dm_channels:
channel_obj: channels.DMChannel
del self._dm_channels[channel_id]
- else:
- raise KeyError(str(channel_id))
def delete_emoji(self, emoji_obj: emojis.GuildEmoji) -> None:
+ with contextlib.suppress(KeyError):
del self._emojis[emoji_obj.id]
+
guild_obj = emoji_obj.guild
+
+ with contextlib.suppress(KeyError):
del guild_obj.emojis[emoji_obj]
def delete_guild(self, guild_obj: guilds.Guild) -> None:
+ with contextlib.suppress(KeyError):
del self._guilds[guild_obj.id]
def delete_message(self, message_obj: messages.Message) -> None:
- if message_obj in self._message_cache:
+ with contextlib.suppress(KeyError):
message_obj = self._message_cache[message_obj.id]
del self._message_cache[message_obj.id]
def delete_member(self, member_obj: users.Member) -> None:
+ with contextlib.suppress(KeyError):
del member_obj.guild.members[member_obj.id]
def delete_reaction(self, message_obj: messages.Message, user_obj: users.User, emoji_obj: emojis.Emoji) -> None:
@@ -133,10 +138,13 @@ class StateRegistryImpl(state_registry.StateRegistry):
for reaction_obj in message_obj.reactions:
if reaction_obj.emoji == emojis and reaction_obj.message.id == reaction_obj.message.id:
message_obj.reactions.remove(reaction_obj)
+ # This emoji will only be referenced once, so shortcut to save time.
+ break
# noinspection PyProtectedMember
def delete_role(self, role_obj: roles.Role) -> None:
guild_obj = role_obj.guild
+ with contextlib.suppress(KeyError):
del guild_obj.roles[role_obj.id]
for member in guild_obj.members.values():
