diff --git a/classes/election_cycle.py b/classes/election_cycle.py index 9ba4c57..d2acd5b 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.HighCouncilNominationMsgId = 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. @@ -364,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 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) );