Files
dakiny-bot/classes/election_cycle.py
T

156 lines
5.8 KiB
Python

"""
Contains the election cycle class
"""
from datetime import datetime, timedelta
import nextcord
from nextcord.ext import commands
from config import DakinyConfig
from utils import get_datetime
class ElectionCycle:
"""
This class represents all the dates required for an election cycle along with all the functions needed to operate
the cycle.
"""
def __init__(self, cycle: int, bot) -> None:
"""
Generates all dates relevant to the election cycle from input election start, term start and end dates.
Args:
election_start (datetime): Date the election cycle starts.
term_start (datetime): Date the term being elected starts.
term_end (datetime): Date the term being elected ends.
bot: the discord bot instance
"""
self.config: DakinyConfig = bot.config
self.cycle = cycle
self.elecion_cycle = self.config.election_cycles[self.cycle]
self.election_start: datetime = get_datetime(self.elecion_cycle.election_start)
self.term_start: datetime = get_datetime(self.elecion_cycle.term_start)
self.term_end: datetime = get_datetime(self.elecion_cycle.term_end)
self.voting_start: datetime = get_datetime(self.elecion_cycle.voting_start)
self.voting_end: datetime = get_datetime(self.elecion_cycle.voting_end)
self.highcouncil_election: datetime = self.voting_end + timedelta(days=1)
self.bot: commands.Bot = bot
def build_election_announcement(self) -> list[str]:
"""
Builds the messages to announce the election is starting.
Returns:
list[str]: A list of message contents in the correct order.
"""
with open("bot_messages/election_announcement.txt", mode="r", encoding="utf8") as file:
message = file.read()
next_election = get_datetime(self.config.election_cycles[(self.cycle + 1) % 4].election_start)
message = message.format(
clan_council_id=self.config.params.clan_council_role_id,
high_council_id=self.config.params.high_council_role_id,
election_start_date=self.election_start.strftime("%B %d"),
term_start_date=self.term_start.strftime("%B %d"),
term_end_date=self.term_end.strftime("%B %d"),
voting_start_date=self.voting_start.strftime("%B %d"),
voting_end_date=self.voting_end.strftime("%B %d"),
highcouncil_election_date=self.highcouncil_election.strftime("%B %d"),
next_election_start_date=next_election.strftime("%B %d"),
)
return message.split("---message_break---")
def build_vote_start(self) -> str:
"""
Builds the voting start message out of template and configurations.
Returns:
str: Message to send
"""
with open("bot_messages/vote_start.txt", mode="r", encoding="utf8") as file:
message = file.read()
vote_end: datetime = get_datetime(self.config.election_cycles[self.cycle].voting_end)
return message.format(
clan_member_id=self.config.params.clan_member_role_id,
voting_emote_id=self.config.params.voting_emote_id,
voting_end_date=vote_end.strftime("%B %d"),
voting_end_day=vote_end.strftime("%A"),
)
async def check_date(self) -> None:
"""
Function checks current date to see if something needs to be run.
"""
today = datetime.today()
print("checking the date")
# check all dates if action is needed
if today.date() == self.election_start.date():
await self.election_start_action()
elif today.date() == self.voting_start.date():
await self.voting_end_action()
elif today.date() == self.voting_end.date():
await self.voting_end_action()
elif today.date() == self.highcouncil_election.date():
await self.highcouncil_election_action()
elif today.date() == self.term_start.date():
await self.term_start_action()
elif today.date() == self.term_end.date():
await self.term_end_action()
else:
print("today was not a special date")
print(today)
async def election_start_action(self) -> None:
"""
Performs required actions for when the election cycle starts.
"""
print("election cycle has started")
await self.message_me("election cycle has started")
# TODO get message, input variables, split into multiple messages, send to correct channel.
async def voting_start_action(self) -> None:
"""
Performs required actions for when voting for candidates starts.
"""
print("Voting has started")
await self.message_me("Voting has started")
async def voting_end_action(self) -> None:
"""
Performs required actions for when voting time has ended.
"""
print("voting has ended")
await self.message_me("voting has ended")
async def highcouncil_election_action(self) -> None:
"""
Performs required actions for when the highcouncil is elected.
"""
print("Highcouncil election has started.")
await self.message_me("Highcouncil election has started.")
async def term_start_action(self) -> None:
msg: str = "the term has started"
print(msg)
self.message_me(msg)
async def term_end_action(self):
msg: str = "the term has ended"
print(msg)
self.message_me(msg)
async def message_me(self, msg: str):
"""
message a string to me
"""
imcompiling: nextcord.User = await self.bot.fetch_user(206602713511886849)
await imcompiling.send(msg)