From 88cec73644155318a7ef5ce449f52b5c6ea42758 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Wed, 7 Dec 2022 22:24:38 -0500 Subject: [PATCH] Create command skeletons. Update set to conform to min and max values decided in create. --- cogs/commands/__init__.py | 3 +- cogs/commands/create.py | 87 +++++++++++++++++++++++++++++++++++++++ cogs/commands/set.py | 80 +++++++++++++++++++---------------- 3 files changed, 133 insertions(+), 37 deletions(-) create mode 100644 cogs/commands/create.py diff --git a/cogs/commands/__init__.py b/cogs/commands/__init__.py index b1422cd..040236b 100644 --- a/cogs/commands/__init__.py +++ b/cogs/commands/__init__.py @@ -4,7 +4,7 @@ Cog loader. Adds all command cogs to the bot. from nextcord.ext import commands -# Command classes +from .create import CreateCog from .delete import DeleteCog from .refresh import RefreshCog from .set import SetCog @@ -17,6 +17,7 @@ def setup(bot: commands.Bot): """ Adds all command classes to bot. """ + bot.add_cog(CreateCog()) bot.add_cog(DeleteCog()) bot.add_cog(RefreshCog(bot)) bot.add_cog(SetCog()) diff --git a/cogs/commands/create.py b/cogs/commands/create.py new file mode 100644 index 0000000..2a87b58 --- /dev/null +++ b/cogs/commands/create.py @@ -0,0 +1,87 @@ +""" +Commands for creating items in bot. +""" + +from typing import Optional + +import nextcord +from nextcord.ext import commands + +# pylint: disable=import-error +from utilities.utils import autofill_groups, MAX_LINE_LENGTH + + +class CreateCog(commands.Cog, name="Create Cog"): + """ + Class holds commands for creating things. + """ + + def __init__(self) -> None: + """ + Default constructor for CreateCog class. + """ + + @nextcord.slash_command(default_member_permissions=nextcord.Permissions(administrator=True)) + async def create(self, interaction: nextcord.Interaction): + """ + Parent slash command for creating things. Never called. + """ + + @create.subcommand(description="Create a new category. Must not exist already in CategoryGroup.") + async def category( + self, + interaction: nextcord.Interaction, + group_id: str = nextcord.SlashOption(name="group", description="Group to add Category to.", required=True), + category_name: str = nextcord.SlashOption(description="Name of new Category.", required=True), + is_ascending: bool = nextcord.SlashOption(description="If higher value is better score.", required=True), + is_time_record: bool = nextcord.SlashOption( + description="If the score should be shown in time format. Cannot be changed.", required=True + ), + is_validated: bool = nextcord.SlashOption( + description="If scores need to be validated before posted.", required=True + ), + size: int = nextcord.SlashOption( + description="How many scores to post in Category. Default is 3.", required=False, default=3, min_value=1 + ), + ) -> None: + pass + + @create.subcommand(description="Create a new Category Group. Must not exist already for Guild.") + async def group( + self, + interaction: nextcord.Interaction, + group_name: str = nextcord.SlashOption(description="Name of new Group.", required=True), + pad_length: int = nextcord.SlashOption( + description="Number of characters alloted to a category. Default is 26.", + required=False, + default=26, + min_value=7, + max_value=MAX_LINE_LENGTH, + ), + username_length: int = nextcord.SlashOption( + description="Number of characters of a username to display. Default is 12.", + required=False, + default=12, + minvalue=1, + max_value=30, + ), + channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption( + description="Channel to post Group in. If left empty, defaults to current channel.", + required=False, + channel_types=[nextcord.ChannelType.text], + default=None, + ), + ) -> None: + pass + + @category.on_autocomplete("group_id") + async def group_autocomplete(self, interaction: nextcord.Interaction, group_id: str): + """ + Collects list of table names and compares to input table name for autocomplete assistance. + + Args: + interaction (nextcord.Interaction): Interaction with command. + group_id (str): String to run autocomplete compared to. + """ + + await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, group_id)) diff --git a/cogs/commands/set.py b/cogs/commands/set.py index bca49c1..f3c6ea5 100644 --- a/cogs/commands/set.py +++ b/cogs/commands/set.py @@ -11,7 +11,7 @@ from sqlalchemy.orm import Session # pylint: disable=import-error from utilities.sql_data_classes import CategoryGroup, Guild, engine -from utilities.utils import autofill_groups +from utilities.utils import autofill_groups, MAX_LINE_LENGTH class SetCog(commands.Cog, name="Set command."): @@ -30,39 +30,6 @@ class SetCog(commands.Cog, name="Set command."): Parent slash command for setting settings. Never called. """ - @set.subcommand( - description="Set character length for a category. Can cut off username/score. Affects all categories." - ) - async def category_pad_length( - self, - interaction: nextcord.Interaction, - group_id: str = nextcord.SlashOption( - name="group", description="Category Group to assign pad length to.", required=True - ), - length: int = nextcord.SlashOption(description="Number of characters.", required=True, default=26), - ) -> None: - """ - Changes guild's category pad length. - - Args: - interaction (nextcord.Interaction): Interaction that invoked the command - group_id (str): String representation of Group id to set pad length in. Is SlashOption. - length (int): Number of characters. Is SlashOption. Defaults to 26. - """ - - with Session(engine) as session: - stmt = ( - select(CategoryGroup) - .where(CategoryGroup.GuildId == interaction.guild_id) - .where(CategoryGroup.GroupId == int(group_id)) - ) - 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 - ) - @set.subcommand(description="Registers the channel for a Category Group's messages.") async def group_output_channel( self, @@ -101,6 +68,45 @@ class SetCog(commands.Cog, name="Set command."): session.commit() await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) + @set.subcommand( + description="Set character length for a category. Can cut off username/score. Affects all categories." + ) + async def pad_length( + self, + interaction: nextcord.Interaction, + group_id: str = nextcord.SlashOption( + name="group", description="Category Group to assign pad length to.", required=True + ), + length: int = nextcord.SlashOption( + description="Number of characters alloted to a category.", + required=True, + default=26, + min_value=7, + max_value=MAX_LINE_LENGTH, + ), + ) -> None: + """ + Changes guild's category pad length. + + Args: + interaction (nextcord.Interaction): Interaction that invoked the command + group_id (str): String representation of Group id to set pad length in. Is SlashOption. + length (int): Number of characters. Is SlashOption. Defaults to 26. + """ + + with Session(engine) as session: + stmt = ( + select(CategoryGroup) + .where(CategoryGroup.GuildId == interaction.guild_id) + .where(CategoryGroup.GroupId == int(group_id)) + ) + 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 + ) + @set.subcommand(description="Registers the channel for submission messages.") async def submission_channel( self, @@ -139,7 +145,9 @@ class SetCog(commands.Cog, name="Set command."): group_id: str = nextcord.SlashOption( name="group", description="Group to change username length of.", required=True ), - length: int = nextcord.SlashOption(description="Number of characters.", required=True, default=12), + length: int = nextcord.SlashOption( + description="Number of characters.", required=True, default=12, min_value=1, max_value=30 + ), ) -> None: """ Changes guild's username length limit. @@ -191,7 +199,7 @@ class SetCog(commands.Cog, name="Set command."): await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) @group_output_channel.on_autocomplete("group_id") - @category_pad_length.on_autocomplete("group_id") + @pad_length.on_autocomplete("group_id") @username_length.on_autocomplete("group_id") async def group_autocomplete(self, interaction: nextcord.Interaction, group_id: str): """