import time from datetime import datetime as dt, timedelta from pyrogram import Client as ren from pyrogram import * from pyrogram.types import * from pyrogram.errors import * from akn.utils.database import db as db_client from akn.utils.logger import LOGS from akn.manage.new_start_funcs import initial_client_bots, initial_client_user from config import * storage_running = {} active_bots = {} @ren.on_callback_query(filters.regex(r"^stopcts_(\d+)$")) async def stopv_client(bot: Client, cb: CallbackQuery): user_id = int(cb.matches[0].group(1)) if user_id not in storage_running: await cb.answer("No active client found for this user.", show_alert=True) return user_bots = storage_running[user_id] if not user_bots: await cb.answer("No active client found for this user.", show_alert=True) return if not user_bots.is_connected: await cb.answer("Client is not connected, cannot stop.", show_alert=True) return try: await user_bots.stop() del storage_running[user_id] await cb.answer("Client stopped successfully.", show_alert=True) except AttributeError: await cb.answer("Client object does not have 'stop' method.", show_alert=True) return except Exception as e: await cb.answer(f"Error stopping client: {type(e).__name__}: {str(e)}", show_alert=True) return @ren.on_callback_query(filters.regex(r"^(rejected_ub|pending_ub|approved_ub)_(\d+)$")) async def ubadmin_action(client: Client, callback: CallbackQuery): global storage_running try: action, user_id = callback.matches[0].groups() action_type = action.split('_')[0] admin_id = callback.from_user.id admin_mention = callback.from_user.mention if admin_id != 6477856957: return await callback.answer("❌ Only Developer", show_alert=True) request = await db_client.session.find_one({"user_id": int(user_id)}) if not request: await callback.answer("❌ User request not found!", show_alert=True) await callback.message.edit_text(f"{callback.message.text}\n\n⚠️ Failed: Request not found") return update_data = { "user_client.$.status": action_type, "last_updated": dt.now().isoformat(), "user_client.$.admin_action": { "by": admin_id, "at": dt.now().isoformat(), "username": callback.from_user.username } } if action_type == "rejected": update_data["user_client.$.admin_action"]["reason"] = "No reason provided" update_result = await db_client.session.update_one( { "_id": request["_id"], "user_client.status": "pending", "user_client.user_id": int(user_id) }, {"$set": update_data} ) if not update_result.modified_count: await callback.answer("❌ Update failed!", show_alert=True) return if action_type == "approved": await handle_approvalub(client, callback, request, user_id, admin_id, admin_mention) status_icon = { "approved": "✅", "rejected": "❌", "pending": "🕒" }.get(action_type, "ℹ️") await callback.message.edit_text( f"{callback.message.text}\n\n" f"{status_icon} Status: {action_type.capitalize()}ed by {admin_mention}\n" f"⏰ {dt.now().strftime('%Y-%m-%d %H:%M:%S')}", reply_markup=None ) await callback.answer(f"Request {action_type}d successfully!") except Exception as e: LOGS.error(f"Admin action error: {str(e)}") await handle_errorub(client, callback, e, action, admin_mention) async def handle_approvalub(client, callback, request, user_id, admin_id, admin_mention): try: string_session = request["user_client"][0]["session_string"] user_bots = initial_client_user(string_session) try: await user_bots.start() bot_user = await user_bots.get_me() except AuthKeyUnregistered as e: await client.send_message( -1002407639480, f"Error reason: AuthKeyUnregistered `{user_id}`" ) await client.send_message(user_id, "Error reason: AuthKeyUnregistered") return except Exception as e: LOGS.error(f"Error handle_approvalub: {str(e)}") await client.send_message( user_id, "⚠️ Userbot approval failed due to technical reasons.\n" "Our team has been notified. Please try again later." ) return global storage_running storage_running[user_id] = user_bots await db_client.session.update_one( { "_id": request["_id"], "user_client.status": "pending", "user_client.user_id": int(user_id) }, { "$set": { "user_client.$.user_id": bot_user.id, "user_client.$.status": "approved", "user_client.$.is_active": True, "user_client.$.username": bot_user.username or "N/A", "user_client.$.started_at": dt.now().isoformat(), "user_client.$.admin_action": { "by": admin_id, "at": dt.now().isoformat() } } } ) await notify_userub(client, user_id, bot_user) await client.send_message( -1002407639480, f"✅ Userbot Approved Successfully\n\n" f"👤 User: {request.get('username', 'N/A')} ({user_id})\n" f"⭐ Username: {bot_user.username}\n" f"🛠 Approved by: {admin_mention}\n" f"⏰ Time: {dt.now().strftime('%Y-%m-%d %H:%M:%S')}" ) except Exception as e: LOGS.error(f"Approval error: {str(e)}") async def notify_userub(client, user_id, bot_user): caption = ( "🎉 **Your Userbot Has Been Approved!**\n\n" f"🔱 Name: {bot_user.first_name}\n" f"✔ Username: @{bot_user.username or 'N/A'}\n" f"🆔 User ID: `{bot_user.id}`\n\n" "You can now userbot try `.ping`\n\n" "Akeno Userbot By akn-dev" ) await client.send_photo( user_id, photo="https://telegra.ph//file/586a3867c3e16ca6bb4fa.jpg", caption=caption, reply_markup=InlineKeyboardMarkup([ [InlineKeyboardButton("Channel", url="https://t.me/RendyProjects")] ]) ) async def handle_errorub(client, callback, error, action, admin_mention): await callback.answer("⚠️ Error", show_alert=True) await callback.message.edit_text( f"{callback.message.text}\n\n❌ Error: {str(error)}" ) await client.send_message( -1002407639480, f"🚨 Admin Action Error\n\n" f"Action: {action}\n" f"Admin: {admin_mention}\n" f"Error: {str(error)}" ) @ren.on_callback_query(filters.regex(r"^(rejected_alldl|pending_alldl|approved_alldl)_(\d+)_(\w+)$")) async def handle_admin_action(client: Client, callback: CallbackQuery): global storage_running action, user_id, uuid = callback.matches[0].groups() action_type = action.split('_')[0] admin_id = callback.from_user.id admin_mention = callback.from_user.mention if admin_id != 6477856957: return await callback.answer("❌ Only Developer", show_alert=True) try: request = await db_client.alldl_bot.find_one({"user_id": int(user_id)}) if not request: await callback.answer("❌ User request not found!", show_alert=True) return await callback.message.edit_text( f"{callback.message.text}\n\n⚠️ Failed: Request not found in database" ) target_bot = None for bot in request.get("bots", []): if bot.get("uuid") == uuid: target_bot = bot break if not target_bot: await callback.answer("❌ No matching bot found!", show_alert=True) return await callback.message.edit_text( f"{callback.message.text}\n\n⚠️ Failed: No matching bot found" ) bot_token = target_bot["bot_token"] update_data = { "bots.$.status": action_type, "bots.$.admin_action": { "by": admin_id, "at": dt.now().strftime("%Y-%m-%d %H:%M:%S"), "username": callback.from_user.username }, "last_updated": dt.now().strftime("%Y-%m-%d %H:%M:%S") } if action_type == "rejected": update_data["bots.$.status"] = "rejected" update_data["bots.$.admin_action"]["reason"] = "No reason provided" update_result = await db_client.alldl_bot.update_one( { "user_id": int(user_id), "bots.uuid": uuid }, {"$set": update_data} ) if not update_result.modified_count: await callback.answer("❌ Update failed!", show_alert=True) return await callback.message.edit_text( f"{callback.message.text}\n\n⚠️ Failed: update unsuccessful" ) if action_type == "approved": try: user_bots = initial_client_bots(bot_token) await user_bots.start() storage_running[user_id] = user_bots bot_user = await user_bots.get_me() bot_username = f"@{bot_user.username}" link_start = f"https://t.me/{bot_user.username}?start=start" await db_client.alldl_bot.update_one( { "user_id": int(user_id), "bots.uuid": uuid }, { "$set": { "bots.$.bot_id": bot_user.id, "bots.$.status": "approved", "bots.$.is_active": True, "bots.$.created_at": dt.now().strftime("%Y-%m-%d %H:%M:%S"), "bots.$.start_time": dt.now(), "bots.$.bot_username": f"@{bot_user.username}", "bots.$.bot_id": bot_user.id, "bots.$.started_at": dt.now().strftime("%Y-%m-%d %H:%M:%S"), "bots.$.admin_action": { "by": admin_id, "at": dt.now().strftime("%Y-%m-%d %H:%M:%S") } }, "$inc": {"deployed_bots": 1} } ) caption = ( "🎉 **Your Azrea Bot Has Been Approved!**\n\n" f"🤖 Bot Name: {bot_user.first_name}\n" f"🔗 Username: {bot_username}\n" f"🆔 Bot ID: `{bot_user.id}`\n\n" "You can now start using your bot!\n\n" "All Downloader Bot By akn-dev" ) keyboard_start_now = InlineKeyboardMarkup([ [InlineKeyboardButton("🚀 Start Using Bot", url=link_start)], [InlineKeyboardButton("📊 Manage Bots", callback_data="my_bots")] ]) await client.send_photo( user_id, photo="https://telegra.ph//file/586a3867c3e16ca6bb4fa.jpg", caption=caption, reply_markup=keyboard_start_now ) await client.send_message( -1002407639480, f"✅ Bot Approved Successfully\n\n" f"👤 User: {request.get('username', 'N/A')} ({user_id})\n" f"🤖 Bot: {bot_username}\n" f"🛠 Approved by: {admin_mention}\n" f"⏰ Time: {dt.now().strftime('%Y-%m-%d %H:%M:%S')}" ) except Exception as e: LOGS.error(f"Bot approval error: {str(e)}") await db_client.alldl_bot.update_one( { "user_id": int(user_id), "bots.uuid": uuid }, { "$set": { "bots.$.is_active": False, "bots.$.error": str(e), "bots.$.status": "error" } } ) await client.send_message( -1002407639480, f"❌ Bot Approval Failed\n\n" f"User ID: {user_id}\n" f"Error: {str(e)}\n" f"UUID: `{uuid}`" ) await client.send_message( user_id, "⚠️ Bot approval failed due to technical reasons.\n" "Our team has been notified. Please try again later." ) return status_icon = { "approved": "✅", "rejected": "❌", "pending": "🕒" }.get(action_type, "ℹ️") await callback.message.edit_text( f"{callback.message.text}\n\n" f"{status_icon} Status: {action_type.capitalize()}ed by {admin_mention}\n" f"⏰ {dt.now().strftime('%Y-%m-%d %H:%M:%S')}", reply_markup=None ) await callback.answer(f"Request {action_type}d successfully!") except Exception as e: LOGS.error(f"Admin action error: {str(e)}") await callback.answer("⚠️ error!", show_alert=True) await callback.message.edit_text( f"{callback.message.text}\n\n" f"❌ Error: {str(e)}" ) await client.send_message( -1002407639480, f"🚨 Admin Action Error\n\n" f"Action: {action}\n" f"Admin: {admin_mention}\n" f"Error: {str(e)}" ) @ren.on_callback_query(filters.regex(r"^(approve_capt|reject_capt)_(\d+)$")) async def handle_captbot_action(client, callback: CallbackQuery): global storage_running action, user_id = callback.matches[0].groups() action_type = action.replace("_capt", "") if callback.from_user.id != 6477856957: return await callback.answer("❌ Only Developer", show_alert=True) update_data = { "status": "approved" if action_type == "approve" else "rejected", "admin_action": { "by": 6477856957, "at": dt.now().strftime("%Y-%m-%d %H:%M:%S") } } if action == "reject_capt": update_data["admin_action"]["reason"] = "No reason provided" await db_client.captcha_bot.update_one( {"user_id": int(user_id)}, {"$set": update_data}, upsert=True ) if action == "approve_capt": request = await db_client.captcha_bot.find_one({"user_id": int(user_id)}) bot_token = request["bot_token"] try: user_bots = initial_client_bots(bot_token, "ApproveBot") await user_bots.start() storage_running[user_id] = user_bots except Exception as e: await client.send_message(user_id, "Sorry Try Again") await client.send_message( -1002407639480, f"Error captcha {str(e)}" ) return try: bot_user = await user_bots.get_me() bot_username = "@" + bot_user.username bot_first_name = bot_user.first_name link_start = "https://t.me/{}?start=start".format(bot_user.username) caption = "" caption += "Bot Name : {}\n".format(bot_first_name) caption += "Bot Username : {}\n".format(bot_username) caption += "Bot ID : {}\n".format(bot_user.id) caption += "Captcha Bot By akn-dev\n" keyboard_start_now = InlineKeyboardMarkup( [ [ InlineKeyboardButton( text="Check Start Bot", url=link_start ), InlineKeyboardButton( text="Stop Client", callback_data=f"stopcts_{user_id}" ) ], ] ) await client.send_photo( user_id, photo="https://telegra.ph//file/586a3867c3e16ca6bb4fa.jpg", caption=caption, reply_markup=keyboard_start_now ) await db_client.captcha_bot.update_one( {"user_id": user_id}, {"$set": {"status": "approved"}}, upsert=True ) except Exception as e: await client.send_message( -1002407639480, f"Error captcha {str(e)}" ) await client.send_message(user_id, "Sorry Try Again") return status_icon = "✅" if action_type == "approve" else "❌" await callback.message.edit_text( f"{callback.message.text}\n\n" f"Status: {status_icon} {action_type.capitalize()}ed by admin", reply_markup=None ) await callback.answer(f"Request {action_type}ed!") @ren.on_callback_query(filters.regex(r"^(approve_sesibot|reject_sesibot)_(\d+)$")) async def handle_sesibot_action(client, callback: CallbackQuery): global storage_running action, user_id = callback.matches[0].groups() action_type = action.replace("_sesibot", "") if callback.from_user.id != 6477856957: return await callback.answer("❌ Only Developer", show_alert=True) update_data = { "status": "approved" if action_type == "approve" else "rejected", "admin_action": { "by": 6477856957, "at": dt.now().strftime("%Y-%m-%d %H:%M:%S") } } if action == "reject_sesibot": update_data["admin_action"]["reason"] = "No reason provided" await db_client.session_bot.update_one( {"user_id": int(user_id)}, {"$set": update_data}, upsert=True ) if action == "approve_sesibot": request = await db_client.session_bot.find_one({"user_id": int(user_id)}) bot_token = request["bot_token"] try: user_bots = initial_client_bots(bot_token, plugins="SessionBot") await user_bots.start() storage_running[user_id] = user_bots except Exception as e: await client.send_message(user_id, "Sorry Try Again") await client.send_message( -1002407639480, f"Error sesibot {str(e)}" ) return try: bot_user = await user_bots.get_me() bot_username = "@" + bot_user.username bot_first_name = bot_user.first_name link_start = "https://t.me/{}?start=start".format(bot_user.username) caption = "" caption += "Bot Name : {}\n".format(bot_first_name) caption += "Bot Username : {}\n".format(bot_username) caption += "Bot ID : {}\n".format(bot_user.id) caption += "Session Bot By akn-dev\n" keyboard_start_now = InlineKeyboardMarkup( [ [ InlineKeyboardButton( text="Check Start Bot", url=link_start ) ], ] ) await client.send_photo( user_id, photo="https://telegra.ph//file/586a3867c3e16ca6bb4fa.jpg", caption=caption, reply_markup=keyboard_start_now ) await db_client.session_bot.update_one( {"user_id": user_id}, {"$set": {"status": "approved"}}, upsert=True ) except Exception as e: await client.send_message( -1002407639480, f"Error sesibot {str(e)}" ) await client.send_message(user_id, "Sorry Try Again") return status_icon = "✅" if action_type == "approve" else "❌" await callback.message.edit_text( f"{callback.message.text}\n\n" f"Status: {status_icon} {action_type.capitalize()}ed by admin", reply_markup=None ) await callback.answer(f"Request {action_type}ed!") @ren.on_callback_query(filters.regex(r"^(approve_magicbot|reject_magicbot)_(\d+)$")) async def handle_magicbot_action(client, callback: CallbackQuery): global storage_running action, user_id = callback.matches[0].groups() action_type = action.replace("_magicbot", "") if callback.from_user.id != 6477856957: return await callback.answer("❌ Only Developer", show_alert=True) update_data = { "status": "approved" if action_type == "approve" else "rejected", "admin_action": { "by": 6477856957, "at": dt.now().strftime("%Y-%m-%d %H:%M:%S") } } if action == "reject_magicbot": update_data["admin_action"]["reason"] = "No reason provided" await db_client.magic_bot.update_one( {"user_id": int(user_id)}, {"$set": update_data}, upsert=True ) if action == "approve_magicbot": request = await db_client.magic_bot.find_one({"user_id": int(user_id)}) bot_token = request["bot_token"] try: user_bots = initial_client_bots(bot_token, plugins="MagicFonts") await user_bots.start() storage_running[user_id] = user_bots except Exception as e: await client.send_message(user_id, "Sorry Try Again") await client.send_message( -1002407639480, f"Error magic {str(e)}" ) return try: bot_user = await user_bots.get_me() bot_username = "@" + bot_user.username bot_first_name = bot_user.first_name link_start = "https://t.me/{}?start=start".format(bot_user.username) caption = "" caption += "Bot Name : {}\n".format(bot_first_name) caption += "Bot Username : {}\n".format(bot_username) caption += "Bot ID : {}\n".format(bot_user.id) caption += "Magic Fonts Bot By akn-dev\n" keyboard_start_now = InlineKeyboardMarkup( [ [ InlineKeyboardButton( text="Check Start Bot", url=link_start ), InlineKeyboardButton( text="Stop Client", callback_data=f"stopcts_{user_id}" ) ], ] ) await client.send_photo( user_id, photo="https://telegra.ph//file/586a3867c3e16ca6bb4fa.jpg", caption=caption, reply_markup=keyboard_start_now ) await db_client.magic_bot.update_one( {"user_id": user_id}, {"$set": {"status": "approved"}}, upsert=True ) except Exception as e: await client.send_message( -1002407639480, f"Error Magic {str(e)}" ) await client.send_message(user_id, "Sorry Try Again") return status_icon = "✅" if action_type == "approve" else "❌" await callback.message.edit_text( f"{callback.message.text}\n\n" f"Status: {status_icon} {action_type.capitalize()}ed by admin", reply_markup=None ) await callback.answer(f"Request {action_type}ed!") @ren.on_callback_query(filters.regex(r"^(approve_ytbot|reject_ytbot)_(\d+)$")) async def handle_ytbot_action(client, callback: CallbackQuery): global storage_running action, user_id = callback.matches[0].groups() action_type = action.replace("_magicbot", "") if callback.from_user.id != 6477856957: return await callback.answer("❌ Only Developer", show_alert=True) update_data = { "status": "approved" if action_type == "approve" else "rejected", "admin_action": { "by": 6477856957, "at": dt.now().strftime("%Y-%m-%d %H:%M:%S") } } if action == "reject_ytbot": update_data["admin_action"]["reason"] = "No reason provided" await db_client.youtube_bot.update_one( {"user_id": int(user_id)}, {"$set": update_data}, upsert=True ) if action == "approve_ytbot": request = await db_client.youtube_bot.find_one({"user_id": int(user_id)}) bot_token = request["bot_token"] try: user_bots = initial_client_bots(bot_token, plugins="Youtube") await user_bots.start() except Exception as e: await client.send_message(user_id, "Sorry Try Again") await client.send_message( -1002407639480, f"Error ytbot {str(e)}" ) return try: bot_user = await user_bots.get_me() bot_username = "@" + bot_user.username bot_first_name = bot_user.first_name link_start = "https://t.me/{}?start=start".format(bot_user.username) caption = "" caption += "Bot Name : {}\n".format(bot_first_name) caption += "Bot Username : {}\n".format(bot_username) caption += "Bot ID : {}\n".format(bot_user.id) caption += "Youtube Bot By akn-dev\n" keyboard_start_now = InlineKeyboardMarkup( [ [ InlineKeyboardButton( text="Check Start Bot", url=link_start ), InlineKeyboardButton( text="Stop Client", callback_data=f"stopcts_{user_id}" ) ], ] ) await client.send_photo( user_id, photo="https://telegra.ph//file/586a3867c3e16ca6bb4fa.jpg", caption=caption, reply_markup=keyboard_start_now ) await db_client.youtube_bot.update_one( {"user_id": user_id}, {"$set": {"status": "approved"}}, upsert=True ) except Exception as e: await client.send_message( -1002407639480, f"Error Ytbot {str(e)}" ) await client.send_message(user_id, "Sorry Try Again") return status_icon = "✅" if action_type == "approve" else "❌" await callback.message.edit_text( f"{callback.message.text}\n\n" f"Status: {status_icon} {action_type.capitalize()}ed by admin", reply_markup=None ) await callback.answer(f"Request {action_type}ed!") @ren.on_callback_query(filters.regex(r"^(approve_geminibot|reject_geminibot)_(\d+)$")) async def handle_geminibot_action(client, callback: CallbackQuery): global storage_running action, user_id = callback.matches[0].groups() action_type = action.replace("_geminibot", "") if callback.from_user.id != 6477856957: return await callback.answer("❌ Only Developer", show_alert=True) update_data = { "status": "approved" if action_type == "approve" else "rejected", "admin_action": { "by": 6477856957, "at": dt.now().strftime("%Y-%m-%d %H:%M:%S") } } if action == "reject_geminibot": update_data["admin_action"]["reason"] = "No reason provided" await db_client.gemini_bot.update_one( {"user_id": int(user_id)}, {"$set": {"status": "approved"}}, upsert=True ) if action == "approve_geminibot": request = await db_client.gemini_bot.find_one({"user_id": int(user_id)}) bot_token = request["bot_token"] try: user_bots = initial_client_bots(bot_token, plugins="Gemini") await user_bots.start() storage_running[user_id] = user_bots except Exception as e: await client.send_message(user_id, "Sorry Try Again") await client.send_message(PRIVATE_LOGS, f"Error {e}") return try: bot_user = await user_bots.get_me() bot_username = "@" + bot_user.username bot_first_name = bot_user.first_name link_start = "https://t.me/{}?start=start".format(bot_user.username) caption = "" caption += "Bot Name : {}\n".format(bot_first_name) caption += "Bot Username : {}\n".format(bot_username) caption += "Bot ID : {}\n".format(bot_user.id) caption += "Gemini Bot By akn-dev\n" keyboard_start_now = InlineKeyboardMarkup( [ [ InlineKeyboardButton( text="Check Start Bot", url=link_start ), InlineKeyboardButton( text="Stop Client", callback_data=f"stopcts_{user_id}" ) ], ] ) await client.send_photo( user_id, photo="https://telegra.ph//file/586a3867c3e16ca6bb4fa.jpg", caption=caption, reply_markup=keyboard_start_now ) await db_client.gemini_bot.update_one( {"user_id": user_id}, {"$set": {"status": "approved"}}, upsert=True ) except Exception as e: await client.send_message( -1002407639480, f"Error Gemini {str(e)}" ) await client.send_message(user_id, "Sorry Try Again") return status_icon = "✅" if action_type == "approve" else "❌" await callback.message.edit_text( f"{callback.message.text}\n\n" f"Status: {status_icon} {action_type.capitalize()}ed by admin", reply_markup=None ) await callback.answer(f"Request {action_type}ed!")