111 lines
4.4 KiB
Python
111 lines
4.4 KiB
Python
"""
|
|
Contains various utlity functions for highscores bot
|
|
"""
|
|
|
|
import nextcord
|
|
from highscores import highscores_data # pylint: disable=import-error
|
|
from highscores import highscores_message_map # pylint: disable=import-error
|
|
|
|
|
|
def format_highscore_message(boss_name: str, guild_id: int):
|
|
"""
|
|
returns a formatted string for a boss's highscore
|
|
@param boss: the dictionary object for the boss
|
|
@param guild_id: the guild id the message is for
|
|
@return the formatted string
|
|
"""
|
|
ret_string = f"**{boss_name}**\n```"
|
|
|
|
# Gather all categories and add to string
|
|
for key, value in highscores_data.get_categories_data(guild_id, boss_name):
|
|
ret_string += f"{key}".ljust(26, " ")
|
|
ret_string += "\n"
|
|
|
|
highscore_size = highscores_data.get_config_highscore_size()
|
|
|
|
# list rankings in a single line. Start with first place for each category and then continue
|
|
for i in range(highscore_size):
|
|
for key, value in highscores_data.get_categories_data(guild_id, boss_name):
|
|
if len(value) > i:
|
|
score_pair = value[i]
|
|
|
|
# pad username to max length.
|
|
username: str = score_pair[0].ljust(
|
|
highscores_data.get_guild_username_length(guild_id), " "
|
|
)
|
|
score: str = str(score_pair[1])
|
|
|
|
# pad score to correct length
|
|
# MM:SS.ms is 7 characters long
|
|
if highscores_data.get_config_time_record(key):
|
|
# 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(26, " ")
|
|
else:
|
|
ret_string += f"{i+1}: submit your score".ljust(26, " ")
|
|
ret_string += "\n"
|
|
|
|
return ret_string + "```"
|
|
|
|
|
|
async def send_highscore_message(
|
|
channel: nextcord.TextChannel, boss_name: str, guild_id: int
|
|
) -> nextcord.Message:
|
|
"""
|
|
Function takes care of checking if a message exists for a boss, then
|
|
creating and sending/editing the message
|
|
@param channel: the channel to send the message in/search for message id
|
|
@param boss: the boss to send the message about
|
|
@param guild_id: the guild id to send message to
|
|
@return: The nextcord message
|
|
"""
|
|
highscore_string = format_highscore_message(boss_name, guild_id)
|
|
|
|
message_id = highscores_data.get_table_message_id(guild_id, boss_name)
|
|
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
|
|
highscores_data.set_table_message_id(guild_id, boss_name, 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] = boss_name
|
|
|
|
return message
|
|
|
|
|
|
async def submit_score(
|
|
interaction: nextcord.Interaction, user: str, boss_name: str, category: str, score: str
|
|
):
|
|
"""
|
|
Adds a score to the high scores and sorts. Removes a score if larger than size limit.
|
|
@param boss: the boss to add score to
|
|
@param category: the category to add score to
|
|
@param user: user who submitted score
|
|
@param score: score to add
|
|
"""
|
|
|
|
highscores_data.add_score(interaction.guild_id, boss_name, category, user, score)
|
|
|
|
# edit message
|
|
highscore_channel_id = highscores_data.get_guild_highscores_channel(interaction.guild_id)
|
|
print(f"Editting message {highscore_channel_id}")
|
|
channel = interaction.guild.get_channel(highscore_channel_id)
|
|
|
|
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, boss_name, interaction.guild_id)
|