From 10e8e5a446db32183c3e9b1a2dfe0c674acb05e4 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Thu, 1 Dec 2022 16:40:06 -0500 Subject: [PATCH] Update settings management to new database. --- cogs/commands/__init__.py | 4 +- cogs/commands/settings_management.py | 81 ++++++++++++++++++---------- 2 files changed, 55 insertions(+), 30 deletions(-) diff --git a/cogs/commands/__init__.py b/cogs/commands/__init__.py index d383dad..0107117 100644 --- a/cogs/commands/__init__.py +++ b/cogs/commands/__init__.py @@ -5,12 +5,10 @@ Cog loader. Adds all command cogs to the bot. from nextcord.ext import commands # Command classes +from .data_management import DataManagementCog from .message_management import MessageManagementCog from .settings_management import SettingsManagementCog -from .data_management import DataManagementCog -# Views for commands -from .views.update_username_verify import UpdateUsernameVerifyView # This function will be called when this extension is loaded. # It is necessary to add these functions to the bot. diff --git a/cogs/commands/settings_management.py b/cogs/commands/settings_management.py index 22f251c..c69e173 100644 --- a/cogs/commands/settings_management.py +++ b/cogs/commands/settings_management.py @@ -6,8 +6,11 @@ from typing import Optional import nextcord from nextcord.ext import commands +from sqlalchemy import select -from highscores import highscores_data # pylint: disable=import-error +# pylint: disable=import-error +from highscores import CategoryGroup, Guild, session +from utilities.utils import autofill_groups class SettingsManagementCog(commands.Cog, name="Settings Management"): @@ -26,10 +29,11 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): Parent slash command, never called. """ - @register.subcommand(description="Registers the channel for perminent highscores messages.") - async def highscore( + @register.subcommand(description="Registers the channel for a Category Group's messages.") + async def group_output_channel( self, interaction: nextcord.Interaction, + category_group: nextcord.SlashOption(description="Category Group to assign to channel.", required=True), channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption( description="Channel to register", required=False, @@ -38,7 +42,7 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): ), ) -> None: """ - Command function to register the highscore printing channel. + Command function to register a CategoryGroup's output channel. Args: interaction (nextcord.Interaction): Interaction that invoked the command @@ -48,10 +52,15 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): if not channel: channel = interaction.channel channel_id = channel.id - guild = highscores_data.get_guild(interaction.guild_id) + stmt = ( + select(CategoryGroup) + .where(CategoryGroup.GuildId == interaction.guild_id) + .where(CategoryGroup.GroupId == category_group) + ) + group = session.scalar(stmt) + group.ChannelId = channel_id + session.commit() await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) - guild.highscore_channel = channel_id - highscores_data.save_guild_data(guild) @register.subcommand(description="Registers the channel for submission messages.") async def submission( @@ -75,10 +84,11 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): if not channel: channel = interaction.channel channel_id = channel.id - guild = highscores_data.get_guild(interaction.guild_id) + stmt = select(Guild).where(Guild.GuildId == interaction.guild_id) + guild = session.scalar(stmt) + guild.SubmissionChannel = channel_id + session.commit() await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) - guild.submission_channel = channel_id - highscores_data.save_guild_data(guild) @register.subcommand(description="Registers the channel for verification messages.") async def verify( @@ -102,10 +112,11 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): if not channel: channel = interaction.channel channel_id = channel.id - guild = highscores_data.get_guild(interaction.guild_id) + stmt = select(Guild).where(Guild.GuildId == interaction.guild_id) + guild = session.scalar(stmt) + guild.VerificationChannel = channel_id + session.commit() await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) - guild.verification_channel = channel_id - highscores_data.save_guild_data(guild) @nextcord.slash_command(default_member_permissions=nextcord.Permissions(administrator=True)) async def set(self, interaction: nextcord.Interaction): @@ -119,9 +130,8 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): async def category_pad_length( self, interaction: nextcord.Interaction, - length: Optional[int] = nextcord.SlashOption( - description="Number of characters.", required=True, default=26 - ), + category_group: nextcord.SlashOption(description="Category Group to assign to channel.", required=True), + length: Optional[int] = nextcord.SlashOption(description="Number of characters.", required=True, default=26), ) -> None: """ Changes guild's category pad length. @@ -131,20 +141,35 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): length (Optional[int]): Number of characters. Is SlashOption. Defaults to 26. """ - guild = highscores_data.get_guild(interaction.guild_id) - guild.category_pad_length = length - highscores_data.save_guild_data(guild) - await interaction.response.send_message( - f"Category pad length set to {length}", ephemeral=True + stmt = ( + select(CategoryGroup) + .where(CategoryGroup.GuildId == interaction.guild_id) + .where(CategoryGroup.GroupId == category_group) ) + group = session.scalar(stmt) + group.PadLength = length + session.commit() + await interaction.response.send_message( + f"Category pad length set to {length} for {group.GroupName}", ephemeral=True + ) + + @group_output_channel.on_autocomplete("category_group") + @category_pad_length.on_autocomplete("category_group") + async def group_id_autocomplete(self, interaction: nextcord.Interaction, category_group: str): + """ + Sends results from autofill_groups back to group_output_channel for autocompletion of categoru_group. + + Args: + interaction (nextcord.Interaction): Interaction with command. + category_group (str): String to run autocompletion against. + """ + await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, category_group)) @set.subcommand(description="Set username length. Limits all submissions to this length.") async def username_length( self, interaction: nextcord.Interaction, - length: Optional[int] = nextcord.SlashOption( - description="Number of characters.", required=True, default=12 - ), + length: Optional[int] = nextcord.SlashOption(description="Number of characters.", required=True, default=12), ) -> None: """ Changes guild's username length limit. @@ -153,7 +178,9 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): interaction (nextcord.Interaction): Interaction that invoked the command length (Optional[int]): Number of charactesr. Is SlashOption. Defaults to 12. """ - guild = highscores_data.get_guild(interaction.guild_id) - guild.username_length = length - highscores_data.save_guild_data(guild) + + stmt = select(Guild).where(Guild.GuildId == interaction.guild_id) + guild = session.scalar(stmt) + guild.UsernameLength = length + session.commit() await interaction.response.send_message(f"Username length set to {length}", ephemeral=True)