aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/trigger.py
blob: 0e76cef9f28593dca3f85f6d2e861fa7100b6d20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# This file is part of Senator Sperma
# Licensed under the GPL-3.0 License
# Copyright (C) 2026 Salmonik @ salmonik@tuta.io
# https://codeberg.org/salmonik/senator-sperma

import os
import discord
from discord.ext import commands
import random
import sqlite3
import logging

logger = logging.getLogger(__name__)


DB_PATH = 'data/triggers.db'


def get_connection():
    return sqlite3.connect(DB_PATH)


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 (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL UNIQUE,
                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 (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                trigger_id INTEGER NOT NULL,
                keyword TEXT NOT NULL,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                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 (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                trigger_id INTEGER NOT NULL,
                response_text TEXT NOT NULL,
                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")

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.author == self.bot.user:
            return

        await self.trigger(message)

    @staticmethod
    async def trigger(message):
        content = message.content.lower()
        with get_connection() as conn:
            c = conn.cursor()
            c.execute("SELECT id, name FROM triggers")
            for trigger_id, name in c.fetchall():
                c.execute("SELECT keyword FROM trigger_keywords WHERE trigger_id=?", (trigger_id,))
                keywords = [row[0] for row in c.fetchall()]
                if any(keyword in content for keyword in keywords):
                    c.execute("SELECT response_text FROM trigger_responses WHERE trigger_id=?", (trigger_id,))
                    responses = [row[0] for row in c.fetchall()]
                    if responses:
                        await message.channel.send(random.choice(responses).replace('\\n', '\n'))
                        logger.info(f"{message.author} triggered {name}")
                    break

    @staticmethod
    def get_type(name):
        with sqlite3.connect(DB_PATH) as conn:
            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,
        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()
            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",
    )
    async def add(
        self,
        ctx: discord.ApplicationContext,
        name = discord.Option(
            str,
            "the name of the trigger",
            autocomplete= discord.utils.basic_autocomplete(get_type),
            required=True
        ),
        trigger = discord.Option(
            str,
            "the trigger to add to the name",
            required=False
        ),
        response = discord.Option(
            str,
            "the response to add to the name",
            required=False
        )
    ):
        with get_connection() as conn:
            c = conn.cursor()
            c.execute("SELECT id FROM triggers WHERE name=?", (name,))
            row = c.fetchone()
            if not row:
                await ctx.respond("Trigger not found.")
                logger.warning(f"Trigger not found: {name}")
                return
            trigger_id = row[0]
            if trigger:
                for keyword in trigger.split('\\'):
                    keyword = keyword.strip()
                    if keyword:
                        c.execute("INSERT INTO trigger_keywords (trigger_id, keyword) VALUES (?, ?)", (trigger_id, keyword))
            if response:
                for resp in response.split('\\'):
                    resp = resp.strip()
                    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",
    )
    async def remove(
        self,
        ctx: discord.ApplicationContext,
        name = discord.Option(
            str,
            "the name of the trigger",
            autocomplete= discord.utils.basic_autocomplete(get_type),
            required=True
        ),
        trigger = discord.Option(
            str,
            "the trigger to remove from the name",
            required=False
        ),
        response = discord.Option(
            str,
            "the response to remove from the name",
            required=False
        )
    ):
        with get_connection() as conn:
            c = conn.cursor()
            c.execute("SELECT id FROM triggers WHERE name=?", (name,))
            row = c.fetchone()
            if not row:
                await ctx.respond("Trigger not found.")
                logger.warning(f"Trigger not found: {name}")
                return
            trigger_id = row[0]
            if trigger:
                c.execute("DELETE FROM trigger_keywords WHERE trigger_id=? AND keyword=?", (trigger_id, trigger))
            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}")

    @trigger_group.command(
        description="Delete the whole trigger",
    )
    async def delete(
        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()
            c.execute("SELECT id FROM triggers WHERE name=?", (name,))
            row = c.fetchone()
            if not row:
                await ctx.respond("Trigger not found.")
                return
            trigger_id = row[0]
            c.execute("DELETE FROM trigger_keywords WHERE trigger_id=?", (trigger_id,))
            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,
        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()
            if name:
                c.execute("SELECT id FROM triggers WHERE name=?", (name,))
                row = c.fetchone()
                if not row:
                    await ctx.respond("Trigger not found.")
                    logger.warning(f"Trigger not found: {name}")
                    return
                trigger_id = row[0]
                c.execute("SELECT keyword FROM trigger_keywords WHERE trigger_id=?", (trigger_id,))
                triggers = [row[0] for row in c.fetchall()]
                c.execute("SELECT response_text FROM trigger_responses WHERE trigger_id=?", (trigger_id,))
                responses = [row[0] for row in c.fetchall()]
                trigger_list = "\n".join(triggers)
                response_list = "\n".join(responses)
                message = f"**Trigger: {name}**\n**Triggers:**\n{trigger_list}\n**Responses:**\n{response_list}"
                await ctx.respond(message)
                logger.info(f"Trigger listed: {name}")
            else:
                c.execute("SELECT id, name FROM triggers")
                messages = []
                for trigger_id, name in c.fetchall():
                    c.execute("SELECT keyword FROM trigger_keywords WHERE trigger_id=?", (trigger_id,))
                    triggers = ", ".join([row[0] for row in c.fetchall()])
                    c.execute("SELECT response_text FROM trigger_responses WHERE trigger_id=?", (trigger_id,))
                    responses = ", ".join([row[0] for row in c.fetchall()])
                    messages.append(f"**{name}**\nTriggers: {triggers}\nResponses: {responses}")
                await ctx.respond("\n\n".join(messages))
                logger.info(f"Triggers listed")

    @trigger_group.command(
        description="how to use trigger group?"
    )
    async def help(
        self,
        ctx: discord.ApplicationContext,
    ):
        await ctx.respond("Trigger create - creates a group for triggers/responses\n"
                          "Trigger add    - adds triggers/responses to a group, \\ is the delimeter\n"
                          "Trigger remove - removes triggers/responses from a group, \\ is the delimeter\n"
                          "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))