diff --git a/bot_messages/vote_start.txt b/bot_messages/vote_start.txt index 5f59e83..f67865e 100644 --- a/bot_messages/vote_start.txt +++ b/bot_messages/vote_start.txt @@ -11,8 +11,4 @@ After a candidate receives 3 votes, their spot is secured in the new council, bu Votes for multiple candidates from the same person will not be counted. RS3 Clan Members -OSRS Clan Members - -==================== -Clan nominees: -{clan_nominees} \ No newline at end of file +OSRS Clan Members \ No newline at end of file diff --git a/classes/election_cycle.py b/classes/election_cycle.py index 57e5a9a..ce16cfc 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -122,33 +122,11 @@ class ElectionCycle: message = file.read() vote_end: datetime = get_datetime(self.config.election_cycles[self.cycle].voting_end) - votes: dict[int, int] = {} - - with Session(engine) as session: - stmt = select(CouncilMembers).where(ElectionCycle == get_election_cycle_name(self.config)) - - for nominee in session.scalars(stmt): - votes[nominee.MemberId] = 0 - - stmt = select(Votes).where(ElectionCycle == get_election_cycle_name(self.config)) - - for vote in session.scalars(stmt): - votes[vote.VotedForId] += 1 - - nominees_str: str = "" - for user_id, num_votes in votes.items(): - nominees_str += f"<@{user_id}>: " - - for num in range(num_votes): - nominees_str += "☑️" - nominees_str += "\n" - return message.format( clan_member_id=self.config.params.clan_member_role_id, voting_emote_id=self.config.params.voting_emote_id, voting_end=vote_end.strftime("%B %d"), voting_end_day=vote_end.strftime("%A"), - clan_nominees=nominees_str, ) async def check_date(self) -> None: diff --git a/utils.py b/utils.py index d36ab45..2b11006 100644 --- a/utils.py +++ b/utils.py @@ -4,7 +4,11 @@ File contains util functions. from datetime import datetime +from sqlalchemy import select +from sqlalchemy.orm import Session + from config import DakinyConfig +from database_setup.sql_data_classes import CouncilMembers, Votes, engine def get_datetime(day: str) -> datetime: @@ -62,3 +66,38 @@ def get_current_cycle(config: DakinyConfig) -> int: return cycle return -1 + + +def build_votes_str(config: DakinyConfig) -> str: + """ + Creates a string that contains all nominees and their votes to be sent as a discord message. + + Args: + config (DakinyConfig): Daking config object + + Returns: + str: String that contains all nominees and their votes. + """ + + votes: dict[int, int] = {} + + with Session(engine) as session: + stmt = select(CouncilMembers).where(CouncilMembers.ElectionCycle == get_election_cycle_name(config)) + + for nominee in session.scalars(stmt): + votes[nominee.MemberId] = 0 + + stmt = select(Votes).where(Votes.ElectionCycle == get_election_cycle_name(config)) + + for vote in session.scalars(stmt): + votes[vote.VotedForId] += 1 + + votes_str: str = "====================" + for user_id, num_votes in votes.items(): + votes_str += f"<@{user_id}>: " + + for num in range(num_votes): + votes_str += "☑️" + votes_str += "\n" + + return votes_str