Voting interacts with database.
This commit is contained in:
+43
-5
@@ -27,15 +27,34 @@ class VoteSelect(nextcord.ui.StringSelect):
|
||||
self.config = config
|
||||
|
||||
async def callback(self, interaction: Interaction):
|
||||
vote_message_str = build_votes_str(self.config)
|
||||
election_name = get_election_cycle_name(self.config)
|
||||
with Session(engine) as session:
|
||||
stmt = select(ElectionInformation.CouncilVoteMsgId).where(
|
||||
ElectionInformation.ElectionCycle == get_election_cycle_name(self.config)
|
||||
ElectionInformation.ElectionCycle == election_name
|
||||
)
|
||||
vote_msg_id = session.scalar(stmt)
|
||||
voted_for = int(self.values[0])
|
||||
new_vote = Votes(ElectionCycle=election_name, VoterId=interaction.user.id, VotedForId=voted_for)
|
||||
|
||||
try:
|
||||
session.add(new_vote)
|
||||
session.commit()
|
||||
|
||||
except exc.IntegrityError:
|
||||
session.rollback()
|
||||
stmt = (
|
||||
select(Votes)
|
||||
.where(Votes.ElectionCycle == election_name)
|
||||
.where(Votes.VoterId == interaction.user.id)
|
||||
)
|
||||
vote = session.scalar(stmt)
|
||||
vote.VotedForId = voted_for
|
||||
session.commit()
|
||||
|
||||
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=self.values[0], view=None)
|
||||
await interaction.edit(content=f"You have voted for <@{self.values[0]}>", view=None)
|
||||
|
||||
|
||||
class VoteView(nextcord.ui.View):
|
||||
@@ -58,8 +77,27 @@ class VoteView(nextcord.ui.View):
|
||||
interaction (nextcord.Interaction): Interaction of user who pressed the button
|
||||
"""
|
||||
|
||||
# TODO get the clan members to vote for. Check if user has voted and add a checkmark to that name.
|
||||
options = [SelectOption(label="one"), SelectOption(label="two"), SelectOption(label="three")]
|
||||
election: str = get_election_cycle_name(self.config)
|
||||
|
||||
with Session(engine) as session:
|
||||
stmt = (
|
||||
select(Votes.VotedForId)
|
||||
.where(Votes.ElectionCycle == election)
|
||||
.where(Votes.VoterId == interaction.user.id)
|
||||
)
|
||||
voted_for = session.scalar(stmt)
|
||||
|
||||
stmt = select(CouncilMembers.MemberId).where(CouncilMembers.ElectionCycle == election)
|
||||
|
||||
options = [
|
||||
SelectOption(
|
||||
label=f"{self.guild.get_member(nominee).name} ☑️"
|
||||
if voted_for == nominee
|
||||
else f"{self.guild.get_member(nominee).name}",
|
||||
value=str(nominee),
|
||||
)
|
||||
for nominee in session.scalars(stmt)
|
||||
]
|
||||
|
||||
view = nextcord.ui.View()
|
||||
view.add_item(VoteSelect(options=options, config=self.config))
|
||||
|
||||
Reference in New Issue
Block a user