diff --git a/core/bot_classes/axobot.py b/core/bot_classes/axobot.py index b6408419e..966cf3e47 100644 --- a/core/bot_classes/axobot.py +++ b/core/bot_classes/axobot.py @@ -313,3 +313,16 @@ async def get_command_mention(self, command_name: str): return f"`{command.qualified_name}`" self.log.error("Trying to mention invalid command: %s", command_name) return f"`{command_name}`" + + async def get_message_from_cache(self, message_id: int) -> discord.Message | None: + "Get a message from the cache" + for msg in self.cached_messages: + if msg.id == message_id: + return msg + return None + + async def add_message_to_cache(self, message: discord.Message): + "Force add a message to the cache" + if await self.get_message_from_cache(message.id) is None: + # pylint: disable=protected-access + self._connection._messages.append(message) diff --git a/modules/roles_react/roles_react.py b/modules/roles_react/roles_react.py index b3aa59b40..b0edee231 100644 --- a/modules/roles_react/roles_react.py +++ b/modules/roles_react/roles_react.py @@ -55,7 +55,7 @@ async def prepare_react(self, payload: discord.RawReactionActionEvent) -> tuple[ chan = self.bot.get_channel(payload.channel_id) if chan is None or isinstance(chan, discord.abc.PrivateChannel): return None - if (msg := self.bot.cached_messages[payload.message_id]) is None: + if (msg := await self.bot.get_message_from_cache(payload.message_id)) is None: try: msg = await chan.fetch_message(payload.message_id) except discord.NotFound: # we don't care about those @@ -65,6 +65,7 @@ async def prepare_react(self, payload: discord.RawReactionActionEvent) -> tuple[ f"Could not fetch roles-reactions message {payload.message_id} in guild {payload.guild_id}: {err}" ) return None + await self.bot.add_message_to_cache(msg) if len(msg.embeds) == 0 or msg.embeds[0].footer.text not in self.footer_texts: return None temp = await self.db_get_role_from_emoji(