From fc195e40b387f4f9b56cba90cb3903a86e6acc12 Mon Sep 17 00:00:00 2001 From: stijndcl Date: Wed, 22 Jun 2022 01:56:13 +0200 Subject: [PATCH] Fix syncing --- didier/cogs/owner.py | 33 +++++++++++++++++++++++---- didier/data/modals/__init__.py | 0 didier/data/modals/custom_commands.py | 20 ++++++++++++++++ didier/didier.py | 7 ------ 4 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 didier/data/modals/__init__.py create mode 100644 didier/data/modals/custom_commands.py diff --git a/didier/cogs/owner.py b/didier/cogs/owner.py index f87dc5e..531d0b8 100644 --- a/didier/cogs/owner.py +++ b/didier/cogs/owner.py @@ -1,12 +1,14 @@ from typing import Optional import discord +from discord import app_commands from discord.ext import commands 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 class Owner(commands.Cog): @@ -14,6 +16,9 @@ class Owner(commands.Cog): client: Didier + # Slash groups + add_slash = app_commands.Group(name="add", description="Add something new to the database") + def __init__(self, client: Didier): self.client = client @@ -26,14 +31,20 @@ class Owner(commands.Cog): @commands.command(name="Sync") async def sync(self, ctx: commands.Context, guild: Optional[discord.Guild] = None): """Sync all application-commands in Discord""" - await self.client.tree.sync(guild=guild) + if guild is not None: + self.client.tree.copy_global_to(guild=guild) + await self.client.tree.sync(guild=guild) + else: + self.client.tree.clear_commands(guild=None) + await self.client.tree.sync() + await ctx.message.add_reaction("🔄") @commands.group(name="Add", case_insensitive=True, invoke_without_command=False) - async def add(self, ctx: commands.Context): - """Command group for [add X] commands""" + async def add_msg(self, ctx: commands.Context): + """Command group for [add X] message commands""" - @add.command(name="Custom") + @add_msg.command(name="Custom") async def add_custom(self, ctx: commands.Context, name: str, *, response: str): """Add a new custom command""" async with self.client.db_session as session: @@ -44,7 +55,7 @@ class Owner(commands.Cog): await ctx.reply("Er bestaat al een commando met deze naam.") await self.client.reject_message(ctx.message) - @add.command(name="Alias") + @add_msg.command(name="Alias") async def add_alias(self, ctx: commands.Context, command: str, alias: str): """Add a new alias for a custom command""" async with self.client.db_session as session: @@ -58,6 +69,18 @@ class Owner(commands.Cog): await ctx.reply("Er bestaat al een commando met deze naam.") await self.client.reject_message(ctx.message) + @add_slash.command(name="custom", description="Add a custom command") + async def add_custom_slash(self, interaction: discord.Interaction): + """Slash command to add a custom command""" + if not self.client.is_owner(interaction.user): + return interaction.response.send_message( + "Je hebt geen toestemming om dit commando uit te voeren.", ephemeral=True + ) + + await interaction.response.defer(ephemeral=True) + modal = CreateCustomCommand() + await interaction.response.send_message(modal) + @commands.group(name="Edit") async def edit(self, ctx: commands.Context): """Command group for [edit X] commands""" diff --git a/didier/data/modals/__init__.py b/didier/data/modals/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/didier/data/modals/custom_commands.py b/didier/data/modals/custom_commands.py new file mode 100644 index 0000000..8e6aa97 --- /dev/null +++ b/didier/data/modals/custom_commands.py @@ -0,0 +1,20 @@ +import traceback + +import discord + + +class CreateCustomCommand(discord.ui.Modal, title="Custom Command"): + """Modal shown to visually create custom commands""" + + name = discord.ui.TextInput(label="Name", placeholder="Name of the command...") + + response = discord.ui.TextInput( + label="Response", style=discord.TextStyle.long, placeholder="Response of the command...", max_length=2000 + ) + + async def on_submit(self, interaction: discord.Interaction) -> None: + await interaction.response.send_message("Submitted", ephemeral=True) + + async def on_error(self, interaction: discord.Interaction, error: Exception) -> None: + await interaction.response.send_message("Errored", ephemeral=True) + traceback.print_tb(error.__traceback__) diff --git a/didier/didier.py b/didier/didier.py index 3cd2648..a36e57c 100644 --- a/didier/didier.py +++ b/didier/didier.py @@ -45,13 +45,6 @@ class Didier(commands.Bot): await self._load_initial_extensions() await self._load_directory_extensions("didier/cogs") - # Sync application commands to the test guild - for guild in settings.DISCORD_TEST_GUILDS: - guild_object = discord.Object(id=guild) - - self.tree.copy_global_to(guild=guild_object) - await self.tree.sync(guild=guild_object) - async def _load_initial_extensions(self): """Load all extensions that should be loaded before the others""" for extension in self.initial_extensions: