Remove message builder, move builders to elecion_cycle.py.
build

This commit is contained in:
2023-07-02 11:04:51 -04:00
parent f0beb7821f
commit edc7722f76
4 changed files with 78 additions and 81 deletions
+67 -15
View File
@@ -7,6 +7,9 @@ from datetime import datetime, timedelta
import nextcord
from nextcord.ext import commands
from config import DakinyConfig
from utils import get_datetime
class ElectionCycle:
"""
@@ -14,7 +17,7 @@ class ElectionCycle:
the cycle.
"""
def __init__(self, election_start: datetime, term_start: datetime, term_end: datetime, bot) -> None:
def __init__(self, cycle: int, bot) -> None:
"""
Generates all dates relevant to the election cycle from input election start, term start and end dates.
@@ -24,13 +27,62 @@ class ElectionCycle:
term_end (datetime): Date the term being elected ends.
bot: the discord bot instance
"""
self._election_start: datetime = election_start
self._term_start: datetime = term_start
self._term_end: datetime = term_end
self._voting_start: datetime = self._election_start + timedelta(days=1)
self._voting_end: datetime = self._voting_start + timedelta(days=1)
self._highcouncil_election: datetime = self._voting_end + timedelta(days=1)
self._bot: commands.Bot = bot
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:
"""
@@ -40,17 +92,17 @@ class ElectionCycle:
print("checking the date")
# check all dates if action is needed
if today.date() == self._election_start.date():
if today.date() == self.election_start.date():
await self.election_start_action()
elif today.date() == self._voting_start.date():
elif today.date() == self.voting_start.date():
await self.voting_end_action()
elif today.date() == self._voting_end.date():
elif today.date() == self.voting_end.date():
await self.voting_end_action()
elif today.date() == self._highcouncil_election.date():
elif today.date() == self.highcouncil_election.date():
await self.highcouncil_election_action()
elif today.date() == self._term_start.date():
elif today.date() == self.term_start.date():
await self.term_start_action()
elif today.date() == self._term_end.date():
elif today.date() == self.term_end.date():
await self.term_end_action()
else:
print("today was not a special date")
@@ -99,5 +151,5 @@ class ElectionCycle:
"""
message a string to me
"""
imcompiling: nextcord.User = await self._bot.fetch_user(206602713511886849)
imcompiling: nextcord.User = await self.bot.fetch_user(206602713511886849)
await imcompiling.send(msg)