didier/didier/data/modals/custom_commands.py

74 lines
2.7 KiB
Python
Raw Normal View History

2022-06-22 01:56:13 +02:00
import traceback
2022-06-30 21:39:13 +02:00
import typing
2022-06-22 01:56:13 +02:00
import discord
from database.crud.custom_commands import create_command, edit_command
from didier import Didier
2022-06-22 01:56:13 +02:00
2022-07-11 22:23:38 +02:00
__all__ = ["CreateCustomCommand", "EditCustomCommand"]
2022-06-22 01:56:13 +02:00
class CreateCustomCommand(discord.ui.Modal, title="Create Custom Command"):
"""Modal to create new custom commands"""
name: discord.ui.TextInput = discord.ui.TextInput(label="Name", placeholder="Didier")
2022-06-22 01:56:13 +02:00
2022-06-22 02:09:16 +02:00
response: discord.ui.TextInput = discord.ui.TextInput(
label="Response", style=discord.TextStyle.long, placeholder="Hmm?", max_length=2000
2022-06-22 01:56:13 +02:00
)
client: Didier
def __init__(self, client: Didier, *args, **kwargs):
super().__init__(*args, **kwargs)
self.client = client
async def on_submit(self, interaction: discord.Interaction):
async with self.client.db_session as session:
2022-06-30 21:49:45 +02:00
command = await create_command(session, str(self.name.value), str(self.response.value))
await interaction.response.send_message(f"Successfully created ``{command.name}``.", ephemeral=True)
async def on_error(self, interaction: discord.Interaction, error: Exception): # type: ignore
await interaction.response.send_message("Something went wrong.", ephemeral=True)
traceback.print_tb(error.__traceback__)
class EditCustomCommand(discord.ui.Modal, title="Edit Custom Command"):
"""Modal to edit an existing custom command
Fills in the current values as defaults
"""
name: discord.ui.TextInput
response: discord.ui.TextInput
original_name: str
client: Didier
def __init__(self, client: Didier, name: str, response: str, *args, **kwargs):
super().__init__(*args, **kwargs)
self.original_name = name
self.client = client
2022-06-27 19:53:44 +02:00
self.add_item(discord.ui.TextInput(label="Name", placeholder="Didier", default=name))
self.add_item(
discord.ui.TextInput(
label="Response", placeholder="Hmm?", default=response, style=discord.TextStyle.long, max_length=2000
)
)
async def on_submit(self, interaction: discord.Interaction):
2022-06-30 21:39:13 +02:00
name_field = typing.cast(discord.ui.TextInput, self.children[0])
response_field = typing.cast(discord.ui.TextInput, self.children[1])
async with self.client.db_session as session:
2022-06-30 21:39:13 +02:00
await edit_command(session, self.original_name, name_field.value, response_field.value)
await interaction.response.send_message(f"Successfully edited ``{self.original_name}``.", ephemeral=True)
2022-06-22 01:56:13 +02:00
async def on_error(self, interaction: discord.Interaction, error: Exception): # type: ignore
await interaction.response.send_message("Something went wrong.", ephemeral=True)
2022-06-22 01:56:13 +02:00
traceback.print_tb(error.__traceback__)