mirror of https://github.com/stijndcl/didier
Invoke custom commands
parent
fd57b5a79b
commit
efdc966611
|
@ -34,8 +34,7 @@ async def create_alias(session: AsyncSession, command: str, alias: str) -> Custo
|
||||||
raise NoResultFoundException
|
raise NoResultFoundException
|
||||||
|
|
||||||
# Check if the alias exists (either as an alias or as a name)
|
# Check if the alias exists (either as an alias or as a name)
|
||||||
alias_instance = await get_command(session, alias)
|
if await get_command(session, alias) is not None:
|
||||||
if alias_instance is not None:
|
|
||||||
raise DuplicateInsertException
|
raise DuplicateInsertException
|
||||||
|
|
||||||
alias_instance = CustomCommandAlias(alias=alias, indexed_alias=clean_name(alias), command=command_instance)
|
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]:
|
async def get_command(session: AsyncSession, message: str) -> Optional[CustomCommand]:
|
||||||
"""Try to get a command out of a message"""
|
"""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)
|
message = clean_name(message)
|
||||||
return (await get_command_by_name(session, message)) or (await get_command_by_alias(session, message))
|
return (await get_command_by_name(session, message)) or (await get_command_by_alias(session, message))
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ from discord.ext import commands
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
|
|
||||||
import settings
|
import settings
|
||||||
|
from database.crud import custom_commands
|
||||||
from database.engine import DBSession
|
from database.engine import DBSession
|
||||||
from didier.utils.discord.prefix import get_prefix
|
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
|
"""Check if the message tries to invoke a custom command
|
||||||
If it does, send the reply associated with it
|
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):
|
if not message.content.startswith(settings.DISCORD_CUSTOM_COMMAND_PREFIX):
|
||||||
return False
|
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:
|
async def on_command_error(self, context: commands.Context, exception: commands.CommandError, /) -> None:
|
||||||
"""Event triggered when a regular command errors"""
|
"""Event triggered when a regular command errors"""
|
||||||
# If developing, print everything to stdout so you don't have to
|
# If developing, print everything to stdout so you don't have to
|
||||||
|
|
Loading…
Reference in New Issue