From 06b46ef27c28ab96bf6c08d1d8253940d31b425a Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Thu, 17 Nov 2022 14:50:48 -0500 Subject: [PATCH] Create general autofill table and category util functions. Update table class to have get categories function. --- classes/table.py | 10 ++++++ cogs/commands/message_management.py | 19 ++++------- utilities/utils.py | 53 +++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/classes/table.py b/classes/table.py index 0f2af7e..e12224e 100644 --- a/classes/table.py +++ b/classes/table.py @@ -123,6 +123,16 @@ class Table: if category.name not in self._categories_map: self._categories_map[category.name] = category + def get_categories(self) -> dict: + """ + Getter for dict of category names and categories. + + Returns: + dict: Dictionary consisting of Category names and Category objects. + """ + # I don't know if this is const data. Don't want to allow data to be written as a result of this function. + return self._categories_map + def get_category(self, category_name: str) -> Category: """ Getter for specific category. Returns None if not found. diff --git a/cogs/commands/message_management.py b/cogs/commands/message_management.py index d032022..8d7259b 100644 --- a/cogs/commands/message_management.py +++ b/cogs/commands/message_management.py @@ -9,7 +9,7 @@ import nextcord from cogs.submission.submission_views import SubmissionCreateButton # pylint: disable=import-error from highscores import highscores_data # pylint: disable=import-error -from utilities.utils import send_highscore_message # pylint: disable=import-error +from utilities.utils import send_highscore_message, autofill_tables # pylint: disable=import-error class MessageManagementCog(commands.Cog, name="Message Management"): @@ -131,7 +131,7 @@ class MessageManagementCog(commands.Cog, name="Message Management"): await interaction.response.send_message(f"Updated table {table_name}!", ephemeral=True) @refresh_table.on_autocomplete("table_name") - async def autofill_tables(self, interaction: nextcord.Interaction, table_name: str): + async def refresh_table_autofill(self, interaction: nextcord.Interaction, table_name: str): """ Collects list of table names and compares to input table name for autocomplete assistance. @@ -139,14 +139,7 @@ class MessageManagementCog(commands.Cog, name="Message Management"): interaction (nextcord.Interaction): Interaction with command. table (str): String to run autocomplete compared to. """ - if not table_name: - await interaction.response.send_autocomplete( - highscores_data.get_guild(interaction.guild_id).get_tables().keys() - ) - return - near_table = [ - name - for name in highscores_data.get_guild(interaction.guild_id).get_tables().keys() - if name.lower().startswith(table_name.lower()) - ] - await interaction.response.send_autocomplete(near_table) + + await interaction.response.send_autocomplete( + autofill_tables(interaction.guild_id, table_name) + ) diff --git a/utilities/utils.py b/utilities/utils.py index 370a6cd..ae94822 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -10,6 +10,59 @@ from classes.category import Category # pylint: disable=import-error from classes.table import Table # pylint: disable=import-error +def autofill_categories(guild_id: int, table_name: str, category_begin: str) -> "list[str]": + """ + Returns all category names that start with category_begin. + If category_begin is empty, returns list of all category names. + + Args: + guild_id (int): Guild id that holds the table and categories. + table_name (str): Table name to search in. + 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. + """ + # if string empty, return all category names + if not category_begin: + return highscores_data.get_guild(guild_id).get_table(table_name).get_categories().keys() + + # else find all categories that start with category_begin + categories = [ + category + for category in highscores_data.get_guild(guild_id) + .get_table(table_name) + .get_categories() + .keys() + if category.lower().startswith(category_begin.lower()) + ] + return categories + + +def autofill_tables(guild_id: int, table_begin: str) -> "list[str]": + """ + Returns all table names that start with table_begin. If table_begin is empty, returns list of all table names. + + Args: + guild_id (int): Guild id that holds the tables. + table_begin (str): The beginning of a table name to search with. Can be empty. + + Returns: + list[str]: List of table names that start with table_begin. + """ + # if string empty, return all table names + if not table_begin: + return highscores_data.get_guild(guild_id).get_tables().keys() + + # else find all tables that start with table_begin + tables = [ + table + for table in highscores_data.get_guild(guild_id).get_tables().keys() + if table.lower().startswith(table_begin.lower()) + ] + return tables + + def format_highscore_message(guild: Guild, table: Table) -> str: """ Creates a formatted string for a boss's highscore discord message.