Corrected autofill function return structures.

This commit is contained in:
alwayscompiling
2022-12-02 16:32:22 -05:00
parent 14602282e9
commit 4e832a084f
4 changed files with 35 additions and 30 deletions
+15 -11
View File
@@ -91,8 +91,8 @@ class DataManagementCog(commands.Cog, name="Table Management"):
async def from_category( async def from_category(
self, self,
interaction: nextcord.Interaction, 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),
category_id: int = nextcord.SlashOption(description="Category 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), user_name: str = nextcord.SlashOption(description="User to delete.", required=True),
): ):
""" """
@@ -100,27 +100,31 @@ class DataManagementCog(commands.Cog, name="Table Management"):
Args: Args:
interaction (nextcord.Interaction): Interaction that invoked the command. 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.
category_id (int): Category 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. 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) 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): for score in session.scalars(stmt):
session.delete(score) session.delete(score)
session.commit() session.commit()
await send_highscore_message(interaction.guild.get_channel(group.ChannelId), group) 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.") @user.subcommand(description="Deletes a user's scores from entire group.")
async def from_group( async def from_group(
self, self,
interaction: nextcord.Interaction, 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), user_name: str = nextcord.SlashOption(description="User to delete.", required=True),
): ):
""" """
@@ -128,13 +132,13 @@ class DataManagementCog(commands.Cog, name="Table Management"):
Args: Args:
interaction (nextcord.Interaction): Interaction that invoked the command. 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. 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) 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) stmt = select(Score).where(Score.CategoryId.in_(category_ids)).where(Score.UserName == user_name)
for score in session.scalars(stmt): for score in session.scalars(stmt):
+4 -4
View File
@@ -2,7 +2,7 @@
Commands for managing/displaying the highscores messages. Commands for managing/displaying the highscores messages.
""" """
from typing import Optional from typing import Optional, Union
import nextcord import nextcord
from nextcord.ext import commands from nextcord.ext import commands
@@ -115,20 +115,20 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
async def refresh_group( async def refresh_group(
self, self,
interaction: nextcord.Interaction, 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: ) -> None:
""" """
Refreshes a table given by user. Refreshes a table given by user.
Args: Args:
interaction (nextcord.Interaction): Interaction that invoked the command. 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 = ( stmt = (
select(CategoryGroup) select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id) .where(CategoryGroup.GuildId == interaction.guild_id)
.where(CategoryGroup.GroupId == group_id) .where(CategoryGroup.GroupId == int(group_id))
) )
group = session.scalar(stmt) group = session.scalar(stmt)
+6 -6
View File
@@ -33,7 +33,7 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"):
async def group_output_channel( async def group_output_channel(
self, self,
interaction: nextcord.Interaction, 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( channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption(
description="Channel to register", description="Channel to register",
required=False, required=False,
@@ -46,7 +46,7 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"):
Args: Args:
interaction (nextcord.Interaction): Interaction that invoked the command 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 channel (nextcord.abc.GuildChannel, optional): Channel to register
Is a SlashOption. Limited to text channels, Defaults to None. Is a SlashOption. Limited to text channels, Defaults to None.
""" """
@@ -56,7 +56,7 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"):
stmt = ( stmt = (
select(CategoryGroup) select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id) .where(CategoryGroup.GuildId == interaction.guild_id)
.where(CategoryGroup.GroupId == group_id) .where(CategoryGroup.GroupId == int(group_id))
) )
group = session.scalar(stmt) group = session.scalar(stmt)
group.ChannelId = channel_id group.ChannelId = channel_id
@@ -131,7 +131,7 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"):
async def category_pad_length( async def category_pad_length(
self, self,
interaction: nextcord.Interaction, 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), length: int = nextcord.SlashOption(description="Number of characters.", required=True, default=26),
) -> None: ) -> None:
""" """
@@ -139,14 +139,14 @@ class SettingsManagementCog(commands.Cog, name="Settings Management"):
Args: Args:
interaction (nextcord.Interaction): Interaction that invoked the command 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. length (int): Number of characters. Is SlashOption. Defaults to 26.
""" """
stmt = ( stmt = (
select(CategoryGroup) select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id) .where(CategoryGroup.GuildId == interaction.guild_id)
.where(CategoryGroup.GroupId == group_id) .where(CategoryGroup.GroupId == int(group_id))
) )
group = session.scalar(stmt) group = session.scalar(stmt)
group.PadLength = length group.PadLength = length
+10 -9
View File
@@ -16,7 +16,7 @@ from utilities.sql_alchemy_connection import Category, CategoryGroup, Guild, Sco
MAX_LINE_LENGTH = 142 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. Returns all category names that start with category_begin.
If category_begin is empty, returns list of all category names. 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. category_begin (str): The beginning of a category name to search with. Can be empty.
Returns: 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 # 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) cat_group_alias = aliased(CategoryGroup, subq)
stmt = select(Category).where(Category.GroupId == cat_group_alias.GroupId) 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 string empty, return all category names
if not category_begin: 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 # else find all categories that start with category_begin
# TODO is it better to have sql handle the starts with or python? # TODO is it better to have sql handle the starts with or python?
categories: dict = { categories: dict = {
cat.CategoryName: cat.CategoryId cat_name: cat_id
for cat in categories_dict.items() for cat_name, cat_id in categories_dict.items()
if category_begin.lower() in cat.CategoryName.lower() if category_begin.lower() in cat_name.CategoryName.lower()
} }
return categories 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. 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. group_begin (str): The beginning of a group name to search with. Can be empty.
Returns: 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. # Select all groups in guild.
stmt = select(CategoryGroup).where(CategoryGroup.GuildId == guild_id) 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 string empty, return all group names
if not group_begin: if not group_begin: