75 lines
2.5 KiB
Python
75 lines
2.5 KiB
Python
"""
|
|
Defines the daily loop that runs the Dakiny bot functions.
|
|
"""
|
|
|
|
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"):
|
|
"""
|
|
Class for performing daily functions.
|
|
"""
|
|
|
|
def __init__(self, bot: commands.Bot) -> None:
|
|
self.bot = bot
|
|
self.current_election = None
|
|
|
|
# pylint doesn't recognize that daily_loop is subclassed from tasks.loop.
|
|
self.daily_loop.start() # pylint: disable=no-member
|
|
|
|
@tasks.loop(time=time(hour=0, tzinfo=timezone("GMT")))
|
|
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
|
|
|
|
@daily_loop.before_loop
|
|
async def before_loop(self):
|
|
"""
|
|
Actions run before the loop starts. Want to wait for bot to be ready and get the current cycle.
|
|
"""
|
|
|
|
await self.bot.wait_until_ready()
|
|
|
|
# Bot needs to know guilds to get the current cycle.
|
|
self.current_election = self.get_current_cycle()
|
|
|
|
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
|