diff --git a/cogs/commands/set.py b/cogs/commands/set.py index f1a5994..9decf06 100644 --- a/cogs/commands/set.py +++ b/cogs/commands/set.py @@ -103,6 +103,58 @@ class SetCog(commands.Cog, name="Set command."): session.commit() await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) + @set.subcommand(description="") + async def is_ascending( + self, + interaction: nextcord.Interaction, + group_id: str = nextcord.SlashOption( + name="group", description="Category Group that Category is in.", required=True + ), + category_id: str = nextcord.SlashOption( + name="category", description="Category to update is_ascending of.", required=True + ), + ascending: bool = nextcord.SlashOption( # noqa: FBT001 + description="Whether the scores are sorted ascending or not.", required=True + ), + ) -> None: + """ + Command to update a category's is_ascending boolean. + + Args: + interaction (nextcord.Interaction): Interaction that invoked the command. + group_id (str): String representation of Group id to register channel to. Is SlashOption. + category_id (str): String representation of Category id to register channel to. Is SlashOption. + ascending (bool): Boolean to set. Determines whether scores are sorted ascending or not. Is SlashOption. + """ + stmt = select(Category).where(col(CategoryGroup.id == group_id)).where(col(Category.id) == int(category_id)) + + with Session(engine) as session: + category = session.scalar(stmt) + if category is None: + await interaction.response.send_message("Category or Group was not found.", ephemeral=True) + return + category.is_ascending = ascending + # session.add(category) test if this is needed. SQLAlchemy can do the update without adding. + session.commit() + + # Use the relationship to get group category is in. + group = category.category_group + if group is None: + await interaction.response.send_message( + ( + f"An error with {category.name} and group relationship. Contact developer. " + "Is_ascending was still updated." + ), + ephemeral=True, + ) + agon_logger.error(f"{category.name}:{category.id} group relationship was None.") + return + + await send_highscore_message(session, interaction.guild, group) + await interaction.response.send_message( + f"{category.name} is_ascending has been updated to {ascending}.", ephemeral=True + ) + @set.subcommand( description="Set character length for a category. Can cut off username/score. Affects all categories." ) @@ -230,6 +282,7 @@ class SetCog(commands.Cog, name="Set command."): await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) @category_size.on_autocomplete("category_id") + @is_ascending.on_autocomplete("category_id") async def category_autocomplete(self, interaction: nextcord.Interaction, category_id: str, group_id: str) -> None: """ Autocomplete function for category names. @@ -245,6 +298,7 @@ class SetCog(commands.Cog, name="Set command."): @category_size.on_autocomplete("group_id") @group_output_channel.on_autocomplete("group_id") + @is_ascending.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) -> None: