2022-06-21 20:30:11 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import discord
|
2022-06-22 01:56:13 +02:00
|
|
|
from discord import app_commands
|
2022-06-21 20:30:11 +02:00
|
|
|
from discord.ext import commands
|
|
|
|
|
2022-06-22 00:49:00 +02:00
|
|
|
from database.crud import custom_commands
|
|
|
|
from database.exceptions.constraints import DuplicateInsertException
|
|
|
|
from database.exceptions.not_found import NoResultFoundException
|
2022-06-21 20:30:11 +02:00
|
|
|
from didier import Didier
|
2022-06-25 01:57:52 +02:00
|
|
|
from didier.data.flags.owner import EditCustomFlags
|
2022-07-11 22:23:38 +02:00
|
|
|
from didier.data.modals.custom_commands import CreateCustomCommand, EditCustomCommand
|
2022-06-21 20:30:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Owner(commands.Cog):
|
|
|
|
"""Cog for owner-only commands"""
|
|
|
|
|
|
|
|
client: Didier
|
|
|
|
|
2022-06-22 01:56:13 +02:00
|
|
|
# Slash groups
|
|
|
|
add_slash = app_commands.Group(name="add", description="Add something new to the database")
|
2022-06-25 01:57:52 +02:00
|
|
|
edit_slash = app_commands.Group(name="edit", description="Edit an existing database entry")
|
2022-06-22 01:56:13 +02:00
|
|
|
|
2022-06-21 20:30:11 +02:00
|
|
|
def __init__(self, client: Didier):
|
|
|
|
self.client = client
|
|
|
|
|
2022-06-22 00:49:00 +02:00
|
|
|
async def cog_check(self, ctx: commands.Context) -> bool:
|
|
|
|
"""Global check for every command in this cog, so we don't have to add
|
|
|
|
is_owner() to every single command separately
|
|
|
|
"""
|
2022-06-22 02:09:16 +02:00
|
|
|
# pylint: disable=W0236 # Pylint thinks this can't be async, but it can
|
2022-06-22 00:49:00 +02:00
|
|
|
return await self.client.is_owner(ctx.author)
|
|
|
|
|
2022-06-25 01:57:52 +02:00
|
|
|
@commands.command(name="Error")
|
|
|
|
async def _error(self, ctx: commands.Context):
|
|
|
|
"""Raise an exception for debugging purposes"""
|
|
|
|
raise Exception("Debug")
|
|
|
|
|
2022-06-21 20:30:11 +02:00
|
|
|
@commands.command(name="Sync")
|
|
|
|
async def sync(self, ctx: commands.Context, guild: Optional[discord.Guild] = None):
|
|
|
|
"""Sync all application-commands in Discord"""
|
2022-06-22 01:56:13 +02:00
|
|
|
if guild is not None:
|
|
|
|
self.client.tree.copy_global_to(guild=guild)
|
|
|
|
await self.client.tree.sync(guild=guild)
|
|
|
|
else:
|
|
|
|
await self.client.tree.sync()
|
|
|
|
|
2022-06-21 20:30:11 +02:00
|
|
|
await ctx.message.add_reaction("🔄")
|
|
|
|
|
2022-06-22 00:49:00 +02:00
|
|
|
@commands.group(name="Add", case_insensitive=True, invoke_without_command=False)
|
2022-06-22 01:56:13 +02:00
|
|
|
async def add_msg(self, ctx: commands.Context):
|
|
|
|
"""Command group for [add X] message commands"""
|
2022-06-22 00:49:00 +02:00
|
|
|
|
2022-06-22 01:56:13 +02:00
|
|
|
@add_msg.command(name="Custom")
|
2022-06-22 00:49:00 +02:00
|
|
|
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:
|
|
|
|
try:
|
|
|
|
await custom_commands.create_command(session, name, response)
|
|
|
|
await self.client.confirm_message(ctx.message)
|
|
|
|
except DuplicateInsertException:
|
|
|
|
await ctx.reply("Er bestaat al een commando met deze naam.")
|
|
|
|
await self.client.reject_message(ctx.message)
|
|
|
|
|
2022-06-22 01:56:13 +02:00
|
|
|
@add_msg.command(name="Alias")
|
2022-06-22 00:49:00 +02:00
|
|
|
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:
|
|
|
|
try:
|
|
|
|
await custom_commands.create_alias(session, command, alias)
|
|
|
|
await self.client.confirm_message(ctx.message)
|
|
|
|
except NoResultFoundException:
|
|
|
|
await ctx.reply(f'Geen commando gevonden voor "{command}".')
|
|
|
|
await self.client.reject_message(ctx.message)
|
|
|
|
except DuplicateInsertException:
|
|
|
|
await ctx.reply("Er bestaat al een commando met deze naam.")
|
|
|
|
await self.client.reject_message(ctx.message)
|
|
|
|
|
2022-06-22 01:56:13 +02:00
|
|
|
@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"""
|
2022-06-23 11:09:43 +02:00
|
|
|
if not await self.client.is_owner(interaction.user):
|
2022-06-22 01:56:13 +02:00
|
|
|
return interaction.response.send_message(
|
|
|
|
"Je hebt geen toestemming om dit commando uit te voeren.", ephemeral=True
|
|
|
|
)
|
|
|
|
|
2022-06-25 01:57:52 +02:00
|
|
|
modal = CreateCustomCommand(self.client)
|
2022-06-23 11:09:43 +02:00
|
|
|
await interaction.response.send_modal(modal)
|
2022-06-22 01:56:13 +02:00
|
|
|
|
2022-06-25 01:57:52 +02:00
|
|
|
@commands.group(name="Edit", case_insensitive=True, invoke_without_command=False)
|
|
|
|
async def edit_msg(self, ctx: commands.Context):
|
2022-06-22 00:49:00 +02:00
|
|
|
"""Command group for [edit X] commands"""
|
|
|
|
|
2022-06-25 01:57:52 +02:00
|
|
|
@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)
|
|
|
|
|
2022-06-21 20:30:11 +02:00
|
|
|
|
|
|
|
async def setup(client: Didier):
|
|
|
|
"""Load the cog"""
|
|
|
|
await client.add_cog(Owner(client))
|