From a2b5b10625126b7e98643af9fe40569c9e004047 Mon Sep 17 00:00:00 2001 From: salmonik Date: Sat, 18 Apr 2026 00:24:50 +0200 Subject: refactor + preparing bot for public use Signed-off-by: salmonik --- cogs/trigger.py | 64 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 32 deletions(-) (limited to 'cogs/trigger.py') diff --git a/cogs/trigger.py b/cogs/trigger.py index 4c6a185..0e76cef 100644 --- a/cogs/trigger.py +++ b/cogs/trigger.py @@ -24,25 +24,25 @@ class Trigger(commands.Cog): def __init__(self, bot): self.bot = bot self.db_check() - + trigger_group = discord.SlashCommandGroup("trigger", "trigger commands") @staticmethod def db_check(): if not os.path.exists('data'): os.makedirs('data') - + db_exists = os.path.exists(DB_PATH) if not db_exists: logger.warning("Triggers database not found. Creating new database.") - + try: conn = sqlite3.connect(DB_PATH) c = conn.cursor() - + # Enable foreign key constraints c.execute('PRAGMA foreign_keys = ON') - + # Create triggers table c.execute(''' CREATE TABLE IF NOT EXISTS triggers ( @@ -51,7 +51,7 @@ class Trigger(commands.Cog): created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP )''') - + # Create trigger_keywords table with foreign key constraint c.execute(''' CREATE TABLE IF NOT EXISTS trigger_keywords ( @@ -62,7 +62,7 @@ class Trigger(commands.Cog): FOREIGN KEY (trigger_id) REFERENCES triggers(id) ON DELETE CASCADE, UNIQUE(trigger_id, keyword) )''') - + # Create trigger_responses table with foreign key constraint c.execute(''' CREATE TABLE IF NOT EXISTS trigger_responses ( @@ -72,29 +72,29 @@ class Trigger(commands.Cog): created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (trigger_id) REFERENCES triggers(id) ON DELETE CASCADE )''') - + # Create indexes for better query performance c.execute('CREATE INDEX IF NOT EXISTS idx_trigger_keywords_trigger_id ON trigger_keywords(trigger_id)') c.execute('CREATE INDEX IF NOT EXISTS idx_trigger_keywords_keyword ON trigger_keywords(keyword)') c.execute('CREATE INDEX IF NOT EXISTS idx_trigger_responses_trigger_id ON trigger_responses(trigger_id)') - + # Run integrity check integrity_check = c.execute('PRAGMA integrity_check').fetchone() if integrity_check and integrity_check[0] == 'ok': logger.info("Triggers database integrity check passed") else: logger.warning(f"Triggers database integrity check: {integrity_check}") - + conn.commit() - + except sqlite3.Error as e: logger.error(f"Triggers database error: {e}") raise - + finally: if 'conn' in locals(): conn.close() - + if not db_exists: logger.info("Triggers database initialized successfully") @@ -102,7 +102,7 @@ class Trigger(commands.Cog): async def on_message(self, message): if message.author == self.bot.user: return - + await self.trigger(message) @staticmethod @@ -128,13 +128,13 @@ class Trigger(commands.Cog): c = conn.cursor() c.execute("SELECT name FROM triggers") return [row[0] for row in c.fetchall()] - + @trigger_group.command( description="Create a new trigger", ) async def create( - self, - ctx: discord.ApplicationContext, + self, + ctx: discord.ApplicationContext, name = discord.Option( str, "the name of the trigger", @@ -146,9 +146,9 @@ class Trigger(commands.Cog): c = conn.cursor() c.execute("INSERT OR IGNORE INTO triggers (name) VALUES (?)", (name,)) conn.commit() - + await ctx.respond("trigger added") - + @trigger_group.command( description="Add a trigger or response to the name", ) @@ -192,10 +192,10 @@ class Trigger(commands.Cog): if resp: c.execute("INSERT INTO trigger_responses (trigger_id, response_text) VALUES (?, ?)", (trigger_id, resp)) conn.commit() - + await ctx.respond("trigger updated") logger.info(f"Trigger updated: {name}") - + @trigger_group.command( description="Remove a trigger or response from the name", ) @@ -233,7 +233,7 @@ class Trigger(commands.Cog): if response: c.execute("DELETE FROM trigger_responses WHERE trigger_id=? AND response_text=?", (trigger_id, response)) conn.commit() - + await ctx.respond("trigger removed") logger.info(f"Trigger removed: {name}") @@ -241,8 +241,8 @@ class Trigger(commands.Cog): description="Delete the whole trigger", ) async def delete( - self, - ctx: discord.ApplicationContext, + self, + ctx: discord.ApplicationContext, name = discord.Option( str, "the name of the trigger", @@ -262,23 +262,23 @@ class Trigger(commands.Cog): c.execute("DELETE FROM trigger_responses WHERE trigger_id=?", (trigger_id,)) c.execute("DELETE FROM triggers WHERE id=?", (trigger_id,)) conn.commit() - + await ctx.respond("trigger deleted") logger.info(f"Trigger deleted: {name}") - - + + @trigger_group.command( description="List all triggers", ) async def list( - self, + self, ctx: discord.ApplicationContext, name = discord.Option( str, "the name of the trigger", autocomplete=discord.utils.basic_autocomplete(get_type), required=True - ) + ) ): with get_connection() as conn: c = conn.cursor() @@ -315,7 +315,7 @@ class Trigger(commands.Cog): description="how to use trigger group?" ) async def help( - self, + self, ctx: discord.ApplicationContext, ): await ctx.respond("Trigger create - creates a group for triggers/responses\n" @@ -324,7 +324,7 @@ class Trigger(commands.Cog): "Trigger delete - deletes the whole group\n" "Trigger list - lists triggers/responses of a group\n" "Trigger help - display this message\n") - - + + def setup(bot): bot.add_cog(Trigger(bot)) -- cgit v1.2.3