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 import nextcord
from nextcord.ext import commands from nextcord.ext import commands
from config import DakinyConfig
from utils import get_datetime
class ElectionCycle: class ElectionCycle:
""" """
@@ -14,7 +17,7 @@ class ElectionCycle:
the cycle. 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. 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. term_end (datetime): Date the term being elected ends.
bot: the discord bot instance bot: the discord bot instance
""" """
self._election_start: datetime = election_start self.config: DakinyConfig = bot.config
self._term_start: datetime = term_start self.cycle = cycle
self._term_end: datetime = term_end self.elecion_cycle = self.config.election_cycles[self.cycle]
self._voting_start: datetime = self._election_start + timedelta(days=1) self.election_start: datetime = get_datetime(self.elecion_cycle.election_start)
self._voting_end: datetime = self._voting_start + timedelta(days=1) self.term_start: datetime = get_datetime(self.elecion_cycle.term_start)
self._highcouncil_election: datetime = self._voting_end + timedelta(days=1) self.term_end: datetime = get_datetime(self.elecion_cycle.term_end)
self._bot: commands.Bot = bot 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: async def check_date(self) -> None:
""" """
@@ -40,17 +92,17 @@ class ElectionCycle:
print("checking the date") print("checking the date")
# check all dates if action is needed # 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() await self.election_start_action()
elif today.date() == self._voting_start.date(): elif today.date() == self.voting_start.date():
await self.voting_end_action() await self.voting_end_action()
elif today.date() == self._voting_end.date(): elif today.date() == self.voting_end.date():
await self.voting_end_action() await self.voting_end_action()
elif today.date() == self._highcouncil_election.date(): elif today.date() == self.highcouncil_election.date():
await self.highcouncil_election_action() await self.highcouncil_election_action()
elif today.date() == self._term_start.date(): elif today.date() == self.term_start.date():
await self.term_start_action() await self.term_start_action()
elif today.date() == self._term_end.date(): elif today.date() == self.term_end.date():
await self.term_end_action() await self.term_end_action()
else: else:
print("today was not a special date") print("today was not a special date")
@@ -99,5 +151,5 @@ class ElectionCycle:
""" """
message a string to me 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) await imcompiling.send(msg)
-59
View File
@@ -1,59 +0,0 @@
"""
Contains the MessageBuilder class.
"""
from datetime import datetime
from config import DakinyConfig
from utils import get_datetime
class MessageBuilder:
"""
Contains all the announcements that the bot will send regarding elections.
"""
def __init__(self, config: DakinyConfig, cycle: int) -> None:
self.config = config
self.cycle = cycle
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()
election_start: datetime = get_datetime(self.config.election_cycles[self.cycle].election_start)
term_start: datetime = get_datetime(self.config.election_cycles[self.cycle].term_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=election_start.strftime("%B %d"),
term_start_date=term_start.strftime("%B %d"),
)
# TODO make sure to split the message on 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"),
)
+6 -4
View File
@@ -5,7 +5,7 @@ Commands for testing.
import nextcord import nextcord
from nextcord.ext import commands from nextcord.ext import commands
from classes.message_builder import MessageBuilder from classes.election_cycle import ElectionCycle
class TestCog(commands.Cog, name="Test commands."): class TestCog(commands.Cog, name="Test commands."):
@@ -34,8 +34,10 @@ class TestCog(commands.Cog, name="Test commands."):
interaction (nextcord.Interaction): Interaction that invoked the command. interaction (nextcord.Interaction): Interaction that invoked the command.
""" """
msg_bulder = MessageBuilder(self.bot.config, 0) cycle = ElectionCycle(0, self.bot)
message = msg_bulder.build_vote_start()
message = cycle.build_election_announcement()
# doesn't tell discord that the message was responded to. Is a test command so I don't really care atm. # doesn't tell discord that the message was responded to. Is a test command so I don't really care atm.
await interaction.channel.send(content=message) for m in message:
await interaction.channel.send(content=m)
+5 -3
View File
@@ -2,7 +2,7 @@
_summary_ _summary_
""" """
from datetime import datetime, time from datetime import time
from nextcord.ext import commands, tasks from nextcord.ext import commands, tasks
from pytz import timezone from pytz import timezone
@@ -17,7 +17,8 @@ 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
self.election = ElectionCycle(datetime(2023, 5, 25), datetime(2023, 5, 29), datetime(2023, 5, 30), self.bot) # TODO need to figure out how to determine the 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):
@@ -32,4 +33,5 @@ class DailyLoop(commands.Cog, name="Daily Loop"):
async def do_thing(self): async def do_thing(self):
""" """ """ """
print("loop") print("loop")
await self.election.check_date() await self.current_election.check_date()
# TODO need to figure out how to change the election cycle after current one is over