forked from TeamUltroid/Ultroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmintools.py
More file actions
454 lines (410 loc) · 13.8 KB
/
admintools.py
File metadata and controls
454 lines (410 loc) · 13.8 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# Ultroid - UserBot
# Copyright (C) 2020 TeamUltroid
#
# This file is a part of < https://github.com/TeamUltroid/Ultroid/ >
# PLease read the GNU Affero General Public License in
# <https://www.github.com/TeamUltroid/Ultroid/blob/main/LICENSE/>.
"""
✘ Commands Available -
• `{i}promote <reply to user/userid/username>`
Promote the user in the chat.
• `{i}demote <reply to user/userid/username>`
Demote the user in the chat.
• `{i}ban <reply to user/userid/username> <reason>`
Ban the user from the chat.
• `{i}unban <reply to user/userid/username> <reason>`
Unban the user from the chat.
• `{i}kick <reply to user/userid/username> <reason>`
Kick the user from the chat.
• `{i}pin <reply to message>`
Pin the message in the chat
for silent pin use ({i}pin silent).
• `{i}unpin (all) <reply to message>`
Unpin the message(s) in the chat.
• `{i}purge <reply to message>`
Purge all messages from the replied message.
• `{i}purgeme <reply to message>`
Purge Only your messages from the replied message.
• `{i}purgeall <reply to message>`
Delete all msgs of replied user.
• `{i}del <reply to message>`
Delete the replied message.
• `{i}edit <new message>`
Edit your last message.
"""
import asyncio
from telethon.errors import BadRequestError
from telethon.errors.rpcerrorlist import UserIdInvalidError
from telethon.tl.functions.channels import EditAdminRequest, EditBannedRequest
from telethon.tl.types import ChatAdminRights, ChatBannedRights
from . import *
@ultroid_cmd(
pattern="promote ?(.*)",
groups_only=True,
)
async def prmte(ult):
xx = await eor(ult, "`Processing...`")
chat = await ult.get_chat()
isAdmin = chat.admin_rights
isCreator = chat.creator
if not isAdmin and not isCreator:
return await xx.edit("`Hmm, I'm not an admin here...`")
await xx.edit("`Promoting...`")
user, rank = await get_user_info(ult)
if not rank:
rank = "Admin"
if not user:
return await xx.edit("`Reply to a user to promote him!`")
try:
await ultroid_bot(
EditAdminRequest(
ult.chat_id,
user.id,
ChatAdminRights(
add_admins=False,
invite_users=True,
change_info=False,
ban_users=True,
delete_messages=True,
pin_messages=True,
),
rank,
)
)
await xx.edit(
f"[{user.first_name}](tg://user?id={user.id}) `is now an admin in {ult.chat.title} with title {rank}.`"
)
except BadRequestError:
return await xx.edit("`I don't have the right to promote you.`")
await asyncio.sleep(5)
await xx.delete()
@ultroid_cmd(
pattern="demote ?(.*)",
groups_only=True,
)
async def dmote(ult):
xx = await eor(ult, "`Processing...`")
chat = await ult.get_chat()
isAdmin = chat.admin_rights
isCreator = chat.creator
if not isAdmin and not isCreator:
return await xx.edit("`Hmm, I'm not an admin here...`")
await xx.edit("`Demoting...`")
user, rank = await get_user_info(ult)
if not rank:
rank = "Not Admin"
if not user:
return await xx.edit("`Reply to a user to demote him!`")
try:
await ultroid_bot(
EditAdminRequest(
ult.chat_id,
user.id,
ChatAdminRights(
add_admins=None,
invite_users=None,
change_info=None,
ban_users=None,
delete_messages=None,
pin_messages=None,
),
rank,
)
)
await xx.edit(
f"[{user.first_name}](tg://user?id={user.id}) `is no longer an admin in {ult.chat.title}`"
)
except BadRequestError:
return await xx.edit("`I don't have the right to demote you.`")
await asyncio.sleep(5)
await xx.delete()
@ultroid_cmd(
pattern="ban ?(.*)",
groups_only=True,
)
async def bban(ult):
xx = await eor(ult, "`Processing...`")
chat = await ult.get_chat()
isAdmin = chat.admin_rights
isCreator = chat.creator
if not isAdmin and not isCreator:
return await xx.edit("`Hmm, I'm not an admin here...`")
user, reason = await get_user_info(ult)
if not user:
return await xx.edit("`Reply to a user or give username to ban him!`")
await xx.edit("`Getting user info...`")
try:
await ultroid_bot(
EditBannedRequest(
ult.chat_id,
user.id,
ChatBannedRights(
until_date=None,
view_messages=True,
),
)
)
except BadRequestError:
return await xx.edit("`I don't have the right to ban a user.`")
except UserIdInvalidError:
await xx.edit("`I couldn't get who he is!`")
try:
reply = await ult.get_reply_message()
if reply:
await reply.delete()
except BadRequestError:
return await xx.edit(
f"[{user.first_name}](tg://user?id={user.id}) **was banned by** [{OWNER_NAME}](tg://user?id={OWNER_ID}) **in** `{ult.chat.title}`\n**Reason**: `{reason}`\n**Messages Deleted**: `False`"
)
if reason:
await xx.edit(
f"[{user.first_name}](tg://user?id={user.id}) **was banned by** [{OWNER_NAME}](tg://user?id={OWNER_ID}) **in** `{ult.chat.title}`\n**Reason**: `{reason}`"
)
else:
await xx.edit(
f"[{user.first_name}](tg://user?id={user.id}) **was banned by** [{OWNER_NAME}](tg://user?id={OWNER_ID}) **in** `{ult.chat.title}`"
)
@ultroid_cmd(
pattern="unban ?(.*)",
groups_only=True,
)
async def uunban(ult):
xx = await eor(ult, "`Processing...`")
chat = await ult.get_chat()
isAdmin = chat.admin_rights
isCreator = chat.creator
if not isAdmin and not isCreator:
return await xx.edit("`Hmm, I'm not an admin here...`")
user, reason = await get_user_info(ult)
if not user:
return await xx.edit("`Reply to a user or give username to unban him!`")
await xx.edit("`Getting user info...`")
try:
await ultroid_bot(
EditBannedRequest(
ult.chat_id,
user.id,
ChatBannedRights(
until_date=None,
view_messages=None,
),
)
)
except BadRequestError:
return await xx.edit("`I don't have the right to unban a user.`")
except UserIdInvalidError:
await xx.edit("`I couldn't get who he is!`")
if reason:
await xx.edit(
f"[{user.first_name}](tg://user?id={user.id}) **was unbanned by** [{OWNER_NAME}](tg://user?id={OWNER_ID}) **in** `{ult.chat.title}`\n**Reason**: `{reason}`"
)
else:
await xx.edit(
f"[{user.first_name}](tg://user?id={user.id}) **was unbanned by** [{OWNER_NAME}](tg://user?id={OWNER_ID}) **in** `{ult.chat.title}`"
)
@ultroid_cmd(
pattern="kick ?(.*)",
groups_only=True,
)
async def kck(ult):
xx = await eor(ult, "`Processing...`")
chat = await ult.get_chat()
isAdmin = chat.admin_rights
isCreator = chat.creator
if not isAdmin and not isCreator:
return await xx.edit("`Hmm, I'm not an admin here...`")
user, reason = await get_user_info(ult)
if not user:
return await xx.edit("`Kick? Whom? I couldn't get his info...`")
await xx.edit("`Kicking...`")
try:
await ultroid_bot.kick_participant(ult.chat_id, user.id)
await asyncio.sleep(0.5)
except BadRequestError:
return await xx.edit("`I don't have the right to kick a user.`")
except Exception as e:
return await xx.edit(
f"`I don't have the right to kick a user.`\n\n**ERROR**:\n`{str(e)}`"
)
if reason:
await xx.edit(
f"[{user.first_name}](tg://user?id={user.id})` was kicked by` [{OWNER_NAME}](tg://user?id={OWNER_ID}) `in {ult.chat.title}`\n**Reason**: `{reason}`"
)
else:
await xx.edit(
f"[{user.first_name}](tg://user?id={user.id})` was kicked by` [{OWNER_NAME}](tg://user?id={OWNER_ID}) `in {ult.chat.title}`"
)
@ultroid_cmd(
pattern="pin ?(.*)",
)
async def pin(msg):
if not msg.is_private:
# for pin(s) in private messages
await msg.get_chat()
cht = await ultroid_bot.get_entity(msg.chat_id)
xx = msg.reply_to_msg_id
tt = msg.text
try:
kk = tt[4]
if kk == "g":
return
except BaseException:
pass
if not msg.is_reply:
return
ch = msg.pattern_match.group(1)
if ch != "silent":
slnt = True
x = await eor(msg, "`Processing...`")
try:
await ultroid_bot.pin_message(msg.chat_id, xx, notify=slnt)
except BadRequestError:
return await x.edit("`Hmm, I'm have no rights here...`")
except Exception as e:
return await x.edit(f"**ERROR:**`{str(e)}`")
await x.edit(f"`Pinned` [this message](https://t.me/c/{cht.id}/{xx})!")
else:
try:
await ultroid_bot.pin_message(msg.chat_id, xx, notify=False)
except BadRequestError:
return await eor(msg, "`Hmm, I'm have no rights here...`")
except Exception as e:
return await eor(msg, f"**ERROR:**`{str(e)}`")
try:
await msg.delete()
except BaseException:
pass
@ultroid_cmd(
pattern="unpin($| (.*))",
)
async def unp(ult):
xx = await eor(ult, "`Processing...`")
if not ult.is_private:
# for (un)pin(s) in private messages
await ult.get_chat()
ch = (ult.pattern_match.group(1)).strip()
msg = ult.reply_to_msg_id
if msg and not ch:
try:
await ultroid_bot.unpin_message(ult.chat_id, msg)
except BadRequestError:
return await xx.edit("`Hmm, I'm have no rights here...`")
except Exception as e:
return await xx.edit(f"**ERROR:**\n`{str(e)}`")
elif ch == "all":
try:
await ultroid_bot.unpin_message(ult.chat_id)
except BadRequestError:
return await xx.edit("`Hmm, I'm have no rights here...`")
except Exception as e:
return await xx.edit(f"**ERROR:**`{str(e)}`")
else:
return await xx.edit(f"Either reply to a message, or, use `{hndlr}unpin all`")
if not msg and ch != "all":
return await xx.edit(f"Either reply to a message, or, use `{hndlr}unpin all`")
await xx.edit("`Unpinned!`")
@ultroid_cmd(
pattern="purge$",
)
async def fastpurger(purg):
chat = await purg.get_input_chat()
msgs = []
count = 0
if not purg.reply_to_msg_id:
return await eod(purg, "`Reply to a message to purge from.`", time=10)
async for msg in ultroid_bot.iter_messages(chat, min_id=purg.reply_to_msg_id):
msgs.append(msg)
count = count + 1
msgs.append(purg.reply_to_msg_id)
if len(msgs) == 100:
await ultroid_bot.delete_messages(chat, msgs)
msgs = []
if msgs:
await ultroid_bot.delete_messages(chat, msgs)
done = await ultroid_bot.send_message(
purg.chat_id,
"__Fast purge complete!__\n**Purged** `" + str(count) + "` **messages.**",
)
await asyncio.sleep(5)
await done.delete()
@ultroid_cmd(
pattern="purgeme$",
)
async def fastpurgerme(purg):
chat = await purg.get_input_chat()
msgs = []
count = 0
if not purg.reply_to_msg_id:
return await eod(purg, "`Reply to a message to purge from.`", time=10)
async for msg in ultroid_bot.iter_messages(
chat, from_user="me", min_id=purg.reply_to_msg_id
):
msgs.append(msg)
count = count + 1
msgs.append(purg.reply_to_msg_id)
if len(msgs) == 100:
await ultroid_bot.delete_messages(chat, msgs)
msgs = []
if msgs:
await ultroid_bot.delete_messages(chat, msgs)
done = await ultroid_bot.send_message(
purg.chat_id,
"__Fast purge complete!__\n**Purged** `" + str(count) + "` **messages.**",
)
await asyncio.sleep(5)
await done.delete()
@ultroid_cmd(
pattern="purgeall$",
)
async def _(e):
xx = await eor(e, "`Processing...`")
if e.reply_to_msg_id:
input = (await e.get_reply_message()).sender_id
user = (await e.client.get_entity(input)).first_name
try:
nos = 0
async for x in e.client.iter_messages(e.chat_id, from_user=input):
await e.client.delete_messages(e.chat_id, x)
nos += 1
await xx.edit(
f"**Purged **`{nos}`** msgs of **[{input}](tg://user?id={input})"
)
except ValueError:
return await eod(xx, str(er), time=5)
else:
return await eod(
xx,
"`Reply to someone's msg to delete.`",
time=5,
)
@ultroid_cmd(
pattern="del$",
)
async def delete_it(delme):
msg_src = await delme.get_reply_message()
if delme.reply_to_msg_id:
try:
await msg_src.delete()
await delme.delete()
except BaseException:
await eod(
delme,
f"Couldn't delete the message.\n\n**ERROR:**\n`{str(e)}`",
time=5,
)
@ultroid_cmd(
pattern="edit",
)
async def editer(edit):
message = edit.text
chat = await edit.get_input_chat()
self_id = await ultroid_bot.get_peer_id("me")
string = str(message[6:])
i = 1
async for message in ultroid_bot.iter_messages(chat, self_id):
if i == 2:
await message.edit(string)
await edit.delete()
break
i = i + 1
HELP.update({f"{__name__.split('.')[1]}": f"{__doc__.format(i=Var.HNDLR)}"})