-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
551 lines (495 loc) · 19.8 KB
/
db.py
File metadata and controls
551 lines (495 loc) · 19.8 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
import asyncpg
import asyncio
DB_URL = ""
pool = None
async def connect_db():
global pool
try:
pool = await asyncpg.create_pool(dsn=DB_URL)
print("[DB] Connected to PostgreSQL ✅")
except Exception as e:
print(f"[DB] Connection Error: {e}")
async def init_db():
async with pool.acquire() as conn:
await conn.execute('''
CREATE TABLE IF NOT EXISTS settings (
guild_id BIGINT PRIMARY KEY,
nickname_toggle INTEGER DEFAULT 0,
nickname_format TEXT DEFAULT 'User_{username}'
);
''')
await conn.execute('''
CREATE TABLE IF NOT EXISTS automod (
guild_id BIGINT PRIMARY KEY,
anti_invite INTEGER DEFAULT 0,
anti_link INTEGER DEFAULT 0,
anti_spam INTEGER DEFAULT 0
);
''')
await conn.execute('''
CREATE TABLE IF NOT EXISTS pin_messages (
guild_id BIGINT,
channel_id BIGINT,
message TEXT,
message_id BIGINT,
enabled INTEGER DEFAULT 0,
PRIMARY KEY (guild_id, channel_id)
);
''')
await conn.execute('''
CREATE TABLE IF NOT EXISTS log_settings (
guild_id BIGINT PRIMARY KEY,
log_channel_id BIGINT,
log_enabled INTEGER DEFAULT 0
);
''')
await conn.execute('''
CREATE TABLE IF NOT EXISTS chatbot_settings (
guild_id BIGINT PRIMARY KEY,
channel_id BIGINT
);
''')
await conn.execute('''
CREATE TABLE IF NOT EXISTS regular_role (
guild_id BIGINT PRIMARY KEY,
role_id BIGINT,
enabled INTEGER DEFAULT 0
);
''')
await conn.execute('''
CREATE TABLE IF NOT EXISTS count_channels (
guild_id BIGINT PRIMARY KEY,
channel_id BIGINT
);
''')
await conn.execute('''
CREATE TABLE IF NOT EXISTS count_progress (
guild_id BIGINT PRIMARY KEY,
count INTEGER DEFAULT 0,
last_user_id BIGINT
);
''')
await conn.execute('''
CREATE TABLE IF NOT EXISTS dm_greet_settings (
guild_id BIGINT PRIMARY KEY,
enabled INTEGER DEFAULT 0,
title TEXT DEFAULT 'Welcome {user}!',
description TEXT DEFAULT 'Glad to have you in {server}!',
image_url TEXT,
footer TEXT
);
''')
await conn.execute('''
CREATE TABLE IF NOT EXISTS welcome_settings (
guild_id BIGINT PRIMARY KEY,
channel_id BIGINT,
description TEXT DEFAULT 'Welcome {user} to {server}!',
image_url TEXT,
big_text TEXT DEFAULT 'Welcome!',
small_text TEXT DEFAULT 'Enjoy your stay!',
enabled INTEGER DEFAULT 0
);
''')
await conn.execute('''
CREATE TABLE IF NOT EXISTS image_settings (
guild_id BIGINT PRIMARY KEY,
channel_id BIGINT,
category TEXT
);
''')
# -------------------- Nickname Settings --------------------
async def get_nick_setting(guild_id):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow(
'SELECT nickname_toggle, nickname_format FROM settings WHERE guild_id = $1',
guild_id
)
return (row['nickname_toggle'], row['nickname_format']) if row else (0, "User_{username}")
except Exception as e:
print(f"[DB] get_nick_setting error: {e}")
return (0, "User_{username}")
async def set_nick_setting(guild_id, value):
try:
async with pool.acquire() as conn:
await conn.execute('''
INSERT INTO settings (guild_id, nickname_toggle)
VALUES ($1, $2)
ON CONFLICT (guild_id) DO UPDATE SET nickname_toggle = EXCLUDED.nickname_toggle
''', guild_id, value)
except Exception as e:
print(f"[DB] set_nick_setting error: {e}")
async def set_nick_format(guild_id, format_str):
try:
async with pool.acquire() as conn:
await conn.execute('''
INSERT INTO settings (guild_id, nickname_format)
VALUES ($1, $2)
ON CONFLICT (guild_id) DO UPDATE SET nickname_format = EXCLUDED.nickname_format
''', guild_id, format_str)
except Exception as e:
print(f"[DB] set_nick_format error: {e}")
# -------------------- Pin Messages --------------------
async def set_pin_message(guild_id, channel_id, message, message_id, enabled=True):
try:
async with pool.acquire() as conn:
await conn.execute('''
INSERT INTO pin_messages (guild_id, channel_id, message, message_id, enabled)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (guild_id, channel_id) DO UPDATE
SET message = EXCLUDED.message,
message_id = EXCLUDED.message_id,
enabled = EXCLUDED.enabled
''', guild_id, channel_id, message, message_id, int(enabled))
except Exception as e:
print(f"[DB] set_pin_message error: {e}")
async def get_pin_message(guild_id, channel_id):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow(
'SELECT message, message_id FROM pin_messages WHERE guild_id = $1 AND channel_id = $2',
guild_id, channel_id
)
return (row['message'], row['message_id']) if row else (None, None)
except Exception as e:
print(f"[DB] get_pin_message error: {e}")
return (None, None)
async def set_pin_enabled(guild_id, channel_id, enabled):
try:
async with pool.acquire() as conn:
await conn.execute('''
INSERT INTO pin_messages (guild_id, channel_id, enabled)
VALUES ($1, $2, $3)
ON CONFLICT (guild_id, channel_id) DO UPDATE
SET enabled = EXCLUDED.enabled
''', guild_id, channel_id, int(enabled))
except Exception as e:
print(f"[DB] set_pin_enabled error: {e}")
async def is_pin_enabled(guild_id, channel_id):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow(
'SELECT enabled FROM pin_messages WHERE guild_id = $1 AND channel_id = $2',
guild_id, channel_id
)
return row['enabled'] == 1 if row else False
except Exception as e:
print(f"[DB] is_pin_enabled error: {e}")
return False
async def get_full_pin_data(guild_id, channel_id):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow(
'SELECT message, message_id, enabled FROM pin_messages WHERE guild_id = $1 AND channel_id = $2',
guild_id, channel_id
)
return (row['message'], row['message_id'], row['enabled'] == 1) if row else (None, None, False)
except Exception as e:
print(f"[DB] get_full_pin_data error: {e}")
return (None, None, False)
# -------------------- Logging --------------------
async def set_log_settings(guild_id, channel_id=None, enabled=None):
try:
async with pool.acquire() as conn:
if channel_id is not None:
await conn.execute('''
INSERT INTO log_settings (guild_id, log_channel_id)
VALUES ($1, $2)
ON CONFLICT (guild_id) DO UPDATE
SET log_channel_id = EXCLUDED.log_channel_id
''', guild_id, channel_id)
if enabled is not None:
await conn.execute('''
INSERT INTO log_settings (guild_id, log_enabled)
VALUES ($1, $2)
ON CONFLICT (guild_id) DO UPDATE
SET log_enabled = EXCLUDED.log_enabled
''', guild_id, int(enabled))
except Exception as e:
print(f"[DB] set_log_settings error: {e}")
async def get_log_channel_id(guild_id):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow(
'SELECT log_channel_id FROM log_settings WHERE guild_id = $1',
guild_id
)
return row['log_channel_id'] if row else None
except Exception as e:
print(f"[DB] get_log_channel_id error: {e}")
return None
async def is_log_enabled(guild_id):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow(
'SELECT log_enabled FROM log_settings WHERE guild_id = $1',
guild_id
)
return row['log_enabled'] == 1 if row else False
except Exception as e:
print(f"[DB] is_log_enabled error: {e}")
return False
# -------------------- Chatbot --------------------
async def set_chatbot_channel(guild_id, channel_id):
try:
async with pool.acquire() as conn:
await conn.execute('''
INSERT INTO chatbot_settings (guild_id, channel_id)
VALUES ($1, $2)
ON CONFLICT (guild_id) DO UPDATE SET channel_id = EXCLUDED.channel_id
''', guild_id, channel_id)
except Exception as e:
print(f"[DB] set_chatbot_channel error: {e}")
async def get_chatbot_channel(guild_id):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow(
'SELECT channel_id FROM chatbot_settings WHERE guild_id = $1',
guild_id
)
return row['channel_id'] if row else None
except Exception as e:
print(f"[DB] get_chatbot_channel error: {e}")
return None
async def remove_chatbot_channel(guild_id):
try:
async with pool.acquire() as conn:
await conn.execute(
'DELETE FROM chatbot_settings WHERE guild_id = $1',
guild_id
)
except Exception as e:
print(f"[DB] remove_chatbot_channel error: {e}")
# -------------------- Regular Role --------------------
async def set_regular_role(guild_id, role_id):
try:
async with pool.acquire() as conn:
await conn.execute('''
INSERT INTO regular_role (guild_id, role_id)
VALUES ($1, $2)
ON CONFLICT (guild_id) DO UPDATE SET role_id = EXCLUDED.role_id
''', guild_id, role_id)
except Exception as e:
print(f"[DB] set_regular_role error: {e}")
async def toggle_regular_role(guild_id, enabled):
try:
async with pool.acquire() as conn:
await conn.execute('''
INSERT INTO regular_role (guild_id, enabled)
VALUES ($1, $2)
ON CONFLICT (guild_id) DO UPDATE SET enabled = EXCLUDED.enabled
''', guild_id, int(enabled))
except Exception as e:
print(f"[DB] toggle_regular_role error: {e}")
async def get_regular_role_settings(guild_id):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow(
'SELECT role_id, enabled FROM regular_role WHERE guild_id = $1',
guild_id
)
return (row['role_id'], row['enabled']) if row else (None, 0)
except Exception as e:
print(f"[DB] get_regular_role_settings error: {e}")
return (None, 0)
# -------------------- Counting -----------------
async def set_count_channel(guild_id: int, channel_id: int):
try:
async with pool.acquire() as conn:
await conn.execute('''
INSERT INTO count_channels (guild_id, channel_id)
VALUES ($1, $2)
ON CONFLICT (guild_id) DO UPDATE SET channel_id = EXCLUDED.channel_id
''', guild_id, channel_id)
except Exception as e:
print(f"[DB] set_count_channel error: {e}")
async def get_count_channel(guild_id: int):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow(
'SELECT channel_id FROM count_channels WHERE guild_id = $1',
guild_id
)
return row['channel_id'] if row else None
except Exception as e:
print(f"[DB] get_count_channel error: {e}")
return None
async def remove_count_channel(guild_id: int):
try:
async with pool.acquire() as conn:
await conn.execute(
'DELETE FROM count_channels WHERE guild_id = $1',
guild_id
)
except Exception as e:
print(f"[DB] remove_count_channel error: {e}")
async def get_current_count(guild_id: int):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow(
'SELECT count FROM count_progress WHERE guild_id = $1',
guild_id
)
return row['count'] if row else 0
except Exception as e:
print(f"[DB] get_current_count error: {e}")
return 0
async def get_last_counter(guild_id: int):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow(
'SELECT last_user_id FROM count_progress WHERE guild_id = $1',
guild_id
)
return row['last_user_id'] if row else None
except Exception as e:
print(f"[DB] get_last_counter error: {e}")
return None
async def update_count(guild_id: int, new_count: int, user_id: int):
try:
async with pool.acquire() as conn:
await conn.execute('''
INSERT INTO count_progress (guild_id, count, last_user_id)
VALUES ($1, $2, $3)
ON CONFLICT (guild_id) DO UPDATE
SET count = EXCLUDED.count, last_user_id = EXCLUDED.last_user_id
''', guild_id, new_count, user_id)
except Exception as e:
print(f"[DB] update_count error: {e}")
async def reset_count(guild_id: int):
try:
async with pool.acquire() as conn:
await conn.execute('''
INSERT INTO count_progress (guild_id, count, last_user_id)
VALUES ($1, 0, NULL)
ON CONFLICT (guild_id) DO UPDATE
SET count = 0, last_user_id = NULL
''', guild_id)
except Exception as e:
print(f"[DB] reset_count error: {e}")
# ----- image loop -----
async def set_image_setting(self, guild_id: int, channel_id: int, category: str):
await self.pool.execute('''
INSERT INTO image_settings (guild_id, channel_id, category)
VALUES ($1, $2, $3)
ON CONFLICT (guild_id) DO UPDATE
SET channel_id = EXCLUDED.channel_id,
category = EXCLUDED.category;
''', guild_id, channel_id, category)
async def get_image_setting(self, guild_id: int):
row = await self.pool.fetchrow('''
SELECT channel_id, category FROM image_settings
WHERE guild_id = $1
''', guild_id)
return dict(row) if row else None
async def clear_image_setting(self, guild_id: int):
await self.pool.execute('''
DELETE FROM image_settings WHERE guild_id = $1
''', guild_id)
async def get_all_image_settings(self):
rows = await self.pool.fetch('''
SELECT guild_id, channel_id, category FROM image_settings
''')
return [dict(row) for row in rows]
# -------------------- DM Greet Settings --------------------
async def get_dm_greet_settings(guild_id):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow('''
SELECT enabled, title, description, image_url, footer
FROM dm_greet_settings
WHERE guild_id = $1
''', guild_id)
if row:
return {
'enabled': bool(row['enabled']),
'title': row['title'],
'description': row['description'],
'image_url': row['image_url'],
'footer': row['footer']
}
return {
'enabled': False,
'title': 'Welcome {user}!',
'description': 'Glad to have you in {server}!',
'image_url': None,
'footer': None
}
except Exception as e:
print(f"[DB] get_dm_greet_settings error: {e}")
return None
async def set_dm_greet_settings(guild_id, enabled=None, title=None, description=None, image_url=None, footer=None):
try:
async with pool.acquire() as conn:
# Ensure row exists
await conn.execute('''
INSERT INTO dm_greet_settings (guild_id)
VALUES ($1)
ON CONFLICT (guild_id) DO NOTHING
''', guild_id)
if enabled is not None:
await conn.execute('''
UPDATE dm_greet_settings SET enabled = $1 WHERE guild_id = $2
''', int(enabled), guild_id)
if title is not None:
await conn.execute('''
UPDATE dm_greet_settings SET title = $1 WHERE guild_id = $2
''', title, guild_id)
if description is not None:
await conn.execute('''
UPDATE dm_greet_settings SET description = $1 WHERE guild_id = $2
''', description, guild_id)
if image_url is not None:
await conn.execute('''
UPDATE dm_greet_settings SET image_url = $1 WHERE guild_id = $2
''', image_url, guild_id)
if footer is not None:
await conn.execute('''
UPDATE dm_greet_settings SET footer = $1 WHERE guild_id = $2
''', footer, guild_id)
except Exception as e:
print(f"[DB] set_dm_greet_settings error: {e}")
# -------------------- Image Settings --------------------
# Set or update the image posting settings for a guild
async def set_image_setting(guild_id: int, channel_id: int, category: str):
try:
async with pool.acquire() as conn:
await conn.execute('''
INSERT INTO image_settings (guild_id, channel_id, category)
VALUES ($1, $2, $3)
ON CONFLICT (guild_id)
DO UPDATE SET
channel_id = EXCLUDED.channel_id,
category = EXCLUDED.category
''', guild_id, channel_id, category)
except Exception as e:
print(f"[DB] set_image_setting error: {e}")
# Get the image posting settings for a specific guild
async def get_image_setting(guild_id: int):
try:
async with pool.acquire() as conn:
row = await conn.fetchrow('''
SELECT channel_id, category
FROM image_settings
WHERE guild_id = $1
''', guild_id)
return {"channel_id": row["channel_id"], "category": row["category"]} if row else None
except Exception as e:
print(f"[DB] get_image_setting error: {e}")
return None
# Get settings for all guilds (useful for background loops)
async def get_all_image_settings():
try:
async with pool.acquire() as conn:
rows = await conn.fetch('SELECT guild_id, channel_id, category FROM image_settings')
return [dict(row) for row in rows]
except Exception as e:
print(f"[DB] get_all_image_settings error: {e}")
return []
# Clear the image posting settings for a guild
async def clear_image_setting(guild_id: int):
try:
async with pool.acquire() as conn:
await conn.execute('DELETE FROM image_settings WHERE guild_id = $1', guild_id)
except Exception as e:
print(f"[DB] clear_image_setting error: {e}")