Create general autofill table and category util functions. Update table class to have get categories function.

This commit is contained in:
alwayscompiling
2022-11-17 14:50:48 -05:00
parent 1858f0c37d
commit 06b46ef27c
3 changed files with 69 additions and 13 deletions
+10
View File
@@ -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.
+6 -13
View File
@@ -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)
)
+53
View File
@@ -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.