From 4e832a084f13c432730f172f637f6d54a70ec7a7 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Fri, 2 Dec 2022 16:08:36 -0500 Subject: [PATCH] Corrected autofill function return structures. --- cogs/commands/data_management.py | 26 +++++++++++++++----------- cogs/commands/message_management.py | 8 ++++---- cogs/commands/settings_management.py | 12 ++++++------ utilities/utils.py | 19 ++++++++++--------- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/cogs/commands/data_management.py b/cogs/commands/data_management.py index 3936729..a26ba8c 100644 --- a/cogs/commands/data_management.py +++ b/cogs/commands/data_management.py @@ -91,8 +91,8 @@ class DataManagementCog(commands.Cog, name="Table Management"): async def from_category( self, interaction: nextcord.Interaction, - group_id: int = nextcord.SlashOption(description="Group to delete user from.", required=True), - category_id: int = nextcord.SlashOption(description="Category to delete user from.", required=True), + group_id: str = nextcord.SlashOption(description="Group to delete user from.", required=True), + category_id: str = nextcord.SlashOption(description="Category to delete user from.", required=True), user_name: str = nextcord.SlashOption(description="User to delete.", required=True), ): """ @@ -100,27 +100,31 @@ class DataManagementCog(commands.Cog, name="Table Management"): Args: interaction (nextcord.Interaction): Interaction that invoked the command. - group_id (int): Group to remove user from. Is SlashOption. - category_id (int): Category to remove user from. Is SlashOption. + group_id (str): String representation of Group id to remove user from. Is SlashOption. + category_id (str): String representation of Category id to remove user from. Is SlashOption. user_name (str): The user to remove. Is SlashOption. """ - stmt = select(CategoryGroup).where(CategoryGroup.GroupId == group_id) + stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id)) group = session.scalar(stmt) - stmt = select(Score).where(Score.CategoryId == category_id) + + stmt = select(Category.CategoryName).where(Category.CategoryId == int(category_id)) + category_name = session.scalar(stmt) + + stmt = select(Score).where(Score.CategoryId == int(category_id)) for score in session.scalars(stmt): session.delete(score) session.commit() await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group) - await interaction.response.send_message(f'User "{user_name}" removed from "{category_id}".', ephemeral=True) + await interaction.response.send_message(f'User "{user_name}" removed from "{category_name}".', ephemeral=True) @user.subcommand(description="Deletes a user's scores from entire group.") async def from_group( self, interaction: nextcord.Interaction, - group_id: int = nextcord.SlashOption(description="Group to delete user from.", required=True), + group_id: str = nextcord.SlashOption(description="Group to delete user from.", required=True), user_name: str = nextcord.SlashOption(description="User to delete.", required=True), ): """ @@ -128,13 +132,13 @@ class DataManagementCog(commands.Cog, name="Table Management"): Args: interaction (nextcord.Interaction): Interaction that invoked the command. - group_id (int): Group to remove user from. Is SlashOption. + group_id (str): String representation of Group id to remove user from. Is SlashOption. user_name (str): The user to remove. Is SlashOption. """ - stmt = select(CategoryGroup).where(CategoryGroup.GroupId == group_id) + stmt = select(CategoryGroup).where(CategoryGroup.GroupId == int(group_id)) group = session.scalar(stmt) - category_ids = select(Category.Id).where(Category.GroupId == group_id).subquery() + category_ids = select(Category.Id).where(Category.GroupId == int(group_id)).subquery() stmt = select(Score).where(Score.CategoryId.in_(category_ids)).where(Score.UserName == user_name) for score in session.scalars(stmt): diff --git a/cogs/commands/message_management.py b/cogs/commands/message_management.py index b495c65..2d139a3 100644 --- a/cogs/commands/message_management.py +++ b/cogs/commands/message_management.py @@ -2,7 +2,7 @@ Commands for managing/displaying the highscores messages. """ -from typing import Optional +from typing import Optional, Union import nextcord from nextcord.ext import commands @@ -115,20 +115,20 @@ class MessageManagementCog(commands.Cog, name="Message Management"): async def refresh_group( self, interaction: nextcord.Interaction, - group_id: int = nextcord.SlashOption(description="Name of table.", required=True), + group_id: str = nextcord.SlashOption(description="Name of table.", required=True), ) -> None: """ Refreshes a table given by user. Args: interaction (nextcord.Interaction): Interaction that invoked the command. - group_id (int): Group id to refresh. Is SlashOption. + group_id (str): Str representation of Group id to refresh. Is SlashOption. """ stmt = ( select(CategoryGroup) .where(CategoryGroup.GuildId == interaction.guild_id) - .where(CategoryGroup.GroupId == group_id) + .where(CategoryGroup.GroupId == int(group_id)) ) group = session.scalar(stmt) diff --git a/cogs/commands/settings_management.py b/cogs/commands/settings_management.py index 60b7f14..1a37d9a 100644 --- a/cogs/commands/settings_management.py +++ b/cogs/commands/settings_management.py @@ -33,7 +33,7 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): async def group_output_channel( self, interaction: nextcord.Interaction, - group_id: int = nextcord.SlashOption(description="Category Group to assign to channel.", required=True), + group_id: str = nextcord.SlashOption(description="Category Group to assign to channel.", required=True), channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption( description="Channel to register", required=False, @@ -46,7 +46,7 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): Args: interaction (nextcord.Interaction): Interaction that invoked the command - group_id (int): Group to register channel to. Is SlashOption. + group_id (str): String representation of Group id to register channel to. Is SlashOption. channel (nextcord.abc.GuildChannel, optional): Channel to register Is a SlashOption. Limited to text channels, Defaults to None. """ @@ -56,7 +56,7 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): stmt = ( select(CategoryGroup) .where(CategoryGroup.GuildId == interaction.guild_id) - .where(CategoryGroup.GroupId == group_id) + .where(CategoryGroup.GroupId == int(group_id)) ) group = session.scalar(stmt) group.ChannelId = channel_id @@ -131,7 +131,7 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): async def category_pad_length( self, interaction: nextcord.Interaction, - group_id: int = nextcord.SlashOption(description="Category Group to assign to channel.", required=True), + group_id: str = nextcord.SlashOption(description="Category Group to assign pad length to.", required=True), length: int = nextcord.SlashOption(description="Number of characters.", required=True, default=26), ) -> None: """ @@ -139,14 +139,14 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"): Args: interaction (nextcord.Interaction): Interaction that invoked the command - group_id (int): Group to set pad length in. Is SlashOption. + 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. """ stmt = ( select(CategoryGroup) .where(CategoryGroup.GuildId == interaction.guild_id) - .where(CategoryGroup.GroupId == group_id) + .where(CategoryGroup.GroupId == int(group_id)) ) group = session.scalar(stmt) group.PadLength = length diff --git a/utilities/utils.py b/utilities/utils.py index 2655580..5d4c3cc 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -16,7 +16,7 @@ from utilities.sql_alchemy_connection import Category, CategoryGroup, Guild, Sco MAX_LINE_LENGTH = 142 -def autofill_categories(guild_id: int, group_name: str, category_begin: str) -> "list[str]": +def autofill_categories(guild_id: int, group_name: str, category_begin: str) -> "dict[str, str]": """ Returns all category names that start with category_begin. If category_begin is empty, returns list of all category names. @@ -27,7 +27,7 @@ def autofill_categories(guild_id: int, group_name: str, category_begin: str) -> category_begin (str): The beginning of a category name to search with. Can be empty. Returns: - list[str]: List of all category names that start with category_begin. + dict[str, str]: List of all category names that start with category_begin. """ # search database for all category names @@ -39,7 +39,7 @@ def autofill_categories(guild_id: int, group_name: str, category_begin: str) -> ) cat_group_alias = aliased(CategoryGroup, subq) stmt = select(Category).where(Category.GroupId == cat_group_alias.GroupId) - categories_dict: dict = {cat.CategoryName: cat.CategoryId for cat in session.scalars(stmt)} + categories_dict: dict = {cat.CategoryName: str(cat.CategoryId) for cat in session.scalars(stmt)} # if string empty, return all category names if not category_begin: @@ -48,14 +48,14 @@ def autofill_categories(guild_id: int, group_name: str, category_begin: str) -> # else find all categories that start with category_begin # TODO is it better to have sql handle the starts with or python? categories: dict = { - cat.CategoryName: cat.CategoryId - for cat in categories_dict.items() - if category_begin.lower() in cat.CategoryName.lower() + cat_name: cat_id + for cat_name, cat_id in categories_dict.items() + if category_begin.lower() in cat_name.CategoryName.lower() } return categories -def autofill_groups(guild_id: int, group_begin: str) -> dict: +def autofill_groups(guild_id: int, group_begin: str) -> "dict[str, str]": """ Returns all group names that start with group_begin. If group_begin is empty, returns list of all group names. @@ -64,13 +64,14 @@ def autofill_groups(guild_id: int, group_begin: str) -> dict: group_begin (str): The beginning of a group name to search with. Can be empty. Returns: - dict: Dict of group names and ids where group name start with group_begin. Key is name, value is id. + dict[str, str]: Dict of group names and ids where group name start with group_begin. Key is name, value is id. """ # Select all groups in guild. stmt = select(CategoryGroup).where(CategoryGroup.GuildId == guild_id) - group_dict: dict = {group.GroupName: group.GroupId for group in session.scalars(stmt)} + # dict has to have key values of same type for discord. + group_dict: dict = {group.GroupName: str(group.GroupId) for group in session.scalars(stmt)} # if string empty, return all group names if not group_begin: