Spaces:
Runtime error
Runtime error
Create admin.py
Browse files- handlers/admin.py +120 -0
handlers/admin.py
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# filename: handlers/admin.py
|
| 2 |
+
|
| 3 |
+
import logging
|
| 4 |
+
from telethon import events
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
import re
|
| 7 |
+
|
| 8 |
+
from core.bot import bot, BATCH_JOBS, PREMIUM_QUEUE, FREE_QUEUE
|
| 9 |
+
from database import manager as db_manager
|
| 10 |
+
import templates
|
| 11 |
+
import config
|
| 12 |
+
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
# This decorator ensures only the owner can use these commands
|
| 16 |
+
@bot.on(events.NewMessage(from_users=config.OWNER_ID))
|
| 17 |
+
async def admin_handler(event):
|
| 18 |
+
# This function acts as a router for all admin commands
|
| 19 |
+
text = event.text.strip()
|
| 20 |
+
command = text.split(' ')[0]
|
| 21 |
+
|
| 22 |
+
# Simple commands
|
| 23 |
+
if command == '/stats':
|
| 24 |
+
await stats_command(event)
|
| 25 |
+
# Commands with arguments
|
| 26 |
+
elif command == '/grantpremium':
|
| 27 |
+
await grant_premium_command(event)
|
| 28 |
+
elif command == '/ban':
|
| 29 |
+
await ban_command(event, ban_status=True)
|
| 30 |
+
elif command == '/unban':
|
| 31 |
+
await ban_command(event, ban_status=False)
|
| 32 |
+
elif command == '/authgroup':
|
| 33 |
+
await auth_group_command(event, status=True)
|
| 34 |
+
elif command == '/deauthgroup':
|
| 35 |
+
await auth_group_command(event, status=False)
|
| 36 |
+
|
| 37 |
+
async def stats_command(event):
|
| 38 |
+
active_batches = len(BATCH_JOBS)
|
| 39 |
+
premium_q_size = PREMIUM_QUEUE.qsize()
|
| 40 |
+
free_q_size = FREE_QUEUE.qsize()
|
| 41 |
+
|
| 42 |
+
stats_text = (
|
| 43 |
+
"**🤖 Bot Status & Health**\n\n"
|
| 44 |
+
f"- **Active Batches:** `{active_batches}`\n"
|
| 45 |
+
f"- **Premium Queue:** `{premium_q_size}` tasks pending\n"
|
| 46 |
+
f"- **Free Queue:** `{free_q_size}` tasks pending\n"
|
| 47 |
+
f"- **Premium Workers:** `{config.PREMIUM_WORKERS}`\n"
|
| 48 |
+
f"- **Free Workers:** `{config.FREE_WORKERS}`"
|
| 49 |
+
)
|
| 50 |
+
await event.reply(stats_text)
|
| 51 |
+
|
| 52 |
+
async def grant_premium_command(event):
|
| 53 |
+
# Expected format: /grantpremium 1d 123456789
|
| 54 |
+
parts = event.text.split()
|
| 55 |
+
if len(parts) != 3:
|
| 56 |
+
await event.reply("Usage: `/grantpremium <duration> <user_id>` (e.g., `1d`, `7m`, `12h`)")
|
| 57 |
+
return
|
| 58 |
+
|
| 59 |
+
duration_str = parts[1]
|
| 60 |
+
user_id_str = parts[2]
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
user_id = int(user_id_str)
|
| 64 |
+
|
| 65 |
+
match = re.match(r"(\d+)([dhm])", duration_str.lower())
|
| 66 |
+
if not match:
|
| 67 |
+
raise ValueError("Invalid duration format.")
|
| 68 |
+
|
| 69 |
+
value, unit = int(match.group(1)), match.group(2)
|
| 70 |
+
|
| 71 |
+
if unit == 'd':
|
| 72 |
+
delta = timedelta(days=value)
|
| 73 |
+
elif unit == 'm':
|
| 74 |
+
delta = timedelta(days=value * 30) # Approx. for months
|
| 75 |
+
elif unit == 'h':
|
| 76 |
+
delta = timedelta(hours=value)
|
| 77 |
+
|
| 78 |
+
success = await db_manager.grant_premium(user_id, delta)
|
| 79 |
+
|
| 80 |
+
if success:
|
| 81 |
+
await event.reply(templates.BotResponses.ADMIN_PREMIUM_GRANTED.format(user_id=user_id, duration=duration_str))
|
| 82 |
+
else:
|
| 83 |
+
await event.reply(f"Error: Could not find user with ID `{user_id}`.")
|
| 84 |
+
|
| 85 |
+
except ValueError as e:
|
| 86 |
+
await event.reply(f"Invalid input: {e}. Please check the User ID and duration format.")
|
| 87 |
+
|
| 88 |
+
async def ban_command(event, ban_status: bool):
|
| 89 |
+
# Expected format: /ban 123456789
|
| 90 |
+
parts = event.text.split()
|
| 91 |
+
if len(parts) != 2:
|
| 92 |
+
await event.reply(f"Usage: `/{'ban' if ban_status else 'unban'} <user_id>`")
|
| 93 |
+
return
|
| 94 |
+
|
| 95 |
+
try:
|
| 96 |
+
user_id = int(parts[1])
|
| 97 |
+
success = await db_manager.ban_user(user_id, ban_status=ban_status)
|
| 98 |
+
if success:
|
| 99 |
+
template = templates.BotResponses.ADMIN_USER_BANNED if ban_status else templates.BotResponses.ADMIN_USER_UNBANNED
|
| 100 |
+
await event.reply(template.format(user_id=user_id))
|
| 101 |
+
else:
|
| 102 |
+
await event.reply(f"Error: Could not find user with ID `{user_id}`.")
|
| 103 |
+
except ValueError:
|
| 104 |
+
await event.reply("Invalid User ID format.")
|
| 105 |
+
|
| 106 |
+
async def auth_group_command(event, status: bool):
|
| 107 |
+
# Expected format: /authgroup -10012345678
|
| 108 |
+
parts = event.text.split()
|
| 109 |
+
if len(parts) != 2:
|
| 110 |
+
await event.reply(f"Usage: `/{'authgroup' if status else 'deauthgroup'} <group_id>`")
|
| 111 |
+
return
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
group_id = int(parts[1])
|
| 115 |
+
await db_manager.authorize_group(group_id, status=status)
|
| 116 |
+
template = templates.BotResponses.ADMIN_GROUP_AUTH if status else templates.BotResponses.ADMIN_GROUP_DEAUTH
|
| 117 |
+
await event.reply(template.format(group_id=group_id))
|
| 118 |
+
except ValueError:
|
| 119 |
+
await event.reply("Invalid Group ID format. Make sure it's a number (and usually negative).")
|
| 120 |
+
|