From a9c3719c298c26459cbfa529bc52d40be07f8130 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sun, 17 Dec 2023 14:44:32 -0500 Subject: [PATCH 1/4] Add two new fields to election information. --- database_setup/database.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/database_setup/database.sql b/database_setup/database.sql index e976e69..4a66960 100644 --- a/database_setup/database.sql +++ b/database_setup/database.sql @@ -3,6 +3,8 @@ CREATE TABLE ElectionInformation( ThreadId bigint unsigned not null, CouncilVoteMsgId bigint unsigned, HighCouncilVoteMsgId bigint unsigned, + CouncilNominationMsgId bigint unsigned, + HighCouncilNominationMsgId bigint unsigned, PRIMARY KEY (ElectionCycle) ); From 8394cdd6fd4db45ebab37e74944dc9d5f9c1ac17 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sun, 17 Dec 2023 15:06:19 -0500 Subject: [PATCH 2/4] Remove nomination views once they are done. --- classes/election_cycle.py | 61 +++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/classes/election_cycle.py b/classes/election_cycle.py index 9ba4c57..3201098 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -201,16 +201,9 @@ class ElectionCycle: thread_msg: nextcord.Message = await parent.send(content=quarter_name) - # create new entry into database. thread: nextcord.Thread = await parent.create_thread( name=quarter_name, message=thread_msg ) - with Session(engine) as session: - new_cycle = ElectionInformation( - ElectionCycle=quarter_name, ThreadId=thread.id - ) - session.add(new_cycle) - session.commit() # post message announcing election # election start message is returned as an array of string because it is too long. send each string @@ -219,10 +212,22 @@ class ElectionCycle: for index, message in enumerate(messages): # if last item, add the nominate button if index + 1 == len(messages): - await thread.send(message, view=NominateView(self.config, self.guild)) + nominate_msg: nextcord.Message = await thread.send( + message, view=NominateView(self.config, self.guild) + ) else: await thread.send(message) + # create new entry into database. + with Session(engine) as session: + new_cycle = ElectionInformation( + ElectionCycle=quarter_name, + ThreadId=thread.id, + CouncilNominationMsgId=nominate_msg.id, + ) + session.add(new_cycle) + session.commit() + async def voting_start_action(self) -> None: """ Sends message informing members that voting has started. @@ -246,6 +251,12 @@ class ElectionCycle: info.CouncilVoteMsgId = vote_msg.id session.commit() + thread: nextcord.Thread = self.guild.get_thread(info.ThreadId) + nominate_msg: nextcord.Message = await thread.fetch_message( + info.CouncilNominationMsgId + ) + await nominate_msg.edit(view=None) + async def voting_end_action(self) -> None: """ Performs required actions for when voting time has ended. @@ -259,18 +270,28 @@ class ElectionCycle: self.get_election_info().ThreadId ) - # remove vote button from vote message + nominate_msg: nextcord.Message = await election_thread.send( + message, view=NominateHighCouncilView(self.config, self.guild) + ) + election_name = get_election_cycle_name(self.config) + with Session(engine) as session: - stmt = select(ElectionInformation.CouncilVoteMsgId).where( + # remove vote button from vote message + stmt = select(ElectionInformation).where( ElectionInformation.ElectionCycle == election_name ) - vote_msg_id = session.scalar(stmt) - vote_msg: nextcord.Message = await election_thread.fetch_message(vote_msg_id) - await vote_msg.edit(view=None) + info = session.scalar(stmt) + vote_msg: nextcord.Message = await election_thread.fetch_message( + info.CouncilVoteMsgId + ) + await vote_msg.edit(view=None) - # add the highcouncil role to everyone with 3 or more votes. set elected to true in database - with Session(engine) as session: + # add the highcouncil nomination message to election information + info.HighCouncilVoteMsgId = nominate_msg.id + session.commit() + + # add the highcouncil role to everyone with 3 or more votes. set elected to true in database stmt = select(Votes.VotedForId).where(Votes.ElectionCycle == election_name) vote_count = collections.Counter(session.scalars(stmt)) @@ -292,10 +313,6 @@ class ElectionCycle: reason=f"{member.display_name} has won a nomination and become a Council Elect.", ) - await election_thread.send( - message, view=NominateHighCouncilView(self.config, self.guild) - ) - async def highcouncil_election_start_action(self) -> None: """ Performs required actions for when the highcouncil is elected. @@ -320,6 +337,12 @@ class ElectionCycle: info.HighCouncilVoteMsgId = vote_msg.id session.commit() + # remove highcouncil nomination view + nomination_msg: nextcord.Message = await election_thread.fetch_message( + info.HighCouncilNominationMsgId + ) + await nomination_msg.edit(view=None) + async def term_start_action(self) -> None: """ Marks the election as over. From 757d2e5c19334d623535a8ae34de460e2aef799f Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sun, 17 Dec 2023 15:27:54 -0500 Subject: [PATCH 3/4] Stored nomination message in wrong place. --- classes/election_cycle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/election_cycle.py b/classes/election_cycle.py index 3201098..3d81d1c 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -288,7 +288,7 @@ class ElectionCycle: await vote_msg.edit(view=None) # add the highcouncil nomination message to election information - info.HighCouncilVoteMsgId = nominate_msg.id + info.HighCouncilNominationMsgId = nominate_msg.id session.commit() # add the highcouncil role to everyone with 3 or more votes. set elected to true in database From 43ef19fd828e2ebb76eff564b65ed38c8e5e0741 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sun, 17 Dec 2023 15:36:04 -0500 Subject: [PATCH 4/4] Cover highcouncil edge case. --- classes/election_cycle.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/classes/election_cycle.py b/classes/election_cycle.py index 3d81d1c..d2acd5b 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -387,8 +387,14 @@ class ElectionCycle: stmt = select(Votes.VotedForId).where(Votes.ElectionCycle == cl_election) cl_votes = collections.Counter(session.scalars(stmt)) - dm_winner = max(dm_votes, key=dm_votes.get) - cl_winner = max(cl_votes, key=cl_votes.get) + try: + dm_winner = max(dm_votes, key=dm_votes.get) + except ValueError: + dm_winner = "No winner" + try: + cl_winner = max(cl_votes, key=cl_votes.get) + except ValueError: + cl_winner = "No winner" election_thread: nextcord.Thread = self.guild.get_thread( self.get_election_info().ThreadId