From 5675dfd20b24f1e0eef06b11d9ec41a894d3a5bf Mon Sep 17 00:00:00 2001 From: salmonik Date: Tue, 28 Apr 2026 20:05:36 +0200 Subject: multitude of changes. but primarly, use db for all data. Signed-off-by: salmonik --- cogs/cron.py | 103 ++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 71 insertions(+), 32 deletions(-) (limited to 'cogs/cron.py') diff --git a/cogs/cron.py b/cogs/cron.py index 4a63e3a..b4f494a 100644 --- a/cogs/cron.py +++ b/cogs/cron.py @@ -4,9 +4,7 @@ # https://codeberg.org/salmonik/senator-sperma import asyncio -import json import logging -import os from datetime import datetime from venv import logger @@ -16,37 +14,74 @@ from discord.ext import commands from cogs.utils import file_search as search_utils from cogs.voice import get_voice_manager +from cogs.utils.database import get_connection, init_database logger = logging.getLogger(__name__) +init_database() + class Cron(commands.Cog): def __init__(self, bot): self.bot = bot self.scheduled_tasks = [] - self.cron_file = "data/cron_jobs.json" self.load_tasks() self.task = bot.loop.create_task(self.schedule_checker()) def load_tasks(self): - """Load scheduled tasks from file""" try: - if os.path.exists(self.cron_file): - with open(self.cron_file, "r") as f: - self.scheduled_tasks = json.load(f) - logger.info(f"Loaded {len(self.scheduled_tasks)} cron jobs") - else: - self.scheduled_tasks = [] + conn = get_connection() + try: + c = conn.cursor() + c.execute("SELECT id, cron_expression, type, channel_id, active, audio_filename, message FROM cron_jobs") + rows = c.fetchall() + finally: + conn.close() + + self.scheduled_tasks = [ + { + "id": row[0], + "cron_expression": row[1], + "type": row[2], + "channel_id": row[3], + "active": bool(row[4]), + "audio_filename": row[5], + "message": row[6], + } + for row in rows + ] + logger.info(f"Loaded {len(self.scheduled_tasks)} cron jobs from database") except Exception as e: logger.error(f"Error loading cron jobs: {e}") self.scheduled_tasks = [] def save_tasks(self): - """Save scheduled tasks to file""" try: - os.makedirs(os.path.dirname(self.cron_file), exist_ok=True) - with open(self.cron_file, "w") as f: - json.dump(self.scheduled_tasks, f, indent=2) + conn = get_connection() + try: + c = conn.cursor() + c.execute("PRAGMA foreign_keys = ON") + + c.execute("DELETE FROM cron_jobs") + + for task in self.scheduled_tasks: + c.execute( + """INSERT INTO cron_jobs (id, cron_expression, type, channel_id, active, audio_filename, message) + VALUES (?, ?, ?, ?, ?, ?, ?)""", + ( + task.get("id"), + task["cron_expression"], + task["type"], + task["channel_id"], + int(task.get("active", True)), + task.get("audio_filename"), + task.get("message"), + ), + ) + + conn.commit() + finally: + conn.close() except Exception as e: logger.error(f"Error saving cron jobs: {e}") @@ -132,11 +167,13 @@ class Cron(commands.Cog): while voice_client.is_playing(): await asyncio.sleep(1) + logger.info("Audio playback finished normally - disconnecting") await voice_client.disconnect() logger.info(f"Finished playing audio: {audio_url}") except Exception as e: logger.error(f"Error playing audio: {e}") if voice_client.is_connected(): + logger.info("Audio playback failed - disconnecting") await voice_client.disconnect() if channel.guild.system_channel: await channel.guild.system_channel.send( @@ -176,8 +213,19 @@ class Cron(commands.Cog): else [discord.OptionChoice(name="Brak wyników", value="")] ) - @commands.slash_command( - name="cron_dodaj_audio", description="Dodaj nowe zadanie cron audio" + def _get_baseurl(self): + """Get base URL from settings""" + try: + from cogs.utils.settings import get_baseurl + + return get_baseurl() + except Exception: + return "" + + trigger_group = discord.SlashCommandGroup("cron", "komendy cron") + + @trigger_group.command( + name="dodaj_audio", description="Dodaj nowe zadanie cron audio" ) async def cron_add_audio( self, @@ -227,8 +275,8 @@ class Cron(commands.Cog): f"Zadanie cron dodane. ID: {task_id}\nSkładnia: `{cron_expression}`\nAkcja: audio\nKanał: <#{channel.id}>" ) - @commands.slash_command( - name="cron_dodaj_wiadomość", description="Dodaj nowe zadanie cron wiadomość" + @trigger_group.command( + name="dodaj_wiadomość", description="Dodaj nowe zadanie cron wiadomość" ) async def cron_add_message( self, @@ -272,17 +320,8 @@ class Cron(commands.Cog): f"Zadanie cron dodane. ID: {task_id}\nSkładnia: `{cron_expression}`\nAkcja: wiadomość\nKanał: <#{channel.id}>" ) - def _get_baseurl(self): - """Get base URL from settings""" - try: - from cogs.utils.settings import get_baseurl - - return get_baseurl() - except Exception: - return "" - - @commands.slash_command( - name="cron_pokaż", description="Pokaż wszystkie zadania cron" + @trigger_group.command( + name="pokaż", description="Pokaż wszystkie zadania cron" ) async def cron_list(self, ctx): """List all cron jobs""" @@ -313,7 +352,7 @@ class Cron(commands.Cog): await ctx.respond(embed=embed) - @commands.slash_command(name="cron_usuń", description="Usuń zadanie cron z ID") + @trigger_group.command(name="usuń", description="Usuń zadanie cron z ID") async def cron_remove( self, ctx, @@ -331,7 +370,7 @@ class Cron(commands.Cog): await ctx.respond(f"Nie znaleziono zadania cron z ID {job_id}") - @commands.slash_command(name="cron_pauza", description="Zapauzuj zadanie cron z ID") + @trigger_group.command(name="pauza", description="Zapauzuj zadanie cron z ID") async def cron_pause( self, ctx, @@ -349,7 +388,7 @@ class Cron(commands.Cog): await ctx.respond(f"Nie znaleziono zadania cron z ID {job_id}") - @commands.slash_command(name="cron_wznów", description="Wznów zadanie cron z ID") + @trigger_group.command(name="wznów", description="Wznów zadanie cron z ID") async def cron_resume( self, ctx, -- cgit v1.2.3