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
|
2022-07-13 22:54:16 +02:00
|
|
|
from overrides import overrides
|
2022-06-22 01:56:13 +02:00
|
|
|
|
2022-06-25 01:57:52 +02:00
|
|
|
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
|
|
|
|
2022-06-25 01:57:52 +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(
|
2022-06-25 01:57:52 +02:00
|
|
|
label="Response", style=discord.TextStyle.long, placeholder="Hmm?", max_length=2000
|
2022-06-22 01:56:13 +02:00
|
|
|
)
|
|
|
|
|
2022-06-25 01:57:52 +02:00
|
|
|
client: Didier
|
|
|
|
|
|
|
|
def __init__(self, client: Didier, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.client = client
|
|
|
|
|
2022-07-13 22:54:16 +02:00
|
|
|
@overrides
|
2022-06-25 01:57:52 +02:00
|
|
|
async def on_submit(self, interaction: discord.Interaction):
|
2022-07-25 20:33:20 +02:00
|
|
|
async with self.client.postgres_session as session:
|
2022-06-30 21:49:45 +02:00
|
|
|
command = await create_command(session, str(self.name.value), str(self.response.value))
|
2022-06-25 01:57:52 +02:00
|
|
|
|
|
|
|
await interaction.response.send_message(f"Successfully created ``{command.name}``.", ephemeral=True)
|
|
|
|
|
2022-07-13 22:54:16 +02:00
|
|
|
@overrides
|
2022-06-25 01:57:52 +02:00
|
|
|
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
|
2022-07-13 22:54:16 +02:00
|
|
|
|
|
|
|
Fills in the current values as defaults for QOL
|
2022-06-25 01:57:52 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
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(
|
2022-06-25 01:57:52 +02:00
|
|
|
discord.ui.TextInput(
|
|
|
|
label="Response", placeholder="Hmm?", default=response, style=discord.TextStyle.long, max_length=2000
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-07-13 22:54:16 +02:00
|
|
|
@overrides
|
2022-06-25 01:57:52 +02:00
|
|
|
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])
|
|
|
|
|
2022-07-25 20:33:20 +02:00
|
|
|
async with self.client.postgres_session as session:
|
2022-06-30 21:39:13 +02:00
|
|
|
await edit_command(session, self.original_name, name_field.value, response_field.value)
|
2022-06-25 01:57:52 +02:00
|
|
|
|
|
|
|
await interaction.response.send_message(f"Successfully edited ``{self.original_name}``.", ephemeral=True)
|
2022-06-22 01:56:13 +02:00
|
|
|
|
2022-07-13 22:54:16 +02:00
|
|
|
@overrides
|
2022-06-25 01:57:52 +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__)
|