diff options
| author | salmonik <raisersoft@proton.me> | 2026-04-29 14:47:32 +0200 |
|---|---|---|
| committer | salmonik <raisersoft@proton.me> | 2026-04-29 14:47:32 +0200 |
| commit | 8a4352ebb5e15ff4be82a9b4e111c1535aab349e (patch) | |
| tree | 2c4d5b4b65bdc8308b35c411cffa7f17b1bfeb4d /cogs/vclogs.py | |
| parent | fix: fallback to plaintext if no json is provided for assistant.py (diff) | |
| parent | proper context for settings (diff) | |
| download | senator-sperma-8a4352ebb5e15ff4be82a9b4e111c1535aab349e.tar.gz senator-sperma-8a4352ebb5e15ff4be82a9b4e111c1535aab349e.zip | |
Merge branch 'dev'
Signed-off-by: salmonik <raisersoft@proton.me>
Diffstat (limited to 'cogs/vclogs.py')
| -rw-r--r-- | cogs/vclogs.py | 72 |
1 files changed, 56 insertions, 16 deletions
diff --git a/cogs/vclogs.py b/cogs/vclogs.py index fb0a2e0..e5ca202 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,66 @@ 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 | with get_connection() as conn: |
| 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 | |||
| 31 | return { | ||
| 32 | row[0]: { | ||
| 33 | "join_time": row[1], | ||
| 34 | "total_active": row[2], | ||
| 35 | "total_afk": row[3], | ||
| 36 | "is_afk": bool(row[4]), | ||
| 37 | "afk_start": row[5], | ||
| 38 | "active_start": row[6], | ||
| 39 | "channel_name": row[7], | ||
| 40 | } | ||
| 41 | for row in rows | ||
| 42 | } | ||
| 43 | except Exception as e: | ||
| 44 | logger.error(f"Error loading voice activity: {e}") | ||
| 45 | return {} | ||
| 29 | 46 | ||
| 30 | 47 | ||
| 31 | def save_activity_data(data): | 48 | def save_activity_data(data): |
| 32 | with open(ACTIVITY_FILE, 'w') as f: | 49 | try: |
| 33 | json.dump(data, f, indent=4) | 50 | with get_connection() as conn: |
| 51 | c = conn.cursor() | ||
| 52 | c.execute("PRAGMA foreign_keys = ON") | ||
| 53 | |||
| 54 | c.execute("DELETE FROM voice_activity") | ||
| 55 | |||
| 56 | for user_id, user_data in data.items(): | ||
| 57 | c.execute( | ||
| 58 | """INSERT INTO voice_activity (user_id, join_time, total_active, total_afk, is_afk, afk_start, active_start, channel_name) | ||
| 59 | VALUES (?, ?, ?, ?, ?, ?, ?, ?)""", | ||
| 60 | ( | ||
| 61 | user_id, | ||
| 62 | user_data.get("join_time"), | ||
| 63 | user_data.get("total_active", 0), | ||
| 64 | user_data.get("total_afk", 0), | ||
| 65 | int(user_data.get("is_afk", False)), | ||
| 66 | user_data.get("afk_start"), | ||
| 67 | user_data.get("active_start"), | ||
| 68 | user_data.get("channel_name"), | ||
| 69 | ), | ||
| 70 | ) | ||
| 71 | conn.commit() | ||
| 72 | logger.info(f"Saved voice activity for {len(data)} users") | ||
| 73 | except Exception as e: | ||
| 74 | logger.error(f"Error saving voice activity: {e}") | ||
| 34 | 75 | ||
| 35 | 76 | ||
| 36 | def format_duration(seconds): | 77 | def format_duration(seconds): |
| @@ -104,9 +145,8 @@ class VcLogs(commands.Cog): | |||
| 104 | 145 | ||
| 105 | if voice_client: | 146 | if voice_client: |
| 106 | if is_playing: | 147 | if is_playing: |
| 107 | logger.info("voice already playing, stopping current audio") | 148 | logger.info("voice already playing, skipping TTS") |
| 108 | voice_client.stop() | 149 | return |
| 109 | await asyncio.sleep(0.1) | ||
| 110 | 150 | ||
| 111 | logger.info("preparing to play audio") | 151 | logger.info("preparing to play audio") |
| 112 | audio_source = discord.FFmpegPCMAudio(tts_file) | 152 | audio_source = discord.FFmpegPCMAudio(tts_file) |
| @@ -169,7 +209,7 @@ class VcLogs(commands.Cog): | |||
| 169 | } | 209 | } |
| 170 | save_activity_data(activity_data) | 210 | save_activity_data(activity_data) |
| 171 | 211 | ||
| 172 | message = f"[{current_time}] {member.name} dołączył na {after.channel.name}" | 212 | message = f"[{current_time}] {member.name} dołączył na {after.channel.name} <t:{int(current_ts)}:R>" |
| 173 | tts_text = f"{member.display_name} sie zespawnił" | 213 | tts_text = f"{member.display_name} sie zespawnił" |
| 174 | tts_channel = after.channel | 214 | tts_channel = after.channel |
| 175 | await log_channel.send(message) | 215 | await log_channel.send(message) |
| @@ -190,7 +230,7 @@ class VcLogs(commands.Cog): | |||
| 190 | afk_str = format_duration(data['total_afk']) | 230 | afk_str = format_duration(data['total_afk']) |
| 191 | 231 | ||
| 192 | message = ( | 232 | message = ( |
| 193 | f"[{current_time}] {member.name} rozłączył się z {before.channel.name}\n" | 233 | f"[{current_time}] {member.name} rozłączył się z {before.channel.name} <t:{int(current_ts)}:R>\n" |
| 194 | f"Aktywny: {active_str} | AFK: {afk_str}" | 234 | f"Aktywny: {active_str} | AFK: {afk_str}" |
| 195 | ) | 235 | ) |
| 196 | tts_text = f"{member.display_name} jest chujem" | 236 | tts_text = f"{member.display_name} jest chujem" |
| @@ -201,7 +241,7 @@ class VcLogs(commands.Cog): | |||
| 201 | del activity_data[user_id] | 241 | del activity_data[user_id] |
| 202 | save_activity_data(activity_data) | 242 | save_activity_data(activity_data) |
| 203 | else: | 243 | else: |
| 204 | message = f"[{current_time}] {member.name} rozłączył się z {before.channel.name}" | 244 | message = f"[{current_time}] {member.name} rozłączył się z {before.channel.name} <t:{int(current_ts)}:R>" |
| 205 | tts_text = f"{member.display_name} jest chujem" | 245 | tts_text = f"{member.display_name} jest chujem" |
| 206 | tts_channel = before.channel | 246 | tts_channel = before.channel |
| 207 | await log_channel.send(message) | 247 | await log_channel.send(message) |
| @@ -225,7 +265,7 @@ class VcLogs(commands.Cog): | |||
| 225 | else: | 265 | else: |
| 226 | activity = "odgłuchnął" | 266 | activity = "odgłuchnął" |
| 227 | 267 | ||
| 228 | message = ( f"[{current_time}] {member.name} {activity} się na {after.channel.name}\n" | 268 | message = ( f"[{current_time}] {member.name} {activity} się na {after.channel.name} <t:{int(current_ts)}:R>\n" |
| 229 | f"AFK: {afk_str}" ) | 269 | f"AFK: {afk_str}" ) |
| 230 | await log_channel.send(message) | 270 | await log_channel.send(message) |
| 231 | logger.info(f"User returned from AFK: {message}") | 271 | logger.info(f"User returned from AFK: {message}") |
| @@ -245,7 +285,7 @@ class VcLogs(commands.Cog): | |||
| 245 | activity = "ogłuchnął" | 285 | activity = "ogłuchnął" |
| 246 | 286 | ||
| 247 | active_str = format_duration(active_since_start) | 287 | active_str = format_duration(active_since_start) |
| 248 | message = ( f"[{current_time}] {member.name} {activity} się na {after.channel.name}\n" | 288 | message = ( f"[{current_time}] {member.name} {activity} się na {after.channel.name} <t:{int(current_ts)}:R>\n" |
| 249 | f"Aktywny: {active_str}" ) | 289 | f"Aktywny: {active_str}" ) |
| 250 | await log_channel.send(message) | 290 | await log_channel.send(message) |
| 251 | logger.info(f"User went AFK: {message}") | 291 | logger.info(f"User went AFK: {message}") |
