From d2f33959ed475414fb4ee3b31fa957ca84059217 Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Sat, 13 Aug 2022 21:46:41 -0400 Subject: [PATCH] Create remove_user command. --- cogs/commands/data_management.py | 45 +++++++++++++++++++ cogs/commands/views/update_username_verify.py | 11 ++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/cogs/commands/data_management.py b/cogs/commands/data_management.py index 8ab86d7..d63222b 100644 --- a/cogs/commands/data_management.py +++ b/cogs/commands/data_management.py @@ -6,6 +6,8 @@ from nextcord.ext import commands import nextcord from highscores import highscores_data # pylint: disable=import-error +from utilities.utils import send_highscore_message # pylint: disable=import-error + from .views.update_username_verify import UpdateUsernameVerifyView @@ -53,6 +55,49 @@ class DataManagementCog(commands.Cog, name="Table Management"): "You shouldn't be using this message.", ephemeral=True ) + @nextcord.slash_command( + description="Removes all of a user's scores. Warning: data is destroyed, use with case.", + default_member_permissions=nextcord.Permissions(administrator=True), + ) + async def remove_user( + self, + interaction: nextcord.Interaction, + username: str = nextcord.SlashOption( + description="User to remove scores for. Note: This is not always a user's discord name." + ), + ): + """ + Searches all of guild's scores data for any score from the user. Removes. Updates table + message is user was removed from that table. + + Args: + interaction (nextcord.Interaction): Interaction that invoked the command + username (str): User to remove from highscores + """ + guild = highscores_data.get_guild(interaction.guild_id) + + user_was_found: bool = False + + # remove user from all tables and categories + for table in guild.tables: + table_updated: bool = False + for category in table.categories: + # user has a score in category + if category.remove_score(username): + user_was_found = True + table_updated = True + + if table_updated: + await send_highscore_message( + interaction.guild.get_channel(guild.highscore_channel), guild, table + ) + + if not user_was_found: + await interaction.send(f'User "{username}" was not found.', ephemeral=True) + else: + await interaction.send(f'User "{username}" was removed from all tables.') + highscores_data.save_guild_data(guild) + @nextcord.slash_command(description="Request a username update. Admins must approve.") async def update_username( self, diff --git a/cogs/commands/views/update_username_verify.py b/cogs/commands/views/update_username_verify.py index b6ffca9..4695865 100644 --- a/cogs/commands/views/update_username_verify.py +++ b/cogs/commands/views/update_username_verify.py @@ -54,6 +54,8 @@ class UpdateUsernameVerifyView(nextcord.ui.View): content += f"\nApproved by <@{interaction.user.id}>." await interaction.edit(content=content, view=None) + user_was_found: bool = False + # update username in all tables and categories for table in guild.tables: table_updated: bool = False @@ -64,10 +66,17 @@ class UpdateUsernameVerifyView(nextcord.ui.View): score.user = new_username category.add_score(score) table_updated = True + user_was_found = True if table_updated: await send_highscore_message( interaction.guild.get_channel(guild.highscore_channel), guild, table ) - highscores_data.save_guild_data(guild) + if not user_was_found: + await interaction.send( + f'User "{old_username}" was not found. Inform user to correct username.', + ephemeral=True, + ) + else: + highscores_data.save_guild_data(guild)