aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/trigger.py
diff options
context:
space:
mode:
authorsalmonik <raisersoft@proton.me>2026-04-18 00:24:50 +0200
committersalmonik <raisersoft@proton.me>2026-04-18 00:24:50 +0200
commita2b5b10625126b7e98643af9fe40569c9e004047 (patch)
tree2b59a929d068bc28bbc21e8c8d8836e4d29270c0 /cogs/trigger.py
parentadd mathematical plotting (diff)
downloadsenator-sperma-a2b5b10625126b7e98643af9fe40569c9e004047.tar.gz
senator-sperma-a2b5b10625126b7e98643af9fe40569c9e004047.zip
refactor + preparing bot for public use
Signed-off-by: salmonik <raisersoft@proton.me>
Diffstat (limited to '')
-rw-r--r--cogs/trigger.py64
1 files changed, 32 insertions, 32 deletions
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):
24 def __init__(self, bot): 24 def __init__(self, bot):
25 self.bot = bot 25 self.bot = bot
26 self.db_check() 26 self.db_check()
27 27
28 trigger_group = discord.SlashCommandGroup("trigger", "trigger commands") 28 trigger_group = discord.SlashCommandGroup("trigger", "trigger commands")
29 29
30 @staticmethod 30 @staticmethod
31 def db_check(): 31 def db_check():
32 if not os.path.exists('data'): 32 if not os.path.exists('data'):
33 os.makedirs('data') 33 os.makedirs('data')
34 34
35 db_exists = os.path.exists(DB_PATH) 35 db_exists = os.path.exists(DB_PATH)
36 if not db_exists: 36 if not db_exists:
37 logger.warning("Triggers database not found. Creating new database.") 37 logger.warning("Triggers database not found. Creating new database.")
38 38
39 try: 39 try:
40 conn = sqlite3.connect(DB_PATH) 40 conn = sqlite3.connect(DB_PATH)
41 c = conn.cursor() 41 c = conn.cursor()
42 42
43 # Enable foreign key constraints 43 # Enable foreign key constraints
44 c.execute('PRAGMA foreign_keys = ON') 44 c.execute('PRAGMA foreign_keys = ON')
45 45
46 # Create triggers table 46 # Create triggers table
47 c.execute(''' 47 c.execute('''
48 CREATE TABLE IF NOT EXISTS triggers ( 48 CREATE TABLE IF NOT EXISTS triggers (
@@ -51,7 +51,7 @@ class Trigger(commands.Cog):
51 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 51 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
52 last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP 52 last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP
53 )''') 53 )''')
54 54
55 # Create trigger_keywords table with foreign key constraint 55 # Create trigger_keywords table with foreign key constraint
56 c.execute(''' 56 c.execute('''
57 CREATE TABLE IF NOT EXISTS trigger_keywords ( 57 CREATE TABLE IF NOT EXISTS trigger_keywords (
@@ -62,7 +62,7 @@ class Trigger(commands.Cog):
62 FOREIGN KEY (trigger_id) REFERENCES triggers(id) ON DELETE CASCADE, 62 FOREIGN KEY (trigger_id) REFERENCES triggers(id) ON DELETE CASCADE,
63 UNIQUE(trigger_id, keyword) 63 UNIQUE(trigger_id, keyword)
64 )''') 64 )''')
65 65
66 # Create trigger_responses table with foreign key constraint 66 # Create trigger_responses table with foreign key constraint
67 c.execute(''' 67 c.execute('''
68 CREATE TABLE IF NOT EXISTS trigger_responses ( 68 CREATE TABLE IF NOT EXISTS trigger_responses (
@@ -72,29 +72,29 @@ class Trigger(commands.Cog):
72 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 72 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
73 FOREIGN KEY (trigger_id) REFERENCES triggers(id) ON DELETE CASCADE 73 FOREIGN KEY (trigger_id) REFERENCES triggers(id) ON DELETE CASCADE
74 )''') 74 )''')
75 75
76 # Create indexes for better query performance 76 # Create indexes for better query performance
77 c.execute('CREATE INDEX IF NOT EXISTS idx_trigger_keywords_trigger_id ON trigger_keywords(trigger_id)') 77 c.execute('CREATE INDEX IF NOT EXISTS idx_trigger_keywords_trigger_id ON trigger_keywords(trigger_id)')
78 c.execute('CREATE INDEX IF NOT EXISTS idx_trigger_keywords_keyword ON trigger_keywords(keyword)') 78 c.execute('CREATE INDEX IF NOT EXISTS idx_trigger_keywords_keyword ON trigger_keywords(keyword)')
79 c.execute('CREATE INDEX IF NOT EXISTS idx_trigger_responses_trigger_id ON trigger_responses(trigger_id)') 79 c.execute('CREATE INDEX IF NOT EXISTS idx_trigger_responses_trigger_id ON trigger_responses(trigger_id)')
80 80
81 # Run integrity check 81 # Run integrity check
82 integrity_check = c.execute('PRAGMA integrity_check').fetchone() 82 integrity_check = c.execute('PRAGMA integrity_check').fetchone()
83 if integrity_check and integrity_check[0] == 'ok': 83 if integrity_check and integrity_check[0] == 'ok':
84 logger.info("Triggers database integrity check passed") 84 logger.info("Triggers database integrity check passed")
85 else: 85 else:
86 logger.warning(f"Triggers database integrity check: {integrity_check}") 86 logger.warning(f"Triggers database integrity check: {integrity_check}")
87 87
88 conn.commit() 88 conn.commit()
89 89
90 except sqlite3.Error as e: 90 except sqlite3.Error as e:
91 logger.error(f"Triggers database error: {e}") 91 logger.error(f"Triggers database error: {e}")
92 raise 92 raise
93 93
94 finally: 94 finally:
95 if 'conn' in locals(): 95 if 'conn' in locals():
96 conn.close() 96 conn.close()
97 97
98 if not db_exists: 98 if not db_exists:
99 logger.info("Triggers database initialized successfully") 99 logger.info("Triggers database initialized successfully")
100 100
@@ -102,7 +102,7 @@ class Trigger(commands.Cog):
102 async def on_message(self, message): 102 async def on_message(self, message):
103 if message.author == self.bot.user: 103 if message.author == self.bot.user:
104 return 104 return
105 105
106 await self.trigger(message) 106 await self.trigger(message)
107 107
108 @staticmethod 108 @staticmethod
@@ -128,13 +128,13 @@ class Trigger(commands.Cog):
128 c = conn.cursor() 128 c = conn.cursor()
129 c.execute("SELECT name FROM triggers") 129 c.execute("SELECT name FROM triggers")
130 return [row[0] for row in c.fetchall()] 130 return [row[0] for row in c.fetchall()]
131 131
132 @trigger_group.command( 132 @trigger_group.command(
133 description="Create a new trigger", 133 description="Create a new trigger",
134 ) 134 )
135 async def create( 135 async def create(
136 self, 136 self,
137 ctx: discord.ApplicationContext, 137 ctx: discord.ApplicationContext,
138 name = discord.Option( 138 name = discord.Option(
139 str, 139 str,
140 "the name of the trigger", 140 "the name of the trigger",
@@ -146,9 +146,9 @@ class Trigger(commands.Cog):
146 c = conn.cursor() 146 c = conn.cursor()
147 c.execute("INSERT OR IGNORE INTO triggers (name) VALUES (?)", (name,)) 147 c.execute("INSERT OR IGNORE INTO triggers (name) VALUES (?)", (name,))
148 conn.commit() 148 conn.commit()
149 149
150 await ctx.respond("trigger added") 150 await ctx.respond("trigger added")
151 151
152 @trigger_group.command( 152 @trigger_group.command(
153 description="Add a trigger or response to the name", 153 description="Add a trigger or response to the name",
154 ) 154 )
@@ -192,10 +192,10 @@ class Trigger(commands.Cog):
192 if resp: 192 if resp:
193 c.execute("INSERT INTO trigger_responses (trigger_id, response_text) VALUES (?, ?)", (trigger_id, resp)) 193 c.execute("INSERT INTO trigger_responses (trigger_id, response_text) VALUES (?, ?)", (trigger_id, resp))
194 conn.commit() 194 conn.commit()
195 195
196 await ctx.respond("trigger updated") 196 await ctx.respond("trigger updated")
197 logger.info(f"Trigger updated: {name}") 197 logger.info(f"Trigger updated: {name}")
198 198
199 @trigger_group.command( 199 @trigger_group.command(
200 description="Remove a trigger or response from the name", 200 description="Remove a trigger or response from the name",
201 ) 201 )
@@ -233,7 +233,7 @@ class Trigger(commands.Cog):
233 if response: 233 if response:
234 c.execute("DELETE FROM trigger_responses WHERE trigger_id=? AND response_text=?", (trigger_id, response)) 234 c.execute("DELETE FROM trigger_responses WHERE trigger_id=? AND response_text=?", (trigger_id, response))
235 conn.commit() 235 conn.commit()
236 236
237 await ctx.respond("trigger removed") 237 await ctx.respond("trigger removed")
238 logger.info(f"Trigger removed: {name}") 238 logger.info(f"Trigger removed: {name}")
239 239
@@ -241,8 +241,8 @@ class Trigger(commands.Cog):
241 description="Delete the whole trigger", 241 description="Delete the whole trigger",
242 ) 242 )
243 async def delete( 243 async def delete(
244 self, 244 self,
245 ctx: discord.ApplicationContext, 245 ctx: discord.ApplicationContext,
246 name = discord.Option( 246 name = discord.Option(
247 str, 247 str,
248 "the name of the trigger", 248 "the name of the trigger",
@@ -262,23 +262,23 @@ class Trigger(commands.Cog):
262 c.execute("DELETE FROM trigger_responses WHERE trigger_id=?", (trigger_id,)) 262 c.execute("DELETE FROM trigger_responses WHERE trigger_id=?", (trigger_id,))
263 c.execute("DELETE FROM triggers WHERE id=?", (trigger_id,)) 263 c.execute("DELETE FROM triggers WHERE id=?", (trigger_id,))
264 conn.commit() 264 conn.commit()
265 265
266 await ctx.respond("trigger deleted") 266 await ctx.respond("trigger deleted")
267 logger.info(f"Trigger deleted: {name}") 267 logger.info(f"Trigger deleted: {name}")
268 268
269 269
270 @trigger_group.command( 270 @trigger_group.command(
271 description="List all triggers", 271 description="List all triggers",
272 ) 272 )
273 async def list( 273 async def list(
274 self, 274 self,
275 ctx: discord.ApplicationContext, 275 ctx: discord.ApplicationContext,
276 name = discord.Option( 276 name = discord.Option(
277 str, 277 str,
278 "the name of the trigger", 278 "the name of the trigger",
279 autocomplete=discord.utils.basic_autocomplete(get_type), 279 autocomplete=discord.utils.basic_autocomplete(get_type),
280 required=True 280 required=True
281 ) 281 )
282 ): 282 ):
283 with get_connection() as conn: 283 with get_connection() as conn:
284 c = conn.cursor() 284 c = conn.cursor()
@@ -315,7 +315,7 @@ class Trigger(commands.Cog):
315 description="how to use trigger group?" 315 description="how to use trigger group?"
316 ) 316 )
317 async def help( 317 async def help(
318 self, 318 self,
319 ctx: discord.ApplicationContext, 319 ctx: discord.ApplicationContext,
320 ): 320 ):
321 await ctx.respond("Trigger create - creates a group for triggers/responses\n" 321 await ctx.respond("Trigger create - creates a group for triggers/responses\n"
@@ -324,7 +324,7 @@ class Trigger(commands.Cog):
324 "Trigger delete - deletes the whole group\n" 324 "Trigger delete - deletes the whole group\n"
325 "Trigger list - lists triggers/responses of a group\n" 325 "Trigger list - lists triggers/responses of a group\n"
326 "Trigger help - display this message\n") 326 "Trigger help - display this message\n")
327 327
328 328
329def setup(bot): 329def setup(bot):
330 bot.add_cog(Trigger(bot)) 330 bot.add_cog(Trigger(bot))