Pull the vote count message out of vote start.

Make its own builder function in utils.
This commit is contained in:
2023-11-03 11:55:21 -04:00
parent a5ef579767
commit fa3284b9e3
3 changed files with 40 additions and 27 deletions
-4
View File
@@ -12,7 +12,3 @@ Votes for multiple candidates from the same person will not be counted.
RS3 Clan Members
OSRS Clan Members
====================
Clan nominees:
{clan_nominees}
-22
View File
@@ -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:
+39
View File
@@ -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