-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathbot.py
258 lines (227 loc) · 9.76 KB
/
bot.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
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
# (c) Jigarvarma2005
# Please make pull request if something wrong
# Coded by noob
# Edit at your own risk
import os
from database import db
from pyrogram import Client, filters
from config import Config
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
import logging
from logging.handlers import RotatingFileHandler
if os.path.exists("log.txt"):
with open("log.txt", "r+") as f_d:
f_d.truncate(0)
# the logging things
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s [%(filename)s:%(lineno)d]",
datefmt="%d-%b-%y %H:%M:%S",
handlers=[
RotatingFileHandler(
"log.txt", maxBytes=50000000, backupCount=10
),
logging.StreamHandler(),
],
)
logging.getLogger("pyrogram").setLevel(logging.WARNING)
JV_BOT = Client("AntiChannelBot",
api_id=Config.API_ID,
api_hash=Config.API_HASH,
bot_token=Config.BOT_TOKEN)
async def whitelist_check(chat_id,channel_id=0):
if not (await db.is_chat_exist(chat_id)):
await db.add_chat_list(chat_id)
_chat_list = await db.get_chat_list(chat_id)
if int(channel_id) in _chat_list:
return True
else:
return False
async def get_channel_id_from_input(bot, message):
try:
a_id = message.text.split(" ",1)[1]
except:
await message.reply_text("Send cmd along with channel id")
return False
if not str(a_id).startswith("-"):
try:
a_id = await bot.get_chat(a_id)
a_id = a_id.id
except:
await message.reply_text("Inavalid channel id")
return False
return a_id
custom_message_filter = filters.create(lambda _, __, message: False if message.forward_from_chat or message.from_user else True)
custom_chat_filter = filters.create(lambda _, __, message: True if message.sender_chat else False)
@JV_BOT.on_message(custom_message_filter & filters.group & custom_chat_filter)
async def main_handler(bot, message):
chat_id = message.chat.id
a_id = message.sender_chat.id
if (await whitelist_check(chat_id, a_id)):
return
elif a_id is chat_id:
return
try:
res = await bot.ban_chat_member(chat_id, a_id)
except:
return await message.reply_text("Promote me as admin, to use me")
if res:
mention = f"@{message.sender_chat.username}" if message.sender_chat.username else message.chat_data.title
await message.reply_text(text=f"{mention} has been banned.\n\n💡 He can write only with his profile but not through other channels.",
reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Unban", callback_data=f"unban_{chat_id}_{a_id}")]]),
)
await message.delete()
@JV_BOT.on_message(filters.command(["start"]) & filters.private)
async def start_handler(bot, message):
await message.reply_text(text="""Hey! Just add me to the chat, and I will block the channels that write to the chat,
check /help for more.""",
reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Bots Channel", url=f"https://t.me/Universal_Projects"),
InlineKeyboardButton("Support Group", url=f"https://t.me/JV_Community")]]),
disable_web_page_preview=True)
@JV_BOT.on_message(filters.command(["help"]) & filters.private)
async def help_handler(bot, message):
await message.reply_text(text="""/ban [channel_id] : ban channel from sending message as channel.
/unban [channel_id] : unban channel from sending message as channel.
/add_whitelist [channel_id] : add channel into whitelist and protect channel for automatic actions.
/del_whitelist [channel_id] : remove channel from whitelist.
/show_whitelist : Show all white list channels.
for more help ask at @JV_Community""",
reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Bots Channel", url=f"https://t.me/Universal_Projects"),
InlineKeyboardButton("Support Group", url=f"https://t.me/JV_Community")]]),
disable_web_page_preview=True)
@JV_BOT.on_callback_query()
async def cb_handler(bot, query):
cb_data = query.data
if cb_data.startswith("unban_"):
an_id = cb_data.split("_")[-1]
chat_id = cb_data.split("_")[-2]
user = await bot.get_chat_member(chat_id, query.from_user.id)
if user.status == "creator" or user.status == "administrator":
pass
elif message.sender_chat.id is chat_id:
pass
else:
return await query.answer("This Message is Not For You!", show_alert=True)
await bot.resolve_peer(an_id)
res = await query.message.chat.unban_member(an_id)
chat_data = await bot.get_chat(an_id)
mention = f"@{chat_data.username}" if chat_data.username else chat_data.title
if res:
await query.message.reply_text(f"{mention} has been unbanned by {query.from_user.mention}")
await query.message.edit_reply_markup(reply_markup=None)
@JV_BOT.on_message(filters.command(["ban"]) & filters.group)
async def cban_handler(bot, message):
chat_id = message.chat.id
user = await bot.get_chat_member(message.chat.id, message.from_user.id)
if user.status == "creator" or user.status == "administrator":
pass
elif message.sender_chat.id is chat_id:
pass
else:
return
try:
a_id = await get_channel_id_from_input(bot, message)
if not a_id:
return
if (await whitelist_check(chat_id, a_id)):
return await message.reply_text("Channel Id found in whitelist, so you can't ban this channel")
await bot.resolve_peer(a_id)
res = await bot.ban_chat_member(chat_id, a_id)
chat_data = await bot.get_chat(a_id)
mention = f"@{chat_data.username}" if chat_data.username else chat_data.title
if res:
await message.reply_text(text=f"{mention} has been banned.\n\n💡 He can write only with his profile but not through other channels.",
reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Unban", callback_data=f"unban_{chat_id}_{a_id}")]]),
)
else:
await message.reply_text("Invalid Channel id, 💡check channel id")
except Exception as e:
print(e)
@JV_BOT.on_message(filters.command(["unban"]) & filters.group)
async def uncban_handler(bot, message):
chat_id = message.chat.id
user = await bot.get_chat_member(message.chat.id, message.from_user.id)
if user.status == "creator" or user.status == "administrator":
pass
else:
return
try:
a_id = await get_channel_id_from_input(bot, message)
if not a_id:
return
if (await whitelist_check(chat_id, a_id)):
return
await bot.resolve_peer(a_id)
res = await message.chat.unban_member(a_id)
chat_data = await bot.get_chat(a_id)
mention = f"@{chat_data.username}" if chat_data.username else chat_data.title
if res:
await message.reply_text(text=f"{mention} has been unbanned by {message.from_user.mention}")
else:
await message.reply_text("Invalid Channel id, 💡check channel id")
except Exception as e:
print(e)
await message.reply_text(e)
@JV_BOT.on_message(filters.command(["add_whitelist"]) & filters.group)
async def add_whitelist_handler(bot, message):
chat_id = message.chat.id
user = await bot.get_chat_member(chat_id, message.from_user.id)
if user.status == "creator" or user.status == "administrator":
pass
elif message.sender_chat.id is chat_id:
pass
else:
return
try:
a_id = await get_channel_id_from_input(bot, message)
if not a_id:
return
if (await whitelist_check(chat_id, a_id)):
return await message.reply_text("Channel Id already found in whitelist")
chk,msg = await db.add_chat_list(chat_id, a_id)
if chk and msg != "":
await message.reply_text(msg)
else:
await message.reply_text("Something wrong happend")
except Exception as e:
print(e)
@JV_BOT.on_message(filters.command(["del_whitelist"]) & filters.group)
async def del_whitelist_handler(bot, message):
chat_id = message.chat.id
user = await bot.get_chat_member(chat_id, message.from_user.id)
if user.status == "creator" or user.status == "administrator":
pass
elif message.sender_chat.id is chat_id:
pass
else:
return
try:
a_id = await get_channel_id_from_input(bot, message)
if not a_id:
return
if not (await whitelist_check(chat_id, a_id)):
return await message.reply_text("Channel Id not found in whitelist")
chk,msg = await db.del_chat_list(message.chat.id, a_id)
if chk:
await message.reply_text(msg)
else:
await message.reply_text("Something wrong happend")
except Exception as e:
print(e)
@JV_BOT.on_message(filters.command(["show_whitelist"]) & filters.group)
async def del_whitelist_handler(bot, message):
chat_id = message.chat.id
user = await bot.get_chat_member(chat_id, message.from_user.id)
if user.status == "creator" or user.status == "administrator":
pass
elif message.sender_chat.id is chat_id:
pass
else:
return
show_wl = await db.get_chat_list(chat_id)
if show_wl:
await message.reply_text(f"This ids found in whitelist\n\n{show_wl}")
else:
await message.reply_text("White list not found.")
if __name__ == "__main__":
JV_BOT.run()