237 lines
8.9 KiB
Python
237 lines
8.9 KiB
Python
"""
|
|
Contains various utlity functions for highscores bot
|
|
"""
|
|
|
|
import nextcord
|
|
|
|
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]":
|
|
"""
|
|
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.
|
|
"""
|
|
|
|
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 categories_list
|
|
|
|
# else find all categories that start with category_begin
|
|
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.
|
|
|
|
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.
|
|
"""
|
|
|
|
tables_list = highscores_data.get_guild(guild_id).get_tables().keys()
|
|
|
|
# if string empty, return all table names
|
|
if not table_begin:
|
|
return tables_list
|
|
|
|
# else find all tables that start with table_begin
|
|
tables = [table for table in tables_list if table.lower().startswith(table_begin.lower())]
|
|
return tables
|
|
|
|
|
|
def autofill_users(guild_id: int, table_name: str, category_name: str, user_begin: str) -> "list[str]":
|
|
"""
|
|
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 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.
|
|
"""
|
|
|
|
# If no category name, get all users in table.
|
|
if category_name is None:
|
|
user_set: 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)
|
|
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:
|
|
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.
|
|
This formatted string is in discord's code block to ensure monospace text.
|
|
Loops through each category to build the message one place at a time.
|
|
|
|
Args:
|
|
guild (Guild): Guild object that is posting the message.
|
|
table (Table): Table object that contains the contents of the message.
|
|
|
|
Returns:
|
|
str: Formatted string for discord message
|
|
"""
|
|
ret_string = f"**{table.name}**\n```"
|
|
|
|
# Gather all categories and add to string
|
|
for category in table.categories:
|
|
ret_string += f"{category.name}".ljust(guild.category_pad_length, " ")
|
|
ret_string += "\n"
|
|
|
|
# list rankings in a single line. Start with first place for each category and then continue
|
|
for i in range(table.size):
|
|
for category in table.categories:
|
|
if len(category.scores) > i:
|
|
# pad username to max length.
|
|
# have to convert category.scores into list to make it subscriptable. no idea why.
|
|
username: str = list(category.scores)[i].user.ljust(guild.username_length, " ")
|
|
|
|
score: str = list(category.scores)[i].get_str()
|
|
|
|
# pad score to correct length
|
|
# MM:SS.ms is 7 characters long
|
|
if category.time_record:
|
|
# if there are single digit minutes, add a space for formatting.
|
|
if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2:
|
|
score = " " + score
|
|
else:
|
|
score = score.rjust(7, " ")
|
|
|
|
ret_string += f"{i+1}: {username} - {score}".ljust(guild.category_pad_length, " ")
|
|
else:
|
|
ret_string += f"{i+1}:".ljust(guild.category_pad_length, " ")
|
|
ret_string += "\n"
|
|
|
|
return ret_string + "```"
|
|
|
|
|
|
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.
|
|
|
|
Args:
|
|
channel (nextcord.TextChannel): Discord channel object where message is sent
|
|
guild (Guild): Guild object for Guild that is posting the message
|
|
table (Table): Table to post to the message
|
|
|
|
Returns:
|
|
nextcord.Message: Message object that was posted with table contents
|
|
"""
|
|
highscore_string = format_highscore_message(guild, table)
|
|
|
|
message_id = table.message_id
|
|
try:
|
|
message: nextcord.Message = await channel.fetch_message(message_id)
|
|
await message.edit(content=highscore_string)
|
|
except nextcord.NotFound:
|
|
# message doesn't exist.
|
|
message: nextcord.Message = await channel.send(highscore_string)
|
|
message_id = message.id
|
|
table.message_id = message_id
|
|
# save message -> boss_name in message_map
|
|
# get guild id from channel
|
|
# channel should come from the guild that is requesting this highscores message anyways.
|
|
# check if guild is in message map already
|
|
if not channel.guild.id in highscores_message_map:
|
|
highscores_message_map[channel.guild.id] = {}
|
|
highscores_message_map[channel.guild.id][message_id] = table.name
|
|
|
|
return message
|
|
|
|
|
|
async def submit_score(
|
|
interaction: nextcord.Interaction, guild: Guild, table: Table, category: Category, score: Score
|
|
) -> None:
|
|
"""
|
|
Adds passed score to table. Then updates table message by calling send_highscore_message.
|
|
Ensures that a highscores channel exists.
|
|
|
|
Args:
|
|
interaction (nextcord.Interaction): Interaction that caused the score submission
|
|
guild (Guild): Guild to submit score to
|
|
table (Table): Table to submit score to
|
|
category (Category): Category to submit score to
|
|
score (Score): Score to submit
|
|
"""
|
|
|
|
category.add_score(score)
|
|
highscores_data.save_guild_data(guild)
|
|
|
|
# edit message
|
|
print(f"Editting message {guild.highscore_channel}")
|
|
channel = interaction.guild.get_channel(guild.highscore_channel)
|
|
|
|
error_response = "Registered Highscores channel does not exist or was never registered."
|
|
error_response += 'Register with "?register" command.'
|
|
assert channel is not None, await interaction.send(error_response, ephemeral=True)
|
|
|
|
await send_highscore_message(channel, guild, table)
|