diff --git a/didier/cogs/owner.py b/didier/cogs/owner.py index af50633..f87dc5e 100644 --- a/didier/cogs/owner.py +++ b/didier/cogs/owner.py @@ -3,6 +3,9 @@ from typing import Optional import discord 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 @@ -14,13 +17,51 @@ class Owner(commands.Cog): def __init__(self, client: Didier): self.client = client + 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 + """ + return await self.client.is_owner(ctx.author) + @commands.command(name="Sync") - @commands.is_owner() 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) 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""" + + @add.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: + 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) + + @add.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: + 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) + + @commands.group(name="Edit") + async def edit(self, ctx: commands.Context): + """Command group for [edit X] commands""" + async def setup(client: Didier): """Load the cog""" diff --git a/didier/didier.py b/didier/didier.py index 06786e0..3cd2648 100644 --- a/didier/didier.py +++ b/didier/didier.py @@ -3,7 +3,6 @@ import sys import traceback import discord -from discord import Message from discord.ext import commands from sqlalchemy.ext.asyncio import AsyncSession @@ -35,6 +34,11 @@ class Didier(commands.Bot): command_prefix=get_prefix, case_insensitive=True, intents=intents, activity=activity, status=status ) + @property + def db_session(self) -> AsyncSession: + """Obtain a database session""" + return DBSession() + async def setup_hook(self) -> None: """Hook called once the bot is initialised""" # Load extensions @@ -48,11 +52,6 @@ class Didier(commands.Bot): self.tree.copy_global_to(guild=guild_object) await self.tree.sync(guild=guild_object) - @property - def db_session(self) -> AsyncSession: - """Obtain a database session""" - return DBSession() - async def _load_initial_extensions(self): """Load all extensions that should be loaded before the others""" for extension in self.initial_extensions: @@ -86,11 +85,19 @@ class Didier(commands.Bot): channel = self.get_channel(reference.channel_id) return await channel.fetch_message(reference.message_id) + async def confirm_message(self, message: discord.Message): + """Add a checkmark to a message""" + await message.add_reaction("✅") + + async def reject_message(self, message: discord.Message): + """Add an X to a message""" + await message.add_reaction("❌") + async def on_ready(self): """Event triggered when the bot is ready""" print(settings.DISCORD_READY_MESSAGE) - async def on_message(self, message: Message, /) -> None: + async def on_message(self, message: discord.Message, /) -> None: """Event triggered when a message is sent""" # Ignore messages by bots if message.author.bot: @@ -101,12 +108,12 @@ class Didier(commands.Bot): await message.add_reaction(settings.DISCORD_BOOS_REACT) # Potential custom command - if self._try_invoke_custom_command(message): + if await self._try_invoke_custom_command(message): return await self.process_commands(message) - async def _try_invoke_custom_command(self, message: Message) -> bool: + async def _try_invoke_custom_command(self, message: discord.Message) -> bool: """Check if the message tries to invoke a custom command If it does, send the reply associated with it """