This commit is contained in:
@@ -1,17 +1,21 @@
|
|||||||
"""Contains various views to be used in the high council nomination process."""
|
"""Contains various views to be used in the high council nomination process."""
|
||||||
|
|
||||||
import nextcord
|
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.dakiny_settings import settings
|
||||||
from dakiny_bot.database.sql_data_classes import CouncilMembers, engine
|
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 (
|
from dakiny_bot.utils import (
|
||||||
NOMINEE_SPLITTER,
|
NOMINEE_SPLITTER,
|
||||||
decorate_cl_nomination,
|
decorate_cl_nomination,
|
||||||
decorate_dm_nomination,
|
decorate_dm_nomination,
|
||||||
get_election_name,
|
get_election_name,
|
||||||
)
|
)
|
||||||
from nextcord.interactions import Interaction
|
|
||||||
from sqlalchemy import select
|
|
||||||
from sqlalchemy.orm import Session
|
|
||||||
|
|
||||||
|
|
||||||
class NominateDMModal(nextcord.ui.Modal):
|
class NominateDMModal(nextcord.ui.Modal):
|
||||||
@@ -52,26 +56,37 @@ class NominateDMModal(nextcord.ui.Modal):
|
|||||||
interaction (Interaction): Interaction with user.
|
interaction (Interaction): Interaction with user.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
speech = decorate_dm_nomination(interaction.user.id, self.speech.value)
|
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_dm_nomination(interaction.user.id, self.speech.value if self.speech.value else "")
|
||||||
|
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
# user already in table
|
# user already in table
|
||||||
election_name = get_election_name()
|
election_name = get_election_name()
|
||||||
stmt = (
|
stmt = (
|
||||||
select(CouncilMembers)
|
select(CouncilMember)
|
||||||
.where(CouncilMembers.ElectionName == election_name)
|
.where(col(CouncilMember.election_name == election_name))
|
||||||
.where(CouncilMembers.MemberId == self.usr_id)
|
.where(col(CouncilMember.member_id == self.usr_id))
|
||||||
)
|
)
|
||||||
council_elect = session.scalar(stmt)
|
council_elect = session.scalar(stmt)
|
||||||
|
|
||||||
if not self.usr_nominated:
|
if council_elect is None:
|
||||||
|
dakiny_logger.error("Did not find member (id: %s) in election %s.", self.usr_id, election_name)
|
||||||
|
return
|
||||||
|
|
||||||
|
if not self.usr_nominated or council_elect.discord_manager_speech_id is None:
|
||||||
msg = await interaction.channel.send(speech)
|
msg = await interaction.channel.send(speech)
|
||||||
|
|
||||||
council_elect.DiscordManagerSpeechId = msg.id
|
council_elect.discord_manager_speech_id = msg.id
|
||||||
session.commit()
|
session.commit()
|
||||||
else:
|
else:
|
||||||
msg: nextcord.Message = await interaction.channel.fetch_message(council_elect.DiscordManagerSpeechId)
|
msg: nextcord.Message = await interaction.channel.fetch_message(council_elect.discord_manager_speech_id)
|
||||||
await msg.edit(content=decorate_dm_nomination(interaction.user.id, self.speech.value))
|
await msg.edit(
|
||||||
|
content=decorate_dm_nomination(interaction.user.id, self.speech.value if self.speech.value else "")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class NominateCLModal(nextcord.ui.Modal):
|
class NominateCLModal(nextcord.ui.Modal):
|
||||||
@@ -112,26 +127,37 @@ class NominateCLModal(nextcord.ui.Modal):
|
|||||||
interaction (Interaction): Interaction with user.
|
interaction (Interaction): Interaction with user.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
speech = decorate_cl_nomination(interaction.user.id, self.speech.value)
|
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_cl_nomination(interaction.user.id, self.speech.value if self.speech.value else "")
|
||||||
|
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
# user already in table
|
# user already in table
|
||||||
election_name = get_election_name()
|
election_name = get_election_name()
|
||||||
stmt = (
|
stmt = (
|
||||||
select(CouncilMembers)
|
select(CouncilMember)
|
||||||
.where(CouncilMembers.ElectionName == election_name)
|
.where(col(CouncilMember.election_name == election_name))
|
||||||
.where(CouncilMembers.MemberId == self.usr_id)
|
.where(col(CouncilMember.member_id == self.usr_id))
|
||||||
)
|
)
|
||||||
council_elect = session.scalar(stmt)
|
council_elect = session.scalar(stmt)
|
||||||
|
|
||||||
if not self.usr_nominated:
|
if council_elect is None:
|
||||||
|
dakiny_logger.error("Did not find member (id: %s) in election %s.", self.usr_id, election_name)
|
||||||
|
return
|
||||||
|
|
||||||
|
if not self.usr_nominated or council_elect.clan_leader_speech_id is None:
|
||||||
msg = await interaction.channel.send(speech)
|
msg = await interaction.channel.send(speech)
|
||||||
|
|
||||||
council_elect.ClanLeaderSpeechId = msg.id
|
council_elect.clan_leader_speech_id = msg.id
|
||||||
session.commit()
|
session.commit()
|
||||||
else:
|
else:
|
||||||
msg: nextcord.Message = await interaction.channel.fetch_message(council_elect.ClanLeaderSpeechId)
|
msg: nextcord.Message = await interaction.channel.fetch_message(council_elect.clan_leader_speech_id)
|
||||||
await msg.edit(content=decorate_cl_nomination(interaction.user.id, self.speech.value))
|
await msg.edit(
|
||||||
|
content=decorate_cl_nomination(interaction.user.id, self.speech.value if self.speech.value else "")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class NominateHighCouncilView(nextcord.ui.View):
|
class NominateHighCouncilView(nextcord.ui.View):
|
||||||
@@ -157,8 +183,16 @@ class NominateHighCouncilView(nextcord.ui.View):
|
|||||||
button (nextcord.ui.Button): Unused. idk why its needed in the decorator
|
button (nextcord.ui.Button): Unused. idk why its needed in the decorator
|
||||||
interaction (nextcord.Interaction): Interaction of user who pressed the button
|
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 council elect only
|
# enforce council elect only
|
||||||
if self.guild.get_role(settings.role_ids.clan_council_elect) not in interaction.user.roles:
|
if (
|
||||||
|
isinstance(interaction.user, nextcord.User)
|
||||||
|
or self.guild.get_role(settings.role_ids.clan_council_elect) not in interaction.user.roles
|
||||||
|
):
|
||||||
await interaction.response.send_message(
|
await interaction.response.send_message(
|
||||||
f"You must have the <@&{settings.role_ids.clan_council_elect}> role to vote.",
|
f"You must have the <@&{settings.role_ids.clan_council_elect}> role to vote.",
|
||||||
ephemeral=True,
|
ephemeral=True,
|
||||||
@@ -170,16 +204,20 @@ class NominateHighCouncilView(nextcord.ui.View):
|
|||||||
speech_content = ""
|
speech_content = ""
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
stmt = (
|
stmt = (
|
||||||
select(CouncilMembers)
|
select(CouncilMember)
|
||||||
.where(CouncilMembers.ElectionName == election_name)
|
.where(col(CouncilMember.election_name == election_name))
|
||||||
.where(CouncilMembers.MemberId == interaction.user.id)
|
.where(col(CouncilMember.member_id == interaction.user.id))
|
||||||
)
|
)
|
||||||
obj = session.scalar(stmt)
|
council_elect = session.scalar(stmt)
|
||||||
|
|
||||||
|
if council_elect is None:
|
||||||
|
dakiny_logger.error("Did not find member (id: %s) in election %s.", interaction.user.id, election_name)
|
||||||
|
return
|
||||||
|
|
||||||
# user needs to exist to get here anyways...
|
# user needs to exist to get here anyways...
|
||||||
if obj.DiscordManagerSpeechId is not None:
|
if council_elect.discord_manager_speech_id is not None:
|
||||||
usr_nominated = True
|
usr_nominated = True
|
||||||
msg = await interaction.channel.fetch_message(obj.DiscordManagerSpeechId)
|
msg = await interaction.channel.fetch_message(council_elect.discord_manager_speech_id)
|
||||||
speech_content = msg.content.split(NOMINEE_SPLITTER)[1]
|
speech_content = msg.content.split(NOMINEE_SPLITTER)[1]
|
||||||
|
|
||||||
await interaction.response.send_modal(NominateDMModal(interaction.user.id, usr_nominated, speech_content))
|
await interaction.response.send_modal(NominateDMModal(interaction.user.id, usr_nominated, speech_content))
|
||||||
@@ -200,8 +238,16 @@ class NominateHighCouncilView(nextcord.ui.View):
|
|||||||
button (nextcord.ui.Button): Unused. idk why its needed in the decorator
|
button (nextcord.ui.Button): Unused. idk why its needed in the decorator
|
||||||
interaction (nextcord.Interaction): Interaction of user who pressed the button
|
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 council elect only
|
# enforce council elect only
|
||||||
if self.guild.get_role(settings.role_ids.clan_council_elect) not in interaction.user.roles:
|
if (
|
||||||
|
isinstance(interaction.user, nextcord.User)
|
||||||
|
or self.guild.get_role(settings.role_ids.clan_council_elect) not in interaction.user.roles
|
||||||
|
):
|
||||||
await interaction.response.send_message(
|
await interaction.response.send_message(
|
||||||
f"You must have the <@&{settings.role_ids.clan_council_elect}> role to vote.",
|
f"You must have the <@&{settings.role_ids.clan_council_elect}> role to vote.",
|
||||||
ephemeral=True,
|
ephemeral=True,
|
||||||
@@ -213,16 +259,20 @@ class NominateHighCouncilView(nextcord.ui.View):
|
|||||||
speech_content = ""
|
speech_content = ""
|
||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
stmt = (
|
stmt = (
|
||||||
select(CouncilMembers)
|
select(CouncilMember)
|
||||||
.where(CouncilMembers.ElectionName == election_name)
|
.where(col(CouncilMember.election_name == election_name))
|
||||||
.where(CouncilMembers.MemberId == interaction.user.id)
|
.where(col(CouncilMember.member_id == interaction.user.id))
|
||||||
)
|
)
|
||||||
obj = session.scalar(stmt)
|
council_elect = session.scalar(stmt)
|
||||||
|
|
||||||
|
if council_elect is None:
|
||||||
|
dakiny_logger.error("Did not find member (id: %s) in election %s.", interaction.user.id, election_name)
|
||||||
|
return
|
||||||
|
|
||||||
# user needs to exist to get here anyways...
|
# user needs to exist to get here anyways...
|
||||||
if obj.ClanLeaderSpeechId is not None:
|
if council_elect.clan_leader_speech_id is not None:
|
||||||
usr_nominated = True
|
usr_nominated = True
|
||||||
msg = await interaction.channel.fetch_message(obj.ClanLeaderSpeechId)
|
msg = await interaction.channel.fetch_message(council_elect.clan_leader_speech_id)
|
||||||
speech_content = msg.content.split(NOMINEE_SPLITTER)[1]
|
speech_content = msg.content.split(NOMINEE_SPLITTER)[1]
|
||||||
|
|
||||||
await interaction.response.send_modal(NominateCLModal(interaction.user.id, usr_nominated, speech_content))
|
await interaction.response.send_modal(NominateCLModal(interaction.user.id, usr_nominated, speech_content))
|
||||||
|
|||||||
Reference in New Issue
Block a user