-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
96 lines (85 loc) · 3.33 KB
/
Copy pathdatabase.py
File metadata and controls
96 lines (85 loc) · 3.33 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
# ScratchBot v1.4.0
import sqlite3
import time
import logging
class Database:
def __init__(self, db_file):
try:
self.conn = sqlite3.connect(db_file)
self.cursor = self.conn.cursor()
self._create_tables()
logging.info("Database connection successful.")
except sqlite3.Error as e:
logging.critical(f"Database connection failed: {e}")
raise
def _create_tables(self):
self.cursor.execute("""
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY
)
""")
self.cursor.execute("""
CREATE TABLE IF NOT EXISTS gifts (
author TEXT PRIMARY KEY,
target TEXT NOT NULL
)
""")
self.cursor.execute("""
CREATE TABLE IF NOT EXISTS user_cooldowns (
username TEXT PRIMARY KEY,
last_command_timestamp REAL NOT NULL
)
""")
self.conn.commit()
def is_comment_seen(self, comment_id):
self.cursor.execute("SELECT 1 FROM comments WHERE id = ?", (comment_id,))
return self.cursor.fetchone() is not None
def add_seen_comments(self, comment_ids):
if not comment_ids:
return
try:
self.cursor.executemany("INSERT OR IGNORE INTO comments (id) VALUES (?)", [(id,) for id in comment_ids])
self.conn.commit()
except sqlite3.Error as e:
logging.error(f"Failed to add seen comments to DB: {e}")
def has_sent_gift(self, author):
self.cursor.execute("SELECT 1 FROM gifts WHERE author = ?", (author,))
return self.cursor.fetchone() is not None
def add_gift(self, author, target):
try:
self.cursor.execute("INSERT INTO gifts (author, target) VALUES (?, ?)", (author, target))
self.conn.commit()
return True
except sqlite3.Error as e:
logging.error(f"Failed to add gift to DB for {author}: {e}")
return False
def is_user_on_cooldown(self, username, cooldown_duration):
self.cursor.execute("SELECT last_command_timestamp FROM user_cooldowns WHERE username = ?", (username,))
result = self.cursor.fetchone()
if result:
time_since_last_command = time.time() - result[0]
if time_since_last_command < cooldown_duration:
return True
return False
def update_user_cooldown(self, username):
timestamp = time.time()
self.cursor.execute(
"INSERT OR REPLACE INTO user_cooldowns (username, last_command_timestamp) VALUES (?, ?)",
(username, timestamp)
)
self.conn.commit()
def close(self):
if self.conn:
self.conn.close()
logging.info("Database connection closed.")
def delete_gift_by_author(self, author):
try:
self.cursor.execute("SELECT 1 FROM gifts WHERE author = ?", (author,))
if self.cursor.fetchone() is None:
return False
self.cursor.execute("DELETE FROM gifts WHERE author = ?", (author,))
self.conn.commit()
return self.cursor.rowcount > 0
except sqlite3.Error as e:
logging.error(f"Failed to delete gift from DB for {author}: {e}")
return False