Fill out rest of group and category command. Fill out doc strings.

This commit is contained in:
alwayscompiling
2022-12-08 13:41:15 -05:00
parent dace72f5cc
commit 93248797a0
+65 -11
View File
@@ -6,12 +6,12 @@ from typing import Optional
import nextcord import nextcord
from nextcord.ext import commands from nextcord.ext import commands
from sqlalchemy import select from sqlalchemy import select, exc
from sqlalchemy.orm import Session from sqlalchemy.orm import Session
# pylint: disable=import-error # pylint: disable=import-error
from utilities.sql_data_classes import Category, CategoryGroup, engine from utilities.sql_data_classes import Category, CategoryGroup, engine
from utilities.utils import MAX_LINE_LENGTH, autofill_groups from utilities.utils import MAX_LINE_LENGTH, autofill_groups, send_highscore_message
class CreateCog(commands.Cog, name="Create Cog"): class CreateCog(commands.Cog, name="Create Cog"):
@@ -30,7 +30,9 @@ class CreateCog(commands.Cog, name="Create Cog"):
Parent slash command for creating things. Never called. Parent slash command for creating things. Never called.
""" """
@create.subcommand(description="Create a new category. Must not exist already in CategoryGroup.") # need all these arguments because discord command.
# pylint: disable=too-many-arguments
@create.subcommand(description="Create a new Category. Must not exist already in Group.")
async def category( async def category(
self, self,
interaction: nextcord.Interaction, interaction: nextcord.Interaction,
@@ -47,6 +49,19 @@ 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 description="How many scores to post in Category. Default is 3.", required=False, default=3, min_value=1
), ),
) -> None: ) -> None:
"""
Creates a new category to be placed into group_id group. Fails if the category name already exists within the
group. Then calls send_highscore_message().
Args:
interaction (nextcord.Interaction): Interaction for the command.
group_id (str): Id of group to add category to. Is SlashOption.
category_name (str): Name of new category. Is SlashOption.
is_ascending (bool): If the scores are sorted ascending or not. Is SlashOption.
is_time_record (bool): If the scores are a time record or not. Is SlashOption.
is_validated (bool): If the scores should be validated or not. Is SlashOption.
size (int): How many sores to show in the category message. Is SlashOption.
"""
new_category = Category( new_category = Category(
CategoryName=category_name, CategoryName=category_name,
@@ -57,27 +72,33 @@ class CreateCog(commands.Cog, name="Create Cog"):
IsValidated=is_validated, IsValidated=is_validated,
) )
# Open connection to engine and add new category.
with Session(engine) as session: with Session(engine) as session:
stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id))
group = session.scalar(stmt)
try: try:
session.add(new_category) session.add(new_category)
session.commit() session.commit()
# TODO get correct exception await send_highscore_message(session, interaction.guild, group)
except Exception as ece: await interaction.response.send_message(
f'"{category_name}" has been added to "{group.GroupName}"!', ephemeral=True
)
except exc.IntegrityError:
# Category name already exists in group # Category name already exists in group
print(ece)
session.rollback() session.rollback()
stmt = select(CategoryGroup.GroupName).where(CategoryGroup.GroupId == int(group_id)) await interaction.response.send_message(
interaction.response.send_message( f'"{category_name}" already exists in "{group.GroupName}". Please rename.', ephemeral=True
f"{category_name} already exists in {session.scalar(stmt)}. Please rename.", ephemeral=True
) )
# need all these arguments because discord command.
# pylint: disable=too-many-arguments
@create.subcommand(description="Create a new Category Group. Must not exist already for Guild.") @create.subcommand(description="Create a new Category Group. Must not exist already for Guild.")
async def group( async def group(
self, self,
interaction: nextcord.Interaction, interaction: nextcord.Interaction,
group_name: str = nextcord.SlashOption(description="Name of new Group.", required=True), group_name: str = nextcord.SlashOption(description="Name of new Group.", required=True),
pad_length: int = nextcord.SlashOption( pad_length: int = nextcord.SlashOption(
description="Number of characters alloted to a category. Default is 26.", description="Number of characters wide a category is printed. Default is 26.",
required=False, required=False,
default=26, default=26,
min_value=7, min_value=7,
@@ -87,7 +108,7 @@ class CreateCog(commands.Cog, name="Create Cog"):
description="Number of characters of a username to display. Default is 12.", description="Number of characters of a username to display. Default is 12.",
required=False, required=False,
default=12, default=12,
minvalue=1, min_value=1,
max_value=30, max_value=30,
), ),
channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption( channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption(
@@ -97,11 +118,44 @@ class CreateCog(commands.Cog, name="Create Cog"):
default=None, default=None,
), ),
) -> None: ) -> None:
"""
Creates a new category group to be placed into the database with the guild's id. Fails if the category group
name already exists within the guild. Then calls send_highscore_message().
Args:
interaction (nextcord.Interaction): Interaction for the command.
group_name (str): Name of new Group. Is SlashOption.
pad_length (int): Number of characters wide a category is printed. Is SlashOption.
username_length (int): Number of characters of a username to display. Is SlashOption.
channel (Optional[nextcord.abc.GuildChannel]): Channel to post group in. If None, defaults to current
channel. Is SlashOption.
"""
# if no channel provided, use current channel. # if no channel provided, use current channel.
if channel is None: if channel is None:
channel = interaction.channel channel = interaction.channel
new_group = CategoryGroup(
GroupName=group_name,
ChannelId=channel.id,
GuildId=interaction.guild_id,
PadLength=pad_length,
UsernameLength=username_length,
)
with Session(engine) as session:
try:
session.add(new_group)
session.commit()
await send_highscore_message(session, interaction.guild, new_group)
await interaction.response.send_message(f'"{group_name}" has been created!', ephemeral=True)
except exc.IntegrityError:
# Category Group already exists in guild
session.rollback()
await interaction.response.send_message(
f'"{group_name}" already exists in "{interaction.guild.name}". Please rename.', ephemeral=True
)
@category.on_autocomplete("group_id") @category.on_autocomplete("group_id")
async def group_autocomplete(self, interaction: nextcord.Interaction, group_id: str): async def group_autocomplete(self, interaction: nextcord.Interaction, group_id: str):
""" """