From 2059814394cb71b75133c45075668caf71c3a540 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Thu, 16 May 2024 15:36:50 -0400 Subject: [PATCH 1/6] Broke edit clan message to own function to allow for usage in other places. --- utils.py | 67 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/utils.py b/utils.py index b462915..3efc71b 100644 --- a/utils.py +++ b/utils.py @@ -397,11 +397,10 @@ async def update_clan_messages(guild: nextcord.Guild): Updates all clan messages to contents in MESSAGES_DIR. Does not to any git work. Is expected to be called after git is pulled. - for ever directory, sort the files by number then post - posting involves checking if there is a message for that particular file then editing it or creating a - new one. the message could be deleted, which would cause a new message to be sent and database updated. + for ever directory, sort the files by number then edits/sends the message. Channel fetching/management is handled by another function. + Message editing and sending is handled by another function. Args: guild (nextcord.Guild): Guild object that all channels and messages belong to. @@ -426,27 +425,9 @@ async def update_clan_messages(guild: nextcord.Guild): message_name = f"{dir_name}/{file_name}" file_path = f"{dir_path}/{file_name}" with open(file_path, "r", encoding="utf8") as reader: - stmt = select(Messages.Id).where(Messages.Path == message_name) - msg_id = session.scalar(stmt) - if msg_id is None: - # don't have a message for this file - msg = await channel.send(reader.read(2000)) - new_message = Messages(Path=message_name, Id=msg.id) - session.add(new_message) - session.commit() - else: - try: - msg: nextcord.Message = await channel.fetch_message(msg_id) - await msg.edit(reader.read(2000)) - except nextcord.NotFound: - msg = await channel.send(reader.read(2000)) - stmt = ( - update(Messages) - .where(Messages.Path == message_name) - .values(Id=msg.id) - ) - session.execute(stmt) - session.commit() + await edit_clan_message( + session, channel, message_name, reader.read(2000) + ) async def get_clan_message_channel( @@ -508,3 +489,41 @@ async def get_clan_message_channel( session.execute(stmt) session.commit() return ret_channel + + +async def edit_clan_message( + session: Session, + channel: nextcord.TextChannel, + message_name: str, + message_content: str, +): + """ + Obtains a message id from database. If no id, then need to send a new message and record the id. + If there is a message, then edit the message. If that message is not found in discord, then need to + send a new message and record the id in the database. + + Args: + session (Session): Open connection to database. + channel (nextcord.TextChannel): Channel object that message belongs to. + message_name (str): Name of the message. Corresponds to the file that contains message content. + message_content (str): Content of the message to edit/send. + """ + stmt = select(Messages.Id).where(Messages.Path == message_name) + msg_id = session.scalar(stmt) + if msg_id is None: + # don't have a message for this file + msg = await channel.send(message_content) + new_message = Messages(Path=message_name, Id=msg.id) + session.add(new_message) + session.commit() + else: + try: + msg: nextcord.Message = await channel.fetch_message(msg_id) + await msg.edit(message_content) + except nextcord.NotFound: + msg = await channel.send(message_content) + stmt = ( + update(Messages).where(Messages.Path == message_name).values(Id=msg.id) + ) + session.execute(stmt) + session.commit() From 65ed5e7b591e1bae8e377eb98aaf18169924a497 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Thu, 16 May 2024 15:37:34 -0400 Subject: [PATCH 2/6] Made function to gather and send the council members message in the clan info channel. --- classes/election_cycle.py | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/classes/election_cycle.py b/classes/election_cycle.py index ef73269..a990ab1 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -23,7 +23,9 @@ from database_setup.sql_data_classes import ( from utils import ( build_highcouncil_votes_str, build_votes_str, + edit_clan_message, get_cl_election_name, + get_cycle_name, get_datetime, get_dm_election_name, get_election_cycle_name, @@ -437,6 +439,7 @@ class ElectionCycle: ) await election_thread.edit(archived=True, locked=True) + await self.edit_clan_council_message() def get_election_info(self) -> ElectionInformation: """ @@ -466,3 +469,53 @@ class ElectionCycle: message += f"to <#{self.get_election_info().ThreadId}> to cast your vote!" await general.send(content=message) + + async def edit_clan_council_message(self): + clan_info_channel: nextcord.TextChannel = await self.guild.fetch_channel( + self.config.params.clan_info_channel_id + ) + + election = get_election_cycle_name(self.config) + with Session(engine) as session: + stmt = ( + select(CouncilMembers.MemberId) + .where(CouncilMembers.ElectionCycle == election) + .where(CouncilMembers.DiscordManager == true()) + ) + discord_manager = session.scalar(stmt) + stmt = ( + select(CouncilMembers.MemberId) + .where(CouncilMembers.ElectionCycle == election) + .where(CouncilMembers.ClanLeader == true()) + ) + clan_leader = session.scalar(stmt) + stmt = ( + select(CouncilMembers.MemberId) + .where(CouncilMembers.ElectionCycle == election) + .where(CouncilMembers.Elected == true()) + ) + + council_members_strs = [ + f"<@{id}>" + for id in session.scalars(stmt) + if id not in (discord_manager, clan_leader) + ] + + quarter = get_cycle_name(self.config) + high_council_str = ( + f"Discord manager: <@{discord_manager}>\nClan Leader: <@{clan_leader}>" + ) + + with open( + "bot_messages/council_members.txt", mode="r", encoding="utf8" + ) as file: + message = file.read() + message.format( + quarter=quarter, + council="\n".join(council_members_strs), + high_council=high_council_str, + ) + + await edit_clan_message( + session, clan_info_channel, "council_members.txt", message + ) From 51d6713940ab707e5297dff9f90a156e17f8612c Mon Sep 17 00:00:00 2001 From: imcompiling Date: Thu, 16 May 2024 15:45:33 -0400 Subject: [PATCH 3/6] format returns the string. --- classes/election_cycle.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/election_cycle.py b/classes/election_cycle.py index a990ab1..b6bb8d0 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -510,7 +510,7 @@ class ElectionCycle: "bot_messages/council_members.txt", mode="r", encoding="utf8" ) as file: message = file.read() - message.format( + message = message.format( quarter=quarter, council="\n".join(council_members_strs), high_council=high_council_str, From a8323c9a88e7e678a9cf16855d8a697887d30b5d Mon Sep 17 00:00:00 2001 From: imcompiling Date: Thu, 16 May 2024 15:55:09 -0400 Subject: [PATCH 4/6] Update the message to look more like current. --- bot_messages/council_members.txt | 11 ++++++----- classes/election_cycle.py | 6 ++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/bot_messages/council_members.txt b/bot_messages/council_members.txt index 093aee1..d0eeb86 100644 --- a/bot_messages/council_members.txt +++ b/bot_messages/council_members.txt @@ -1,7 +1,8 @@ -Here's the {quarter} Clan Council: - -Dakiny Council: -{council} +Here's the **{quarter}** Clan Council: Dakiny High Council: -{high_council} \ No newline at end of file +<@{clan_leader}> - Clan Leader +<@{discord_manager}> - Discord Manager + +Dakiny Council: +{council} \ No newline at end of file diff --git a/classes/election_cycle.py b/classes/election_cycle.py index b6bb8d0..2f10901 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -502,9 +502,6 @@ class ElectionCycle: ] quarter = get_cycle_name(self.config) - high_council_str = ( - f"Discord manager: <@{discord_manager}>\nClan Leader: <@{clan_leader}>" - ) with open( "bot_messages/council_members.txt", mode="r", encoding="utf8" @@ -513,7 +510,8 @@ class ElectionCycle: message = message.format( quarter=quarter, council="\n".join(council_members_strs), - high_council=high_council_str, + clan_leader=clan_leader, + discord_manager=discord_manager, ) await edit_clan_message( From 39fd55146ee4c6268be8327e27baf90f0a37e9c7 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Thu, 16 May 2024 16:04:08 -0400 Subject: [PATCH 5/6] move edit clan council message into utils. --- classes/election_cycle.py | 53 ++--------------------------------- utils.py | 58 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 52 deletions(-) diff --git a/classes/election_cycle.py b/classes/election_cycle.py index 2f10901..fa506cf 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -23,9 +23,8 @@ from database_setup.sql_data_classes import ( from utils import ( build_highcouncil_votes_str, build_votes_str, - edit_clan_message, + edit_clan_council_message, get_cl_election_name, - get_cycle_name, get_datetime, get_dm_election_name, get_election_cycle_name, @@ -439,7 +438,7 @@ class ElectionCycle: ) await election_thread.edit(archived=True, locked=True) - await self.edit_clan_council_message() + await edit_clan_council_message(self.guild, self.config) def get_election_info(self) -> ElectionInformation: """ @@ -469,51 +468,3 @@ class ElectionCycle: message += f"to <#{self.get_election_info().ThreadId}> to cast your vote!" await general.send(content=message) - - async def edit_clan_council_message(self): - clan_info_channel: nextcord.TextChannel = await self.guild.fetch_channel( - self.config.params.clan_info_channel_id - ) - - election = get_election_cycle_name(self.config) - with Session(engine) as session: - stmt = ( - select(CouncilMembers.MemberId) - .where(CouncilMembers.ElectionCycle == election) - .where(CouncilMembers.DiscordManager == true()) - ) - discord_manager = session.scalar(stmt) - stmt = ( - select(CouncilMembers.MemberId) - .where(CouncilMembers.ElectionCycle == election) - .where(CouncilMembers.ClanLeader == true()) - ) - clan_leader = session.scalar(stmt) - stmt = ( - select(CouncilMembers.MemberId) - .where(CouncilMembers.ElectionCycle == election) - .where(CouncilMembers.Elected == true()) - ) - - council_members_strs = [ - f"<@{id}>" - for id in session.scalars(stmt) - if id not in (discord_manager, clan_leader) - ] - - quarter = get_cycle_name(self.config) - - with open( - "bot_messages/council_members.txt", mode="r", encoding="utf8" - ) as file: - message = file.read() - message = message.format( - quarter=quarter, - council="\n".join(council_members_strs), - clan_leader=clan_leader, - discord_manager=discord_manager, - ) - - await edit_clan_message( - session, clan_info_channel, "council_members.txt", message - ) diff --git a/utils.py b/utils.py index 3efc71b..e09a6c5 100644 --- a/utils.py +++ b/utils.py @@ -6,7 +6,7 @@ import os from datetime import datetime import nextcord -from sqlalchemy import select, update +from sqlalchemy import select, true, update from sqlalchemy.orm import Session from config import DakinyConfig @@ -527,3 +527,59 @@ async def edit_clan_message( ) session.execute(stmt) session.commit() + + +async def edit_clan_council_message(guild: nextcord.Guild, config: DakinyConfig): + """ + Gets the winners of the elections and updates the perminant message that states the current council. + + Args: + guild (nextcord.Guild): Guild object that to use to get channel. + config (DakinyConfig): Configs + """ + clan_info_channel: nextcord.TextChannel = await guild.fetch_channel( + config.params.clan_info_channel_id + ) + + election = get_election_cycle_name(config) + with Session(engine) as session: + stmt = ( + select(CouncilMembers.MemberId) + .where(CouncilMembers.ElectionCycle == election) + .where(CouncilMembers.DiscordManager == true()) + ) + discord_manager = session.scalar(stmt) + stmt = ( + select(CouncilMembers.MemberId) + .where(CouncilMembers.ElectionCycle == election) + .where(CouncilMembers.ClanLeader == true()) + ) + clan_leader = session.scalar(stmt) + stmt = ( + select(CouncilMembers.MemberId) + .where(CouncilMembers.ElectionCycle == election) + .where(CouncilMembers.Elected == true()) + ) + + council_members_strs = [ + f"<@{id}>" + for id in session.scalars(stmt) + if id not in (discord_manager, clan_leader) + ] + + quarter = get_cycle_name(config) + + with open( + "bot_messages/council_members.txt", mode="r", encoding="utf8" + ) as file: + message = file.read() + message = message.format( + quarter=quarter, + council="\n".join(council_members_strs), + clan_leader=clan_leader, + discord_manager=discord_manager, + ) + + await edit_clan_message( + session, clan_info_channel, "council_members.txt", message + ) From 7d54a0c30bb6b347dfc87a32c68fc352f21cc7d0 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Thu, 16 May 2024 16:10:16 -0400 Subject: [PATCH 6/6] Create refresh council info command. Change method name. --- classes/election_cycle.py | 4 ++-- cogs/commands/__init__.py | 2 +- cogs/commands/refresh.py | 22 +++++++++++++++++++++- utils.py | 2 +- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/classes/election_cycle.py b/classes/election_cycle.py index fa506cf..a60cdd4 100644 --- a/classes/election_cycle.py +++ b/classes/election_cycle.py @@ -23,7 +23,7 @@ from database_setup.sql_data_classes import ( from utils import ( build_highcouncil_votes_str, build_votes_str, - edit_clan_council_message, + edit_clan_council_info, get_cl_election_name, get_datetime, get_dm_election_name, @@ -438,7 +438,7 @@ class ElectionCycle: ) await election_thread.edit(archived=True, locked=True) - await edit_clan_council_message(self.guild, self.config) + await edit_clan_council_info(self.guild, self.config) def get_election_info(self) -> ElectionInformation: """ diff --git a/cogs/commands/__init__.py b/cogs/commands/__init__.py index 725f143..df6a0c2 100644 --- a/cogs/commands/__init__.py +++ b/cogs/commands/__init__.py @@ -21,7 +21,7 @@ def setup(bot: commands.Bot): """ bot.add_cog(RecordCog(bot)) - bot.add_cog(RefreshCog()) + bot.add_cog(RefreshCog(bot)) bot.add_cog(RemoveCog()) bot.add_cog(RequestCog(bot)) bot.add_cog(TestCog(bot)) diff --git a/cogs/commands/refresh.py b/cogs/commands/refresh.py index e04b067..cadc1a5 100644 --- a/cogs/commands/refresh.py +++ b/cogs/commands/refresh.py @@ -5,7 +5,7 @@ Contained commands and views for refresh commands. import nextcord from nextcord.ext import commands -from utils import update_clan_messages +from utils import edit_clan_council_info, update_clan_messages class RefreshCog(commands.Cog, name="Refresh Commands"): @@ -13,6 +13,13 @@ class RefreshCog(commands.Cog, name="Refresh Commands"): Class holds refresh commands. """ + def __init__(self, bot) -> None: + """ + Default constructor for RefreshCog class. Does nothing. + """ + super().__init__() + self.bot: commands.Bot = bot + @nextcord.slash_command( default_member_permissions=nextcord.Permissions(administrator=True) ) @@ -32,3 +39,16 @@ class RefreshCog(commands.Cog, name="Refresh Commands"): await interaction.response.defer(ephemeral=True) await update_clan_messages(interaction.guild) await interaction.followup.send("Messages have been refreshed.", ephemeral=True) + + @refresh.subcommand(description="Updates the current council info message.") + async def council_info(self, interaction: nextcord.Interaction): + """ + Calls edit_clan_council_info(). + + Args: + interaction (nextcord.Interaction): Interaction with the command. + """ + await edit_clan_council_info(interaction.guild, self.bot.config) + await interaction.response.send_message( + "Clan Council Info message updated.", ephemeral=True + ) diff --git a/utils.py b/utils.py index e09a6c5..0dbbfdb 100644 --- a/utils.py +++ b/utils.py @@ -529,7 +529,7 @@ async def edit_clan_message( session.commit() -async def edit_clan_council_message(guild: nextcord.Guild, config: DakinyConfig): +async def edit_clan_council_info(guild: nextcord.Guild, config: DakinyConfig): """ Gets the winners of the elections and updates the perminant message that states the current council.