mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-10 17:15:47 +02:00
Editing of custom commands, add posix flags
This commit is contained in:
parent
257eae6fa7
commit
d6a560851b
9 changed files with 184 additions and 22 deletions
|
|
@ -8,7 +8,8 @@ from database.crud import custom_commands
|
|||
from database.exceptions.constraints import DuplicateInsertException
|
||||
from database.exceptions.not_found import NoResultFoundException
|
||||
from didier import Didier
|
||||
from didier.data.modals.custom_commands import CreateCustomCommand
|
||||
from didier.data.modals.custom_commands import CreateCustomCommand, EditCustomCommand
|
||||
from didier.data.flags.owner import EditCustomFlags
|
||||
|
||||
|
||||
class Owner(commands.Cog):
|
||||
|
|
@ -18,6 +19,7 @@ class Owner(commands.Cog):
|
|||
|
||||
# Slash groups
|
||||
add_slash = app_commands.Group(name="add", description="Add something new to the database")
|
||||
edit_slash = app_commands.Group(name="edit", description="Edit an existing database entry")
|
||||
|
||||
def __init__(self, client: Didier):
|
||||
self.client = client
|
||||
|
|
@ -29,6 +31,11 @@ class Owner(commands.Cog):
|
|||
# pylint: disable=W0236 # Pylint thinks this can't be async, but it can
|
||||
return await self.client.is_owner(ctx.author)
|
||||
|
||||
@commands.command(name="Error")
|
||||
async def _error(self, ctx: commands.Context):
|
||||
"""Raise an exception for debugging purposes"""
|
||||
raise Exception("Debug")
|
||||
|
||||
@commands.command(name="Sync")
|
||||
async def sync(self, ctx: commands.Context, guild: Optional[discord.Guild] = None):
|
||||
"""Sync all application-commands in Discord"""
|
||||
|
|
@ -77,14 +84,43 @@ class Owner(commands.Cog):
|
|||
"Je hebt geen toestemming om dit commando uit te voeren.", ephemeral=True
|
||||
)
|
||||
|
||||
# await interaction.response.defer(ephemeral=True)
|
||||
modal = CreateCustomCommand()
|
||||
modal = CreateCustomCommand(self.client)
|
||||
await interaction.response.send_modal(modal)
|
||||
|
||||
@commands.group(name="Edit")
|
||||
async def edit(self, ctx: commands.Context):
|
||||
@commands.group(name="Edit", case_insensitive=True, invoke_without_command=False)
|
||||
async def edit_msg(self, ctx: commands.Context):
|
||||
"""Command group for [edit X] commands"""
|
||||
|
||||
@edit_msg.command(name="Custom")
|
||||
async def edit_custom_msg(self, ctx: commands.Context, command: str, *, flags: EditCustomFlags):
|
||||
"""Edit an existing custom command"""
|
||||
async with self.client.db_session as session:
|
||||
try:
|
||||
await custom_commands.edit_command(session, command, flags.name, flags.response)
|
||||
return await self.client.confirm_message(ctx.message)
|
||||
except NoResultFoundException:
|
||||
await ctx.reply(f"Geen commando gevonden voor ``{command}``.")
|
||||
return await self.client.reject_message(ctx.message)
|
||||
|
||||
@edit_slash.command(name="custom", description="Edit a custom command")
|
||||
@app_commands.describe(command="The name of the command to edit")
|
||||
async def edit_custom_slash(self, interaction: discord.Interaction, command: str):
|
||||
"""Slash command to edit a custom command"""
|
||||
if not await self.client.is_owner(interaction.user):
|
||||
return interaction.response.send_message(
|
||||
"Je hebt geen toestemming om dit commando uit te voeren.", ephemeral=True
|
||||
)
|
||||
|
||||
async with self.client.db_session as session:
|
||||
_command = await custom_commands.get_command(session, command)
|
||||
if _command is None:
|
||||
return await interaction.response.send_message(
|
||||
f"Geen commando gevonden voor ``{command}``.", ephemeral=True
|
||||
)
|
||||
|
||||
modal = EditCustomCommand(self.client, _command.name, _command.response)
|
||||
await interaction.response.send_modal(modal)
|
||||
|
||||
|
||||
async def setup(client: Didier):
|
||||
"""Load the cog"""
|
||||
|
|
|
|||
1
didier/data/flags/__init__.py
Normal file
1
didier/data/flags/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
|||
from .posix import PosixFlags
|
||||
10
didier/data/flags/owner.py
Normal file
10
didier/data/flags/owner.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
from typing import Optional
|
||||
|
||||
from didier.data.flags import PosixFlags
|
||||
|
||||
|
||||
class EditCustomFlags(PosixFlags):
|
||||
"""Flags for the edit custom command"""
|
||||
|
||||
name: Optional[str] = None
|
||||
response: Optional[str] = None
|
||||
14
didier/data/flags/posix.py
Normal file
14
didier/data/flags/posix.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from discord.ext import commands
|
||||
|
||||
|
||||
class PosixFlags(commands.FlagConverter, delimiter=" ", prefix="--"):
|
||||
"""Base class to add POSIX-like flags to commands
|
||||
|
||||
Example usage:
|
||||
>>> class Flags(PosixFlags):
|
||||
>>> name: str
|
||||
>>> async def command(ctx, *, flags: Flags):
|
||||
>>> ...
|
||||
This can now be called in Discord as
|
||||
command --name here-be-name
|
||||
"""
|
||||
|
|
@ -2,19 +2,66 @@ import traceback
|
|||
|
||||
import discord
|
||||
|
||||
from database.crud.custom_commands import create_command, edit_command
|
||||
from didier import Didier
|
||||
|
||||
class CreateCustomCommand(discord.ui.Modal, title="Custom Command"):
|
||||
"""Modal shown to visually create custom commands"""
|
||||
|
||||
name: discord.ui.TextInput = discord.ui.TextInput(label="Name", placeholder="Name of the command")
|
||||
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")
|
||||
|
||||
response: discord.ui.TextInput = discord.ui.TextInput(
|
||||
label="Response", style=discord.TextStyle.long, placeholder="Response of the command", max_length=2000
|
||||
label="Response", style=discord.TextStyle.long, placeholder="Hmm?", max_length=2000
|
||||
)
|
||||
|
||||
async def on_submit(self, interaction: discord.Interaction) -> None:
|
||||
await interaction.response.send_message("Submitted", ephemeral=True)
|
||||
client: Didier
|
||||
|
||||
async def on_error(self, interaction: discord.Interaction, error: Exception) -> None: # type: ignore
|
||||
await interaction.response.send_message("Errored", ephemeral=True)
|
||||
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:
|
||||
command = await create_command(session, self.name.value, 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
|
||||
|
||||
self.name = self.add_item(discord.ui.TextInput(label="Name", placeholder="Didier", default=name))
|
||||
self.response = 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):
|
||||
async with self.client.db_session as session:
|
||||
await edit_command(session, self.original_name, self.name.value, self.response.value)
|
||||
|
||||
await interaction.response.send_message(f"Successfully edited ``{self.original_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__)
|
||||
|
|
|
|||
|
|
@ -129,8 +129,9 @@ class Didier(commands.Bot):
|
|||
|
||||
async def on_command_error(self, context: commands.Context, exception: commands.CommandError, /) -> None:
|
||||
"""Event triggered when a regular command errors"""
|
||||
# If developing, print everything to stdout so you don't have to
|
||||
# check the logs all the time
|
||||
# Print everything to the logs/stderr
|
||||
await super().on_command_error(context, exception)
|
||||
|
||||
# If developing, do nothing special
|
||||
if settings.SANDBOX:
|
||||
print(traceback.format_exc(), file=sys.stderr)
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue