From d4e19284f0d3fd4254a2d6635fadd32cbc5f550f Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Fri, 12 Aug 2022 22:55:19 -0400 Subject: [PATCH] Create view for verifyng username update request. --- cogs/commands/data_management.py | 6 +- cogs/commands/views/update_username_verify.py | 71 +++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 cogs/commands/views/update_username_verify.py diff --git a/cogs/commands/data_management.py b/cogs/commands/data_management.py index 9d28f37..8aec22a 100644 --- a/cogs/commands/data_management.py +++ b/cogs/commands/data_management.py @@ -67,9 +67,9 @@ class DataManagement(commands.Cog, name="Table Management"): verify_channel = interaction.guild.get_channel(guild.verification_channel) embed: nextcord.Embed = nextcord.Embed(title="Username change request.") - embed.add_field(name="Old username", value=old_username) - embed.add_field(name="New username", value=new_username) + embed.add_field(name="Old Username", value=old_username) + embed.add_field(name="New Username", value=new_username) await verify_channel.send( - content=f"Username change request from <@{interaction.user.id}>", embed=embed + content=f"Username change request from <@{interaction.user.id}>.", embed=embed ) await interaction.send("Request sent to admins for verification.", ephemeral=True) diff --git a/cogs/commands/views/update_username_verify.py b/cogs/commands/views/update_username_verify.py new file mode 100644 index 0000000..faa7f16 --- /dev/null +++ b/cogs/commands/views/update_username_verify.py @@ -0,0 +1,71 @@ +"""View for allowing a username update.""" + +import nextcord + +from highscores import highscores_data # pylint: disable=import-error +from utilities.utils import send_highscore_message # pylint: disable=import-error + + +class UpdateUsernameVerifyView(nextcord.ui.View): + """Defines view for allowing a username update.""" + + def __init__(self): + """ + Default constructor for UpdateUsernameVerifyView. Calls super constructor with no timeout. + """ + super().__init__(timeout=None) + + @nextcord.ui.button( + label="Deny", style=nextcord.ButtonStyle.red, custom_id="deny-username-update" + ) + async def deny_button( + self, button: nextcord.ui.Button, interaction: nextcord.Interaction + ): # pylint: disable=unused-argument + """ + Button for denying username update. Deletes message. + + Args: + button (nextcord.ui.Button): unused + interaction (nextcord.Interaction): Interaction with the button + """ + await interaction.message.delete() + + @nextcord.ui.button( + label="Approve", style=nextcord.ButtonStyle.green, custom_id="approve-username-update" + ) + async def approve_button( + self, button: nextcord.ui.Button, interaction: nextcord.Interaction + ): # pylint: disable=unused-argument + """ + Button for approving username update. Tells guild data to update username, then updates + message to show it is complete. + + Args: + button (nextcord.ui.Button): unused + interaction (nextcord.Interaction): Interaction with the button + """ + embed_content = interaction.message.embeds[0].to_dict() + old_username: str = embed_content["Old Username"] + new_username: str = embed_content["New Username"] + + guild = highscores_data.get_guild(interaction.guild_id) + + content = interaction.message.content + content += f"\nApproved by <@{interaction.user.id}>." + await interaction.edit(content=content) + + tables = guild.update_username(old_username, new_username) + + # 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 + )