From abd9be6341e94cdc681b49d1bfba81a3e8fd740e Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sat, 1 Jul 2023 23:10:48 -0400 Subject: [PATCH] Rework message builder to format from text file. --- bot_messages/message_builder.py | 58 --------------------------------- bot_messages/vote_start.txt | 12 ++++--- classes/message_builder.py | 37 +++++++++++++++++++++ cogs/commands/test.py | 2 +- utils.py | 18 ++++++++++ 5 files changed, 63 insertions(+), 64 deletions(-) delete mode 100644 bot_messages/message_builder.py create mode 100644 classes/message_builder.py create mode 100644 utils.py diff --git a/bot_messages/message_builder.py b/bot_messages/message_builder.py deleted file mode 100644 index dda3ebc..0000000 --- a/bot_messages/message_builder.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -Contains the MessageBuilder class. -""" - -from datetime import datetime - -from config import DakinyConfig - - -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 get_vote_start(self) -> str: - """ - Builds the voting start message out of template and configurations. - - Returns: - str: Message to send - """ - - vote_end = self.get_datetime(self.config.election_cycles[self.cycle].voting_end) - - message = "====================\n" - message += f"<@&{self.config.params.clan_member_role_id}>, it's time to vote for clan council.\n\n" - message += f"React with <:{self.config.params.voting_emote_id}> to vote. " - message += f"Voting ends next Wednesday , {vote_end.date()}." - message += f"Announcement of the results to whole clan will happen {vote_end.date()}.\n\n" - message += "After a candidate receives 3 votes, their spot is secured in the new council, " - message += "but you can still vote for them to show your support.\n\n" - message += "**Just a reminder:** only the people listed below are eligible to vote, and each " - message += "clan member may **only vote for one person**. Votes from guests, diplomats, or allies " - message += "will be annulled.\nVotes for multiple candidates from the same person will not be counted. \n\n" - message += "RS3 Clan Members\n" - message += "OSRS Clan Members" - - return message - - def get_datetime(self, day: str) -> datetime: - """ - Takes in a string month and day and returns a datetime object of that Month/Day this year. - - Args: - day (str): Month/Day in that format. - - Returns: - datetime: Datetime representation of Month/Day. - """ - today = datetime.today() - month = int(day.split("/")[0]) - day = int(day.split("/")[1]) - - return datetime(today.year, month, day) diff --git a/bot_messages/vote_start.txt b/bot_messages/vote_start.txt index 5cd100d..d8662ba 100644 --- a/bot_messages/vote_start.txt +++ b/bot_messages/vote_start.txt @@ -1,12 +1,14 @@ -<@&{{clan_member_id}}>, it's time to vote for clan council. +==================== +<@&{clan_member_id}>, it's time to vote for clan council. -React with <:Rank_Admin:479008185240518656> to vote. -Voting ends next Wednesday , {{voting_end_date}}. -Announcement of the results to whole clan will happen April 6th. +React with <{voting_emote_id}> to vote. +Voting ends next {voting_end_day}, {voting_end_date}. +Results will then be announced. After a candidate receives 3 votes, their spot is secured in the new council, but you can still vote for them to show your support. -**Just a reminder:** only the people listed below are eligible to vote, and each clan member may **only vote for one person**. Votes from guests, diplomats, or allies will be annulled. Votes for multiple candidates from the same person will not be counted. +**Just a reminder:** only the people listed below are eligible to vote, and each clan member may **only vote for one person**. Votes from guests, diplomats, or allies will be annulled. +Votes for multiple candidates from the same person will not be counted. RS3 Clan Members OSRS Clan Members \ No newline at end of file diff --git a/classes/message_builder.py b/classes/message_builder.py new file mode 100644 index 0000000..559130b --- /dev/null +++ b/classes/message_builder.py @@ -0,0 +1,37 @@ +""" +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 get_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"), + ) diff --git a/cogs/commands/test.py b/cogs/commands/test.py index ed15292..8a433ce 100644 --- a/cogs/commands/test.py +++ b/cogs/commands/test.py @@ -5,7 +5,7 @@ Commands for testing. import nextcord from nextcord.ext import commands -from bot_messages.message_builder import MessageBuilder +from classes.message_builder import MessageBuilder class TestCog(commands.Cog, name="Test commands."): diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..7a2fc75 --- /dev/null +++ b/utils.py @@ -0,0 +1,18 @@ +from datetime import datetime + + +def get_datetime(day: str) -> datetime: + """ + Takes in a string month and day and returns a datetime object of that Month/Day this year. + + Args: + day (str): Month/Day in that format. + + Returns: + datetime: Datetime representation of Month/Day. + """ + today = datetime.today() + month = int(day.split("/")[0]) + day = int(day.split("/")[1]) + + return datetime(today.year, month, day)