Daily loop is complete. Properly maintains the current election.

This commit is contained in:
2023-07-26 16:24:57 -04:00
parent 198d5acea0
commit c5d148ad49
2 changed files with 45 additions and 10 deletions
+3
View File
@@ -41,6 +41,7 @@ class ElectionCycle:
self.highcouncil_election_start: datetime = self.highcouncil_election_announce + timedelta(days=3) self.highcouncil_election_start: datetime = self.highcouncil_election_announce + timedelta(days=3)
self.highcouncil_election_end: datetime = self.term_start - timedelta(days=1) self.highcouncil_election_end: datetime = self.term_start - timedelta(days=1)
self.guild: nextcord.Guild = guild self.guild: nextcord.Guild = guild
self.election_over: bool = False
def build_election_announcement(self) -> list[str]: def build_election_announcement(self) -> list[str]:
""" """
@@ -199,6 +200,8 @@ class ElectionCycle:
""" """
Moves the election channel into the archive category. 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) 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) archive: nextcord.CategoryChannel = self.guild.get_channel(self.config.params.archive_channel_group_id)
+42 -10
View File
@@ -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 nextcord.ext import commands, tasks
from pytz import timezone from pytz import timezone
from classes.election_cycle import ElectionCycle from classes.election_cycle import ElectionCycle
from config import DakinyConfig
from utils import get_datetime
class DailyLoop(commands.Cog, name="Daily Loop"): 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: def __init__(self, bot: commands.Bot) -> None:
self.bot = bot self.bot = bot
# TODO need to figure out how to determine the current cycle self.current_election = self.get_current_cycle()
self.current_election = ElectionCycle(0, self.bot)
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self):
@@ -27,11 +28,42 @@ class DailyLoop(commands.Cog, name="Daily Loop"):
""" """
await self.bot.wait_until_ready() await self.bot.wait_until_ready()
# pylint doesn't recognize that periodic_update_guild_data is subclassed from tasks.loop. # 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"))) @tasks.loop(time=time(hour=0, tzinfo=timezone("GMT")))
async def do_thing(self): async def daily_loop(self):
""" """ """
print("loop") Runs daily. Checks if the current election is None. If so, checks if there is an active election. If there is
await self.current_election.check_date() an active election, runs the check_date() function for the election to perform actions. Finally checks if the
# TODO need to figure out how to change the election cycle after current one is over 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