diff options
author | 2022-10-22 13:35:38 +0100 | |
---|---|---|
committer | 2022-10-22 13:35:38 +0100 | |
commit | afb2ff6a13c7a9ce598ac617b10ff533556e702d (patch) | |
tree | f6e62dd89d3b052c7a27008562aeb74f714c6af0 | |
parent | Merge pull request #2298 from shtlrs/issue-2294-zen-command (diff) |
Store time of last vote in redis to prevent vote triggering early
Previously if a vote was sent and ended within a day a new one could be sent immediately
-rw-r--r-- | bot/exts/recruitment/talentpool/_review.py | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/bot/exts/recruitment/talentpool/_review.py b/bot/exts/recruitment/talentpool/_review.py index e3ac1086d..0455f13c4 100644 --- a/bot/exts/recruitment/talentpool/_review.py +++ b/bot/exts/recruitment/talentpool/_review.py @@ -8,6 +8,7 @@ from collections import Counter from datetime import datetime, timedelta, timezone from typing import List, Optional, Union +from async_rediscache import RedisCache from botcore.site_api import ResponseCodeError from dateutil.parser import isoparse from discord import Embed, Emoji, Member, Message, NotFound, PartialMessage, TextChannel @@ -52,6 +53,11 @@ NOMINATION_MESSAGE_REGEX = re.compile( class Reviewer: """Manages, formats, and publishes reviews of helper nominees.""" + # RedisCache[ + # "last_vote_date": float | POSIX UTC timestamp. + # ] + status_cache = RedisCache() + def __init__(self, bot: Bot, pool: 'TalentPool'): self.bot = bot self._pool = pool @@ -82,8 +88,18 @@ class Reviewer: """ voting_channel = self.bot.get_channel(Channels.nomination_voting) + last_vote_timestamp = await self.status_cache.get("last_vote_date") + if last_vote_timestamp: + last_vote_date = datetime.fromtimestamp(last_vote_timestamp, tz=timezone.utc) + time_since_last_vote = datetime.now(timezone.utc) - last_vote_date + + if time_since_last_vote < MIN_REVIEW_INTERVAL: + log.debug("Most recent review was less than %s ago, cancelling check", MIN_REVIEW_INTERVAL) + return False + else: + log.info("Date of last vote not found in cache, a vote may be sent early") + review_count = 0 - is_first_message = True async for msg in voting_channel.history(): # Try and filter out any non-review messages. We also only want to count # one message from reviews split over multiple messages. We use fixed text @@ -91,14 +107,6 @@ class Reviewer: if not msg.author.bot or "for Helper!" not in msg.content: continue - if is_first_message: - time_since_message_created = datetime.now(timezone.utc) - msg.created_at - if time_since_message_created < MIN_REVIEW_INTERVAL: - log.debug("Most recent review was less than %s ago, cancelling check", MIN_REVIEW_INTERVAL) - return False - - is_first_message = False - review_count += 1 if review_count >= MAX_ONGOING_REVIEWS: @@ -181,6 +189,9 @@ class Reviewer: ) message = await thread.send(f"<@&{Roles.mod_team}> <@&{Roles.admins}>") + now = datetime.now(tz=timezone.utc) + await self.status_cache.set("last_vote_date", now.timestamp()) + if update_database: nomination = self._pool.cache.get(user_id) await self.bot.api_client.patch(f"bot/nominations/{nomination['id']}", json={"reviewed": True}) |