From c5d148ad497eed516baa3862bda75b28460686be Mon Sep 17 00:00:00 2001 From: imcompiling Date: Wed, 26 Jul 2023 16:24:57 -0400 Subject: [PATCH] Daily loop is complete. Properly maintains the current election. --- classes/election_cycle.py | 3 +++ cogs/loops/daily_loop.py | 52 +++++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/classes/election_cycle.py b/classes/election_cycle.py index 1dca109..3ae32b2 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -41,6 +41,7 @@ class ElectionCycle: self.highcouncil_election_start: datetime = self.highcouncil_election_announce + timedelta(days=3) self.highcouncil_election_end: datetime = self.term_start - timedelta(days=1) self.guild: nextcord.Guild = guild + self.election_over: bool = False def build_election_announcement(self) -> list[str]: """ @@ -199,6 +200,8 @@ class ElectionCycle: """ Moves the election channel into the archive category. """ + + self.election_over = True election_channel: nextcord.TextChannel = self.guild.get_channel(self.get_election_info().ElectionChannel) archive: nextcord.CategoryChannel = self.guild.get_channel(self.config.params.archive_channel_group_id) diff --git a/cogs/loops/daily_loop.py b/cogs/loops/daily_loop.py index eed954b..372f86c 100644 --- a/cogs/loops/daily_loop.py +++ b/cogs/loops/daily_loop.py @@ -1,13 +1,15 @@ """ -_summary_ +Defines the daily loop that runs the Dakiny bot functions. """ -from datetime import time +from datetime import datetime, time from nextcord.ext import commands, tasks from pytz import timezone from classes.election_cycle import ElectionCycle +from config import DakinyConfig +from utils import get_datetime class DailyLoop(commands.Cog, name="Daily Loop"): @@ -17,8 +19,7 @@ class DailyLoop(commands.Cog, name="Daily Loop"): def __init__(self, bot: commands.Bot) -> None: self.bot = bot - # TODO need to figure out how to determine the current cycle - self.current_election = ElectionCycle(0, self.bot) + self.current_election = self.get_current_cycle() @commands.Cog.listener() async def on_ready(self): @@ -27,11 +28,42 @@ class DailyLoop(commands.Cog, name="Daily Loop"): """ await self.bot.wait_until_ready() # pylint doesn't recognize that periodic_update_guild_data is subclassed from tasks.loop. - await self.do_thing.start() # pylint: disable=no-member + await self.daily_loop.start() # pylint: disable=no-member @tasks.loop(time=time(hour=0, tzinfo=timezone("GMT"))) - async def do_thing(self): - """ """ - print("loop") - await self.current_election.check_date() - # TODO need to figure out how to change the election cycle after current one is over + async def daily_loop(self): + """ + Runs daily. Checks if the current election is None. If so, checks if there is an active election. If there is + an active election, runs the check_date() function for the election to perform actions. Finally checks if the + election is over to delete the current election object. + """ + + if self.current_election is None: + self.current_election = self.get_current_cycle() + + if self.current_election is not None: + await self.current_election.check_date() + if self.current_election.election_over: + self.current_election = None + + def get_current_cycle(self) -> ElectionCycle: + """ + Creates an object for the current election cycle if one is active. An election is considered active + between the election start and the term start. + + Returns: + ElectionCycle: Current election cycle, None if there is no election happening. + """ + + today = datetime.today() + config: DakinyConfig = self.bot.config + current_election = None + + for cycle, election in enumerate(config.election_cycles): + if ( + today.date() > get_datetime(election.election_start).date() + and today.date() < get_datetime(election.term_start).date() + ): + current_election = ElectionCycle(cycle, config, self.bot.get_guild(config.params.guild_id)) + + return current_election