diff --git a/bot/cogs/contest_events.py b/bot/cogs/contest_events.py index c2aff0a..3d4af98 100644 --- a/bot/cogs/contest_events.py +++ b/bot/cogs/contest_events.py @@ -150,8 +150,6 @@ class ContestEventsCog(commands.Cog): except ValueError: pass - # TODO: Only update the votecount during the VOTING period! - with self.bot.get_session() as session: guild: Guild = session.query(Guild).get(payload.guild_id) if payload.channel_id == guild.submission_channel and helpers.is_upvote(payload.emoji): @@ -172,9 +170,12 @@ class ContestEventsCog(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 = [] - message = self.bot.get_message(payload.channel_id, payload.message_id) - await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE)) + if submission.period.voting: + submission.votes = [] + message = self.bot.get_message(payload.channel_id, payload.message_id) + await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE)) + else: + logger.debug(f'All reactions cleared on Submission ({submission.id}) outside of it\'s voting period.') @commands.Cog.listener() async def on_raw_reaction_clear_emoji(self, payload: discord.RawReactionClearEmojiEvent) -> None: @@ -188,9 +189,12 @@ class ContestEventsCog(commands.Cog): logger.warning(f'Witnessed all upvote reactions removed from message {payload.message_id},' f' but no Submission found in database.') else: - submission.votes = [] - message = self.bot.get_message(payload.channel_id, payload.message_id) - await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE)) + if submission.period.voting: + submission.votes = [] + message = self.bot.get_message(payload.channel_id, payload.message_id) + await message.add_reaction(self.bot.get_emoji(constants.Emoji.UPVOTE)) + else: + logger.debug(f'Upvote reactions cleared on Submission ({submission.id}) outside of it\'s voting period.') def setup(bot) -> None: diff --git a/bot/models.py b/bot/models.py index 2f2fd48..caf7600 100644 --- a/bot/models.py +++ b/bot/models.py @@ -160,8 +160,14 @@ class Submission(Base): return found - async def update(self, bot: 'ContestBot', message: discord.Message = None) -> None: - """Updates the number of votes in the database by thoroughly evaluating the message.""" + async def update(self, bot: 'ContestBot', message: discord.Message = None, force: bool = True) -> None: + """ + Updates the number of votes in the database by thoroughly evaluating the message. + + :param bot: A instance of the bot to use to query and act on messages. + :param message: The message correlating to this Submission + :param force: If True, update the submission even outside of it's relevant voting period. + """ saw_self, current, old = False, set(), set(self.votes) # Votes currently on the message and votes only on the submission for reaction in message.reactions: @@ -199,7 +205,8 @@ class Submission(Base): ) # Update the current list of votes - self.votes = list(current) + if self.period.voting: + self.votes = list(current) if len(to_remove) > 0: if report: report += ' ' @@ -207,7 +214,7 @@ class Submission(Base): if report: logger.debug(report) # If we never saw ourselves in the reaction, add the Upvote emoji - if not saw_self: + if not saw_self and self.period.voting: await message.add_reaction(constants.Emoji.UPVOTE)