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
|
# 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 logging
from cogs.utils.database import get_connection, init_database
logger = logging.getLogger(__name__)
init_database()
_SELECT_KEYWORD_SQL = "SELECT keyword FROM trigger_keywords WHERE trigger_id=?"
_SELECT_RESPONSE_SQL = "SELECT response_text FROM trigger_responses WHERE trigger_id=?"
_SELECT_TRIGGER_ID_SQL = "SELECT id FROM triggers WHERE name=?"
_TRIGGER_NOT_FOUND_MSG = "Trigger not found."
_TRIGGER_NAME_OPTION = "the name of the trigger"
class Trigger(commands.Cog):
def __init__(self, bot):
self.bot = bot
trigger_group = discord.SlashCommandGroup("trigger", "trigger commands")
@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_SQL, (trigger_id,))
keywords = [row[0] for row in c.fetchall()]
if any(keyword in content for keyword in keywords):
c.execute(_SELECT_RESPONSE_SQL, (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 get_connection() 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,
_TRIGGER_NAME_OPTION,
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,
_TRIGGER_NAME_OPTION,
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_TRIGGER_ID_SQL, (name,))
row = c.fetchone()
if not row:
await ctx.respond(_TRIGGER_NOT_FOUND_MSG)
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,
_TRIGGER_NAME_OPTION,
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_TRIGGER_ID_SQL, (name,))
row = c.fetchone()
if not row:
await ctx.respond(_TRIGGER_NOT_FOUND_MSG)
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,
_TRIGGER_NAME_OPTION,
autocomplete= discord.utils.basic_autocomplete(get_type),
required=True
)
):
with get_connection() as conn:
c = conn.cursor()
c.execute(_SELECT_TRIGGER_ID_SQL, (name,))
row = c.fetchone()
if not row:
await ctx.respond(_TRIGGER_NOT_FOUND_MSG)
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,
_TRIGGER_NAME_OPTION,
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_MSG)
logger.warning(f"Trigger not found: {name}")
return
trigger_id = row[0]
c.execute(_SELECT_KEYWORD_SQL, (trigger_id,))
triggers = [row[0] for row in c.fetchall()]
c.execute(_SELECT_RESPONSE_SQL, (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_SQL, (trigger_id,))
triggers = ", ".join([row[0] for row in c.fetchall()])
c.execute(_SELECT_RESPONSE_SQL, (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("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))
|