From a8def8fd69313b9f1a944358e64d69975013b175 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Tue, 24 Oct 2023 16:49:54 -0400 Subject: [PATCH] First start on nomination modal. --- classes/election_cycle.py | 9 +++++-- classes/nominate_views.py | 53 +++++++++++++++++++++++++++++++++++++++ cogs/commands/test.py | 4 ++- dakiny_bot.py | 2 ++ 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 classes/nominate_views.py diff --git a/classes/election_cycle.py b/classes/election_cycle.py index 274f856..38f2182 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -8,6 +8,7 @@ import nextcord from sqlalchemy import select from sqlalchemy.orm import Session +from classes.nominate_views import NominateView from config import DakinyConfig from database_setup.sql_data_classes import ElectionInformation, engine from utils import get_datetime @@ -166,8 +167,12 @@ class ElectionCycle: # election start message is returned as an array of string because it is too long. send each string # individually. messages = self.build_election_announcement() - for message in messages: - await election_channel.send(message) + for index, message in enumerate(messages): + # if last item, add the nominate button + if index + 1 == len(messages): + await election_channel.send(message, view=NominateView()) + else: + await election_channel.send(message) async def voting_start_action(self) -> None: """ diff --git a/classes/nominate_views.py b/classes/nominate_views.py new file mode 100644 index 0000000..9d6fb14 --- /dev/null +++ b/classes/nominate_views.py @@ -0,0 +1,53 @@ +""" +Contains various views to be used in the nomination process. +""" + +import nextcord +from nextcord.interactions import Interaction + + +class NominateModal(nextcord.ui.Modal): + """ + Modal for nominating oneself. User writes a speech and then the callback sends the speech into the correct channel. + """ + + def __init__(self): + super().__init__( + "Nomination", + timeout=5 * 60, # 5 minutes + ) + + self.speech = nextcord.ui.TextInput( + label="Nomination Speech", + style=nextcord.TextInputStyle.paragraph, + placeholder="Please write your speech here.", + required=True, + max_length=1500, + ) + self.add_item(self.speech) + + async def callback(self, interaction: Interaction) -> None: + """ + Sends the nomination speech into the voting channel and enters the user into the voting database. + + Args: + interaction (Interaction): Interaction with user. + + """ + # TODO add nominee to database, make sure user hasn't nominated themself before, + # make the nomination message better + + await interaction.channel.send(self.speech.value) + + +class NominateView(nextcord.ui.View): + """ + The button to press to start your nomination. + """ + + def __init__(self): + super().__init__(timeout=None) + + @nextcord.ui.button(label="Nominate Yourself", style=nextcord.ButtonStyle.primary, custom_id="nominate") + async def nominate(self, button: nextcord.ui.Button, interaction: nextcord.Interaction) -> None: + await interaction.response.send_modal(NominateModal()) diff --git a/cogs/commands/test.py b/cogs/commands/test.py index 948a331..3bf93a9 100644 --- a/cogs/commands/test.py +++ b/cogs/commands/test.py @@ -6,6 +6,7 @@ import nextcord from nextcord.ext import commands from classes.election_cycle import ElectionCycle +from config import DakinyConfig class TestCog(commands.Cog, name="Test commands."): @@ -51,7 +52,8 @@ class TestCog(commands.Cog, name="Test commands."): interaction (nextcord.Interaction): _description_ """ - cycle = ElectionCycle(0, self.bot) + config: DakinyConfig = self.bot.config + cycle = ElectionCycle(4, config, self.bot.get_guild(config.params.guild_id)) await cycle.election_start_action() @test.subcommand() diff --git a/dakiny_bot.py b/dakiny_bot.py index 076cd15..a60c266 100644 --- a/dakiny_bot.py +++ b/dakiny_bot.py @@ -8,6 +8,7 @@ from hydra.core.config_store import ConfigStore from nextcord.ext import commands import database_setup.create_database as create_database +from classes.nominate_views import NominateView from config import DakinyConfig cs = ConfigStore.instance() @@ -49,6 +50,7 @@ def main(cfg: DakinyConfig): """When Discord is connected""" assert bot.user is not None create_database # pylint: disable=pointless-statement + bot.add_view(NominateView()) print(f"{bot.user.name} has connected to Discord!") # Run Discord bot