Skip to content

Commit

Permalink
fix(roles-react): better message cache usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ZRunner committed Dec 1, 2024
1 parent 06cd32b commit 55cd300
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
13 changes: 13 additions & 0 deletions core/bot_classes/axobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 2 additions & 1 deletion modules/roles_react/roles_react.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 55cd300

Please sign in to comment.