Updated files in classes directory to use new configs. Ensured usages are updated.

This commit is contained in:
2024-09-03 15:57:50 -04:00
parent 2244765d87
commit 3b289e74c8
8 changed files with 78 additions and 109 deletions
+30 -33
View File
@@ -9,7 +9,7 @@ from classes.nominate_highcouncil_views import NominateHighCouncilView
from classes.nominate_views import NominateView from classes.nominate_views import NominateView
from classes.voting_highcouncil_views import VoteHighcouncilView from classes.voting_highcouncil_views import VoteHighcouncilView
from classes.voting_views import VoteView from classes.voting_views import VoteView
from DakinySettings import DakinySettings from DakinySettings import settings
from database_setup.sql_data_classes import ( from database_setup.sql_data_classes import (
CouncilMembers, CouncilMembers,
ElectionInformation, ElectionInformation,
@@ -39,7 +39,7 @@ class Election:
the cycle. the cycle.
""" """
def __init__(self, cycle: int, config: DakinySettings, guild: nextcord.Guild) -> None: def __init__(self, cycle: int, guild: nextcord.Guild) -> None:
""" """
Generates all dates relevant to the election cycle determined by the config and cycle. Generates all dates relevant to the election cycle determined by the config and cycle.
@@ -48,9 +48,8 @@ class Election:
config (DakinyConfig): The entire config object. config (DakinyConfig): The entire config object.
guild (nextcord.Guild): The guild that this election is running in. guild (nextcord.Guild): The guild that this election is running in.
""" """
self.config: DakinySettings = config
self.cycle = cycle self.cycle = cycle
self.election_cycle = self.config.election_cycles[self.cycle] self.election_cycle = settings.election_cycles[self.cycle]
self.election_start: datetime = get_datetime(self.election_cycle.election_start) self.election_start: datetime = get_datetime(self.election_cycle.election_start)
self.term_start: datetime = get_datetime(self.election_cycle.term_start) self.term_start: datetime = get_datetime(self.election_cycle.term_start)
self.term_end: datetime = get_datetime(self.election_cycle.term_end) self.term_end: datetime = get_datetime(self.election_cycle.term_end)
@@ -74,12 +73,12 @@ class Election:
message = file.read() message = file.read()
next_election = get_datetime( next_election = get_datetime(
self.config.election_cycles[(self.cycle + 1) % len(self.config.election_cycles)].election_start settings.election_cycles[(self.cycle + 1) % len(settings.election_cycles)].election_start
) )
message = message.format( message = message.format(
clan_council_id=self.config.params.clan_council_role_id, clan_council_id=settings.role_ids.clan_council,
high_council_id=self.config.params.high_council_role_id, high_council_id=settings.role_ids.high_council,
election_start=self.election_start.strftime("%B %d"), election_start=self.election_start.strftime("%B %d"),
term_start=self.term_start.strftime("%B %d"), term_start=self.term_start.strftime("%B %d"),
term_end=self.term_end.strftime("%B %d"), term_end=self.term_end.strftime("%B %d"),
@@ -102,7 +101,7 @@ class Election:
message = file.read() message = file.read()
return message.format( return message.format(
clan_council_elect_role_id=self.config.params.clan_council_elect_role_id, clan_council_elect_role_id=settings.role_ids.clan_council_elect,
highcouncil_election_announce=self.highcouncil_election_announce.strftime("%B %d"), highcouncil_election_announce=self.highcouncil_election_announce.strftime("%B %d"),
highcouncil_election_start=self.highcouncil_election_start.strftime("%B %d"), highcouncil_election_start=self.highcouncil_election_start.strftime("%B %d"),
highcouncil_election_end=self.highcouncil_election_end.strftime("%B %d"), highcouncil_election_end=self.highcouncil_election_end.strftime("%B %d"),
@@ -120,7 +119,7 @@ class Election:
message = file.read() message = file.read()
return message.format( return message.format(
clan_council_elect_role_id=self.config.params.clan_council_elect_role_id, clan_council_elect_role_id=settings.role_ids.clan_council_elect,
term_start=self.term_start.strftime("%B %d"), term_start=self.term_start.strftime("%B %d"),
) )
@@ -133,10 +132,10 @@ class Election:
""" """
with Path(f"{TEMPLATES_DIR}/vote_start.txt", encoding="utf8").open() as file: with Path(f"{TEMPLATES_DIR}/vote_start.txt", encoding="utf8").open() as file:
message = file.read() message = file.read()
vote_end: datetime = get_datetime(self.config.election_cycles[self.cycle].voting_end) vote_end: datetime = get_datetime(settings.election_cycles[self.cycle].voting_end)
return message.format( return message.format(
clan_member_id=self.config.params.clan_member_role_id, clan_member_id=settings.role_ids.clan_member,
voting_end=vote_end.strftime("%B %d"), voting_end=vote_end.strftime("%B %d"),
voting_end_day=vote_end.strftime("%A"), voting_end_day=vote_end.strftime("%A"),
) )
@@ -162,8 +161,8 @@ class Election:
async def election_start_action(self) -> None: async def election_start_action(self) -> None:
"""Performs required actions for when the election cycle starts.""" """Performs required actions for when the election cycle starts."""
# create new channel for election # create new channel for election
parent: nextcord.TextChannel = self.guild.get_channel(self.config.params.voting_parent_id) parent: nextcord.TextChannel = self.guild.get_channel(settings.channel_ids.voting_parent)
quarter_name: str = get_election_name(self.config) quarter_name: str = get_election_name(settings)
thread_msg: nextcord.Message = await parent.send(content=quarter_name) thread_msg: nextcord.Message = await parent.send(content=quarter_name)
@@ -176,7 +175,7 @@ class Election:
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):
nominate_msg: nextcord.Message = await thread.send(message, view=NominateView(self.config, self.guild)) nominate_msg: nextcord.Message = await thread.send(message, view=NominateView(self.guild))
else: else:
await thread.send(message) await thread.send(message)
@@ -201,12 +200,12 @@ class Election:
election_thread: nextcord.Thread = self.guild.get_thread(self.get_election_info().ThreadId) election_thread: nextcord.Thread = self.guild.get_thread(self.get_election_info().ThreadId)
await election_thread.send(message) await election_thread.send(message)
vote_msg = await election_thread.send( vote_msg = await election_thread.send(
build_votes_str(self.config, self.guild), build_votes_str(settings, self.guild),
view=VoteView(self.config, self.guild), view=VoteView(self.guild),
) )
with Session(engine) as session: with Session(engine) as session:
stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == get_election_name(self.config)) stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == get_election_name(settings))
info = session.scalar(stmt) info = session.scalar(stmt)
info.CouncilVoteMsgId = vote_msg.id info.CouncilVoteMsgId = vote_msg.id
session.commit() session.commit()
@@ -218,14 +217,12 @@ class Election:
async def voting_end_action(self) -> None: async def voting_end_action(self) -> None:
"""Performs required actions for when voting time has ended.""" """Performs required actions for when voting time has ended."""
message = self.build_highcouncil_election_announce() message = self.build_highcouncil_election_announce()
council_elect_role: nextcord.Role = self.guild.get_role(self.config.params.clan_council_elect_role_id) council_elect_role: nextcord.Role = self.guild.get_role(settings.role_ids.clan_council_elect)
election_thread: nextcord.Thread = self.guild.get_thread(self.get_election_info().ThreadId) election_thread: nextcord.Thread = self.guild.get_thread(self.get_election_info().ThreadId)
nominate_msg: nextcord.Message = await election_thread.send( nominate_msg: nextcord.Message = await election_thread.send(message, view=NominateHighCouncilView(self.guild))
message, view=NominateHighCouncilView(self.config, self.guild)
)
election_name = get_election_name(self.config) election_name = get_election_name(settings)
with Session(engine) as session: with Session(engine) as session:
# remove vote button from vote message # remove vote button from vote message
@@ -270,12 +267,12 @@ class Election:
election_thread: nextcord.Thread = self.guild.get_thread(self.get_election_info().ThreadId) election_thread: nextcord.Thread = self.guild.get_thread(self.get_election_info().ThreadId)
await election_thread.send(message) await election_thread.send(message)
vote_msg = await election_thread.send( vote_msg = await election_thread.send(
build_highcouncil_votes_str(self.config, self.guild), build_highcouncil_votes_str(settings, self.guild),
view=VoteHighcouncilView(self.config, self.guild), view=VoteHighcouncilView(self.guild),
) )
with Session(engine) as session: with Session(engine) as session:
stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == get_election_name(self.config)) stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == get_election_name(settings))
info = session.scalar(stmt) info = session.scalar(stmt)
info.HighCouncilVoteMsgId = vote_msg.id info.HighCouncilVoteMsgId = vote_msg.id
session.commit() session.commit()
@@ -288,10 +285,10 @@ class Election:
"""Marks the election as over.""" """Marks the election as over."""
self.election_over = True self.election_over = True
elect_role: nextcord.Role = self.guild.get_role(self.config.params.clan_council_elect_role_id) elect_role: nextcord.Role = self.guild.get_role(settings.role_ids.clan_council_elect)
council_role: nextcord.Role = self.guild.get_role(self.config.params.clan_council_role_id) council_role: nextcord.Role = self.guild.get_role(settings.role_ids.clan_council)
election_name = get_election_name(self.config) election_name = get_election_name(settings)
with Session(engine) as session: with Session(engine) as session:
# role management # role management
@@ -356,7 +353,7 @@ class Election:
# clear view from vote message # clear view from vote message
stmt = select(ElectionInformation.HighCouncilVoteMsgId).where( stmt = select(ElectionInformation.HighCouncilVoteMsgId).where(
ElectionInformation.ElectionName == get_election_name(self.config) ElectionInformation.ElectionName == get_election_name(settings)
) )
vote_msg_id = session.scalar(stmt) vote_msg_id = session.scalar(stmt)
vote_msg: nextcord.Message = await election_thread.fetch_message(vote_msg_id) vote_msg: nextcord.Message = await election_thread.fetch_message(vote_msg_id)
@@ -369,7 +366,7 @@ class Election:
await election_thread.send("This election cycle is now over. Thank you everyone for participating!") await election_thread.send("This election cycle is now over. Thank you everyone for participating!")
await election_thread.edit(archived=True, locked=True) await election_thread.edit(archived=True, locked=True)
await edit_clan_council_info(self.guild, self.config) await edit_clan_council_info(self.guild, settings)
def get_election_info(self) -> ElectionInformation: def get_election_info(self) -> ElectionInformation:
""" """
@@ -379,14 +376,14 @@ class Election:
ElectionInformation: The information from the database about the current election. ElectionInformation: The information from the database about the current election.
""" """
with Session(engine) as session: with Session(engine) as session:
stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == get_election_name(self.config)) stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == get_election_name(settings))
return session.scalar(stmt) return session.scalar(stmt)
async def election_reminder(self) -> None: async def election_reminder(self) -> None:
"""Sends a message to general chat reminder clan about election.""" """Sends a message to general chat reminder clan about election."""
general: nextcord.TextChannel = self.guild.get_channel(self.config.params.general_channel_id) general: nextcord.TextChannel = self.guild.get_channel(settings.channel_ids.general)
message = f"Reminder to <@&{self.config.params.clan_member_role_id}>! Clan elections are happening! Head over " message = f"Reminder to <@&{settings.role_ids.clan_member}>! Clan elections are happening! Head over "
message += f"to <#{self.get_election_info().ThreadId}> to cast your vote!" message += f"to <#{self.get_election_info().ThreadId}> to cast your vote!"
await general.send(content=message) await general.send(content=message)
+12 -21
View File
@@ -1,7 +1,7 @@
"""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 DakinySettings import DakinySettings from DakinySettings import settings
from database_setup.sql_data_classes import CouncilMembers, engine from database_setup.sql_data_classes import CouncilMembers, engine
from nextcord.interactions import Interaction from nextcord.interactions import Interaction
from sqlalchemy import select from sqlalchemy import select
@@ -23,7 +23,6 @@ class NominateDMModal(nextcord.ui.Modal):
def __init__( def __init__(
self, self,
config: DakinySettings,
usr_id: int, usr_id: int,
usr_nominated: bool, # noqa: FBT001 usr_nominated: bool, # noqa: FBT001
speech_content: str, speech_content: str,
@@ -33,7 +32,6 @@ class NominateDMModal(nextcord.ui.Modal):
timeout=5 * 60, # 5 minutes timeout=5 * 60, # 5 minutes
) )
self.usr_id: int = usr_id self.usr_id: int = usr_id
self.config: DakinySettings = config
self.usr_nominated = usr_nominated self.usr_nominated = usr_nominated
self.speech = nextcord.ui.TextInput( self.speech = nextcord.ui.TextInput(
@@ -58,7 +56,7 @@ class NominateDMModal(nextcord.ui.Modal):
with Session(engine) as session: with Session(engine) as session:
# user already in table # user already in table
election_name = get_election_name(self.config) election_name = get_election_name(settings)
stmt = ( stmt = (
select(CouncilMembers) select(CouncilMembers)
.where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.ElectionName == election_name)
@@ -85,7 +83,6 @@ class NominateCLModal(nextcord.ui.Modal):
def __init__( def __init__(
self, self,
config: DakinySettings,
usr_id: int, usr_id: int,
usr_nominated: bool, # noqa: FBT001 usr_nominated: bool, # noqa: FBT001
speech_content: str, speech_content: str,
@@ -95,7 +92,6 @@ class NominateCLModal(nextcord.ui.Modal):
timeout=5 * 60, # 5 minutes timeout=5 * 60, # 5 minutes
) )
self.usr_id: int = usr_id self.usr_id: int = usr_id
self.config: DakinySettings = config
self.usr_nominated = usr_nominated self.usr_nominated = usr_nominated
self.speech = nextcord.ui.TextInput( self.speech = nextcord.ui.TextInput(
@@ -120,7 +116,7 @@ class NominateCLModal(nextcord.ui.Modal):
with Session(engine) as session: with Session(engine) as session:
# user already in table # user already in table
election_name = get_election_name(self.config) election_name = get_election_name(settings)
stmt = ( stmt = (
select(CouncilMembers) select(CouncilMembers)
.where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.ElectionName == election_name)
@@ -141,9 +137,8 @@ class NominateCLModal(nextcord.ui.Modal):
class NominateHighCouncilView(nextcord.ui.View): class NominateHighCouncilView(nextcord.ui.View):
"""The buttons to press to start your High Council nomination.""" """The buttons to press to start your High Council nomination."""
def __init__(self, config: DakinySettings, guild: nextcord.Guild): def __init__(self, guild: nextcord.Guild):
super().__init__(timeout=None) super().__init__(timeout=None)
self.config: DakinySettings = config
self.guild: nextcord.Guild = guild self.guild: nextcord.Guild = guild
@nextcord.ui.button( @nextcord.ui.button(
@@ -163,14 +158,14 @@ class NominateHighCouncilView(nextcord.ui.View):
interaction (nextcord.Interaction): Interaction of user who pressed the button interaction (nextcord.Interaction): Interaction of user who pressed the button
""" """
# enforce council elect only # enforce council elect only
if self.guild.get_role(self.config.params.clan_council_elect_role_id) not in interaction.user.roles: if 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 <@&{self.config.params.clan_council_elect_role_id}> role to vote.", f"You must have the <@&{settings.role_ids.clan_council_elect}> role to vote.",
ephemeral=True, ephemeral=True,
) )
return return
election_name = get_election_name(self.config) election_name = get_election_name(settings)
usr_nominated = False usr_nominated = False
speech_content = "" speech_content = ""
with Session(engine) as session: with Session(engine) as session:
@@ -187,9 +182,7 @@ class NominateHighCouncilView(nextcord.ui.View):
msg = await interaction.channel.fetch_message(obj.DiscordManagerSpeechId) msg = await interaction.channel.fetch_message(obj.DiscordManagerSpeechId)
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(NominateDMModal(interaction.user.id, usr_nominated, speech_content))
NominateDMModal(self.config, interaction.user.id, usr_nominated, speech_content)
)
@nextcord.ui.button( @nextcord.ui.button(
label="Nominate for Clan Leader", label="Nominate for Clan Leader",
@@ -208,14 +201,14 @@ class NominateHighCouncilView(nextcord.ui.View):
interaction (nextcord.Interaction): Interaction of user who pressed the button interaction (nextcord.Interaction): Interaction of user who pressed the button
""" """
# enforce council elect only # enforce council elect only
if self.guild.get_role(self.config.params.clan_council_elect_role_id) not in interaction.user.roles: if 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 <@&{self.config.params.clan_council_elect_role_id}> role to vote.", f"You must have the <@&{settings.role_ids.clan_council_elect}> role to vote.",
ephemeral=True, ephemeral=True,
) )
return return
election_name = get_election_name(self.config) election_name = get_election_name(settings)
usr_nominated = False usr_nominated = False
speech_content = "" speech_content = ""
with Session(engine) as session: with Session(engine) as session:
@@ -232,6 +225,4 @@ class NominateHighCouncilView(nextcord.ui.View):
msg = await interaction.channel.fetch_message(obj.ClanLeaderSpeechId) msg = await interaction.channel.fetch_message(obj.ClanLeaderSpeechId)
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(NominateCLModal(interaction.user.id, usr_nominated, speech_content))
NominateCLModal(self.config, interaction.user.id, usr_nominated, speech_content)
)
+7 -12
View File
@@ -1,7 +1,7 @@
"""Contains various views to be used in the nomination process.""" """Contains various views to be used in the nomination process."""
import nextcord import nextcord
from DakinySettings import DakinySettings from DakinySettings import settings
from database_setup.sql_data_classes import CouncilMembers, engine from database_setup.sql_data_classes import CouncilMembers, engine
from nextcord.interactions import Interaction from nextcord.interactions import Interaction
from sqlalchemy import select from sqlalchemy import select
@@ -18,7 +18,6 @@ class NominateModal(nextcord.ui.Modal):
def __init__( def __init__(
self, self,
config: DakinySettings,
usr_id: int, usr_id: int,
usr_nominated: bool, # noqa: FBT001 usr_nominated: bool, # noqa: FBT001
speech_content: str, speech_content: str,
@@ -28,7 +27,6 @@ class NominateModal(nextcord.ui.Modal):
timeout=5 * 60, # 5 minutes timeout=5 * 60, # 5 minutes
) )
self.usr_id: int = usr_id self.usr_id: int = usr_id
self.config: DakinySettings = config
self.usr_nominated = usr_nominated self.usr_nominated = usr_nominated
self.speech = nextcord.ui.TextInput( self.speech = nextcord.ui.TextInput(
@@ -50,7 +48,7 @@ class NominateModal(nextcord.ui.Modal):
""" """
speech = decorate_nomination(interaction.user.id, self.speech.value) speech = decorate_nomination(interaction.user.id, self.speech.value)
election_name = get_election_name(self.config) election_name = get_election_name(settings)
with Session(engine) as session: with Session(engine) as session:
if not self.usr_nominated: if not self.usr_nominated:
@@ -79,9 +77,8 @@ class NominateModal(nextcord.ui.Modal):
class NominateView(nextcord.ui.View): 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: DakinySettings, guild: nextcord.Guild): def __init__(self, guild: nextcord.Guild):
super().__init__(timeout=None) super().__init__(timeout=None)
self.config: DakinySettings = config
self.guild: nextcord.Guild = guild self.guild: nextcord.Guild = guild
@nextcord.ui.button( @nextcord.ui.button(
@@ -101,14 +98,14 @@ 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 # enforce members only
if self.guild.get_role(self.config.params.clan_member_role_id) not in interaction.user.roles: if self.guild.get_role(settings.role_ids.clan_member) not in interaction.user.roles:
await interaction.response.send_message( await interaction.response.send_message(
f"You must have the <@&{self.config.params.clan_member_role_id}> role to vote.", f"You must have the <@&{settings.role_ids.clan_member}> role to vote.",
ephemeral=True, ephemeral=True,
) )
return return
election_name = get_election_name(self.config) election_name = get_election_name(settings)
usr_nominated = False usr_nominated = False
speech_content = "" speech_content = ""
with Session(engine) as session: with Session(engine) as session:
@@ -123,6 +120,4 @@ class NominateView(nextcord.ui.View):
msg = await interaction.channel.fetch_message(obj.SpeechMsgId) msg = await interaction.channel.fetch_message(obj.SpeechMsgId)
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(interaction.user.id, usr_nominated, speech_content))
NominateModal(self.config, interaction.user.id, usr_nominated, speech_content)
)
+12 -15
View File
@@ -1,7 +1,7 @@
"""Contains various views to be used while voting for high council. Controls the logic when performing the vote.""" """Contains various views to be used while voting for high council. Controls the logic when performing the vote."""
import nextcord import nextcord
from DakinySettings import DakinySettings from DakinySettings import settings
from database_setup.sql_data_classes import ( from database_setup.sql_data_classes import (
CouncilMembers, CouncilMembers,
ElectionInformation, ElectionInformation,
@@ -18,14 +18,13 @@ from utils import VoteType, build_highcouncil_votes_str, get_election_name
class VoteSelect(nextcord.ui.StringSelect): class VoteSelect(nextcord.ui.StringSelect):
"""Vote view string select.""" """Vote view string select."""
def __init__(self, options: list["SelectOption"], config: DakinySettings, election_type: str) -> None: def __init__(self, options: list["SelectOption"], election_type: str) -> None:
super().__init__( super().__init__(
placeholder="Choose 1", placeholder="Choose 1",
min_values=1, min_values=1,
max_values=1, max_values=1,
options=options, options=options,
) )
self.config = config
self.election_type = election_type self.election_type = election_type
async def callback(self, interaction: Interaction) -> None: async def callback(self, interaction: Interaction) -> None:
@@ -38,7 +37,7 @@ class VoteSelect(nextcord.ui.StringSelect):
Args: Args:
interaction (Interaction): Interaction with this callback. interaction (Interaction): Interaction with this callback.
""" """
election_name: str = get_election_name(self.config) election_name: str = get_election_name(settings)
with Session(engine) as session: with Session(engine) as session:
stmt = select(ElectionInformation.HighCouncilVoteMsgId).where( stmt = select(ElectionInformation.HighCouncilVoteMsgId).where(
ElectionInformation.ElectionName == election_name ElectionInformation.ElectionName == election_name
@@ -71,7 +70,7 @@ class VoteSelect(nextcord.ui.StringSelect):
session.execute(stmt) session.execute(stmt)
session.commit() session.commit()
vote_message_str = build_highcouncil_votes_str(self.config, interaction.guild) vote_message_str = build_highcouncil_votes_str(settings, interaction.guild)
vote_msg: nextcord.Message = await interaction.channel.fetch_message(vote_msg_id) vote_msg: nextcord.Message = await interaction.channel.fetch_message(vote_msg_id)
await vote_msg.edit(content=vote_message_str) await vote_msg.edit(content=vote_message_str)
@@ -83,9 +82,8 @@ class VoteSelect(nextcord.ui.StringSelect):
class VoteHighcouncilView(nextcord.ui.View): class VoteHighcouncilView(nextcord.ui.View):
"""The button to press to start voting for HighCouncil.""" """The button to press to start voting for HighCouncil."""
def __init__(self, config: DakinySettings, guild: nextcord.Guild): def __init__(self, guild: nextcord.Guild):
super().__init__(timeout=None) super().__init__(timeout=None)
self.config = config
self.guild = guild self.guild = guild
@nextcord.ui.button( @nextcord.ui.button(
@@ -106,14 +104,14 @@ class VoteHighcouncilView(nextcord.ui.View):
interaction (nextcord.Interaction): Interaction of user who pressed the button interaction (nextcord.Interaction): Interaction of user who pressed the button
""" """
# enforce council elect only # enforce council elect only
if self.guild.get_role(self.config.params.clan_council_elect_role_id) not in interaction.user.roles: if 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 <@&{self.config.params.clan_council_elect_role_id}> role to vote.", f"You must have the <@&{settings.role_ids.clan_council_elect}> role to vote.",
ephemeral=True, ephemeral=True,
) )
return return
election_name: str = get_election_name(self.config) election_name: str = get_election_name(settings)
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
@@ -152,7 +150,7 @@ class VoteHighcouncilView(nextcord.ui.View):
) )
view = nextcord.ui.View() view = nextcord.ui.View()
view.add_item(VoteSelect(options=options, config=self.config, election_type=VoteType.CLANLEADER)) view.add_item(VoteSelect(options=options, election_type=VoteType.CLANLEADER))
await interaction.response.send_message( await interaction.response.send_message(
content="Please vote for a Clan Leader!", content="Please vote for a Clan Leader!",
@@ -178,14 +176,14 @@ class VoteHighcouncilView(nextcord.ui.View):
interaction (nextcord.Interaction): Interaction of user who pressed the button interaction (nextcord.Interaction): Interaction of user who pressed the button
""" """
# enforce council elect only # enforce council elect only
if self.guild.get_role(self.config.params.clan_council_elect_role_id) not in interaction.user.roles: if 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 <@&{self.config.params.clan_council_elect_role_id}> role to vote.", f"You must have the <@&{settings.role_ids.clan_council_elect}> role to vote.",
ephemeral=True, ephemeral=True,
) )
return return
election_name: str = get_election_name(self.config) election_name: str = get_election_name(settings)
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
@@ -227,7 +225,6 @@ class VoteHighcouncilView(nextcord.ui.View):
view.add_item( view.add_item(
VoteSelect( VoteSelect(
options=options, options=options,
config=self.config,
election_type=VoteType.DISCORDMANAGER, election_type=VoteType.DISCORDMANAGER,
) )
) )
+9 -11
View File
@@ -1,7 +1,7 @@
"""Contains various views to be used while voting. Controls the logic when performing the vote.""" """Contains various views to be used while voting. Controls the logic when performing the vote."""
import nextcord import nextcord
from DakinySettings import DakinySettings from DakinySettings import settings
from database_setup.sql_data_classes import ( from database_setup.sql_data_classes import (
CouncilMembers, CouncilMembers,
ElectionInformation, ElectionInformation,
@@ -18,14 +18,13 @@ from utils import VoteType, build_votes_str, get_election_name
class VoteSelect(nextcord.ui.StringSelect): class VoteSelect(nextcord.ui.StringSelect):
"""Vote view string select.""" """Vote view string select."""
def __init__(self, options: list["SelectOption"], config: DakinySettings) -> None: def __init__(self, options: list["SelectOption"]) -> None:
super().__init__( super().__init__(
placeholder="Choose 1", placeholder="Choose 1",
min_values=1, min_values=1,
max_values=1, max_values=1,
options=options, options=options,
) )
self.config = config
async def callback(self, interaction: Interaction) -> None: async def callback(self, interaction: Interaction) -> None:
""" """
@@ -37,7 +36,7 @@ class VoteSelect(nextcord.ui.StringSelect):
Args: Args:
interaction (Interaction): Interaction with this callback. interaction (Interaction): Interaction with this callback.
""" """
election_name = get_election_name(self.config) election_name = get_election_name(settings)
with Session(engine) as session: with Session(engine) as session:
stmt = select(ElectionInformation.CouncilVoteMsgId).where(ElectionInformation.ElectionName == election_name) stmt = select(ElectionInformation.CouncilVoteMsgId).where(ElectionInformation.ElectionName == election_name)
vote_msg_id = session.scalar(stmt) vote_msg_id = session.scalar(stmt)
@@ -67,7 +66,7 @@ class VoteSelect(nextcord.ui.StringSelect):
session.execute(stmt) session.execute(stmt)
session.commit() session.commit()
vote_message_str = build_votes_str(self.config, interaction.guild) vote_message_str = build_votes_str(settings, interaction.guild)
vote_msg: nextcord.Message = await interaction.channel.fetch_message(vote_msg_id) vote_msg: nextcord.Message = await interaction.channel.fetch_message(vote_msg_id)
await vote_msg.edit(content=vote_message_str) await vote_msg.edit(content=vote_message_str)
@@ -79,9 +78,8 @@ class VoteSelect(nextcord.ui.StringSelect):
class VoteView(nextcord.ui.View): class VoteView(nextcord.ui.View):
"""The button to press to start your nomination.""" """The button to press to start your nomination."""
def __init__(self, config: DakinySettings, guild: nextcord.Guild): def __init__(self, guild: nextcord.Guild):
super().__init__(timeout=None) super().__init__(timeout=None)
self.config = config
self.guild = guild self.guild = guild
@nextcord.ui.button(label="Vote", style=nextcord.ButtonStyle.primary, custom_id="vote") @nextcord.ui.button(label="Vote", style=nextcord.ButtonStyle.primary, custom_id="vote")
@@ -97,14 +95,14 @@ class VoteView(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 # enforce members only
if self.guild.get_role(self.config.params.clan_member_role_id) not in interaction.user.roles: if self.guild.get_role(settings.role_ids.clan_member) not in interaction.user.roles:
await interaction.response.send_message( await interaction.response.send_message(
f"You must have the <@&{self.config.params.clan_member_role_id}> role to vote.", f"You must have the <@&{settings.role_ids.clan_member}> role to vote.",
ephemeral=True, ephemeral=True,
) )
return return
election_name: str = get_election_name(self.config) election_name: str = get_election_name(settings)
with Session(engine) as session: with Session(engine) as session:
stmt = ( stmt = (
@@ -139,7 +137,7 @@ class VoteView(nextcord.ui.View):
) )
view = nextcord.ui.View() view = nextcord.ui.View()
view.add_item(VoteSelect(options=options, config=self.config)) view.add_item(VoteSelect(options))
await interaction.response.send_message( await interaction.response.send_message(
content="Please vote for a clan member!", content="Please vote for a clan member!",
-7
View File
@@ -47,7 +47,6 @@ class TestCog(commands.Cog, name="Test commands."):
config: DakinySettings = self.bot.config config: DakinySettings = self.bot.config
cycle = Election( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
) )
await cycle.election_start_action() await cycle.election_start_action()
@@ -65,7 +64,6 @@ class TestCog(commands.Cog, name="Test commands."):
config: DakinySettings = self.bot.config config: DakinySettings = self.bot.config
cycle = Election( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
) )
await cycle.voting_start_action() await cycle.voting_start_action()
@@ -83,7 +81,6 @@ class TestCog(commands.Cog, name="Test commands."):
config: DakinySettings = self.bot.config config: DakinySettings = self.bot.config
cycle = Election( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
) )
await cycle.voting_end_action() await cycle.voting_end_action()
@@ -101,7 +98,6 @@ class TestCog(commands.Cog, name="Test commands."):
config: DakinySettings = self.bot.config config: DakinySettings = self.bot.config
cycle = Election( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
) )
await cycle.highcouncil_election_start_action() await cycle.highcouncil_election_start_action()
@@ -119,7 +115,6 @@ class TestCog(commands.Cog, name="Test commands."):
config: DakinySettings = self.bot.config config: DakinySettings = self.bot.config
cycle = Election( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
) )
await cycle.election_reminder() await cycle.election_reminder()
@@ -137,7 +132,6 @@ class TestCog(commands.Cog, name="Test commands."):
config: DakinySettings = self.bot.config config: DakinySettings = self.bot.config
cycle = Election( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
) )
await cycle.check_date() await cycle.check_date()
@@ -155,7 +149,6 @@ class TestCog(commands.Cog, name="Test commands."):
config: DakinySettings = self.bot.config config: DakinySettings = self.bot.config
cycle = Election( cycle = Election(
get_current_election_cycle(config), get_current_election_cycle(config),
config,
self.bot.get_guild(config.params.guild_id), self.bot.get_guild(config.params.guild_id),
) )
await cycle.term_start_action() await cycle.term_start_action()
+3 -5
View File
@@ -10,6 +10,7 @@ from sqlalchemy import select
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
from classes.election import Election from classes.election import Election
from DakinySettings import settings
from database_setup.sql_data_classes import UserBirthday, engine from database_setup.sql_data_classes import UserBirthday, engine
from main import birthday_list from main import birthday_list
from utils import get_current_election_cycle from utils import get_current_election_cycle
@@ -17,8 +18,6 @@ from utils import get_current_election_cycle
if TYPE_CHECKING: if TYPE_CHECKING:
import nextcord import nextcord
from DakinySettings import DakinySettings
KANDY = 239547993106284544 KANDY = 239547993106284544
@@ -76,14 +75,13 @@ class DailyLoop(commands.Cog, name="Daily Loop"):
Returns: Returns:
Election: Current election cycle, None if there is no election happening. Election: Current election cycle, None if there is no election happening.
""" """
config: DakinySettings = self.bot.config cycle = get_current_election_cycle(settings)
cycle = get_current_election_cycle(config)
# prevent exception from using cycle as an Int in an array # prevent exception from using cycle as an Int in an array
if cycle is None: if cycle is None:
return None return None
return Election(cycle, config, self.bot.get_guild(config.params.guild_id)) return Election(cycle, self.bot.get_guild(settings.params.guild_id))
def update_birthdays(self) -> None: def update_birthdays(self) -> None:
"""Goes through database to collect all birthdays that are today. Adds user's id to birthday list.""" """Goes through database to collect all birthdays that are today. Adds user's id to birthday list."""
+5 -5
View File
@@ -10,7 +10,7 @@ from classes.nominate_highcouncil_views import NominateHighCouncilView
from classes.nominate_views import NominateView from classes.nominate_views import NominateView
from classes.voting_highcouncil_views import VoteHighcouncilView from classes.voting_highcouncil_views import VoteHighcouncilView
from classes.voting_views import VoteView from classes.voting_views import VoteView
from DakinySettings import DakinySettings from DakinySettings import settings
from database_setup import create_database from database_setup import create_database
birthday_list: list[int] = [] birthday_list: list[int] = []
@@ -50,10 +50,10 @@ def main() -> None:
"""When Discord is connected.""" """When Discord is connected."""
assert bot.user is not None # noqa: S101 documentation does this assert bot.user is not None # noqa: S101 documentation does this
create_database # noqa: B018 create_database # noqa: B018
bot.add_view(NominateView(cfg, bot.get_guild(cfg.params.guild_id))) bot.add_view(NominateView(bot.get_guild(settings.params.guild_id)))
bot.add_view(NominateHighCouncilView(cfg, bot.get_guild(cfg.params.guild_id))) bot.add_view(NominateHighCouncilView(bot.get_guild(settings.params.guild_id)))
bot.add_view(VoteView(cfg, bot.get_guild(cfg.params.guild_id))) bot.add_view(VoteView(bot.get_guild(settings.params.guild_id)))
bot.add_view(VoteHighcouncilView(cfg, bot.get_guild(cfg.params.guild_id))) bot.add_view(VoteHighcouncilView(bot.get_guild(settings.params.guild_id)))
print(f"{bot.user.name} has connected to Discord!") # noqa: T201 TODO make logging to print my stuff to console print(f"{bot.user.name} has connected to Discord!") # noqa: T201 TODO make logging to print my stuff to console
# Run Discord bot # Run Discord bot