From 3cfc87b7e1e980a690ed02fcbf551b674a011241 Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Mon, 17 May 2021 18:14:33 +0200 Subject: [PATCH] fetching from db & dataclass --- cogs/events.py | 7 ++- data/database_classes/__init__.py | 0 data/database_classes/custom_commands.py | 15 +++++ functions/database/custom_commands.py | 74 ++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 data/database_classes/__init__.py create mode 100644 data/database_classes/custom_commands.py create mode 100644 functions/database/custom_commands.py diff --git a/cogs/events.py b/cogs/events.py index 5c25224..77b51f9 100644 --- a/cogs/events.py +++ b/cogs/events.py @@ -3,7 +3,7 @@ import datetime import discord from discord.ext import commands from functions import checks, easterEggResponses -from functions.database import stats, muttn +from functions.database import stats, muttn, custom_commands import pytz import time import traceback @@ -75,6 +75,11 @@ class Events(commands.Cog): if eER: await message.channel.send(eER) + # Check for custom commands + custom = custom_commands.is_custom_command(message.content) + if custom: + await message.channel.send(custom.response) + # Earn XP & Message count stats.sentMessage(message) diff --git a/data/database_classes/__init__.py b/data/database_classes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/data/database_classes/custom_commands.py b/data/database_classes/custom_commands.py new file mode 100644 index 0000000..42421b5 --- /dev/null +++ b/data/database_classes/custom_commands.py @@ -0,0 +1,15 @@ +from attr import dataclass + + +@dataclass +class CustomCommand: + """ + Class to store custom commands being triggered + """ + id: int = None + name: str = None + response: str = None + alias_used: str = None + + def __nonzero__(self): + return self.id is not None diff --git a/functions/database/custom_commands.py b/functions/database/custom_commands.py new file mode 100644 index 0000000..43e7240 --- /dev/null +++ b/functions/database/custom_commands.py @@ -0,0 +1,74 @@ +from data.database_classes.custom_commands import CustomCommand +from functions.database import utils + + +def is_custom_command(message: str) -> CustomCommand: + """ + Check if a message triggers a custom command + These use "?" as a prefix + """ + # Message didn't call a custom command + if not message.startswith("?"): + return CustomCommand() + + # Ignore capitals & spaces, strip off prefix + message = message.lower().replace(" ", "")[1:] + + by_name = _find_by_name(message) + + # Command was found by its name + if by_name: + return CustomCommand(*by_name) + + # Check if a command exists with this alias instead + return CustomCommand(*_find_by_alias(message), alias_used=message) + + +def _find_by_name(message): + """ + Find a command by its name + """ + connection = utils.connect() + cursor = connection.cursor() + + cursor.execute("SELECT response FROM custom_commands WHERE name = %s", message) + + return cursor.fetchone() + + +def _find_by_alias(message): + """ + Find a command by one of its aliases + """ + connection = utils.connect() + cursor = connection.cursor() + + cursor.execute("SELECT command FROM custom_command_aliases WHERE alias = %s", message) + + res = cursor.fetchone() + + # No command matched this alias + if not res: + return () + + cursor.execute("SELECT response FROM custom_commands WHERE id = %s", res) + + return cursor.fetchone() + + +def is_name_free(name) -> bool: + """ + Check if a name is already in use by a command + Includes aliases + """ + connection = utils.connect() + cursor = connection.cursor() + + cursor.execute("SELECT id from custom_commands WHERE name = %s", name) + + if cursor.fetchone(): + return False + + cursor.execute("SELECT command FROM custom_command_aliases WHERE alias = %s", name) + + return len(cursor.fetchone()) != 0