Small rewrite to consolidate autofill_user function. Sort imports in message_management.py.

This commit is contained in:
alwayscompiling
2022-11-22 17:02:18 -05:00
parent a0c5527bf6
commit 03c7a9303a
2 changed files with 28 additions and 28 deletions
+9 -17
View File
@@ -4,12 +4,12 @@ Commands for managing/displaying the highscores messages.
from typing import Optional from typing import Optional
from nextcord.ext import commands
import nextcord import nextcord
from nextcord.ext import commands
from cogs.submission.submission_views import SubmissionCreateButton # pylint: disable=import-error from cogs.submission.submission_views import SubmissionCreateButton # pylint: disable=import-error
from highscores import highscores_data # 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"): 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) guild = highscores_data.get_guild(interaction.guild_id)
highscores_channel: nextcord.TextChannel = interaction.guild.get_channel( highscores_channel: nextcord.TextChannel = interaction.guild.get_channel(guild.highscore_channel)
guild.highscore_channel
)
await interaction.response.defer(ephemeral=True) await interaction.response.defer(ephemeral=True)
for table in guild.tables: for table in guild.tables:
try: try:
table_message: nextcord.Message = await highscores_channel.fetch_message( table_message: nextcord.Message = await highscores_channel.fetch_message(table.message_id)
table.message_id
)
await table_message.delete() await table_message.delete()
except nextcord.NotFound: except nextcord.NotFound:
# silently ignore that messages are not found. They got deleted already. # 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 # Error checking for tables existing
# TODO fill out response string once functionality created. # TODO fill out response string once functionality created.
error_response = "No tables have been created for this guild. Please create some tables." error_response = "No tables have been created for this guild. Please create some tables."
assert ( assert len(highscores_data.get_guild(interaction.guild_id).tables) > 0, await interaction.send(
len(highscores_data.get_guild(interaction.guild_id).tables) > 0 error_response, ephemeral=True
), await interaction.send(error_response, ephemeral=True) )
# awaiting response because posting scores takes a while # awaiting response because posting scores takes a while
await interaction.response.defer(ephemeral=True) await interaction.response.defer(ephemeral=True)
@@ -109,9 +105,7 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
async def refresh_table( async def refresh_table(
self, self,
interaction: nextcord.Interaction, interaction: nextcord.Interaction,
table_name: Optional[str] = nextcord.SlashOption( table_name: Optional[str] = nextcord.SlashOption(description="Name of table.", required=True),
description="Name of table.", required=True
),
) -> None: ) -> None:
""" """
Refreshes a table given by user. 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. table (str): String to run autocomplete compared to.
""" """
await interaction.response.send_autocomplete( await interaction.response.send_autocomplete(autofill_tables(interaction.guild_id, table_name))
autofill_tables(interaction.guild_id, table_name)
)
+19 -11
View File
@@ -3,11 +3,12 @@ Contains various utlity functions for highscores bot
""" """
import nextcord 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.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 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]": 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 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 Returns list of users that match user_begin. Either all users in table or just users in category if category is
function. None. Might be sexpensive due to unioning all users in table.
Args: Args:
guild_id (int): Guild id corresponding to table and category. 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. user_begin (str): Beginning of user name to search with. Can be empty.
Returns: Returns:
list[str]: List of users that start with user_begin. 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(): for category in highscores_data.get_guild(guild_id).get_table(table_name).get_categories().values():
user_set = user_set | set(category.get_users()) 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 string empty, return all users
if not user_begin: if not user_begin: