Fix syncing

pull/115/head
stijndcl 2022-06-22 01:56:13 +02:00
parent d8192cfa0a
commit fc195e40b3
4 changed files with 48 additions and 12 deletions

View File

@ -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"""

View File

View File

@ -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__)

View File

@ -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: