From a9b889234aba54a2a4fd8c53a5f25ef3438cc0fb Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Wed, 7 Dec 2022 22:40:47 -0500 Subject: [PATCH] Start filling out create category. --- cogs/commands/create.py | 34 +++++++++++++++++++++++++++++++--- cogs/commands/set.py | 2 +- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/cogs/commands/create.py b/cogs/commands/create.py index 2a87b58..ce6f893 100644 --- a/cogs/commands/create.py +++ b/cogs/commands/create.py @@ -6,9 +6,12 @@ from typing import Optional import nextcord from nextcord.ext import commands +from sqlalchemy import select +from sqlalchemy.orm import Session # pylint: disable=import-error -from utilities.utils import autofill_groups, MAX_LINE_LENGTH +from utilities.sql_data_classes import Category, CategoryGroup, engine +from utilities.utils import MAX_LINE_LENGTH, autofill_groups class CreateCog(commands.Cog, name="Create Cog"): @@ -44,7 +47,29 @@ class CreateCog(commands.Cog, name="Create Cog"): description="How many scores to post in Category. Default is 3.", required=False, default=3, min_value=1 ), ) -> None: - pass + + new_category = Category( + CategoryName=category_name, + GroupId=int(group_id), + Size=size, + IsAscending=is_ascending, + IsTimeRecord=is_time_record, + IsValidated=is_validated, + ) + + with Session(engine) as session: + try: + session.add(new_category) + session.commit() + # TODO get correct exception + except Exception as ece: + # Category name already exists in group + print(ece) + session.rollback() + stmt = select(CategoryGroup.GroupName).where(CategoryGroup.GroupId == int(group_id)) + interaction.response.send_message( + f"{category_name} already exists in {session.scalar(stmt)}. Please rename.", ephemeral=True + ) @create.subcommand(description="Create a new Category Group. Must not exist already for Guild.") async def group( @@ -72,7 +97,10 @@ class CreateCog(commands.Cog, name="Create Cog"): default=None, ), ) -> None: - pass + + # if no channel provided, use current channel. + if channel is None: + channel = interaction.channel @category.on_autocomplete("group_id") async def group_autocomplete(self, interaction: nextcord.Interaction, group_id: str): diff --git a/cogs/commands/set.py b/cogs/commands/set.py index f3c6ea5..82e738b 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, MAX_LINE_LENGTH +from utilities.utils import MAX_LINE_LENGTH, autofill_groups class SetCog(commands.Cog, name="Set command."):