forked from Createbots23/TGVid-Comp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadmin_panel.py
More file actions
190 lines (167 loc) · 7.16 KB
/
admin_panel.py
File metadata and controls
190 lines (167 loc) · 7.16 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
from config import Config
import traceback
from helper.database import db
from pyrogram.types import Message
from pyrogram import Client, filters
from pyrogram.errors import FloodWait, InputUserDeactivated, UserIsBlocked, PeerIdInvalid
import os
import sys
import time
import asyncio
import logging
import datetime
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
@Client.on_message(filters.command(["stats", "status"]) & filters.user(Config.ADMIN))
async def get_stats(bot, message):
total_users = await db.total_users_count()
uptime = time.strftime("%Hh%Mm%Ss", time.gmtime(
time.time() - Config.BOT_UPTIME))
start_t = time.time()
st = await message.reply('**Aᴄᴄᴇꜱꜱɪɴɢ Tʜᴇ Dᴇᴛᴀɪʟꜱ.....**')
end_t = time.time()
time_taken_s = (end_t - start_t) * 1000
await st.edit(text=f"**--Bᴏᴛ Sᴛᴀᴛᴜꜱ--** \n\n**⌚️ Bᴏᴛ Uᴩᴛɪᴍᴇ:** {uptime} \n**🐌 Cᴜʀʀᴇɴᴛ Pɪɴɢ:** `{time_taken_s:.3f} ᴍꜱ` \n**👭 Tᴏᴛᴀʟ Uꜱᴇʀꜱ:** `{total_users}`")
# Restart to cancell all process
@Client.on_message(filters.private & filters.command("restart") & filters.user(Config.ADMIN))
async def restart_bot(b, m):
await m.reply_text("🔄__Rᴇꜱᴛᴀʀᴛɪɴɢ.....__")
os.execl(sys.executable, sys.executable, *sys.argv)
@Client.on_message(filters.command("broadcast") & filters.user(Config.ADMIN) & filters.reply)
async def broadcast_handler(bot: Client, m: Message):
await bot.send_message(Config.LOG_CHANNEL, f"{m.from_user.mention} or {m.from_user.id} Iꜱ ꜱᴛᴀʀᴛᴇᴅ ᴛʜᴇ Bʀᴏᴀᴅᴄᴀꜱᴛ......")
all_users = await db.get_all_users()
broadcast_msg = m.reply_to_message
sts_msg = await m.reply_text("Bʀᴏᴀᴅᴄᴀꜱᴛ Sᴛᴀʀᴛᴇᴅ..!")
done = 0
failed = 0
success = 0
start_time = time.time()
total_users = await db.total_users_count()
async for user in all_users:
sts = await send_msg(user['id'], broadcast_msg)
if sts == 200:
success += 1
else:
failed += 1
if sts == 400:
await db.delete_user(user['id'])
done += 1
if not done % 20:
await sts_msg.edit(f"Bʀᴏᴀᴅᴄᴀꜱᴛ Iɴ Pʀᴏɢʀᴇꜱꜱ: \nTᴏᴛᴀʟ Uꜱᴇʀꜱ {total_users} \nCᴏᴍᴩʟᴇᴛᴇᴅ: {done} / {total_users}\nSᴜᴄᴄᴇꜱꜱ: {success}\nFᴀɪʟᴇᴅ: {failed}")
completed_in = datetime.timedelta(seconds=int(time.time() - start_time))
await sts_msg.edit(f"Bʀᴏᴀᴅᴄᴀꜱᴛ Cᴏᴍᴩʟᴇᴛᴇᴅ: \nCᴏᴍᴩʟᴇᴛᴇᴅ Iɴ `{completed_in}`.\n\nTᴏᴛᴀʟ Uꜱᴇʀꜱ {total_users}\nCᴏᴍᴩʟᴇᴛᴇᴅ: {done} / {total_users}\nSᴜᴄᴄᴇꜱꜱ: {success}\nFᴀɪʟᴇᴅ: {failed}")
async def send_msg(user_id, message):
try:
await message.forward(chat_id=int(user_id))
return 200
except FloodWait as e:
await asyncio.sleep(e.value)
return send_msg(user_id, message)
except InputUserDeactivated:
logger.info(f"{user_id} : Dᴇᴀᴄᴛɪᴠᴀᴛᴇᴅ")
return 400
except UserIsBlocked:
logger.info(f"{user_id} : Bʟᴏᴄᴋᴇᴅ Tʜᴇ Bᴏᴛ")
return 400
except PeerIdInvalid:
logger.info(f"{user_id} : Uꜱᴇʀ Iᴅ Iɴᴠᴀʟɪᴅ")
return 400
except Exception as e:
logger.error(f"{user_id} : {e}")
return 500
@Client.on_message(filters.private & filters.command("ban_user") & filters.user(Config.ADMIN))
async def ban(c: Client, m: Message):
if len(m.command) == 1:
await m.reply_text(
f"Use this command to ban any user from the bot.\n\n"
f"Usage:\n\n"
f"`/ban_user user_id ban_duration ban_reason`\n\n"
f"Eg: `/ban_user 1234567 28 You misused me.`\n"
f"This will ban user with id `1234567` for `28` days for the reason `You misused me`.",
quote=True
)
return
try:
user_id = int(m.command[1])
ban_duration = int(m.command[2])
ban_reason = ' '.join(m.command[3:])
ban_log_text = f"Banning user {user_id} for {ban_duration} days for the reason {ban_reason}."
try:
await c.send_message(
user_id,
f"You are banned to use this bot for **{ban_duration}** day(s) for the reason __{ban_reason}__ \n\n"
f"**Message from the admin**"
)
ban_log_text += '\n\nUser notified successfully!'
except:
traceback.print_exc()
ban_log_text += f"\n\nUser notification failed! \n\n`{traceback.format_exc()}`"
await db.ban_user(user_id, ban_duration, ban_reason)
print(ban_log_text)
await m.reply_text(
ban_log_text,
quote=True
)
except:
traceback.print_exc()
await m.reply_text(
f"Error occoured! Traceback given below\n\n`{traceback.format_exc()}`",
quote=True
)
@Client.on_message(filters.private & filters.command("unban_user") & filters.user(Config.ADMIN))
async def unban(c: Client, m: Message):
if len(m.command) == 1:
await m.reply_text(
f"Use this command to unban any user.\n\n"
f"Usage:\n\n`/unban_user user_id`\n\n"
f"Eg: `/unban_user 1234567`\n"
f"This will unban user with id `1234567`.",
quote=True
)
return
try:
user_id = int(m.command[1])
unban_log_text = f"Unbanning user {user_id}"
try:
await c.send_message(
user_id,
f"Your ban was lifted!"
)
unban_log_text += '\n\nUser notified successfully!'
except:
traceback.print_exc()
unban_log_text += f"\n\nUser notification failed! \n\n`{traceback.format_exc()}`"
await db.remove_ban(user_id)
print(unban_log_text)
await m.reply_text(
unban_log_text,
quote=True
)
except:
traceback.print_exc()
await m.reply_text(
f"Error occurred! Traceback given below\n\n`{traceback.format_exc()}`",
quote=True
)
@Client.on_message(filters.private & filters.command("banned_users") & filters.user(Config.ADMIN))
async def _banned_users(_, m: Message):
all_banned_users = await db.get_all_banned_users()
banned_usr_count = 0
text = ''
async for banned_user in all_banned_users:
user_id = banned_user['id']
ban_duration = banned_user['ban_status']['ban_duration']
banned_on = banned_user['ban_status']['banned_on']
ban_reason = banned_user['ban_status']['ban_reason']
banned_usr_count += 1
text += f"> **user_id**: `{user_id}`, **Ban Duration**: `{ban_duration}`, " \
f"**Banned on**: `{banned_on}`, **Reason**: `{ban_reason}`\n\n"
reply_text = f"Total banned user(s): `{banned_usr_count}`\n\n{text}"
if len(reply_text) > 4096:
with open('banned-users.txt', 'w') as f:
f.write(reply_text)
await m.reply_document('banned-users.txt', True)
os.remove('banned-users.txt')
return
await m.reply_text(reply_text, True)