From c3f30e62c403ce3e2635a5e9c46f2e11a705da0c Mon Sep 17 00:00:00 2001 From: jackra1n <45038833+jackra1n@users.noreply.github.com> Date: Mon, 5 Feb 2024 22:43:14 +0100 Subject: [PATCH] Increase message cache to 3k from 1k, make karma/post code more readable - Log when bot needs to fetch from API for karma reactions --- bot/cogs/karma.py | 45 +++++++++++++++++++++++++++++++-------------- bot/core/bot.py | 3 ++- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/bot/cogs/karma.py b/bot/cogs/karma.py index b477187..f6920fb 100644 --- a/bot/cogs/karma.py +++ b/bot/cogs/karma.py @@ -732,24 +732,35 @@ async def process_reaction(self, payload: discord.RawReactionActionEvent, add_re upvote_emotes = await self._get_karma_upvote_emotes(payload.guild_id) downvote_emotes = await self._get_karma_downvote_emotes(payload.guild_id) - if payload.emoji.id in upvote_emotes: - karma_modifier = 1 if add_reaction else -1 - post_votes_modifier = 1 if add_reaction else 0 - elif payload.emoji.id in downvote_emotes: - karma_modifier = -1 if add_reaction else 1 - post_votes_modifier = -1 if add_reaction else 0 - else: + if payload.emoji.id not in [*upvote_emotes, *downvote_emotes]: return - - server = self.bot.get_guild(payload.guild_id) or await self.bot.fetch_guild(payload.guild_id) - channel = self.bot.get_channel(payload.channel_id) or await self.bot.fetch_channel(payload.channel_id) + + server = self.bot.get_guild(payload.guild_id) + if server is None: + logger.warning(f"Server {payload.guild_id} not found in cache for karma reaction. Fetching from API.") + server = await self.bot.fetch_guild(payload.guild_id) + channel = self.bot.get_channel(payload.channel_id) + if channel is None: + logger.warning(f"Channel {payload.channel_id} not found in cache for karma reaction. Fetching from API.") + channel = await self.bot.fetch_channel(payload.channel_id) await self._insert_user(user) await self._insert_server(server) await self._insert_channel(channel) - await self._update_karma(payload, user, karma_modifier) - await self._update_post_votes(payload, user, post_votes_modifier, 0) + is_upvote = payload.emoji.id in upvote_emotes + + karma_amount = 1 # Assume positive karma + (upvote, downvote) = (1, 0) # Assume upvote + if not add_reaction: + karma_amount *= -1 + upvote *= -1 + if not is_upvote: + karma_amount *= -1 + (upvote, downvote) = (downvote, upvote) + + await self._update_karma(payload, user, karma_amount) + await self._update_post_votes(payload, user, upvote, downvote) async def _insert_user(self, user: discord.Member): stmt = ''' @@ -824,7 +835,10 @@ async def check_payload(self, payload: discord.RawReactionActionEvent) -> discor return None if message.author.bot: return None - reaction_user = payload.member or self.bot.get_user(payload.user_id) or await self.bot.fetch_user(payload.user_id) + reaction_user = payload.member or self.bot.get_user(payload.user_id) + if not reaction_user: + logger.warning(f"User {payload.user_id} not found in cache for karma reaction. Fetching from API.") + reaction_user = await self.bot.fetch_user(payload.user_id) if reaction_user == message.author: return None return message.author @@ -832,7 +846,10 @@ async def check_payload(self, payload: discord.RawReactionActionEvent) -> discor async def __get_message_from_payload(self, payload: discord.RawReactionActionEvent) -> discord.Message | None: potential_message = [message for message in self.bot.cached_messages if message.id == payload.message_id] cached_message = potential_message[0] if potential_message else None - return cached_message or await self.bot.get_channel(payload.channel_id).fetch_message(payload.message_id) + if not cached_message: + logger.warning(f"Message {payload.message_id} not found in cache for karma reaction. Fetching from API.") + cached_message = await self.bot.get_channel(payload.channel_id).fetch_message(payload.message_id) + return cached_message async def send_conclusion(self, ctx: commands.Context, kasino_id: int, winner: int): kasino = await self.bot.db.fetchrow('SELECT * FROM kasino WHERE id = $1', kasino_id) diff --git a/bot/core/bot.py b/bot/core/bot.py index c834128..8b64e46 100644 --- a/bot/core/bot.py +++ b/bot/core/bot.py @@ -41,7 +41,8 @@ def __init__(self) -> None: super().__init__( command_prefix=commands.when_mentioned_or(config.PREFIX), intents=intents, - owner_id=276462585690193921 + owner_id=276462585690193921, + max_messages=3000 ) self.version = Version()