Invoke custom commands

pull/115/head
stijndcl 2022-06-22 00:22:26 +02:00
parent fd57b5a79b
commit efdc966611
2 changed files with 17 additions and 3 deletions

View File

@ -34,8 +34,7 @@ async def create_alias(session: AsyncSession, command: str, alias: str) -> Custo
raise NoResultFoundException
# Check if the alias exists (either as an alias or as a name)
alias_instance = await get_command(session, alias)
if alias_instance is not None:
if await get_command(session, alias) is not None:
raise DuplicateInsertException
alias_instance = CustomCommandAlias(alias=alias, indexed_alias=clean_name(alias), command=command_instance)
@ -47,7 +46,7 @@ async def create_alias(session: AsyncSession, command: str, alias: str) -> Custo
async def get_command(session: AsyncSession, message: str) -> Optional[CustomCommand]:
"""Try to get a command out of a message"""
# Search lowercase & without spaces, and strip the prefix
# Search lowercase & without spaces
message = clean_name(message)
return (await get_command_by_name(session, message)) or (await get_command_by_alias(session, message))

View File

@ -8,6 +8,7 @@ from discord.ext import commands
from sqlalchemy.ext.asyncio import AsyncSession
import settings
from database.crud import custom_commands
from database.engine import DBSession
from didier.utils.discord.prefix import get_prefix
@ -109,9 +110,23 @@ class Didier(commands.Bot):
"""Check if the message tries to invoke a custom command
If it does, send the reply associated with it
"""
# Doesn't start with the custom command prefix
if not message.content.startswith(settings.DISCORD_CUSTOM_COMMAND_PREFIX):
return False
async with self.db_session as session:
# Remove the prefix
content = message.content[len(settings.DISCORD_CUSTOM_COMMAND_PREFIX) :]
command = await custom_commands.get_command(session, content)
# Command found
if command is not None:
await message.reply(command.response, mention_author=False)
return True
# Nothing found
return False
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