import time from datetime import datetime as dt from akn.manage.parameter import generate_random_string from pyrogram import Client as ren from pyrogram import * from pyrogram.types import * from akn.utils.database import db from akn.utils.logger import LOGS from config import * def create_bot_controls(bot_uuid_str, restart_count=0): return InlineKeyboardMarkup([ [ InlineKeyboardButton("🔄 Restart", callback_data=f"restartdl_{bot_uuid_str}"), InlineKeyboardButton("🛑 Stop", callback_data=f"stopbotdl_{bot_uuid_str}") ], [ InlineKeyboardButton("📊 Stats", callback_data=f"botstatsdl_{bot_uuid_str}"), InlineKeyboardButton("⚙️ Settings", callback_data=f"botsettingsdl_{bot_uuid_str}") ], [ InlineKeyboardButton("🔙 Back", callback_data="my_bots") ] ]) @ren.on_callback_query(filters.regex(r"^stopbotdl_(\w+)$")) async def handle_stop_botx(client: Client, callback: CallbackQuery): try: bots_uuid = callback.matches[0].group(1) user_id = callback.from_user.id bot_data = await db.alldl_bot.find_one( {"user_id": user_id, "bots.uuid": bots_uuid}, {"bots.$": 1} ) if not bot_data or not bot_data.get("bots"): return await callback.answer("❌ Bot not found!", show_alert=True) bot = bot_data["bots"][0] if not bot.get("is_active"): return await callback.answer("⚠️ Bot is already stopped.", show_alert=True) await db.alldl_bot.update_one( {"user_id": user_id, "bots.uuid": bots_uuid}, {"$set": { "bots.$.is_active": False, "bots.$.stopped_at": dt.now().strftime("%Y-%m-%d %H:%M:%S") }} ) await callback.message.edit_reply_markup( create_bot_controls(bots_uuid, restart_count=bot.get("restart_count", 0)) ) await callback.answer("🛑 Bot has been 24 hours after stopped successfully!") except Exception as e: LOGS.error(f"Stop handler error for UUID {bots_uuid}: {str(e)}") await callback.answer("⚠️ Failed to stop the bot!", show_alert=True) @ren.on_callback_query(filters.regex(r"^(restartdl)_(\w+)$")) async def handle_restart_botx(client: Client, callback: CallbackQuery): try: _, bots_uuid = callback.matches[0].groups() user_id = callback.from_user.id bot_data = await db.alldl_bot.find_one( {"user_id": user_id, "bots.uuid": bots_uuid}, {"bots.$": 1} ) if not bot_data or not bot_data.get("bots"): return await callback.answer("❌ Bot not found!", show_alert=True) bot = bot_data["bots"][0] if not bot.get("is_active"): return await callback.answer("❌ Bot is not active!", show_alert=True) if bot.get("status") != "approved": return await callback.answer("❌ Only approved bots can be restarted!", show_alert=True) try: client_name = generate_random_string(12) new_client = Client( name=client_name, api_id=API_ID, api_hash=API_HASH, bot_token=bot["bot_token"], plugins={"root": "akn.AllDownloaderBot"} ) await new_client.start() await db.alldl_bot.update_one( {"user_id": user_id, "bots.uuid": bots_uuid}, {"$set": { "bots.$.is_active": True, "bots.$.last_restart": dt.now().strftime("%Y-%m-%d %H:%M:%S"), "bots.$.restart_count": bot.get("restart_count", 0) + 1 }} ) await callback.message.edit_reply_markup( create_bot_controls(bots_uuid, restart_count=bot.get("restart_count", 0) + 1) ) await callback.answer("✅ Bot restarted successfully!") except Exception as e: LOGS.error(f"Restart error for UUID {bots_uuid}: {str(e)}") await callback.answer("❌ Failed to restart the bot!", show_alert=True) except Exception as e: LOGS.error(f"Handler restart_botx error: {str(e)}") await callback.answer("⚠️ error!", show_alert=True)