diff --git a/classes/election_cycle.py b/classes/election_cycle.py index ce16cfc..210c2f3 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -11,8 +11,8 @@ from sqlalchemy.orm import Session from classes.nominate_views import NominateView from classes.voting_views import VoteView from config import DakinyConfig -from database_setup.sql_data_classes import CouncilMembers, ElectionInformation, Votes, engine -from utils import get_datetime, get_election_cycle_name +from database_setup.sql_data_classes import ElectionInformation, engine +from utils import build_votes_str, get_datetime, get_election_cycle_name class ElectionCycle: @@ -183,7 +183,15 @@ class ElectionCycle: message = self.build_vote_start() clan_member_role: nextcord.Role = self.guild.get_role(self.config.params.clan_member_role_id) election_channel: nextcord.TextChannel = self.guild.get_channel(self.get_election_info().ElectionChannel) - await election_channel.send(message, view=VoteView()) + await election_channel.send(message, view=VoteView(self.config, self.guild)) + vote_msg = await election_channel.send(build_votes_str(self.config)) + + with Session(engine) as session: + stmt = select(ElectionInformation).where(ElectionCycle == get_election_cycle_name(self.config)) + info = session.scalar(stmt) + info.CouncilVoteMsgId = vote_msg.id + session.commit() + await election_channel.set_permissions(clan_member_role, send_messages=False) async def voting_end_action(self) -> None: diff --git a/classes/voting_views.py b/classes/voting_views.py index 36c476d..9c34966 100644 --- a/classes/voting_views.py +++ b/classes/voting_views.py @@ -2,40 +2,39 @@ Contains various views to be used while voting. Controls the logic when performing the vote. """ -from typing import List, Optional +from typing import List import nextcord from nextcord import SelectOption +from nextcord.ext import commands from nextcord.interactions import Interaction -from nextcord.utils import MISSING from sqlalchemy import select from sqlalchemy.orm import Session -from config import DakinyConfig -from database_setup.sql_data_classes import CouncilMembers, engine -from utils import get_election_cycle_name - - -class VoteActionView(nextcord.ui.View): - """ - Modal for nominating oneself. User writes a speech and then the callback sends the speech into the correct channel. - """ - - def __init__(self): - super().__init__(timeout=300) - self.add_item(VoteSelect()) +from config import DakinyConfig, ElectionCycle +from database_setup.sql_data_classes import CouncilMembers, ElectionInformation, engine +from utils import build_votes_str, get_election_cycle_name class VoteSelect(nextcord.ui.StringSelect): - def __init__(self) -> None: + def __init__(self, options: List["SelectOption"], config: DakinyConfig) -> None: super().__init__( placeholder="Choose 1", min_values=1, max_values=1, - options=[SelectOption(label="one"), SelectOption(label="two"), SelectOption(label="three")], + options=options, ) + self.config = config async def callback(self, interaction: Interaction): + vote_message_str = build_votes_str(self.config) + with Session(engine) as session: + stmt = select(ElectionInformation.CouncilVoteMsgId).where( + ElectionCycle == get_election_cycle_name(self.config) + ) + vote_msg_id = session.scalar(stmt) + 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) @@ -44,8 +43,10 @@ class VoteView(nextcord.ui.View): The button to press to start your nomination. """ - def __init__(self): + def __init__(self, config: DakinyConfig, guild: nextcord.Guild): super().__init__(timeout=None) + self.config = config + self.guild = guild @nextcord.ui.button(label="Vote", style=nextcord.ButtonStyle.primary, custom_id="vote") async def vote(self, button: nextcord.ui.Button, interaction: nextcord.Interaction) -> None: @@ -57,6 +58,14 @@ 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")] + + view = nextcord.ui.View() + view.add_item(VoteSelect(options=options, config=self.config)) + await interaction.response.send_message( - content="Please vote for a clan member!", view=VoteActionView(), ephemeral=True + content="Please vote for a clan member!", + view=view, + ephemeral=True, ) diff --git a/dakiny_bot.py b/dakiny_bot.py index 8347da1..0c0cc53 100644 --- a/dakiny_bot.py +++ b/dakiny_bot.py @@ -52,7 +52,7 @@ def main(cfg: DakinyConfig): assert bot.user is not None create_database # pylint: disable=pointless-statement bot.add_view(NominateView(cfg)) - bot.add_view(VoteView()) + bot.add_view(VoteView(cfg, bot.get_guild(cfg.params.guild_id))) print(f"{bot.user.name} has connected to Discord!") # Run Discord bot