""" 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.table import Table # pylint: disable=import-error 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)