Should have handled username updating code in the command, didn't need to create new functions for the command.
This commit is contained in:
+8
-28
@@ -153,15 +153,19 @@ class Category:
|
|||||||
return self._scores_map[user]
|
return self._scores_map[user]
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def remove_score(self, score: str) -> None:
|
def remove_score(self, user: str) -> Score:
|
||||||
"""
|
"""
|
||||||
Removes a user's score from dictionary storage.
|
Removes a user's score from dictionary storage.
|
||||||
|
|
||||||
Args:
|
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)
|
if user in self._scores_map:
|
||||||
del score
|
return self._scores_map.pop(user)
|
||||||
|
return None
|
||||||
|
|
||||||
def to_dict(self) -> dict:
|
def to_dict(self) -> dict:
|
||||||
"""
|
"""
|
||||||
@@ -188,27 +192,3 @@ class Category:
|
|||||||
str: string of json representation of Category
|
str: string of json representation of Category
|
||||||
"""
|
"""
|
||||||
return json.dumps(self.to_dict(), indent=4)
|
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
|
|
||||||
|
|||||||
@@ -209,23 +209,3 @@ class Guild:
|
|||||||
str: string of json representation of Guild
|
str: string of json representation of Guild
|
||||||
"""
|
"""
|
||||||
return json.dumps(self.to_dict(), indent=4)
|
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
|
|
||||||
|
|||||||
@@ -162,24 +162,3 @@ class Table:
|
|||||||
str: string of json representation of Table
|
str: string of json representation of Table
|
||||||
"""
|
"""
|
||||||
return json.dumps(self.to_dict(), indent=4)
|
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
|
|
||||||
|
|||||||
@@ -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:
|
Args:
|
||||||
interaction (nextcord.Interaction): Interaction that invoked the command
|
interaction (nextcord.Interaction): Interaction that invoked the command
|
||||||
|
|||||||
@@ -54,18 +54,18 @@ class UpdateUsernameVerifyView(nextcord.ui.View):
|
|||||||
content += f"\nApproved by <@{interaction.user.id}>."
|
content += f"\nApproved by <@{interaction.user.id}>."
|
||||||
await interaction.edit(content=content, view=None)
|
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 table_updated:
|
||||||
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(
|
await send_highscore_message(
|
||||||
interaction.guild.get_channel(guild.highscore_channel), guild, table
|
interaction.guild.get_channel(guild.highscore_channel), guild, table
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user