-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
326 lines (258 loc) · 10.9 KB
/
admin.py
File metadata and controls
326 lines (258 loc) · 10.9 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import os
from dotenv import load_dotenv
from aiogram import Router, types, F
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton, ReplyKeyboardRemove
from aiogram.filters import Command
import strings
import json
from database import db
import psutil
import time
from aiogram.types import FSInputFile
from aiogram.fsm.context import FSMContext
from states import BroadcastStates, BanUserStates, UnbanUserStates, UnpenaltyUserStates
load_dotenv()
router = Router()
# Load Admin IDs from env
ADMIN_IDS = set()
raw_admins = os.getenv("ADMIN_IDS", "")
if raw_admins:
try:
ADMIN_IDS = {int(x.strip()) for x in raw_admins.split(",") if x.strip()}
except ValueError:
print("⚠️ Error parsing ADMIN_IDS")
def is_admin(user_id: int) -> bool:
return user_id in ADMIN_IDS
@router.message(Command("admin"))
async def admin_help(message: types.Message):
if not is_admin(message.from_user.id):
await message.reply(strings.ADMIN_ONLY)
return
await message.reply(strings.ADMIN_HELP, parse_mode='Markdown')
@router.message(Command("ban"))
async def ban_user(message: types.Message, state: FSMContext):
if not is_admin(message.from_user.id):
await message.reply(strings.ADMIN_ONLY)
return
args = message.text.split()
if len(args) < 2:
await state.set_state(BanUserStates.waiting_for_user_id)
await message.reply(strings.PROMPT_BAN_USER_ID)
return
await process_ban_user(message, args[1])
@router.message(BanUserStates.waiting_for_user_id)
async def process_ban_user_id(message: types.Message, state: FSMContext):
if not is_admin(message.from_user.id): return
await state.clear()
await process_ban_user(message, message.text)
async def process_ban_user(message: types.Message, user_id_str: str):
try:
target_id = int(user_id_str)
# Self-ban check
if target_id == message.from_user.id:
await message.reply("⛔ You cannot ban yourself!")
return
db.ban_user(target_id)
await message.reply(strings.ADMIN_BAN_SUCCESS.format(user_id=target_id))
except ValueError:
await message.reply("⚠️ Invalid User ID.")
@router.message(Command("unban"))
async def unban_user(message: types.Message, state: FSMContext):
if not is_admin(message.from_user.id):
await message.reply(strings.ADMIN_ONLY)
return
args = message.text.split()
if len(args) < 2:
await state.set_state(UnbanUserStates.waiting_for_user_id)
await message.reply(strings.PROMPT_UNBAN_USER_ID)
return
await process_unban_user(message, args[1])
@router.message(UnbanUserStates.waiting_for_user_id)
async def process_unban_user_id(message: types.Message, state: FSMContext):
if not is_admin(message.from_user.id): return
await state.clear()
await process_unban_user(message, message.text)
async def process_unban_user(message: types.Message, user_id_str: str):
try:
target_id = int(user_id_str)
db.unban_user(target_id)
await message.reply(strings.ADMIN_UNBAN_SUCCESS.format(user_id=target_id))
except ValueError:
await message.reply("⚠️ Invalid User ID.")
START_TIME = time.time()
@router.message(Command("system"))
async def system_stats(message: types.Message):
if not is_admin(message.from_user.id): return
cpu = psutil.cpu_percent()
ram = psutil.virtual_memory().percent
uptime = int(time.time() - START_TIME)
msg = f"""
🖥️ *System Status*
CPU: {cpu}%
RAM: {ram}%
Uptime: {uptime}s
"""
await message.reply(msg, parse_mode='Markdown')
@router.message(Command("broadcast"))
async def broadcast(message: types.Message, state: FSMContext):
if not is_admin(message.from_user.id): return
text = message.text.replace("/broadcast", "").strip()
if not text:
await state.set_state(BroadcastStates.waiting_for_message)
await message.reply(strings.PROMPT_BROADCAST_MESSAGE)
return
await process_broadcast_text(message, text)
@router.message(BroadcastStates.waiting_for_message)
async def process_broadcast_message(message: types.Message, state: FSMContext):
if not is_admin(message.from_user.id): return
await state.clear()
await process_broadcast_text(message, message.text)
async def process_broadcast_text(message: types.Message, text: str):
count = len(db.users)
# Store text and current time in DB
db.set_pending_broadcast(message.from_user.id, text, time.time())
# Add confirm/cancel keyboard
kb = [
[KeyboardButton(text="Confirm"), KeyboardButton(text="Cancel")]
]
keyboard = ReplyKeyboardMarkup(keyboard=kb, resize_keyboard=True, one_time_keyboard=True)
await message.reply(strings.ADMIN_BROADCAST_CONFIRM.format(count=count, text=text), reply_markup=keyboard)
@router.message(F.text == "Confirm")
async def confirm_broadcast(message: types.Message):
if not is_admin(message.from_user.id): return
user_id = message.from_user.id
broadcast_data = db.get_pending_broadcast(user_id)
if not broadcast_data:
await message.reply(strings.ADMIN_NO_BROADCAST, reply_markup=ReplyKeyboardRemove())
return
text = broadcast_data['text']
timestamp = broadcast_data['timestamp']
# Check timeout (30 minutes = 1800 seconds)
if time.time() - timestamp > 1800:
db.clear_pending_broadcast(user_id)
await message.reply("❌ Broadcast request expired.", reply_markup=ReplyKeyboardRemove())
return
db.clear_pending_broadcast(user_id) # Clear pending
count = 0
for uid in db.users:
try:
await message.bot.send_message(uid, f"📢 *Announcement*\n\n{text}", parse_mode='Markdown')
count += 1
except Exception:
pass # User blocked bot
await message.reply(strings.ADMIN_BROADCAST_SUCCESS.format(count=count), reply_markup=ReplyKeyboardRemove())
@router.message(F.text == "Cancel")
async def cancel_broadcast(message: types.Message):
if not is_admin(message.from_user.id): return
user_id = message.from_user.id
if db.get_pending_broadcast(user_id):
db.clear_pending_broadcast(user_id)
await message.reply(strings.ADMIN_BROADCAST_CANCEL, reply_markup=ReplyKeyboardRemove())
else:
await message.reply(strings.ADMIN_NO_BROADCAST, reply_markup=ReplyKeyboardRemove())
@router.message(Command("logs"))
async def get_logs(message: types.Message):
if not is_admin(message.from_user.id): return
log_file = "activity.log"
if os.path.exists(log_file) and os.path.getsize(log_file) > 0:
from datetime import datetime
await message.reply_document(
FSInputFile(log_file),
caption=f"📜 Activity Log — {datetime.now().strftime('%Y-%m-%d')}"
)
else:
await message.reply("📂 Activity log is empty or missing.")
@router.message(Command("stats"))
async def stats(message: types.Message):
if not is_admin(message.from_user.id): return
total_users = len(db.users)
now = time.time()
active_today = sum(1 for u in db.users.values() if u.get('last_active', 0) > now - 86400)
active_week = sum(1 for u in db.users.values() if u.get('last_active', 0) > now - 7*86400)
perf = db.stats.get('performance', {'total_time': 0, 'count': 0})
avg_time = (perf['total_time'] / perf['count']) if perf['count'] > 0 else 0
errors = db.stats.get('errors', {})
error_summary = "\n".join([f"- {k}: {v}" for k, v in errors.items()]) or "None"
msg = f"""
📊 *Admin Dashboard*
👥 *User Stats*
Total: {total_users}
Active (24h): {active_today}
Active (7d): {active_week}
⚡ *Performance*
Avg Gen Time: {avg_time:.3f}s
Total Gens: {perf['count']}
⚠️ *Errors*
{error_summary}
🛠 *Commands*
/users - List all User IDs
/userban - List Banned Users
/command - View Command Usage
"""
await message.reply(msg, parse_mode='Markdown')
@router.message(Command("users"))
async def list_users(message: types.Message):
if not is_admin(message.from_user.id): return
if not db.users:
await message.reply("👥 No users found.")
return
user_list = "\n".join([f"{i+1}. {uid}" for i, uid in enumerate(db.users.keys())])
# Simple truncation to avoid hitting telegram limits (4096 chars)
if len(user_list) > 4000:
user_list = user_list[:4000] + "\n... (truncated)"
await message.reply(f"👥 *User List* ({len(db.users)})\n\n{user_list}", parse_mode='Markdown')
@router.message(Command("userban"))
async def list_banned(message: types.Message):
if not is_admin(message.from_user.id): return
if not db.banned:
await message.reply("🚫 No banned users.")
return
banned_list = "\n".join([f"{i+1}. {uid}" for i, uid in enumerate(db.banned)])
await message.reply(f"🚫 *Banned Users* ({len(db.banned)})\n\n{banned_list}", parse_mode='Markdown')
@router.message(Command("command"))
async def list_commands(message: types.Message):
if not is_admin(message.from_user.id): return
msg = f"""
🔢 *Command Usage*
```
{json.dumps(db.stats, indent=2)}
```
"""
await message.reply(msg, parse_mode='Markdown')
@router.message(Command("penalties"))
async def list_penalties(message: types.Message):
if not is_admin(message.from_user.id): return
penalties = []
current_time = time.time()
for uid, data in db.security.items():
penalty_end = data.get('penalty_end', 0)
if penalty_end > current_time:
remaining = int(penalty_end - current_time)
penalties.append(f"User: `{uid}` | Remaining: {remaining}s")
if not penalties:
await message.reply("✅ No active penalties.")
else:
await message.reply("⚠️ *Active Penalties*\n\n" + "\n".join(penalties), parse_mode='Markdown')
@router.message(Command("unpenalty"))
async def unpenalty_user(message: types.Message, state: FSMContext):
if not is_admin(message.from_user.id): return
args = message.text.split()
if len(args) < 2:
await state.set_state(UnpenaltyUserStates.waiting_for_user_id)
await message.reply(strings.PROMPT_UNPENALTY_USER_ID)
return
await process_unpenalty_user(message, args[1])
@router.message(UnpenaltyUserStates.waiting_for_user_id)
async def process_unpenalty_user_id(message: types.Message, state: FSMContext):
if not is_admin(message.from_user.id): return
await state.clear()
await process_unpenalty_user(message, message.text)
async def process_unpenalty_user(message: types.Message, user_id_str: str):
target_id = user_id_str
if target_id in db.security:
db.security[target_id]['penalty_end'] = 0
db.security[target_id]['violations'] = 0
db.save()
await message.reply(f"✅ Penalty removed for user `{target_id}`.", parse_mode='Markdown')
else:
await message.reply("⚠️ User not found in security database.")