Skip to content

Commit

Permalink
Increase message cache to 3k from 1k, make karma/post code more readable
Browse files Browse the repository at this point in the history
- Log when bot needs to fetch from API for karma reactions
  • Loading branch information
jackra1n committed Feb 5, 2024
1 parent 2983818 commit c3f30e6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
45 changes: 31 additions & 14 deletions bot/cogs/karma.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '''
Expand Down Expand Up @@ -824,15 +835,21 @@ 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

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)
Expand Down
3 changes: 2 additions & 1 deletion bot/core/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit c3f30e6

Please sign in to comment.