-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
108 lines (85 loc) · 3.8 KB
/
main.py
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
from pyrogram import filters
from pyrogram.errors import ChatAdminRequired, UserAdminInvalid
from pyrogram.handlers import MessageHandler, CallbackQueryHandler
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from Msg import return_reason
from handlers import unblock, private, exit, start, help
from utils import *
app = Client("the_reason")
app.add_handler(CallbackQueryHandler(unblock, ~filters.regex("exit"))) # check msg in group
app.add_handler(CallbackQueryHandler(exit, filters.regex("exit"))) # delete check msg
app.add_handler(MessageHandler(private, filters.private & filters.command("my_bans"))) # check msg in private
app.add_handler(MessageHandler(start, filters.command(["start", "start@"+Msg.bot_username]))) # start msg
app.add_handler(MessageHandler(help, filters.command(["help", "help@"+Msg.bot_username]))) # help msg
@app.on_message(filters.group & filters.command(["ban", "ban@" + Msg.bot_username]))
def ban(app: Client, msg: Message):
""" ban user with a reason """
if len(msg.command) < 2 or (not msg.reply_to_message and len(msg.command) < 3):
msg.reply(Msg.need_reason)
return
if not is_admin(msg):
try:
msg.delete()
except ChatAdminRequired:
pass
return
if msg.reply_to_message and len(msg.entities) == 1:
user = msg.reply_to_message.from_user
elif len(msg.entities) >= 2:
if msg.entities[1].type == "mention":
user = app.get_users(msg.text[msg.entities[1].offset:msg.entities[1].offset + msg.entities[1].length])
elif msg.entities[1].type == "text_mention":
user = msg.entities[1].user
kick_msg = None
try:
kick_msg = msg.chat.ban_member(user.id)
except ChatAdminRequired:
msg.reply(Msg.need_permissions)
except UserAdminInvalid:
msg.reply(Msg.another_admin)
return
id_baned = save_ban(msg, app) # save details to db
msg.reply(Msg.ban_success.format(id_baned), reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton(Msg.exit_btn, "exit")]]))
if isinstance(kick_msg, Message):
try:
kick_msg.delete()
except ChatAdminRequired:
msg.reply(Msg.need_permissions)
@app.on_message(filters.group & filters.command(["check", "check@" + Msg.bot_username]
) & ~filters.reply)
def check(_, msg: Message):
""" check the reason of user-baned with id provided """
if not is_admin(msg):
msg.reply(Msg.only_admins)
return
if len(msg.command) < 2:
msg.reply(Msg.need_id)
return
reason = get_reason(int(msg.command[1]), msg.chat.id)
if not reason:
msg.reply(Msg.not_found)
return
msg.reply(return_reason(reason), reply_markup=InlineKeyboardMarkup([
[InlineKeyboardButton(Msg.unblock_btn, str(msg.command[1]))],
[InlineKeyboardButton(Msg.exit_btn, "exit")]
]))
@app.on_message(filters.group & filters.command(["check", "check@" + Msg.bot_username]
) & filters.reply)
def reply(_, msg: Message):
""" check the reason of user-baned with reply to forward msg """
if not is_admin(msg):
msg.reply(Msg.only_admins)
return
if msg.reply_to_message.forward_sender_name and not msg.reply_to_message.forward_sender_name.startswith(
msg.reply_to_message.from_user.first_name): # check if the user didn't hide forward msgs
msg.reply(Msg.hide_sender)
return
reason = get_reason(msg.reply_to_message.from_user.id, msg.chat.id)
if not reason:
msg.reply(Msg.not_found)
return
msg.reply(return_reason(reason), reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton(Msg.unblock_btn, str(msg.command[1]))
]]))
app.run()