import asyncio import time import logging from pyrogram.errors import FloodWait, MessageIdInvalid from Systumm.utils.database import Database from Systumm.config import Telegram from Systumm.bot import SystummBot db = Database(Telegram.DATABASE_URL, Telegram.SESSION_NAME) async def run_24h_sweeper(): """Silently runs in the background and cleans expired files every hour.""" await asyncio.sleep(60) # Wait 1 minute after boot before starting the loop while True: try: logging.info("Starting 24-hour background sweep...") current_time = time.time() twenty_four_hours_ago = current_time - 86400 # 24 Hours in seconds # Find all files where upload time is less than 24 hours ago expired_files = db.file.find({"time": {"$lt": twenty_four_hours_ago}}) async for file_data in expired_files: chat_id = file_data.get("user_id") user_msg_id = file_data.get("user_msg_id") log_msg_id = file_data.get("log_msg_id") db_id = file_data.get("_id") # 1. Edit User Message to show expired warning if user_msg_id and chat_id: try: if str(chat_id).startswith("-100"): # If it's in a channel, just remove the inline download button await SystummBot.edit_message_reply_markup( chat_id=chat_id, message_id=user_msg_id, reply_markup=None ) else: # If it's a private chat, replace the text entirely and drop buttons await SystummBot.edit_message_text( chat_id=chat_id, message_id=user_msg_id, text="**⚠️ The generated link has expired.**\n\n_Please resend the file to generate a new link which will be valid for 24 hours._", reply_markup=None ) except FloodWait as e: await asyncio.sleep(e.value) except Exception as e: logging.debug(f"Sweeper: Could not edit user msg {user_msg_id}: {e}") # 2. Delete the physical file from the Log Channel if log_msg_id: try: await SystummBot.delete_messages(chat_id=Telegram.FLOG_CHANNEL, message_ids=log_msg_id) except Exception as e: logging.debug(f"Sweeper: Could not delete log msg {log_msg_id}: {e}") # 3. Hard Delete from MongoDB Database & Update Link Count await db.delete_one_file(db_id) await db.count_links(chat_id, "-") logging.info("Sweep complete. Sleeping for 1 hour.") except Exception as e: logging.error(f"Sweeper Critical Error: {e}") # Sleep for 3600 seconds (1 hour) before doing the next sweep await asyncio.sleep(3600)