Merge commit 'f9da9fe7ef1b4d134884bba202af6a8ae5b06203'
build_latest / build (push) Successful in 1m10s
build_release / build (push) Successful in 49s

This commit is contained in:
2023-12-13 17:36:01 -05:00
4 changed files with 78 additions and 15 deletions
+35 -4
View File
@@ -6,7 +6,7 @@ import collections
from datetime import datetime, timedelta from datetime import datetime, timedelta
import nextcord import nextcord
from sqlalchemy import select from sqlalchemy import select, true
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from classes.nominate_views import NominateView from classes.nominate_views import NominateView
@@ -210,7 +210,7 @@ class ElectionCycle:
for index, message in enumerate(messages): for index, message in enumerate(messages):
# if last item, add the nominate button # if last item, add the nominate button
if index + 1 == len(messages): if index + 1 == len(messages):
await thread.send(message, view=NominateView(self.config)) await thread.send(message, view=NominateView(self.config, self.guild))
else: else:
await thread.send(message) await thread.send(message)
@@ -276,9 +276,12 @@ class ElectionCycle:
council_member.Elected = True council_member.Elected = True
session.commit() session.commit()
user: nextcord.Member = self.guild.get_member(user_id) member: nextcord.Member = self.guild.get_member(user_id)
await user.add_roles(council_elect_role) await member.add_roles(
council_elect_role,
reason=f"{member.display_name} has won a nomination and become a Council Elect.",
)
await election_thread.send(message) await election_thread.send(message)
@@ -300,6 +303,34 @@ class ElectionCycle:
self.election_over = True self.election_over = True
elect_role: nextcord.Role = self.guild.get_role(
self.config.params.clan_council_elect_role_id
)
council_role: nextcord.Role = self.guild.get_role(
self.config.params.clan_council_role_id
)
# role management
with Session(engine) as session:
stmt = (
select(CouncilMembers.MemberId)
.where(
CouncilMembers.ElectionCycle == get_election_cycle_name(self.config)
)
.where(CouncilMembers.Elected == true())
)
for member_id in session.scalars(stmt):
member = self.guild.get_member(member_id)
await member.remove_roles(
elect_role,
reason=f"{member.display_name} no longer needs the elect role.",
)
await member.add_roles(
council_role,
reason=f"{member.display_name} has been elected to council.",
)
election_thread: nextcord.Thread = self.guild.get_thread( election_thread: nextcord.Thread = self.guild.get_thread(
self.get_election_info().ThreadId self.get_election_info().ThreadId
) )
+37 -7
View File
@@ -19,7 +19,13 @@ class NominateModal(nextcord.ui.Modal):
Modal for nominating oneself. User writes a speech and then the callback sends the speech into the correct channel. Modal for nominating oneself. User writes a speech and then the callback sends the speech into the correct channel.
""" """
def __init__(self, config: DakinyConfig, usr_id: int, usr_nominated: bool, speech_content: str): def __init__(
self,
config: DakinyConfig,
usr_id: int,
usr_nominated: bool,
speech_content: str,
):
super().__init__( super().__init__(
"Nomination", "Nomination",
timeout=5 * 60, # 5 minutes timeout=5 * 60, # 5 minutes
@@ -70,8 +76,12 @@ class NominateModal(nextcord.ui.Modal):
) )
obj = session.scalar(stmt) obj = session.scalar(stmt)
msg: nextcord.Message = await interaction.channel.fetch_message(obj.SpeechMsgId) msg: nextcord.Message = await interaction.channel.fetch_message(
await msg.edit(content=decorate_nomination(interaction.user.id, self.speech.value)) obj.SpeechMsgId
)
await msg.edit(
content=decorate_nomination(interaction.user.id, self.speech.value)
)
class NominateView(nextcord.ui.View): class NominateView(nextcord.ui.View):
@@ -79,12 +89,19 @@ class NominateView(nextcord.ui.View):
The button to press to start your nomination. The button to press to start your nomination.
""" """
def __init__(self, config): def __init__(self, config: DakinyConfig, guild: nextcord.Guild):
super().__init__(timeout=None) super().__init__(timeout=None)
self.config: DakinyConfig = config self.config: DakinyConfig = config
self.guild: nextcord.Guild = guild
@nextcord.ui.button(label="Nominate Yourself", style=nextcord.ButtonStyle.primary, custom_id="nominate") @nextcord.ui.button(
async def nominate(self, button: nextcord.ui.Button, interaction: nextcord.Interaction) -> None: label="Nominate Yourself",
style=nextcord.ButtonStyle.primary,
custom_id="nominate",
)
async def nominate(
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
) -> None:
""" """
Gathers previous information about the user if they have already nominated themselves, then launches Gathers previous information about the user if they have already nominated themselves, then launches
the nomination modal to get their speech. the nomination modal to get their speech.
@@ -94,6 +111,17 @@ class NominateView(nextcord.ui.View):
interaction (nextcord.Interaction): Interaction of user who pressed the button interaction (nextcord.Interaction): Interaction of user who pressed the button
""" """
# enforce members only
if (
self.guild.get_role(self.config.params.clan_member_role_id)
not in interaction.user.roles
):
await interaction.response.send_message(
f"You must have the <@&{self.config.params.clan_member_role_id}> role to vote.",
ephemeral=True,
)
return
cycle = get_election_cycle_name(self.config) cycle = get_election_cycle_name(self.config)
usr_nominated = False usr_nominated = False
speech_content = "" speech_content = ""
@@ -110,7 +138,9 @@ class NominateView(nextcord.ui.View):
speech_content = msg.content.split(NOMINEE_SPLITTER)[1] speech_content = msg.content.split(NOMINEE_SPLITTER)[1]
await interaction.response.send_modal( await interaction.response.send_modal(
NominateModal(self.config, interaction.user.id, usr_nominated, speech_content) NominateModal(
self.config, interaction.user.id, usr_nominated, speech_content
)
) )
+2 -2
View File
@@ -130,9 +130,9 @@ class VoteView(nextcord.ui.View):
options = [ options = [
SelectOption( SelectOption(
label=f"{self.guild.get_member(nominee).nick} ☑️" label=f"{self.guild.get_member(nominee).display_name} ☑️"
if voted_for == nominee if voted_for == nominee
else f"{self.guild.get_member(nominee).nick}", else f"{self.guild.get_member(nominee).display_name}",
value=str(nominee), value=str(nominee),
) )
for nominee in session.scalars(stmt) for nominee in session.scalars(stmt)
+4 -2
View File
@@ -20,7 +20,9 @@ birthday_list: list[int] = []
logger = logging.getLogger("nextcord") logger = logging.getLogger("nextcord")
logger.setLevel(logging.DEBUG) logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename="nextcord.log", encoding="utf-8", mode="w") handler = logging.FileHandler(filename="nextcord.log", encoding="utf-8", mode="w")
handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s")) handler.setFormatter(
logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s")
)
logger.addHandler(handler) logger.addHandler(handler)
@@ -51,7 +53,7 @@ def main(cfg: DakinyConfig):
"""When Discord is connected""" """When Discord is connected"""
assert bot.user is not None assert bot.user is not None
create_database # pylint: disable=pointless-statement create_database # pylint: disable=pointless-statement
bot.add_view(NominateView(cfg)) bot.add_view(NominateView(cfg, bot.get_guild(cfg.params.guild_id)))
bot.add_view(VoteView(cfg, bot.get_guild(cfg.params.guild_id))) bot.add_view(VoteView(cfg, bot.get_guild(cfg.params.guild_id)))
print(f"{bot.user.name} has connected to Discord!") print(f"{bot.user.name} has connected to Discord!")