aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/vclogs.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--cogs/vclogs.py55
1 files changed, 26 insertions, 29 deletions
diff --git a/cogs/vclogs.py b/cogs/vclogs.py
index 404ffe0..e5ca202 100644
--- a/cogs/vclogs.py
+++ b/cogs/vclogs.py
@@ -23,11 +23,10 @@ init_database()
23 23
24def load_activity_data(): 24def load_activity_data():
25 try: 25 try:
26 conn = get_connection() 26 with get_connection() as conn:
27 c = conn.cursor() 27 c = conn.cursor()
28 c.execute("SELECT user_id, join_time, total_active, total_afk, is_afk, afk_start, active_start, channel_name FROM voice_activity") 28 c.execute("SELECT user_id, join_time, total_active, total_afk, is_afk, afk_start, active_start, channel_name FROM voice_activity")
29 rows = c.fetchall() 29 rows = c.fetchall()
30 conn.close()
31 30
32 return { 31 return {
33 row[0]: { 32 row[0]: {
@@ -48,30 +47,28 @@ def load_activity_data():
48 47
49def save_activity_data(data): 48def save_activity_data(data):
50 try: 49 try:
51 conn = get_connection() 50 with get_connection() as conn:
52 c = conn.cursor() 51 c = conn.cursor()
53 c.execute("PRAGMA foreign_keys = ON") 52 c.execute("PRAGMA foreign_keys = ON")
54 53
55 c.execute("DELETE FROM voice_activity") 54 c.execute("DELETE FROM voice_activity")
56 55
57 for user_id, user_data in data.items(): 56 for user_id, user_data in data.items():
58 c.execute( 57 c.execute(
59 """INSERT INTO voice_activity (user_id, join_time, total_active, total_afk, is_afk, afk_start, active_start, channel_name) 58 """INSERT INTO voice_activity (user_id, join_time, total_active, total_afk, is_afk, afk_start, active_start, channel_name)
60 VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", 59 VALUES (?, ?, ?, ?, ?, ?, ?, ?)""",
61 ( 60 (
62 user_id, 61 user_id,
63 user_data.get("join_time"), 62 user_data.get("join_time"),
64 user_data.get("total_active", 0), 63 user_data.get("total_active", 0),
65 user_data.get("total_afk", 0), 64 user_data.get("total_afk", 0),
66 int(user_data.get("is_afk", False)), 65 int(user_data.get("is_afk", False)),
67 user_data.get("afk_start"), 66 user_data.get("afk_start"),
68 user_data.get("active_start"), 67 user_data.get("active_start"),
69 user_data.get("channel_name"), 68 user_data.get("channel_name"),
70 ), 69 ),
71 ) 70 )
72 71 conn.commit()
73 conn.commit()
74 conn.close()
75 logger.info(f"Saved voice activity for {len(data)} users") 72 logger.info(f"Saved voice activity for {len(data)} users")
76 except Exception as e: 73 except Exception as e:
77 logger.error(f"Error saving voice activity: {e}") 74 logger.error(f"Error saving voice activity: {e}")