From 7427763b5710c20bef9c4602647a9f316fad488d Mon Sep 17 00:00:00 2001 From: Xevion Date: Wed, 17 Feb 2021 05:18:25 -0600 Subject: [PATCH] Implement Submission.update and get/fetch message helper functions Added and implemented helper functions for getting and fetching messages given a payload's integer channel and message IDs. Implemented Submission.votes list modifications and Submission.update in code (tested with real bot) Committed constants.py containing ReactionMarker used in models.py --- bot/bot.py | 8 ++++++++ bot/cogs/contest.py | 34 +++++++++------------------------- bot/constants.py | 8 ++++++++ 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/bot/bot.py b/bot/bot.py index 1a992e3..7262d05 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -106,3 +106,11 @@ class ContestBot(commands.Bot): for submission in submissions: message: discord.PartialMessage = channel.get_partial_message(submission.id) await message.add_reaction(self.get_emoji(constants.Emoji.UPVOTE)) + + def get_message(self, channel_id: int, message_id: int) -> discord.PartialMessage: + channel: discord.TextChannel = self.get_channel(channel_id) + return channel.get_partial_message(message_id) + + async def fetch_message(self, channel_id: int, message_id: int) -> discord.Message: + channel: discord.TextChannel = self.get_channel(channel_id) + return await channel.fetch_message(message_id) diff --git a/bot/cogs/contest.py b/bot/cogs/contest.py index b47ebd9..6f4464a 100644 --- a/bot/cogs/contest.py +++ b/bot/cogs/contest.py @@ -279,11 +279,7 @@ class ContestCog(commands.Cog): if submission is None: logger.warning(f'Upvote reaction added to message {payload.message_id}, but no Submission found in database.') else: - submission.increment() - # Make sure our reaction exists, verify vote count - self_reacted = await submission.verify(await message.fetch(), self.bot.user) - if not self_reacted: - await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE)) + await submission.update(self.bot, message=await message.fetch()) else: # Remove the emoji since it's not supposed to be there anyways. # If permissions were setup correctly, only moderators or admins should be able to trigger this. @@ -309,16 +305,8 @@ class ContestCog(commands.Cog): if submission is None: logger.warning(f'Upvote reaction removed from message {payload.message_id}, but no Submission found in database.') else: - submission.decrement() - - # Get the actual number of votes from the message - channel: discord.TextChannel = self.bot.get_channel(payload.channel_id) - message: discord.Message = await channel.fetch_message(payload.message_id) - self_reacted = await submission.verify(await message.fetch(), self.bot.user) - - # Make sure our reaction exists - if not self_reacted: - await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE)) + message = await self.bot.fetch_message(payload.channel_id, payload.message_id) + await submission.update(self.bot, message=message) @commands.Cog.listener() async def on_raw_reaction_clear(self, payload: discord.RawReactionActionEvent) -> None: @@ -330,10 +318,8 @@ class ContestCog(commands.Cog): if submission is None: logger.warning(f'Witnessed reactions removed from message {payload.message_id}, but no Submission found in database.') else: - submission.votes = 0 - # TODO: Add helper function for getting partial/full messages easily given a payload or channel id and message id - channel: discord.TextChannel = self.bot.get_channel(payload.channel_id) - message: discord.PartialMessage = channel.get_partial_message(payload.message_id) + submission.votes = [] + message = self.bot.get_message(payload.channel_id, payload.message_id) await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE)) @commands.Cog.listener() @@ -345,13 +331,11 @@ class ContestCog(commands.Cog): if helpers.is_upvote(payload.emoji): submission: Submission = session.query(Submission).get(payload.message_id) if submission is None: - logger.warning( - f'Witnessed all upvote reactions removed from message {payload.message_id}, but no Submission found in database.') + logger.warning(f'Witnessed all upvote reactions removed from message {payload.message_id},' + f' but no Submission found in database.') else: - submission.votes = 0 - - channel: discord.TextChannel = self.bot.get_channel(payload.channel_id) - message: discord.PartialMessage = channel.get_partial_message(payload.message_id) + submission.votes = [] + message = self.bot.get_message(payload.channel_id, payload.message_id) await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE)) diff --git a/bot/constants.py b/bot/constants.py index 6916ce0..f6bb3a6 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -2,6 +2,8 @@ import logging import os # Path Constants +from collections import namedtuple + BASE_DIR = os.path.dirname(os.path.abspath(os.path.join(__file__, '..'))) TOKEN = os.path.join(BASE_DIR, 'token.dat') DATABASE = os.path.join(BASE_DIR, 'database.db') @@ -10,7 +12,13 @@ DATABASE_URI = f'sqlite:///{DATABASE}' # Other constants LOGGING_LEVEL = logging.DEBUG + # Emote references class Emoji(object): + """A constants class storing the IDs of various Emojis used by the bot.""" UPVOTE = 810310002220859393 DOWNVOTE = 810310019840213002 + + +# Named Tuples +ReactionMarker = namedtuple("ReactionMarker", ["message", "user", "emoji"], defaults=[Emoji.UPVOTE])