"""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["fields"][0]["value"] new_username: str = embed_content["fields"][1]["value"] guild = highscores_data.get_guild(interaction.guild_id) content = interaction.message.content 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 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 user_was_found = 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 "{old_username}" was not found. Inform user to correct username.', ephemeral=True, ) else: highscores_data.save_guild_data(guild)