Files
dakiny-bot/dakiny_bot/classes/nominate_views.py
T
imcompiling 45f17e09d3
build_develop / build (push) Successful in 44s
Feat: Update Nominate classes to use models.
2025-07-24 14:28:39 -04:00

150 lines
5.6 KiB
Python

"""Contains various views to be used in the nomination process."""
import nextcord
from nextcord.interactions import Interaction
from sqlalchemy import select
from sqlalchemy.orm import Session
from sqlmodel import col
from dakiny_bot.dakiny_settings import settings
from dakiny_bot.database.db import engine
from dakiny_bot.database.models import CouncilMember
from dakiny_bot.logger import dakiny_logger
from dakiny_bot.utils import NOMINEE_SPLITTER, decorate_nomination, get_election_name
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,
usr_id: int,
usr_nominated: bool, # noqa: FBT001
speech_content: str,
):
super().__init__(
"Nomination",
timeout=5 * 60, # 5 minutes
)
self.usr_id: int = usr_id
self.usr_nominated = usr_nominated
self.speech = nextcord.ui.TextInput(
label="Nomination Speech",
style=nextcord.TextInputStyle.paragraph,
placeholder="Please write your speech here.",
required=True,
max_length=1500,
default_value=speech_content,
)
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.
"""
if interaction.user is None or not isinstance(interaction.channel, nextcord.TextChannel):
# ignore none... Don't know what situation this is.
# if not textchannel, don't know how callback happened
return
speech = decorate_nomination(interaction.user.id, self.speech.value if self.speech.value else "")
election_name = get_election_name()
with Session(engine) as session:
if not self.usr_nominated:
msg = await interaction.channel.send(speech)
council_member = CouncilMember(
election_name=election_name,
member_id=interaction.user.id,
speech_msg_id=msg.id,
)
session.add(council_member)
session.commit()
else:
# user already nominated, just need to update the election speech
stmt = (
select(CouncilMember)
.where(col(CouncilMember.election_name == election_name))
.where(col(CouncilMember.member_id == self.usr_id))
)
council_elect = session.scalar(stmt)
if council_elect is None:
dakiny_logger.error("Did not find member (id: %s) in election %s.", self.usr_id, election_name)
return
if council_elect.speech_msg_id is None:
msg = await interaction.channel.send(speech)
council_elect.speech_msg_id = msg.id
session.commit()
else:
msg: nextcord.Message = await interaction.channel.fetch_message(council_elect.speech_msg_id)
await msg.edit(content=speech)
class NominateView(nextcord.ui.View):
"""The button to press to start your nomination."""
def __init__(self, guild: nextcord.Guild):
super().__init__(timeout=None)
self.guild: nextcord.Guild = guild
@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: # noqa: ARG002
"""
Button to nominate oneself for Clan Council.
Gathers previous information about the user if they have already nominated themselves, then launches
the nomination modal to get their speech.
Args:
button (nextcord.ui.Button): Unused. idk why its needed in the decorator
interaction (nextcord.Interaction): Interaction of user who pressed the button
"""
if interaction.user is None or not isinstance(interaction.channel, nextcord.TextChannel):
# ignore none... Don't know what situation this is.
# if not textchannel, don't know how callback happened
return
# enforce members only
if (
isinstance(interaction.user, nextcord.User)
or self.guild.get_role(settings.role_ids.clan_member) not in interaction.user.roles
):
await interaction.response.send_message(
f"You must have the <@&{settings.role_ids.clan_member}> role to vote.",
ephemeral=True,
)
return
election_name = get_election_name()
usr_nominated = False
speech_content = ""
with Session(engine) as session:
stmt = (
select(CouncilMember)
.where(col(CouncilMember.election_name == election_name))
.where(col(CouncilMember.member_id == interaction.user.id))
)
obj = session.scalar(stmt)
if obj is not None and obj.speech_msg_id is not None:
usr_nominated = True
msg = await interaction.channel.fetch_message(obj.speech_msg_id)
speech_content = msg.content.split(NOMINEE_SPLITTER)[1]
await interaction.response.send_modal(NominateModal(interaction.user.id, usr_nominated, speech_content))