File size: 6,447 Bytes
9aab5bc 796c343 9aab5bc 61535c6 9aab5bc 796c343 9aab5bc 796c343 9aab5bc 796c343 9aab5bc |
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
from datetime import datetime as dt
import logging
from database import db
from config import PRIVATE_GROUP_ID, API_ID, API_HASH
from Detection.manager.builder_session import generate_random_string
from pyrogram import Client, filters
from pyrogram.errors import AuthKeyUnregistered
from pyrogram.types import (
CallbackQuery,
InlineKeyboardMarkup,
InlineKeyboardButton
)
LOGS = logging.getLogger(__name__)
def initial_client_user(session: str, plugins: str = "UserBot"):
client_name = generate_random_string(12)
return Client(
"{}".format(client_name),
api_id=API_ID,
api_hash=API_HASH,
session_string=session,
plugins={"root": f"Detection.{plugins}"}
)
@Client.on_callback_query(filters.regex(r"^statusub_(\d+)$"))
async def check_request(bot: Client, cb: CallbackQuery):
user_id = int(cb.matches[0].group(1))
request = await db.users_detection.find_one({"user_id": user_id})
if not request:
await cb.answer("No active requests found", show_alert=True)
return
status_icon = (
"π’"
if request["user_client"][0]["status"] == "approved"
else "π΄"
if request["user_client"][0]["status"] == "rejected"
else "β οΈ"
if request["user_client"][0]["status"] == "stopped"
else "π‘"
)
await cb.answer(
f"Request Status: {status_icon} {request['user_client'][0]['status'].capitalize()}\n"
f"Submitted: {request['user_client'][0].get('timestamp', 'Not available')}\n",
show_alert=True
)
@Client.on_callback_query(filters.regex(r"^(rejected_ub|pending_ub|approved_ub)_(\d+)$"))
async def admins_callback(client: Client, callback: CallbackQuery):
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.users_detection.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
if action_type == "rejected":
await callback.answer("Soon Fixed", 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(
PRIVATE_GROUP_ID,
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
await db.users_detection.update_one(
{"_id": request["_id"]},
{
"$set": {
"user_client.$[target].user_id": bot_user.id,
"user_client.$[target].status": "approved",
"user_client.$[target].is_active": True,
"user_client.$[target].username": bot_user.username or "N/A",
"user_client.$[target].started_at": dt.now().isoformat(),
"user_client.$[target].admin_action": {
"by": admin_id,
"at": dt.now().isoformat()
}
}
},
array_filters=[{"target.user_id": int(user_id)}]
)
await notify_userub(client, user_id, bot_user)
await client.send_message(
PRIVATE_GROUP_ID,
f"β
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 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(
PRIVATE_GROUP_ID,
f"π¨ Admin Action Error\n\n"
f"Action: {action}\n"
f"Admin: {admin_mention}\n"
f"Error: {str(error)}"
)
async def notify_userub(client, user_id, bot_user):
caption = (
"**Your Detection 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"
)
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")]
])
) |