From 03c7a9303a622b4863026e657c0413f8f2842dd1 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Tue, 22 Nov 2022 17:02:18 -0500 Subject: [PATCH] Small rewrite to consolidate autofill_user function. Sort imports in message_management.py. --- cogs/commands/message_management.py | 26 +++++++++---------------- utilities/utils.py | 30 ++++++++++++++++++----------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/cogs/commands/message_management.py b/cogs/commands/message_management.py index 8d7259b..5a57f7e 100644 --- a/cogs/commands/message_management.py +++ b/cogs/commands/message_management.py @@ -4,12 +4,12 @@ Commands for managing/displaying the highscores messages. from typing import Optional -from nextcord.ext import commands import nextcord +from nextcord.ext import commands 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, autofill_tables # pylint: disable=import-error +from utilities.utils import autofill_tables, send_highscore_message # pylint: disable=import-error class MessageManagementCog(commands.Cog, name="Message Management"): @@ -40,17 +40,13 @@ class MessageManagementCog(commands.Cog, name="Message Management"): """ guild = highscores_data.get_guild(interaction.guild_id) - highscores_channel: nextcord.TextChannel = interaction.guild.get_channel( - guild.highscore_channel - ) + highscores_channel: nextcord.TextChannel = interaction.guild.get_channel(guild.highscore_channel) await interaction.response.defer(ephemeral=True) for table in guild.tables: try: - table_message: nextcord.Message = await highscores_channel.fetch_message( - table.message_id - ) + table_message: nextcord.Message = await highscores_channel.fetch_message(table.message_id) await table_message.delete() except nextcord.NotFound: # silently ignore that messages are not found. They got deleted already. @@ -85,9 +81,9 @@ class MessageManagementCog(commands.Cog, name="Message Management"): # Error checking for tables existing # TODO fill out response string once functionality created. error_response = "No tables have been created for this guild. Please create some tables." - assert ( - len(highscores_data.get_guild(interaction.guild_id).tables) > 0 - ), await interaction.send(error_response, ephemeral=True) + assert len(highscores_data.get_guild(interaction.guild_id).tables) > 0, await interaction.send( + error_response, ephemeral=True + ) # awaiting response because posting scores takes a while await interaction.response.defer(ephemeral=True) @@ -109,9 +105,7 @@ class MessageManagementCog(commands.Cog, name="Message Management"): async def refresh_table( self, interaction: nextcord.Interaction, - table_name: Optional[str] = nextcord.SlashOption( - description="Name of table.", required=True - ), + table_name: Optional[str] = nextcord.SlashOption(description="Name of table.", required=True), ) -> None: """ Refreshes a table given by user. @@ -140,6 +134,4 @@ class MessageManagementCog(commands.Cog, name="Message Management"): table (str): String to run autocomplete compared to. """ - await interaction.response.send_autocomplete( - autofill_tables(interaction.guild_id, table_name) - ) + await interaction.response.send_autocomplete(autofill_tables(interaction.guild_id, table_name)) diff --git a/utilities/utils.py b/utilities/utils.py index 4418fb1..954d224 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -3,11 +3,12 @@ Contains various utlity functions for highscores bot """ import nextcord -from highscores import highscores_data, highscores_message_map # pylint: disable=import-error -from classes.score import Score # pylint: disable=import-error -from classes.guild import Guild # pylint: disable=import-error + from classes.category import Category # pylint: disable=import-error +from classes.guild import Guild # pylint: disable=import-error +from classes.score import Score # pylint: disable=import-error from classes.table import Table # pylint: disable=import-error +from highscores import highscores_data, highscores_message_map # pylint: disable=import-error def autofill_categories(guild_id: int, table_name: str, category_begin: str) -> "list[str]": @@ -86,26 +87,33 @@ def autofill_tables(guild_id: int, table_begin: str) -> "list[str]": return tables -def autofill_table_users(guild_id: int, table_name: str, user_begin: str) -> "list[str]": +def autofill_users(guild_id: int, table_name: str, category_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. + Returns list of users that match user_begin. Either all users in table or just users in category if category is + None. Might be sexpensive due to unioning all users in table. Args: guild_id (int): Guild id corresponding to table and category. - table_name (str): Table to look in for categories. + table_name (str): Table to look in for category. + category_name (str): Category to look in for users. Can be None if not looking for category. 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 + # If no category name, get all users in table. + if category_name is None: + 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()) + 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) + users_list = list(user_set) + else: + 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: