diff --git a/classes/category.py b/classes/category.py index dd34dcd..a81b608 100644 --- a/classes/category.py +++ b/classes/category.py @@ -153,15 +153,19 @@ class Category: return self._scores_map[user] return None - def remove_score(self, score: str) -> None: + def remove_score(self, user: str) -> Score: """ Removes a user's score from dictionary storage. Args: - score (str): user whose score to remove + user (str): user whose score to remove + + Returns: + Score: User's Score or None """ - score = self._scores_map.pop(score) - del score + if user in self._scores_map: + return self._scores_map.pop(user) + return None def to_dict(self) -> dict: """ @@ -188,27 +192,3 @@ class Category: str: string of json representation of Category """ return json.dumps(self.to_dict(), indent=4) - - def update_username(self, old_username: str, new_username: str) -> bool: - """ - Checks score map for old username. Pops old username to remove from map. - Updates score's username and calls self.add_score with new score to add back - to map and sort. - - Args: - old_username (str): Username to replace - new_username (str): Username to replace with - - Returns: - bool: True indicating a score was updated, False otherwise - """ - - ret_val: bool = False - - if old_username in self._scores_map: - ret_val = True - score: Score = self._scores_map.pop(old_username) - score.user = new_username - self.add_score(score) - - return ret_val diff --git a/classes/guild.py b/classes/guild.py index dc7ede4..30c1177 100644 --- a/classes/guild.py +++ b/classes/guild.py @@ -209,23 +209,3 @@ class Guild: str: string of json representation of Guild """ return json.dumps(self.to_dict(), indent=4) - - def update_username(self, old_username: str, new_username: str) -> "list[Table]": - """ - Collects list of tables that had a username updated. Sends old and new username - to the table function to update username. - - Args: - old_username (str): Username to replace. Sent to Table function - new_username (str): Username to replace with. Sent to Table function - - Returns: - list[Table]: All the tables that had a username updated - """ - updated_tables: "list[Table]" = [] - - for table in self.tables: - if table.update_username(old_username, new_username): - updated_tables.append(table) - - return updated_tables diff --git a/classes/table.py b/classes/table.py index 7b81571..0f2af7e 100644 --- a/classes/table.py +++ b/classes/table.py @@ -162,24 +162,3 @@ class Table: str: string of json representation of Table """ return json.dumps(self.to_dict(), indent=4) - - def update_username(self, old_username: str, new_username: str) -> bool: - """ - Calls Category update_username function for each category. Returns true if a category - was updated to inform if the table message needs rewritten. - - Args: - old_username (str): Username to replace. Sent to Category function - new_username (str): Username to replace with. Sent to Category function - - Returns: - bool: True if category was updated and requires message update - """ - - ret_val: bool = False - - for category in self.categories: - if category.update_username(old_username, new_username): - ret_val = True - - return ret_val diff --git a/cogs/commands/data_management.py b/cogs/commands/data_management.py index 5d9be3b..8ab86d7 100644 --- a/cogs/commands/data_management.py +++ b/cogs/commands/data_management.py @@ -65,7 +65,8 @@ class DataManagementCog(commands.Cog, name="Table Management"): ), ): """ - _summary_ + Creates a request to send to admins for changing a username. Uses + UpdateUsernameVerifyView to do the work. Args: interaction (nextcord.Interaction): Interaction that invoked the command diff --git a/cogs/commands/views/update_username_verify.py b/cogs/commands/views/update_username_verify.py index edfe2ed..b6ffca9 100644 --- a/cogs/commands/views/update_username_verify.py +++ b/cogs/commands/views/update_username_verify.py @@ -54,20 +54,20 @@ class UpdateUsernameVerifyView(nextcord.ui.View): content += f"\nApproved by <@{interaction.user.id}>." await interaction.edit(content=content, view=None) - tables = guild.update_username(old_username, new_username) + # update username in all tables and categories + for table in guild.tables: + table_updated: bool = False + for category in table.categories: + score = category.remove_score(old_username) + if score: + # score exists + score.user = new_username + category.add_score(score) + table_updated = True - # if no tables updated, send informing message and return. - if len(tables) == 0: - await interaction.send( - f'Username "{old_username}" not found. Have user try again with valid username.', - ephemeral=True, - ) - return - - # update all table messages - for table in tables: - await send_highscore_message( - interaction.guild.get_channel(guild.highscore_channel), guild, table - ) + if table_updated: + await send_highscore_message( + interaction.guild.get_channel(guild.highscore_channel), guild, table + ) highscores_data.save_guild_data(guild)