Update utils function args to use new data classes instead of various args.

This commit is contained in:
alwayscompiling
2022-08-03 19:28:47 -04:00
parent 80e4602621
commit 027da38574
2 changed files with 74 additions and 45 deletions
+18 -1
View File
@@ -1,6 +1,9 @@
"""View for verification buttons""" """View for verification buttons"""
import nextcord import nextcord
from data.score import Score, ScoreTime # pylint: disable=import-error
from highscores import highscores_data # pylint: disable=import-error
from utilities.utils import submit_score # pylint: disable=import-error from utilities.utils import submit_score # pylint: disable=import-error
@@ -39,7 +42,21 @@ class VerificationView(nextcord.ui.View):
user = field["value"] user = field["value"]
try: try:
await submit_score(interaction, user, boss_name, category, score) submission_guild = highscores_data.get_guild(interaction.guild_id)
submission_table = submission_guild.get_table(boss_name)
submission_category = submission_table.get_category(category)
if submission_category.time_record:
submission_score: ScoreTime = ScoreTime(user, float(score))
else:
submission_score: Score = Score(user, int(score))
await submit_score(
interaction,
submission_guild,
submission_table,
submission_category,
submission_score,
)
await interaction.message.edit(f"Approved by <@{interaction.user.id}>.", view=None) await interaction.message.edit(f"Approved by <@{interaction.user.id}>.", view=None)
except UnboundLocalError: except UnboundLocalError:
await interaction.send("Instruct user to submit with all fields.") await interaction.send("Instruct user to submit with all fields.")
+56 -44
View File
@@ -3,42 +3,47 @@ Contains various utlity functions for highscores bot
""" """
import nextcord import nextcord
from highscores import highscores_data # pylint: disable=import-error
from highscores import highscores_message_map # pylint: disable=import-error from highscores import highscores_message_map # pylint: disable=import-error
from data.score import Score # pylint: disable=import-error
from data.guild import Guild # pylint: disable=import-error
from data.category import Category # pylint: disable=import-error
from data.table import Table # pylint: disable=import-error
def format_highscore_message(boss_name: str, guild_id: int): def format_highscore_message(guild: Guild, table: Table) -> str:
""" """
returns a formatted string for a boss's highscore Creates a formatted string for a boss's highscore discord message.
@param boss: the dictionary object for the boss This formatted string is in discord's code block to ensure monospace text.
@param guild_id: the guild id the message is for Loops through each category to build the message one place at a time.
@return the formatted string
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"**{boss_name}**\n```" ret_string = f"**{table.name}**\n```"
# Gather all categories and add to string # Gather all categories and add to string
for category_name in highscores_data.get_table_categories(guild_id, boss_name): for category in table.categories:
ret_string += f"{category_name}".ljust(26, " ") ret_string += f"{category.name}".ljust(26, " ")
ret_string += "\n" ret_string += "\n"
highscore_size = highscores_data.get_config_highscore_size() highscore_size = guild.highscore_size
# list rankings in a single line. Start with first place for each category and then continue # list rankings in a single line. Start with first place for each category and then continue
for i in range(highscore_size): for i in range(highscore_size):
for category_name, category_data in highscores_data.get_categories_data( for category in table.categories:
guild_id, boss_name if len(category.scores) > i:
):
if len(category_data) > i:
# pad username to max length. # pad username to max length.
username: str = list(category_data.keys())[i].ljust( username: str = category.scores[i].user.ljust(guild.username_length, " ")
highscores_data.get_guild_username_length(guild_id), " "
) score: str = category.scores[i].get_str()
# turns category data value into a string. Very janky.
score: str = str(list(category_data.values())[i][0])
# pad score to correct length # pad score to correct length
# MM:SS.ms is 7 characters long # MM:SS.ms is 7 characters long
if highscores_data.get_config_time_record(category_name): if category.time_record:
# if there are single digit minutes, add a space for formatting. # if there are single digit minutes, add a space for formatting.
if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2: if score.find(":") and len(score.split(":", maxsplit=1)[0]) < 2:
score = " " + score score = " " + score
@@ -54,19 +59,23 @@ def format_highscore_message(boss_name: str, guild_id: int):
async def send_highscore_message( async def send_highscore_message(
channel: nextcord.TextChannel, boss_name: str, guild_id: int channel: nextcord.TextChannel, guild: Guild, table: Table
) -> nextcord.Message: ) -> nextcord.Message:
""" """
Function takes care of checking if a message exists for a boss, then Function takes care of checking if a message exists for a table.
creating and sending/editing the message Creates and sends/edits 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) 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: try:
message: nextcord.Message = await channel.fetch_message(message_id) message: nextcord.Message = await channel.fetch_message(message_id)
await message.edit(content=highscore_string) await message.edit(content=highscore_string)
@@ -74,38 +83,41 @@ async def send_highscore_message(
# message doesn't exist. # message doesn't exist.
message: nextcord.Message = await channel.send(highscore_string) message: nextcord.Message = await channel.send(highscore_string)
message_id = message.id message_id = message.id
highscores_data.set_table_message_id(guild_id, boss_name, message_id) table.message_id = message_id
# save message -> boss_name in message_map # save message -> boss_name in message_map
# get guild id from channel # get guild id from channel
# channel should come from the guild that is requesting this highscores message anyways. # channel should come from the guild that is requesting this highscores message anyways.
# check if guild is in message map already # check if guild is in message map already
if not channel.guild.id in highscores_message_map: if not channel.guild.id in highscores_message_map:
highscores_message_map[channel.guild.id] = {} highscores_message_map[channel.guild.id] = {}
highscores_message_map[channel.guild.id][message_id] = boss_name highscores_message_map[channel.guild.id][message_id] = table.name
return message return message
async def submit_score( async def submit_score(
interaction: nextcord.Interaction, user: str, boss_name: str, category: str, score: str interaction: nextcord.Interaction, guild: Guild, table: Table, category: Category, score: Score
): ) -> None:
""" """
Adds a score to the high scores and sorts. Removes a score if larger than size limit. Adds passed score to table. Then updates table message by calling send_highscore_message.
@param boss: the boss to add score to Ensures that a highscores channel exists.
@param category: the category to add score to
@param user: user who submitted score Args:
@param score: score to add 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
""" """
highscores_data.add_score(interaction.guild_id, boss_name, category, user, score) table.get_category(category).add_score(score)
# edit message # edit message
highscore_channel_id = highscores_data.get_guild_highscores_channel(interaction.guild_id) print(f"Editting message {guild.highscore_channel}")
print(f"Editting message {highscore_channel_id}") channel = interaction.guild.get_channel(guild.highscore_channel)
channel = interaction.guild.get_channel(highscore_channel_id)
error_response = "Registered Highscores channel does not exist or was never registered." error_response = "Registered Highscores channel does not exist or was never registered."
error_response += 'Register with "?register" command.' error_response += 'Register with "?register" command.'
assert channel is not None, await interaction.send(error_response, ephemeral=True) assert channel is not None, await interaction.send(error_response, ephemeral=True)
await send_highscore_message(channel, boss_name, interaction.guild_id) await send_highscore_message(channel, guild, table)