Improve message sent to Discord for missed reminder

This commit is contained in:
2025-07-06 06:02:05 +02:00
parent 9f2814a3d5
commit d7ea1c9ec4
6 changed files with 68 additions and 2 deletions

View File

@ -2,6 +2,7 @@ from __future__ import annotations
import os
import platform
from pathlib import Path
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
import pytz
@ -11,6 +12,8 @@ from apscheduler.schedulers.asyncio import AsyncIOScheduler
from dotenv import load_dotenv
from loguru import logger
from discord_reminder_bot.helpers import generate_state
load_dotenv(verbose=True)
default_sentry_dsn: str = "https://c4c61a52838be9b5042144420fba5aaa@o4505228040339456.ingest.us.sentry.io/4508707268984832"
@ -57,3 +60,40 @@ def get_scheduler() -> AsyncIOScheduler:
scheduler: AsyncIOScheduler = get_scheduler()
def export_reminder_jobs_to_markdown() -> None:
"""Loop through the APScheduler database and save each job's data to a markdown file if changed."""
data_dir: str = os.getenv("DATA_DIR", default="./data")
logger.info(f"Exporting reminder jobs to markdown files in directory: {data_dir}")
for job in scheduler.get_jobs():
job_state: str = generate_state(job.__getstate__(), job)
file_path: Path = Path(data_dir) / "reminder_data" / f"{job.id}.md"
file_path.parent.mkdir(parents=True, exist_ok=True)
try:
if file_path.exists():
existing_content = file_path.read_text(encoding="utf-8")
if existing_content == job_state:
logger.debug(f"No changes for {file_path}, skipping write.")
continue
file_path.write_text(job_state, encoding="utf-8")
logger.info(f"Data saved to {file_path}")
except OSError as e:
logger.error(f"Failed to save data to {file_path}: {e}")
def get_markdown_contents_from_markdown_file(job_id: str) -> str:
"""Get the contents of a markdown file for a specific job ID.
Args:
job_id (str): The ID of the job.
Returns:
str: The contents of the markdown file, or an empty string if the file does not exist.
"""
data_dir: str = os.getenv("DATA_DIR", default="./data")
file_path: Path = Path(data_dir) / "reminder_data" / f"{job_id}.md"
if file_path.exists():
return file_path.read_text(encoding="utf-8")
return ""