File size: 4,347 Bytes
21bc372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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)