Should have handled username updating code in the command, didn't need to create new functions for the command.

This commit is contained in:
alwayscompiling
2022-08-13 21:26:50 -04:00
parent 73e350c845
commit 652533d071
5 changed files with 24 additions and 84 deletions
+8 -28
View File
@@ -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
-20
View File
@@ -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
-21
View File
@@ -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
+2 -1
View File
@@ -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
+14 -14
View File
@@ -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)