Create autofill functions for users in category and in table. Add function in category class to return a list of all users.

This commit is contained in:
alwayscompiling
2022-11-22 16:26:31 -05:00
parent 8155fb0c8f
commit a0c5527bf6
2 changed files with 80 additions and 21 deletions
+70 -18
View File
@@ -23,22 +23,46 @@ def autofill_categories(guild_id: int, table_name: str, category_begin: str) ->
Returns:
list[str]: List of all category names that start with category_begin.
"""
categories_list = highscores_data.get_guild(guild_id).get_table(table_name).get_categories().keys()
# 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()
return categories_list
# 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())
]
categories = [category for category in categories_list if category.lower().startswith(category_begin.lower())]
return categories
def autofill_category_users(guild_id: int, table_name: str, category_name: str, user_begin: str) -> "list[str]":
"""
Returns list of users that are in the table and category that also start with the user_begin string.
Args:
guild_id (int): Guild id corresponding to table and category.
table_name (str): Table to look in for category.
category_name (str): Category to look in for users.
user_begin (str): Beginning of user name to search with. Can be empty.
Returns:
list[str]: List of users that start with user_begin.
"""
users_list: "list[str]" = (
highscores_data.get_guild(guild_id).get_table(table_name).get_category(category_name).get_users()
)
# if string empty, return all users
if not user_begin:
return users_list
# else find all users that start with user_begin
users = [user for user in users_list if user.lower().startswith(user_begin.lower())]
return users
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.
@@ -50,19 +74,49 @@ def autofill_tables(guild_id: int, table_begin: str) -> "list[str]":
Returns:
list[str]: List of table names that start with table_begin.
"""
tables_list = highscores_data.get_guild(guild_id).get_tables().keys()
# if string empty, return all table names
if not table_begin:
return highscores_data.get_guild(guild_id).get_tables().keys()
return tables_list
# 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())
]
tables = [table for table in tables_list if table.lower().startswith(table_begin.lower())]
return tables
def autofill_table_users(guild_id: int, table_name: str, user_begin: str) -> "list[str]":
"""
Gathers all users in a table's categories and performs autofill function on it. This is an expensive
function.
Args:
guild_id (int): Guild id corresponding to table and category.
table_name (str): Table to look in for categories.
user_begin (str): Beginning of user name to search with. Can be empty.
Returns:
list[str]: List of users that start with user_begin.
"""
user_set: set
for category in highscores_data.get_guild(guild_id).get_table(table_name).get_categories().values():
user_set = user_set | set(category.get_users())
users_list = list(user_set)
# if string empty, return all users
if not user_begin:
return users_list
# else find all users that start with user_begin
users = [user for user in users_list if user.lower().startswith(user_begin.lower())]
return users
def format_highscore_message(guild: Guild, table: Table) -> str:
"""
Creates a formatted string for a boss's highscore discord message.
@@ -110,9 +164,7 @@ def format_highscore_message(guild: Guild, table: Table) -> str:
return ret_string + "```"
async def send_highscore_message(
channel: nextcord.TextChannel, guild: Guild, table: Table
) -> nextcord.Message:
async def send_highscore_message(channel: nextcord.TextChannel, guild: Guild, table: Table) -> nextcord.Message:
"""
Function takes care of checking if a message exists for a table.
Creates and sends/edits the message.