diff --git a/bot/commands.py b/bot/commands.py index d0d3150..ae18518 100644 --- a/bot/commands.py +++ b/bot/commands.py @@ -7,7 +7,13 @@ from discord import app_commands from bot.config import message_patterns, update_patterns from bot.log import logger from bot.utils import compile_stats, fetch_game_stats, perform_search -from database import add_pattern, add_user_to_blacklist, is_user_blacklisted +from database import ( + add_aka_response, + search_aka, + add_pattern, + add_user_to_blacklist, + is_user_blacklisted, +) GUILD_ID = 1110531063161299074 @@ -32,6 +38,21 @@ async def setup(bot): bot.tree.on_error = on_tree_error + @bot.tree.command( + name="add_aka_message", + description="Add a new aka message to the database.", + guild=discord.Object(id=GUILD_ID), + ) + @app_commands.checks.has_permissions(administrator=True) + async def add_aka_message( + interaction: discord.Interaction, aka: str, response: str + ): + """Slash command to add a new aka pattern to the database.""" + add_aka_response(aka, response) + await interaction.response.send_message( + f"Pattern added!\n**AKA:** `{aka}`\n**Response:** `{response}`" + ) + @bot.tree.command( name="add_pattern", description="Add a new message pattern to the database.", @@ -96,9 +117,35 @@ async def setup(bot): await interaction.response.send_message(stats_message, ephemeral=True) + @bot.tree.command( + name="aka", + description="Check if the input matches any predefined aka patterns.", + guild=discord.Object(id=GUILD_ID), + ) + async def aka(interaction: discord.Interaction, input: str): + """ + Slash command to check if the input matches any predefined aka patterns. + """ + # Check if the user is blacklisted + if is_user_blacklisted(interaction.user.id): + await interaction.response.send_message( + "You are blacklisted from using this command.", ephemeral=True + ) + return + + # Search the database for a match + response = search_aka(input) + + if response: + await interaction.response.send_message(response, ephemeral=False) + else: + await interaction.response.send_message( + "No matching aka patterns found.", ephemeral=True + ) + @bot.tree.command( name="meme", - description="Check if the input matches any predefined memess.", + description="Check if the input matches any predefined memes.", guild=discord.Object(id=GUILD_ID), ) async def meme(interaction: discord.Interaction, input: str): diff --git a/database/__init__.py b/database/__init__.py index 497905e..f37952a 100644 --- a/database/__init__.py +++ b/database/__init__.py @@ -126,3 +126,33 @@ def is_user_blacklisted(user_id: int) -> bool: conn.close() return result is not None + + +def add_aka_response(aka: str, response: str) -> None: + """ + Insert a new AKA/response pair into the database. + """ + conn = sqlite3.connect(DB_PATH) + cursor = conn.cursor() + + cursor.execute( + "INSERT INTO aka_list (aka, response) VALUES (?, ?)", (aka, response) + ) + + conn.commit() + conn.close() + + +def search_aka(keyword: str) -> str | None: + conn = sqlite3.connect(DB_PATH) + cursor = conn.cursor() + + # Exact match (case-insensitive) + cursor.execute( + "SELECT response FROM aka_list WHERE LOWER(aka) = LOWER(?) LIMIT 1", (keyword,) + ) + + row = cursor.fetchone() + conn.close() + + return row[0] if row else None diff --git a/database/schema.sql b/database/schema.sql index f548607..d7c610a 100644 --- a/database/schema.sql +++ b/database/schema.sql @@ -16,3 +16,9 @@ CREATE TABLE IF NOT EXISTS black_list ( date_assigned TEXT, reason TEXT ); + +CREATE TABLE IF NOT EXISTS aka_list ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + aka TEXT NOT NULL, + response TEXT NOT NULL +);