diff options
Diffstat (limited to '')
| -rw-r--r-- | cogs/vclogs.py | 70 |
1 files changed, 57 insertions, 13 deletions
diff --git a/cogs/vclogs.py b/cogs/vclogs.py index d0c4825..404ffe0 100644 --- a/cogs/vclogs.py +++ b/cogs/vclogs.py | |||
| @@ -4,7 +4,6 @@ | |||
| 4 | # https://codeberg.org/salmonik/senator-sperma | 4 | # https://codeberg.org/salmonik/senator-sperma |
| 5 | 5 | ||
| 6 | import datetime | 6 | import datetime |
| 7 | import json | ||
| 8 | import logging | 7 | import logging |
| 9 | import os | 8 | import os |
| 10 | import asyncio | 9 | import asyncio |
| @@ -13,24 +12,69 @@ from discord.ext import commands | |||
| 13 | from gtts import gTTS | 12 | from gtts import gTTS |
| 14 | from cogs.voice import get_voice_manager | 13 | from cogs.voice import get_voice_manager |
| 15 | from cogs.utils.settings import get_volume, save_settings | 14 | from cogs.utils.settings import get_volume, save_settings |
| 15 | from cogs.utils.database import get_connection, init_database | ||
| 16 | 16 | ||
| 17 | import tempfile | 17 | import tempfile |
| 18 | 18 | ||
| 19 | logger = logging.getLogger(__name__) | 19 | logger = logging.getLogger(__name__) |
| 20 | 20 | ||
| 21 | ACTIVITY_FILE = os.path.join(tempfile.gettempdir(), 'voice_activity.json') | 21 | init_database() |
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | def load_activity_data(): | 24 | def load_activity_data(): |
| 25 | if os.path.exists(ACTIVITY_FILE): | 25 | try: |
| 26 | with open(ACTIVITY_FILE, 'r') as f: | 26 | conn = get_connection() |
| 27 | return json.load(f) | 27 | c = conn.cursor() |
| 28 | return {} | 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() | ||
| 30 | conn.close() | ||
| 31 | |||
| 32 | return { | ||
| 33 | row[0]: { | ||
| 34 | "join_time": row[1], | ||
| 35 | "total_active": row[2], | ||
| 36 | "total_afk": row[3], | ||
| 37 | "is_afk": bool(row[4]), | ||
| 38 | "afk_start": row[5], | ||
| 39 | "active_start": row[6], | ||
| 40 | "channel_name": row[7], | ||
| 41 | } | ||
| 42 | for row in rows | ||
| 43 | } | ||
| 44 | except Exception as e: | ||
| 45 | logger.error(f"Error loading voice activity: {e}") | ||
| 46 | return {} | ||
| 29 | 47 | ||
| 30 | 48 | ||
| 31 | def save_activity_data(data): | 49 | def save_activity_data(data): |
| 32 | with open(ACTIVITY_FILE, 'w') as f: | 50 | try: |
| 33 | json.dump(data, f, indent=4) | 51 | conn = get_connection() |
| 52 | c = conn.cursor() | ||
| 53 | c.execute("PRAGMA foreign_keys = ON") | ||
| 54 | |||
| 55 | c.execute("DELETE FROM voice_activity") | ||
| 56 | |||
| 57 | for user_id, user_data in data.items(): | ||
| 58 | c.execute( | ||
| 59 | """INSERT INTO voice_activity (user_id, join_time, total_active, total_afk, is_afk, afk_start, active_start, channel_name) | ||
| 60 | VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", | ||
| 61 | ( | ||
| 62 | user_id, | ||
| 63 | user_data.get("join_time"), | ||
| 64 | user_data.get("total_active", 0), | ||
| 65 | user_data.get("total_afk", 0), | ||
| 66 | int(user_data.get("is_afk", False)), | ||
| 67 | user_data.get("afk_start"), | ||
| 68 | user_data.get("active_start"), | ||
| 69 | user_data.get("channel_name"), | ||
| 70 | ), | ||
| 71 | ) | ||
| 72 | |||
| 73 | conn.commit() | ||
| 74 | conn.close() | ||
| 75 | logger.info(f"Saved voice activity for {len(data)} users") | ||
| 76 | except Exception as e: | ||
| 77 | logger.error(f"Error saving voice activity: {e}") | ||
| 34 | 78 | ||
| 35 | 79 | ||
| 36 | def format_duration(seconds): | 80 | def format_duration(seconds): |
| @@ -168,7 +212,7 @@ class VcLogs(commands.Cog): | |||
| 168 | } | 212 | } |
| 169 | save_activity_data(activity_data) | 213 | save_activity_data(activity_data) |
| 170 | 214 | ||
| 171 | message = f"[{current_time}] {member.name} dołączył na {after.channel.name}" | 215 | message = f"[{current_time}] {member.name} dołączył na {after.channel.name} <t:{int(current_ts)}:R>" |
| 172 | tts_text = f"{member.display_name} sie zespawnił" | 216 | tts_text = f"{member.display_name} sie zespawnił" |
| 173 | tts_channel = after.channel | 217 | tts_channel = after.channel |
| 174 | await log_channel.send(message) | 218 | await log_channel.send(message) |
| @@ -189,7 +233,7 @@ class VcLogs(commands.Cog): | |||
| 189 | afk_str = format_duration(data['total_afk']) | 233 | afk_str = format_duration(data['total_afk']) |
| 190 | 234 | ||
| 191 | message = ( | 235 | message = ( |
| 192 | f"[{current_time}] {member.name} rozłączył się z {before.channel.name}\n" | 236 | f"[{current_time}] {member.name} rozłączył się z {before.channel.name} <t:{int(current_ts)}:R>\n" |
| 193 | f"Aktywny: {active_str} | AFK: {afk_str}" | 237 | f"Aktywny: {active_str} | AFK: {afk_str}" |
| 194 | ) | 238 | ) |
| 195 | tts_text = f"{member.display_name} jest chujem" | 239 | tts_text = f"{member.display_name} jest chujem" |
| @@ -200,7 +244,7 @@ class VcLogs(commands.Cog): | |||
| 200 | del activity_data[user_id] | 244 | del activity_data[user_id] |
| 201 | save_activity_data(activity_data) | 245 | save_activity_data(activity_data) |
| 202 | else: | 246 | else: |
| 203 | message = f"[{current_time}] {member.name} rozłączył się z {before.channel.name}" | 247 | message = f"[{current_time}] {member.name} rozłączył się z {before.channel.name} <t:{int(current_ts)}:R>" |
| 204 | tts_text = f"{member.display_name} jest chujem" | 248 | tts_text = f"{member.display_name} jest chujem" |
| 205 | tts_channel = before.channel | 249 | tts_channel = before.channel |
| 206 | await log_channel.send(message) | 250 | await log_channel.send(message) |
| @@ -224,7 +268,7 @@ class VcLogs(commands.Cog): | |||
| 224 | else: | 268 | else: |
| 225 | activity = "odgłuchnął" | 269 | activity = "odgłuchnął" |
| 226 | 270 | ||
| 227 | message = ( f"[{current_time}] {member.name} {activity} się na {after.channel.name}\n" | 271 | message = ( f"[{current_time}] {member.name} {activity} się na {after.channel.name} <t:{int(current_ts)}:R>\n" |
| 228 | f"AFK: {afk_str}" ) | 272 | f"AFK: {afk_str}" ) |
| 229 | await log_channel.send(message) | 273 | await log_channel.send(message) |
| 230 | logger.info(f"User returned from AFK: {message}") | 274 | logger.info(f"User returned from AFK: {message}") |
| @@ -244,7 +288,7 @@ class VcLogs(commands.Cog): | |||
| 244 | activity = "ogłuchnął" | 288 | activity = "ogłuchnął" |
| 245 | 289 | ||
| 246 | active_str = format_duration(active_since_start) | 290 | active_str = format_duration(active_since_start) |
| 247 | message = ( f"[{current_time}] {member.name} {activity} się na {after.channel.name}\n" | 291 | message = ( f"[{current_time}] {member.name} {activity} się na {after.channel.name} <t:{int(current_ts)}:R>\n" |
| 248 | f"Aktywny: {active_str}" ) | 292 | f"Aktywny: {active_str}" ) |
| 249 | await log_channel.send(message) | 293 | await log_channel.send(message) |
| 250 | logger.info(f"User went AFK: {message}") | 294 | logger.info(f"User went AFK: {message}") |
