Flush out view with approve and deny actions.

This commit is contained in:
2023-07-29 18:18:31 -04:00
parent 9cae7d002a
commit 7789e679ce
+40 -5
View File
@@ -1,7 +1,9 @@
""" """
Contains commands to request things from admins. Contains commands and views for request commands.
""" """
from typing import Optional
import nextcord import nextcord
from nextcord.ext import commands from nextcord.ext import commands
@@ -26,9 +28,15 @@ class RequestCog(commands.Cog, name="Request Commands"):
""" """
@request.subcommand(description="Request to change your Dakiny nickname") @request.subcommand(description="Request to change your Dakiny nickname")
async def change_username(self, interaction: nextcord.Interaction, new_username: str): 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 username change. Sends a message to admins to request a nickname change.
Args: Args:
interaction (nextcord.Interaction): Interaction that invoked the command. interaction (nextcord.Interaction): Interaction that invoked the command.
@@ -36,6 +44,33 @@ class RequestCog(commands.Cog, name="Request Commands"):
""" """
config: DakinyConfig = self.bot.config config: DakinyConfig = self.bot.config
content = f'<@{interaction.user.id}> would like to change their username to "{new_username}"' content = f'<@{interaction.user.id}> would like to change their username to "{new_nickname}"'
await self.bot.get_channel(config.params.admin_channel_id).send(content=content) 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) 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, id: int, new_nickname: str, *, timeout: float | None = 180, auto_defer: bool = True):
super().__init__(timeout=timeout, auto_defer=auto_defer)
self.id = 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.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.send_message(content=content, view=None)
else:
await interaction.response.send_message("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.send_message(content=content, view=None)