""" Contains commands and views for request commands. """ import nextcord from nextcord.ext import commands from config import DakinyConfig class RequestCog(commands.Cog, name="Request Commands"): """ Class holds request commands. """ def __init__(self, bot) -> None: """ Default constructor for RequestCog class. Does nothing. """ self.bot: commands.Bot = bot @nextcord.slash_command() async def request(self, interaction: nextcord.Interaction): """ Parent slash command for help. Never called. """ @request.subcommand(description="Request to change your Dakiny nickname") async def change_nickname( self, interaction: nextcord.Interaction, new_nickname: str = nextcord.SlashOption( required=True, min_length=2, max_length=32, description="Your requested new nickname." ), ): """ Sends a message to admins to request a nickname change. Args: interaction (nextcord.Interaction): Interaction that invoked the command. new_username(str): The username to change to. """ config: DakinyConfig = self.bot.config content = f'<@{interaction.user.id}> would like to change their username to "{new_nickname}."' view = UsernameRequest(interaction.user.id, new_nickname) await self.bot.get_channel(config.params.admin_channel_id).send(content=content, view=view) await interaction.response.send_message("Request sent to admins.", ephemeral=True) class UsernameRequest(nextcord.ui.View): """ View provides approve and deny buttons and action for a username change request. """ def __init__(self, user_id: int, new_nickname: str, *, timeout: float | None = 180, auto_defer: bool = True): super().__init__(timeout=timeout, auto_defer=auto_defer) self.user_id = user_id self.new_nickname = new_nickname @nextcord.ui.button(label="Approve", style=nextcord.ButtonStyle.green) async def approve(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): user = interaction.guild.get_member(self.user_id) if user is not None: user.edit(nick=self.new_nickname) content = interaction.message.content + f" Approved by <@{interaction.user.id}.>" await interaction.response.edit_message(content=content, view=None) else: await interaction.response.edit_message( content="An error occured while changing the user's nickname.", view=None ) @nextcord.ui.button(label="Deny", style=nextcord.ButtonStyle.red) async def deny(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): content = interaction.message.content + f" Denied by <@{interaction.user.id}.>" await interaction.response.edit_message(content=content, view=None)