From 80c4f053bc17abad1a6cac3d1b1c1f18362df19f Mon Sep 17 00:00:00 2001 From: imcompiling Date: Fri, 3 Nov 2023 13:38:23 -0400 Subject: [PATCH] Add abstaining from voting. --- classes/voting_views.py | 21 ++++++++++++++++----- utils.py | 3 ++- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/classes/voting_views.py b/classes/voting_views.py index 86afc4f..5c86712 100644 --- a/classes/voting_views.py +++ b/classes/voting_views.py @@ -6,13 +6,12 @@ from typing import List import nextcord from nextcord import SelectOption -from nextcord.ext import commands from nextcord.interactions import Interaction -from sqlalchemy import select +from sqlalchemy import exc, select from sqlalchemy.orm import Session -from config import DakinyConfig, ElectionCycle -from database_setup.sql_data_classes import CouncilMembers, ElectionInformation, engine +from config import DakinyConfig +from database_setup.sql_data_classes import CouncilMembers, ElectionInformation, Votes, engine from utils import build_votes_str, get_election_cycle_name @@ -33,7 +32,12 @@ class VoteSelect(nextcord.ui.StringSelect): ElectionInformation.ElectionCycle == election_name ) vote_msg_id = session.scalar(stmt) + + if self.values[0] == "Abstain": + voted_for = None + else: voted_for = int(self.values[0]) + new_vote = Votes(ElectionCycle=election_name, VoterId=interaction.user.id, VotedForId=voted_for) try: @@ -54,7 +58,13 @@ class VoteSelect(nextcord.ui.StringSelect): vote_message_str = build_votes_str(self.config) vote_msg = await interaction.channel.fetch_message(vote_msg_id) await vote_msg.edit(content=vote_message_str) - await interaction.edit(content=f"You have voted for <@{self.values[0]}>", view=None) + + if voted_for is not None: + msg = f"You have voted for <@{voted_for}>" + else: + msg = "You have abstained from voting." + + await interaction.edit(content=msg, view=None) class VoteView(nextcord.ui.View): @@ -98,6 +108,7 @@ class VoteView(nextcord.ui.View): ) for nominee in session.scalars(stmt) ] + options.append(SelectOption(label="Abstain ☑️" if voted_for is None else "Abstain", value="Abstain")) view = nextcord.ui.View() view.add_item(VoteSelect(options=options, config=self.config)) diff --git a/utils.py b/utils.py index b3a9f23..8f2774b 100644 --- a/utils.py +++ b/utils.py @@ -90,7 +90,8 @@ def build_votes_str(config: DakinyConfig) -> str: stmt = select(Votes).where(Votes.ElectionCycle == get_election_cycle_name(config)) for vote in session.scalars(stmt): - votes[vote.VotedForId] += 1 + if vote.VotedForId is not None: + votes[vote.VotedForId] += 1 votes_str: str = "====================" for user_id, num_votes in votes.items():