From 19e654aed046e344671039b7b2c7f1896ceaf690 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Tue, 3 Sep 2024 10:59:28 -0400 Subject: [PATCH 1/7] Update ruff version --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 0c9a912..53f77b3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,7 +21,7 @@ setuptools = "^70.0.0" [tool.poetry.group.dev.dependencies] -ruff = "^0.4.5" +ruff = "^0.6.3" [build-system] requires = ["poetry-core"] From 7b521893d3e11dcd85778dedc301aade2fbddb2b Mon Sep 17 00:00:00 2001 From: imcompiling Date: Tue, 3 Sep 2024 10:59:39 -0400 Subject: [PATCH 2/7] Rename ParticipationPoints to EventPoints. --- cogs/commands/view.py | 10 ++++------ database_setup/database.sql | 2 +- database_setup/sql_data_classes.py | 2 +- utils.py | 16 ++++------------ 4 files changed, 10 insertions(+), 20 deletions(-) diff --git a/cogs/commands/view.py b/cogs/commands/view.py index 7794fc4..25f0892 100644 --- a/cogs/commands/view.py +++ b/cogs/commands/view.py @@ -7,7 +7,7 @@ from sqlalchemy import select from sqlalchemy.orm import Session from database_setup.sql_data_classes import ( - ParticipationPoints, + EventPoints, Presences, Votes, engine, @@ -49,14 +49,12 @@ class ViewCog(commands.Cog, name="View Commands"): with Session(engine) as session: if quarter_name: ppdf = pd.read_sql_query( - sql=session.query(ParticipationPoints) - .where(ParticipationPoints.Cycle.startswith(quarter_name)) - .statement, + sql=session.query(EventPoints).where(EventPoints.Cycle.startswith(quarter_name)).statement, con=engine, ) else: ppdf = pd.read_sql_query( - sql=session.query(ParticipationPoints).statement, + sql=session.query(EventPoints).statement, con=engine, ) @@ -203,7 +201,7 @@ class ViewCog(commands.Cog, name="View Commands"): quarter_name (str): String to run autocomplete on. """ with Session(engine) as session: - stmt = select(ParticipationPoints.Cycle).distinct() + stmt = select(EventPoints.Cycle).distinct() quarters = session.scalars(stmt) # if empty compare string diff --git a/database_setup/database.sql b/database_setup/database.sql index 652d7ca..17d7368 100644 --- a/database_setup/database.sql +++ b/database_setup/database.sql @@ -41,7 +41,7 @@ CREATE TABLE Presences ( Presence varchar(100) not null ); -CREATE TABLE ParticipationPoints ( +CREATE TABLE EventPoints ( Cycle varchar(30) not null, UserId varchar(32) not null, DiscordMember boolean not null, diff --git a/database_setup/sql_data_classes.py b/database_setup/sql_data_classes.py index 6bdd89c..abbdd8c 100644 --- a/database_setup/sql_data_classes.py +++ b/database_setup/sql_data_classes.py @@ -14,7 +14,7 @@ CouncilMembers = Base.classes.CouncilMembers ElectionInformation = Base.classes.ElectionInformation EventPointsLimit = Base.classes.EventPointsLimit Messages = Base.classes.Messages -ParticipationPoints = Base.classes.ParticipationPoints +EventPoints = Base.classes.EventPoints Presences = Base.classes.Presences UserBirthday = Base.classes.UserBirthday Votes = Base.classes.Votes diff --git a/utils.py b/utils.py index ce4ddd9..d093b6a 100644 --- a/utils.py +++ b/utils.py @@ -15,9 +15,9 @@ from sqlalchemy.orm import Session from config import DakinyConfig from database_setup.sql_data_classes import ( CouncilMembers, + EventPoints, EventPointsLimit, Messages, - ParticipationPoints, Votes, engine, ) @@ -318,16 +318,12 @@ def record_event_points(config: DakinyConfig, users: Sequence[str], add_points: quarter_name = get_quarter_name(config) with Session(engine) as session: for user in users: - stmt = ( - select(ParticipationPoints) - .where(ParticipationPoints.UserId == user) - .where(ParticipationPoints.Cycle == quarter_name) - ) + stmt = select(EventPoints).where(EventPoints.UserId == user).where(EventPoints.Cycle == quarter_name) points = session.scalar(stmt) if points is None: add_points = get_allowed_points(session, config.params.event_points_weekly_limit, user, add_points) - new_points = ParticipationPoints( + new_points = EventPoints( Cycle=quarter_name, UserId=user, DiscordMember=is_member, @@ -371,11 +367,7 @@ def get_allowed_points(session: Session, points_limit: int, user: str, points: i add_points = points session.add(new_limit) else: - add_points = ( - points - if limit.Points + points <= points_limit - else (max(0, points_limit - limit.Points)) - ) + add_points = points if limit.Points + points <= points_limit else (max(0, points_limit - limit.Points)) limit.Points += add_points session.commit() From 8fb59ba394feebb024d40bf95b85b40c9ae49d29 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Tue, 3 Sep 2024 11:00:48 -0400 Subject: [PATCH 3/7] Remove unused noqa. --- cogs/commands/record.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/commands/record.py b/cogs/commands/record.py index 5e9f1c9..1aad3b7 100644 --- a/cogs/commands/record.py +++ b/cogs/commands/record.py @@ -65,7 +65,7 @@ class RecordCog(commands.Cog, name="Record Commands"): ) @record.subcommand(description="Record a miscellaneous point for a clan member.") - async def point( # noqa: PLR0913 + async def point( self, interaction: nextcord.Interaction, activity: str = nextcord.SlashOption( From 306e0d687a8d3f875b017f4defbe854efd3c6e08 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Tue, 3 Sep 2024 11:10:10 -0400 Subject: [PATCH 4/7] Update ElectionCycle to ElectionName in database creation script. --- database_setup/database.sql | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/database_setup/database.sql b/database_setup/database.sql index 17d7368..bffac4d 100644 --- a/database_setup/database.sql +++ b/database_setup/database.sql @@ -1,15 +1,15 @@ CREATE TABLE ElectionInformation( - ElectionCycle varchar(30) not null unique, + ElectionName varchar(30) not null unique, ThreadId bigint unsigned not null, CouncilVoteMsgId bigint unsigned, HighCouncilVoteMsgId bigint unsigned, CouncilNominationMsgId bigint unsigned, HighCouncilNominationMsgId bigint unsigned, - PRIMARY KEY (ElectionCycle) + PRIMARY KEY (ElectionName) ); CREATE TABLE CouncilMembers( - ElectionCycle varchar(30) not null, + ElectionName varchar(30) not null, MemberId bigint unsigned not null, SpeechMsgId bigint unsigned not null, Elected boolean DEFAULT false, @@ -17,17 +17,17 @@ CREATE TABLE CouncilMembers( ClanLeader boolean DEFAULT false, DiscordManagerSpeechId bigint unsigned, ClanLeaderSpeechId bigint unsigned, - FOREIGN KEY (ElectionCycle) REFERENCES ElectionInformation(ElectionCycle), - PRIMARY KEY (ElectionCycle, MemberId) + FOREIGN KEY (ElectionName) REFERENCES ElectionInformation(ElectionName), + PRIMARY KEY (ElectionName, MemberId) ); CREATE TABLE Votes( - ElectionCycle varchar(30) not null, + ElectionName varchar(30) not null, VoterId bigint unsigned not null, VotedForId bigint unsigned, Type varchar(14) not null DEFAULT 'council' check("Type" in ('council', 'discordmanager', 'clanleader')), - FOREIGN KEY (ElectionCycle) REFERENCES ElectionInformation(ElectionCycle), - PRIMARY KEY (ElectionCycle, VoterId, Type) + FOREIGN KEY (ElectionName) REFERENCES ElectionInformation(ElectionName), + PRIMARY KEY (ElectionName, VoterId, Type) ); CREATE TABLE UserBirthday ( From eb5102578d29031afcd73358a0bd3c8b25556072 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Tue, 3 Sep 2024 11:24:28 -0400 Subject: [PATCH 5/7] Change ElectionCycle class to Election. Change ElectionCycle column in database to ElectionName. Update some variables for clarification. --- classes/{election_cycle.py => election.py} | 38 +++++++++------------- classes/nominate_highcouncil_views.py | 16 ++++----- classes/nominate_views.py | 12 +++---- classes/voting_highcouncil_views.py | 14 ++++---- classes/voting_views.py | 14 ++++---- cogs/commands/test.py | 16 ++++----- cogs/commands/view.py | 4 +-- cogs/loops/daily_loop.py | 12 +++---- config.py | 4 +-- utils.py | 20 ++++++------ 10 files changed, 71 insertions(+), 79 deletions(-) rename classes/{election_cycle.py => election.py} (92%) diff --git a/classes/election_cycle.py b/classes/election.py similarity index 92% rename from classes/election_cycle.py rename to classes/election.py index 6b3dc89..6ff2011 100644 --- a/classes/election_cycle.py +++ b/classes/election.py @@ -31,9 +31,9 @@ from utils import ( REQUIRED_VOTES_TO_BE_ELECTED = 3 -class ElectionCycle: +class Election: """ - ElectionCycle class. + Election class. This class represents all the dates required for an election cycle along with all the functions needed to operate the cycle. @@ -183,7 +183,7 @@ class ElectionCycle: # create new entry into database. with Session(engine) as session: new_cycle = ElectionInformation( - ElectionCycle=quarter_name, + ElectionName=quarter_name, ThreadId=thread.id, CouncilNominationMsgId=nominate_msg.id, ) @@ -206,9 +206,7 @@ class ElectionCycle: ) with Session(engine) as session: - stmt = select(ElectionInformation).where( - ElectionInformation.ElectionCycle == get_election_name(self.config) - ) + stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == get_election_name(self.config)) info = session.scalar(stmt) info.CouncilVoteMsgId = vote_msg.id session.commit() @@ -231,7 +229,7 @@ class ElectionCycle: with Session(engine) as session: # remove vote button from vote message - stmt = select(ElectionInformation).where(ElectionInformation.ElectionCycle == election_name) + stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == election_name) info = session.scalar(stmt) vote_msg: nextcord.Message = await election_thread.fetch_message(info.CouncilVoteMsgId) await vote_msg.edit(view=None) @@ -243,7 +241,7 @@ class ElectionCycle: # add the highcouncil role to everyone with 3 or more votes. set elected to true in database stmt = ( select(Votes.VotedForId) - .where(Votes.ElectionCycle == election_name) + .where(Votes.ElectionName == election_name) .where(Votes.Type == VoteType.COUNCIL) ) vote_count = collections.Counter(session.scalars(stmt)) @@ -252,7 +250,7 @@ class ElectionCycle: if votes >= REQUIRED_VOTES_TO_BE_ELECTED: stmt = ( select(CouncilMembers) - .where(CouncilMembers.ElectionCycle == election_name) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.MemberId == user_id) ) council_member = session.scalar(stmt) @@ -277,9 +275,7 @@ class ElectionCycle: ) with Session(engine) as session: - stmt = select(ElectionInformation).where( - ElectionInformation.ElectionCycle == get_election_name(self.config) - ) + stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == get_election_name(self.config)) info = session.scalar(stmt) info.HighCouncilVoteMsgId = vote_msg.id session.commit() @@ -295,13 +291,13 @@ class ElectionCycle: 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) - election_cycle = get_election_name(self.config) + election_name = get_election_name(self.config) with Session(engine) as session: # role management stmt = ( select(CouncilMembers.MemberId) - .where(CouncilMembers.ElectionCycle == election_cycle) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.Elected == true()) ) @@ -320,13 +316,13 @@ class ElectionCycle: # ties are undefined behavior. stmt = ( select(Votes.VotedForId) - .where(Votes.ElectionCycle == election_cycle) + .where(Votes.ElectionName == election_name) .where(Votes.Type == VoteType.DISCORDMANAGER) ) dm_votes = collections.Counter(session.scalars(stmt)) stmt = ( select(Votes.VotedForId) - .where(Votes.ElectionCycle == election_cycle) + .where(Votes.ElectionName == election_name) .where(Votes.Type == VoteType.CLANLEADER) ) cl_votes = collections.Counter(session.scalars(stmt)) @@ -335,7 +331,7 @@ class ElectionCycle: dm_winner = max(dm_votes, key=dm_votes.get) stmt = ( update(CouncilMembers) - .where(CouncilMembers.ElectionCycle == election_cycle) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.MemberId == dm_winner) .values(DiscordManager=True) ) @@ -346,7 +342,7 @@ class ElectionCycle: cl_winner = max(cl_votes, key=cl_votes.get) stmt = ( update(CouncilMembers) - .where(CouncilMembers.ElectionCycle == election_cycle) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.MemberId == cl_winner) .values(ClanLeader=True) ) @@ -360,7 +356,7 @@ class ElectionCycle: # clear view from vote message stmt = select(ElectionInformation.HighCouncilVoteMsgId).where( - ElectionInformation.ElectionCycle == get_election_name(self.config) + ElectionInformation.ElectionName == get_election_name(self.config) ) vote_msg_id = session.scalar(stmt) vote_msg: nextcord.Message = await election_thread.fetch_message(vote_msg_id) @@ -383,9 +379,7 @@ class ElectionCycle: ElectionInformation: The information from the database about the current election. """ with Session(engine) as session: - stmt = select(ElectionInformation).where( - ElectionInformation.ElectionCycle == get_election_name(self.config) - ) + stmt = select(ElectionInformation).where(ElectionInformation.ElectionName == get_election_name(self.config)) return session.scalar(stmt) async def election_reminder(self) -> None: diff --git a/classes/nominate_highcouncil_views.py b/classes/nominate_highcouncil_views.py index 9fdbcc8..620bde9 100644 --- a/classes/nominate_highcouncil_views.py +++ b/classes/nominate_highcouncil_views.py @@ -58,10 +58,10 @@ class NominateDMModal(nextcord.ui.Modal): with Session(engine) as session: # user already in table - cycle = get_election_name(self.config) + election_name = get_election_name(self.config) stmt = ( select(CouncilMembers) - .where(CouncilMembers.ElectionCycle == cycle) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.MemberId == self.usr_id) ) council_elect = session.scalar(stmt) @@ -120,10 +120,10 @@ class NominateCLModal(nextcord.ui.Modal): with Session(engine) as session: # user already in table - cycle = get_election_name(self.config) + election_name = get_election_name(self.config) stmt = ( select(CouncilMembers) - .where(CouncilMembers.ElectionCycle == cycle) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.MemberId == self.usr_id) ) council_elect = session.scalar(stmt) @@ -170,13 +170,13 @@ class NominateHighCouncilView(nextcord.ui.View): ) return - cycle = get_election_name(self.config) + election_name = get_election_name(self.config) usr_nominated = False speech_content = "" with Session(engine) as session: stmt = ( select(CouncilMembers) - .where(CouncilMembers.ElectionCycle == cycle) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.MemberId == interaction.user.id) ) obj = session.scalar(stmt) @@ -215,13 +215,13 @@ class NominateHighCouncilView(nextcord.ui.View): ) return - cycle = get_election_name(self.config) + election_name = get_election_name(self.config) usr_nominated = False speech_content = "" with Session(engine) as session: stmt = ( select(CouncilMembers) - .where(CouncilMembers.ElectionCycle == cycle) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.MemberId == interaction.user.id) ) obj = session.scalar(stmt) diff --git a/classes/nominate_views.py b/classes/nominate_views.py index b6d8578..f704420 100644 --- a/classes/nominate_views.py +++ b/classes/nominate_views.py @@ -50,13 +50,14 @@ class NominateModal(nextcord.ui.Modal): """ speech = decorate_nomination(interaction.user.id, self.speech.value) + election_name = get_election_name(self.config) with Session(engine) as session: if not self.usr_nominated: msg = await interaction.channel.send(speech) council_member = CouncilMembers( - ElectionCycle=get_election_name(self.config), + ElectionName=election_name, MemberId=interaction.user.id, SpeechMsgId=msg.id, ) @@ -64,10 +65,9 @@ class NominateModal(nextcord.ui.Modal): session.commit() else: # user already nominated, just need to update the election speech - cycle = get_election_name(self.config) stmt = ( select(CouncilMembers) - .where(CouncilMembers.ElectionCycle == cycle) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.MemberId == self.usr_id) ) obj = session.scalar(stmt) @@ -91,7 +91,7 @@ class NominateView(nextcord.ui.View): ) async def nominate(self, button: nextcord.ui.Button, interaction: nextcord.Interaction) -> None: # noqa: ARG002 """ - Button to nominate onself for Clan Council. + 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. @@ -108,13 +108,13 @@ class NominateView(nextcord.ui.View): ) return - cycle = get_election_name(self.config) + election_name = get_election_name(self.config) usr_nominated = False speech_content = "" with Session(engine) as session: stmt = ( select(CouncilMembers) - .where(CouncilMembers.ElectionCycle == cycle) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.MemberId == interaction.user.id) ) obj = session.scalar(stmt) diff --git a/classes/voting_highcouncil_views.py b/classes/voting_highcouncil_views.py index 98d3b1e..e3497b2 100644 --- a/classes/voting_highcouncil_views.py +++ b/classes/voting_highcouncil_views.py @@ -41,14 +41,14 @@ class VoteSelect(nextcord.ui.StringSelect): election_name: str = get_election_name(self.config) with Session(engine) as session: stmt = select(ElectionInformation.HighCouncilVoteMsgId).where( - ElectionInformation.ElectionCycle == election_name + ElectionInformation.ElectionName == election_name ) vote_msg_id = session.scalar(stmt) voted_for = None if self.values[0] == "Abstain" else int(self.values[0]) new_vote = Votes( - ElectionCycle=election_name, + ElectionName=election_name, VoterId=interaction.user.id, VotedForId=voted_for, Type=self.election_type, @@ -63,7 +63,7 @@ class VoteSelect(nextcord.ui.StringSelect): session.rollback() stmt = ( update(Votes) - .where(Votes.ElectionCycle == election_name) + .where(Votes.ElectionName == election_name) .where(Votes.VoterId == interaction.user.id) .where(Votes.Type == self.election_type) .values(VotedForId=voted_for) @@ -118,7 +118,7 @@ class VoteHighcouncilView(nextcord.ui.View): with Session(engine) as session: stmt = ( select(Votes.VotedForId) - .where(Votes.ElectionCycle == election_name) + .where(Votes.ElectionName == election_name) .where(Votes.VoterId == interaction.user.id) .where(Votes.Type == VoteType.CLANLEADER) ) @@ -126,7 +126,7 @@ class VoteHighcouncilView(nextcord.ui.View): stmt = ( select(CouncilMembers.MemberId) - .where(CouncilMembers.ElectionCycle == election_name) + .where(CouncilMembers.ElectionName == election_name) .filter(CouncilMembers.ClanLeaderSpeechId.is_not(None)) ) @@ -190,7 +190,7 @@ class VoteHighcouncilView(nextcord.ui.View): with Session(engine) as session: stmt = ( select(Votes.VotedForId) - .where(Votes.ElectionCycle == election_name) + .where(Votes.ElectionName == election_name) .where(Votes.VoterId == interaction.user.id) .where(Votes.Type == VoteType.DISCORDMANAGER) ) @@ -198,7 +198,7 @@ class VoteHighcouncilView(nextcord.ui.View): stmt = ( select(CouncilMembers.MemberId) - .where(CouncilMembers.ElectionCycle == election_name) + .where(CouncilMembers.ElectionName == election_name) .filter(CouncilMembers.DiscordManagerSpeechId.is_not(None)) ) diff --git a/classes/voting_views.py b/classes/voting_views.py index 09dd647..4a51238 100644 --- a/classes/voting_views.py +++ b/classes/voting_views.py @@ -39,15 +39,13 @@ class VoteSelect(nextcord.ui.StringSelect): """ election_name = get_election_name(self.config) with Session(engine) as session: - stmt = select(ElectionInformation.CouncilVoteMsgId).where( - ElectionInformation.ElectionCycle == election_name - ) + stmt = select(ElectionInformation.CouncilVoteMsgId).where(ElectionInformation.ElectionName == election_name) vote_msg_id = session.scalar(stmt) voted_for = None if self.values[0] == "Abstain" else int(self.values[0]) new_vote = Votes( - ElectionCycle=election_name, + ElectionName=election_name, VoterId=interaction.user.id, VotedForId=voted_for, Type=VoteType.COUNCIL, @@ -61,7 +59,7 @@ class VoteSelect(nextcord.ui.StringSelect): session.rollback() stmt = ( update(Votes) - .where(Votes.ElectionCycle == election_name) + .where(Votes.ElectionName == election_name) .where(Votes.VoterId == interaction.user.id) .where(Votes.Type == VoteType.COUNCIL) .values(VotedForId=voted_for) @@ -106,18 +104,18 @@ class VoteView(nextcord.ui.View): ) return - election: str = get_election_name(self.config) + election_name: str = get_election_name(self.config) with Session(engine) as session: stmt = ( select(Votes.VotedForId) - .where(Votes.ElectionCycle == election) + .where(Votes.ElectionName == election_name) .where(Votes.VoterId == interaction.user.id) .where(Votes.Type == VoteType.COUNCIL) ) voted_for = session.scalar(stmt) - stmt = select(CouncilMembers.MemberId).where(CouncilMembers.ElectionCycle == election) + stmt = select(CouncilMembers.MemberId).where(CouncilMembers.ElectionName == election_name) options = [ SelectOption( diff --git a/cogs/commands/test.py b/cogs/commands/test.py index c8dd53e..9226496 100644 --- a/cogs/commands/test.py +++ b/cogs/commands/test.py @@ -5,7 +5,7 @@ from typing import TYPE_CHECKING import nextcord from nextcord.ext import application_checks, commands -from classes.election_cycle import ElectionCycle +from classes.election import Election from utils import GUILDS, get_current_election_cycle if TYPE_CHECKING: @@ -45,7 +45,7 @@ class TestCog(commands.Cog, name="Test commands."): interaction (nextcord.Interaction): Interaction with the message. """ config: DakinyConfig = self.bot.config - cycle = ElectionCycle( + cycle = Election( get_current_election_cycle(config), config, self.bot.get_guild(config.params.guild_id), @@ -63,7 +63,7 @@ class TestCog(commands.Cog, name="Test commands."): interaction (nextcord.Interaction): Interaction with the message. """ config: DakinyConfig = self.bot.config - cycle = ElectionCycle( + cycle = Election( get_current_election_cycle(config), config, self.bot.get_guild(config.params.guild_id), @@ -81,7 +81,7 @@ class TestCog(commands.Cog, name="Test commands."): interaction (nextcord.Interaction): Interaction with the message. """ config: DakinyConfig = self.bot.config - cycle = ElectionCycle( + cycle = Election( get_current_election_cycle(config), config, self.bot.get_guild(config.params.guild_id), @@ -99,7 +99,7 @@ class TestCog(commands.Cog, name="Test commands."): interaction (nextcord.Interaction): Interaction with the message. """ config: DakinyConfig = self.bot.config - cycle = ElectionCycle( + cycle = Election( get_current_election_cycle(config), config, self.bot.get_guild(config.params.guild_id), @@ -117,7 +117,7 @@ class TestCog(commands.Cog, name="Test commands."): interaction (nextcord.Interaction): Interaction with the message. """ config: DakinyConfig = self.bot.config - cycle = ElectionCycle( + cycle = Election( get_current_election_cycle(config), config, self.bot.get_guild(config.params.guild_id), @@ -135,7 +135,7 @@ class TestCog(commands.Cog, name="Test commands."): interaction (nextcord.Interaction): Interaction with the message. """ config: DakinyConfig = self.bot.config - cycle = ElectionCycle( + cycle = Election( get_current_election_cycle(config), config, self.bot.get_guild(config.params.guild_id), @@ -153,7 +153,7 @@ class TestCog(commands.Cog, name="Test commands."): interaction (nextcord.Interaction): Interaction with the message. """ config: DakinyConfig = self.bot.config - cycle = ElectionCycle( + cycle = Election( get_current_election_cycle(config), config, self.bot.get_guild(config.params.guild_id), diff --git a/cogs/commands/view.py b/cogs/commands/view.py index 25f0892..96a9ef4 100644 --- a/cogs/commands/view.py +++ b/cogs/commands/view.py @@ -149,7 +149,7 @@ class ViewCog(commands.Cog, name="View Commands"): election_name (str): Is SlashOption. The name of the election to view votes for. election_type(str): Is SlashOption. The type of election to search for. Choice from StrEnum VoteType. """ - stmt = select(Votes).where(Votes.ElectionCycle == election_name).where(Votes.Type == election_type) + stmt = select(Votes).where(Votes.ElectionName == election_name).where(Votes.Type == election_type) with Session(engine) as session: # Check if election exists. @@ -178,7 +178,7 @@ class ViewCog(commands.Cog, name="View Commands"): election_name (str): String to run autocomplete on. """ with Session(engine) as session: - stmt = select(Votes.ElectionCycle).distinct() + stmt = select(Votes.ElectionName).distinct() elections = session.scalars(stmt) # if empty compare string diff --git a/cogs/loops/daily_loop.py b/cogs/loops/daily_loop.py index c250629..61b6c45 100644 --- a/cogs/loops/daily_loop.py +++ b/cogs/loops/daily_loop.py @@ -9,7 +9,7 @@ from pytz import timezone from sqlalchemy import select from sqlalchemy.orm import Session -from classes.election_cycle import ElectionCycle +from classes.election import Election from database_setup.sql_data_classes import UserBirthday, engine from main import birthday_list from utils import get_current_election_cycle @@ -43,7 +43,7 @@ class DailyLoop(commands.Cog, name="Daily Loop"): election is over to delete the current election object. """ if self.current_election is None: - self.current_election = self.get_election_cycle() + self.current_election = self.get_election() if self.current_election is not None: await self.current_election.check_date() @@ -65,16 +65,16 @@ class DailyLoop(commands.Cog, name="Daily Loop"): await self.bot.wait_until_ready() # Bot needs to know guilds to get the current cycle. - self.current_election = self.get_election_cycle() + self.current_election = self.get_election() - def get_election_cycle(self) -> ElectionCycle: + def get_election(self) -> Election: """ Creates an object for the current election cycle if one is active. An election is considered active between the election start and the term start. Returns: - ElectionCycle: Current election cycle, None if there is no election happening. + Election: Current election cycle, None if there is no election happening. """ config: DakinyConfig = self.bot.config cycle = get_current_election_cycle(config) @@ -83,7 +83,7 @@ class DailyLoop(commands.Cog, name="Daily Loop"): if cycle is None: return None - return ElectionCycle(cycle, config, self.bot.get_guild(config.params.guild_id)) + return Election(cycle, config, self.bot.get_guild(config.params.guild_id)) def update_birthdays(self) -> None: """Goes through database to collect all birthdays that are today. Adds user's id to birthday list.""" diff --git a/config.py b/config.py index c7746da..3ba32f1 100644 --- a/config.py +++ b/config.py @@ -27,7 +27,7 @@ class Params: @dataclass -class ElectionCycle: +class Election: """Class defines relevant election dates.""" election_start: str @@ -45,5 +45,5 @@ class ElectionCycle: class DakinyConfig: """Class defines all configs required for Dakiny Bot.""" - election_cycles: list[ElectionCycle] + election_cycles: list[Election] params: Params diff --git a/utils.py b/utils.py index d093b6a..e5a7b73 100644 --- a/utils.py +++ b/utils.py @@ -148,7 +148,7 @@ def build_votes_str(config: DakinyConfig, guild: nextcord.Guild) -> str: election_name = get_election_name(config) with Session(engine) as session: - stmt = select(CouncilMembers).where(CouncilMembers.ElectionCycle == election_name) + stmt = select(CouncilMembers).where(CouncilMembers.ElectionName == election_name) for nominee in sorted( session.scalars(stmt), @@ -156,7 +156,7 @@ def build_votes_str(config: DakinyConfig, guild: nextcord.Guild) -> str: ): votes[nominee.MemberId] = 0 - stmt = select(Votes).where(Votes.ElectionCycle == election_name).where(Votes.Type == VoteType.COUNCIL) + stmt = select(Votes).where(Votes.ElectionName == election_name).where(Votes.Type == VoteType.COUNCIL) for vote in session.scalars(stmt): if vote.VotedForId is not None: @@ -187,7 +187,7 @@ def build_highcouncil_votes_str(config: DakinyConfig, guild: nextcord.Guild) -> with Session(engine) as session: stmt = ( select(CouncilMembers) - .where(CouncilMembers.ElectionCycle == election_name) + .where(CouncilMembers.ElectionName == election_name) .filter(CouncilMembers.ClanLeaderSpeechId.is_not(None)) ) @@ -197,7 +197,7 @@ def build_highcouncil_votes_str(config: DakinyConfig, guild: nextcord.Guild) -> ): cl_votes[nominee.MemberId] = 0 - stmt = select(Votes).where(Votes.ElectionCycle == election_name).where(Votes.Type == VoteType.CLANLEADER) + stmt = select(Votes).where(Votes.ElectionName == election_name).where(Votes.Type == VoteType.CLANLEADER) for vote in session.scalars(stmt): if vote.VotedForId is not None: @@ -205,7 +205,7 @@ def build_highcouncil_votes_str(config: DakinyConfig, guild: nextcord.Guild) -> stmt = ( select(CouncilMembers) - .where(CouncilMembers.ElectionCycle == election_name) + .where(CouncilMembers.ElectionName == election_name) .filter(CouncilMembers.DiscordManagerSpeechId.is_not(None)) ) @@ -215,7 +215,7 @@ def build_highcouncil_votes_str(config: DakinyConfig, guild: nextcord.Guild) -> ): dm_votes[nominee.MemberId] = 0 - stmt = select(Votes).where(Votes.ElectionCycle == election_name).where(Votes.Type == VoteType.DISCORDMANAGER) + stmt = select(Votes).where(Votes.ElectionName == election_name).where(Votes.Type == VoteType.DISCORDMANAGER) for vote in session.scalars(stmt): if vote.VotedForId is not None: @@ -512,23 +512,23 @@ async def edit_clan_council_info(guild: nextcord.Guild, config: DakinyConfig) -> """ clan_info_channel: nextcord.TextChannel = await guild.fetch_channel(config.params.clan_info_channel_id) - election = get_election_name(config) + election_name = get_election_name(config) with Session(engine) as session: stmt = ( select(CouncilMembers.MemberId) - .where(CouncilMembers.ElectionCycle == election) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.DiscordManager == true()) ) discord_manager = session.scalar(stmt) stmt = ( select(CouncilMembers.MemberId) - .where(CouncilMembers.ElectionCycle == election) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.ClanLeader == true()) ) clan_leader = session.scalar(stmt) stmt = ( select(CouncilMembers.MemberId) - .where(CouncilMembers.ElectionCycle == election) + .where(CouncilMembers.ElectionName == election_name) .where(CouncilMembers.Elected == true()) ) From 7e0ff1da18b090a1476ff7dd3b081fe8926db8c2 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Tue, 3 Sep 2024 11:42:20 -0400 Subject: [PATCH 6/7] Rename EventPoints.Cycle to EventPoints.QuarterName. --- .vscode/settings.json | 1 + cogs/commands/view.py | 4 ++-- database_setup/database.sql | 2 +- utils.py | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index f1b7934..a8cedf8 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "cSpell.words": [ "aiofiles", + "autofilled", "blurple", "CLANLEADER", "Dakiny", diff --git a/cogs/commands/view.py b/cogs/commands/view.py index 96a9ef4..accd83a 100644 --- a/cogs/commands/view.py +++ b/cogs/commands/view.py @@ -49,7 +49,7 @@ class ViewCog(commands.Cog, name="View Commands"): with Session(engine) as session: if quarter_name: ppdf = pd.read_sql_query( - sql=session.query(EventPoints).where(EventPoints.Cycle.startswith(quarter_name)).statement, + sql=session.query(EventPoints).where(EventPoints.QuarterName.startswith(quarter_name)).statement, con=engine, ) else: @@ -201,7 +201,7 @@ class ViewCog(commands.Cog, name="View Commands"): quarter_name (str): String to run autocomplete on. """ with Session(engine) as session: - stmt = select(EventPoints.Cycle).distinct() + stmt = select(EventPoints.QuarterName).distinct() quarters = session.scalars(stmt) # if empty compare string diff --git a/database_setup/database.sql b/database_setup/database.sql index bffac4d..8430856 100644 --- a/database_setup/database.sql +++ b/database_setup/database.sql @@ -42,7 +42,7 @@ CREATE TABLE Presences ( ); CREATE TABLE EventPoints ( - Cycle varchar(30) not null, + QuarterName varchar(30) not null, UserId varchar(32) not null, DiscordMember boolean not null, Points int unsigned DEFAULT 0, diff --git a/utils.py b/utils.py index e5a7b73..091fee2 100644 --- a/utils.py +++ b/utils.py @@ -318,13 +318,13 @@ def record_event_points(config: DakinyConfig, users: Sequence[str], add_points: quarter_name = get_quarter_name(config) with Session(engine) as session: for user in users: - stmt = select(EventPoints).where(EventPoints.UserId == user).where(EventPoints.Cycle == quarter_name) + stmt = select(EventPoints).where(EventPoints.UserId == user).where(EventPoints.QuarterName == quarter_name) points = session.scalar(stmt) if points is None: add_points = get_allowed_points(session, config.params.event_points_weekly_limit, user, add_points) new_points = EventPoints( - Cycle=quarter_name, + QuarterName=quarter_name, UserId=user, DiscordMember=is_member, Points=add_points, From a55a90b922dc4bcc6170b2d10e445a334fe41a16 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Tue, 3 Sep 2024 11:52:18 -0400 Subject: [PATCH 7/7] Fixed missed column name change. --- cogs/commands/view.py | 8 ++++---- database_setup/database.sql | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cogs/commands/view.py b/cogs/commands/view.py index accd83a..65ff0c2 100644 --- a/cogs/commands/view.py +++ b/cogs/commands/view.py @@ -62,11 +62,11 @@ class ViewCog(commands.Cog, name="View Commands"): await interaction.followup.send(f'Quarter "{quarter_name}" is invalid.', ephemeral=True) return - cycles = ppdf.groupby("Cycle") + quarters = ppdf.groupby("QuarterName") content = "" - for cycle in cycles.groups: - content += f"**{cycle}**\n" - unsorted_all = cycles.get_group(cycle).groupby("DiscordMember") + for quarter in quarters.groups: + content += f"**{quarter}**\n" + unsorted_all = quarters.get_group(quarter).groupby("DiscordMember") members = unsorted_all.get_group(name=True) # there can be no non-discord members sometimes. diff --git a/database_setup/database.sql b/database_setup/database.sql index 8430856..26e1fb9 100644 --- a/database_setup/database.sql +++ b/database_setup/database.sql @@ -46,7 +46,7 @@ CREATE TABLE EventPoints ( UserId varchar(32) not null, DiscordMember boolean not null, Points int unsigned DEFAULT 0, - PRIMARY KEY (Cycle, UserId) + PRIMARY KEY (QuarterName, UserId) ); CREATE TABLE Messages (