Add the add presence request.
build_develop / build (push) Successful in 1m33s

This commit is contained in:
2023-12-20 21:45:20 -05:00
parent 09ff620e51
commit fbd8419ccf
+108 -9
View File
@@ -4,8 +4,10 @@ Contains commands and views for request commands.
import nextcord import nextcord
from nextcord.ext import commands from nextcord.ext import commands
from sqlalchemy.orm import Session
from config import DakinyConfig from config import DakinyConfig
from database_setup.sql_data_classes import Presences, engine
class RequestCog(commands.Cog, name="Request Commands"): class RequestCog(commands.Cog, name="Request Commands"):
@@ -25,12 +27,46 @@ class RequestCog(commands.Cog, name="Request Commands"):
Parent slash command for help. Never called. Parent slash command for help. Never called.
""" """
@request.subcommand(
description="Request to add a presence to the bot's presence list."
)
async def add_presence(
self,
interaction: nextcord.Interaction,
presence: str = nextcord.SlashOption(
required=True,
min_length=1,
max_length=100,
description="The presence you want to add.",
),
):
"""
Sends a message to admins to request to add a presence.
Args:
interaction (nextcord.Interaction): Interaction that invoked the command.
presence (str): The presence to add.
"""
config: DakinyConfig = self.bot.config
content = f'<@{interaction.user.id}> would like to add presence"{presence}."'
view = PresenceChange(presence)
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
)
@request.subcommand(description="Request to change your Dakiny nickname") @request.subcommand(description="Request to change your Dakiny nickname")
async def change_nickname( async def change_nickname(
self, self,
interaction: nextcord.Interaction, interaction: nextcord.Interaction,
new_nickname: str = nextcord.SlashOption( new_nickname: str = nextcord.SlashOption(
required=True, min_length=2, max_length=32, description="Your requested new nickname." required=True,
min_length=2,
max_length=32,
description="Your requested new nickname.",
), ),
): ):
""" """
@@ -44,8 +80,12 @@ 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_nickname}."' content = f'<@{interaction.user.id}> would like to change their username to "{new_nickname}."'
view = UsernameRequest(interaction.user.id, 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 self.bot.get_channel(config.params.admin_channel_id).send(
await interaction.response.send_message("Request sent to admins.", ephemeral=True) content=content, view=view
)
await interaction.response.send_message(
"Request sent to admins.", ephemeral=True
)
class UsernameRequest(nextcord.ui.View): class UsernameRequest(nextcord.ui.View):
@@ -53,13 +93,19 @@ class UsernameRequest(nextcord.ui.View):
View provides approve and deny buttons and action for a username change request. 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): def __init__(
super().__init__(timeout=timeout, auto_defer=auto_defer) self,
user_id: int,
new_nickname: str,
):
super().__init__(timeout=None, auto_defer=True)
self.user_id = user_id self.user_id = user_id
self.new_nickname = new_nickname self.new_nickname = new_nickname
@nextcord.ui.button(label="Approve", style=nextcord.ButtonStyle.green) @nextcord.ui.button(label="Approve", style=nextcord.ButtonStyle.green)
async def approve(self, button: nextcord.ui.Button, interaction: nextcord.Interaction): async def approve(
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
):
""" """
Approve button for a nickname change request. Changes the target's nickname to self.new_nickname. Approve button for a nickname change request. Changes the target's nickname to self.new_nickname.
Catches an error if the target as a higher role than the bot. Catches an error if the target as a higher role than the bot.
@@ -73,7 +119,10 @@ class UsernameRequest(nextcord.ui.View):
if user is not None: if user is not None:
try: try:
await user.edit(nick=self.new_nickname) await user.edit(nick=self.new_nickname)
content = interaction.message.content + f"\nApproved by <@{interaction.user.id}>." content = (
interaction.message.content
+ f"\nApproved by <@{interaction.user.id}>."
)
await interaction.response.edit_message(content=content, view=None) await interaction.response.edit_message(content=content, view=None)
except nextcord.errors.Forbidden: except nextcord.errors.Forbidden:
await interaction.response.send_message( await interaction.response.send_message(
@@ -84,10 +133,13 @@ class UsernameRequest(nextcord.ui.View):
interaction.message.content interaction.message.content
+ "\n\nFailed. User has too high permissions for bot to change nickname." + "\n\nFailed. User has too high permissions for bot to change nickname."
) )
await interaction.followup.edit_message(interaction.message.id, content=content, view=None) await interaction.followup.edit_message(
interaction.message.id, content=content, view=None
)
else: else:
await interaction.response.edit_message( await interaction.response.edit_message(
content="An error occured while changing the user's nickname.", view=None content="An error occured while changing the user's nickname.",
view=None,
) )
@nextcord.ui.button(label="Deny", style=nextcord.ButtonStyle.red) @nextcord.ui.button(label="Deny", style=nextcord.ButtonStyle.red)
@@ -101,3 +153,50 @@ class UsernameRequest(nextcord.ui.View):
""" """
content = interaction.message.content + f"\nDenied by <@{interaction.user.id}>." content = interaction.message.content + f"\nDenied by <@{interaction.user.id}>."
await interaction.response.edit_message(content=content, view=None) await interaction.response.edit_message(content=content, view=None)
class PresenceChange(nextcord.ui.View):
"""
View provides approve and deny buttons and action for a username change request.
"""
def __init__(
self,
presence: str,
):
super().__init__(timeout=None, auto_defer=True)
self.presence = presence
@nextcord.ui.button(label="Approve", style=nextcord.ButtonStyle.green)
async def approve(
self, button: nextcord.ui.Button, interaction: nextcord.Interaction
):
"""
Approve button for a presence add request. Adds the presence to the database.
Removes the view from the interaction message.
Args:
button (nextcord.ui.Button): Unused.
interaction (nextcord.Interaction): The interaction of the user clicking the button.
"""
with Session(engine) as session:
session.add(Presences(Presence=self.presence))
session.commit()
content = (
interaction.message.content + f"\nApproved by <@{interaction.user.id}>."
)
await interaction.response.edit_message(content=content, view=None)
@nextcord.ui.button(label="Deny", style=nextcord.ButtonStyle.red)
async def deny(self, button: nextcord.ui.Button, interaction: nextcord.Interaction):
"""
Deny button for a presence add request. Just removes the view and logs who denied the request.
Args:
button (nextcord.ui.Button): Unused.
interaction (nextcord.Interaction): The interaction of the user clicking the button.
"""
content = interaction.message.content + f"\nDenied by <@{interaction.user.id}>."
await interaction.response.edit_message(content=content, view=None)