-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.py
395 lines (333 loc) · 12.4 KB
/
channel.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @trojanzhex
import re
import asyncio
from pyrogram import Client, filters
from pyrogram.types import Message, InlineKeyboardButton, InlineKeyboardMarkup
from pyrogram.errors import UserAlreadyParticipant
from bot import Bot
from config import AUTH_USERS, DOC_SEARCH, VID_SEARCH, MUSIC_SEARCH, PHO_SEARCH
from database.mdb import (
savefiles,
deletefiles,
deletegroupcol,
channelgroup,
ifexists,
deletealldetails,
findgroupid,
channeldetails,
countfilters
)
@Client.on_message(filters.group & filters.command(["add"]))
async def addchannel(client: Bot, message: Message):
if message.from_user.id not in AUTH_USERS:
return
try:
cmd, text = message.text.split(" ", 1)
except:
await message.reply_text(
"<i>Enter in correct format!\n\n<code>/add channelid</code> \n"
"\n\nGet Channel id from @ChannelidHEXbot",
)
return
try:
if not text.startswith("@"):
chid = int(text)
if not len(text) == 14:
await message.reply_text(
"Enter valid channel ID"
)
return
elif text.startswith("@"):
chid = text
if not len(chid) > 2:
await message.reply_text(
"Enter valid channel username"
)
return
except Exception:
await message.reply_text(
"Enter a valid ID\n"
"ID will be in <b>-100xxxxxxxxxx</b> format\n"
"You can also use username of channel with @ symbol",
)
return
try:
invitelink = await client.export_chat_invite_link(chid)
except:
await message.reply_text(
"<i>Add me as admin in your channel with admin rights - 'Invite Users via Link' and try again</i>",
)
return
try:
user = await client.USER.get_me()
except:
user.first_name = " "
try:
await client.USER.join_chat(invitelink)
except UserAlreadyParticipant:
pass
except Exception as e:
print(e)
await message.reply_text(
f"<i>User {user.first_name} couldn't join your channel! Make sure user is not banned in channel."
"\n\nOr manually add the user to your channel and try again</i>",
)
return
try:
chatdetails = await client.USER.get_chat(chid)
except:
await message.reply_text(
"<i>Send a message to your channel and try again</i>"
)
return
intmsg = await message.reply_text(
"<i>Please wait while I'm adding your channel files to DB"
"\n\nIt may take some time if you have more files in channel!!"
"\nDon't give any other commands now!</i>"
)
channel_id = chatdetails.id
channel_name = chatdetails.title
group_id = message.chat.id
group_name = message.chat.title
already_added = await ifexists(channel_id, group_id)
if already_added:
await intmsg.edit_text("Channel already added to db!")
return
docs = []
if DOC_SEARCH == "yes":
try:
async for msg in client.USER.search_messages(channel_id,filter='document'):
try:
file_name = msg.document.file_name.replace("_"," ").replace("S01","S01 ").replace("S02","S02 ").replace("S03","S03 ").replace("S04","S04 ").replace("S05","S05 ").replace("S06","S06 ").replace("S07","S07 ").replace("S08","S08 ").replace("S09","S09 ").replace("S10","S10 ")
file_id = msg.document.file_id
file_size = msg.document.file_size
link = msg.link
data = {
'_id': file_id,
'channel_id' : channel_id,
'file_name': file_name,
'file_size': file_size,
'link': link
}
docs.append(data)
except:
pass
except:
pass
await asyncio.sleep(5)
if PHO_SEARCH == "yes":
try:
async for msg in client.USER.search_messages(channel_id,filter='photo'):
try:
file_name = msg.caption.split("\n")[0]
file_id = msg.photo.file_id
file_size = "📺"
link = msg.link
data = {
'_id': file_id,
'channel_id' : channel_id,
'file_name': file_name,
'file_size': file_size,
'link': link
}
docs.append(data)
except:
pass
except:
pass
await asyncio.sleep(5)
if VID_SEARCH == "yes":
try:
async for msg in client.USER.search_messages(channel_id,filter='video'):
try:
file_name = msg.video.file_name.replace("_"," ").replace("S01","S01 ").replace("S02","S02 ").replace("S03","S03 ").replace("S04","S04 ").replace("S05","S05 ").replace("S06","S06 ").replace("S07","S07 ").replace("S08","S08 ").replace("S09","S09 ").replace("S10","S10 ")
file_id = msg.video.file_id
file_size = msg.video.file_size
link = msg.link
data = {
'_id': file_id,
'channel_id' : channel_id,
'file_name': file_name,
'file_size': file_size,
'link': link
}
docs.append(data)
except:
pass
except:
pass
await asyncio.sleep(5)
if MUSIC_SEARCH == "yes":
try:
async for msg in client.USER.search_messages(channel_id,filter='audio'):
try:
file_name = msg.audio.file_name.replace("_"," ")
file_id = msg.audio.file_id
file_size = msg.audio.file_size
link = msg.link
data = {
'_id': file_id,
'channel_id' : channel_id,
'file_name': file_name,
'file_size': file_size,
'link': link
}
docs.append(data)
except:
pass
except:
pass
if docs:
await savefiles(docs, group_id)
else:
await intmsg.edit_text("Channel couldn't be added. Try after some time!")
return
await channelgroup(channel_id, channel_name, group_id, group_name)
await intmsg.edit_text("Channel added successfully!")
@Client.on_message(filters.group & filters.command(["del"]))
async def deletechannelfilters(client: Bot, message: Message):
if message.from_user.id not in AUTH_USERS:
return
try:
cmd, text = message.text.split(" ", 1)
except:
await message.reply_text(
"<i>Enter in correct format!\n\n<code>/del channelid</code> or\n"
"<code>/del @channelusername</code></i>"
"\n\nrun /filterstats to see connected channels",
)
return
try:
if not text.startswith("@"):
chid = int(text)
if not len(text) == 14:
await message.reply_text(
"Enter valid channel ID\n\nrun /filterstats to see connected channels"
)
return
elif text.startswith("@"):
chid = text
if not len(chid) > 2:
await message.reply_text(
"Enter valid channel username"
)
return
except Exception:
await message.reply_text(
"Enter a valid ID\n"
"run /filterstats to see connected channels\n"
"You can also use username of channel with @ symbol",
)
return
try:
chatdetails = await client.USER.get_chat(chid)
except:
await message.reply_text(
"<i>User must be present in given channel.\n\n"
"If user is already present, send a message to your channel and try again</i>"
)
return
intmsg = await message.reply_text(
"<i>Please wait while I'm deleteing your channel"
"\n\nDon't give any other commands now!</i>"
)
channel_id = chatdetails.id
channel_name = chatdetails.title
group_id = message.chat.id
group_name = message.chat.title
already_added = await ifexists(channel_id, group_id)
if not already_added:
await intmsg.edit_text("That channel is not currently added in db!")
return
delete_files = await deletefiles(channel_id, channel_name, group_id, group_name)
if delete_files:
await intmsg.edit_text(
"Channel deleted successfully!"
)
else:
await intmsg.edit_text(
"Couldn't delete Channel"
)
@Client.on_message(filters.group & filters.command(["delall"]))
async def delallconfirm(client: Bot, message: Message):
await message.reply_text(
"Are you sure?? This will disconnect all connected channels and deletes all filters in group",
reply_markup=InlineKeyboardMarkup([
[InlineKeyboardButton(text="YES",callback_data="delallconfirm")],
[InlineKeyboardButton(text="CANCEL",callback_data="delallcancel")]
])
)
async def deleteallfilters(client: Bot, message: Message):
if message.reply_to_message.from_user.id not in AUTH_USERS:
return
intmsg = await message.reply_to_message.reply_text(
"<i>Please wait while I'm deleteing your channel.</i>"
"\n\nDon't give any other commands now!</i>"
)
group_id = message.reply_to_message.chat.id
await deletealldetails(group_id)
delete_all = await deletegroupcol(group_id)
if delete_all == 0:
await intmsg.edit_text(
"All filters from group deleted successfully!"
)
elif delete_all == 1:
await intmsg.edit_text(
"Nothing to delete!!"
)
elif delete_all == 2:
await intmsg.edit_text(
"Couldn't delete filters. Try again after sometime.."
)
@Client.on_message(filters.group & filters.command(["filterstats"]))
async def stats(client: Bot, message: Message):
if message.from_user.id not in AUTH_USERS:
return
group_id = message.chat.id
group_name = message.chat.title
stats = f"Stats for Auto Filter Bot in {group_name}\n\n<b>Connected channels ;</b>"
chdetails = await channeldetails(group_id)
if chdetails:
n = 0
for eachdetail in chdetails:
details = f"\n{n+1} : {eachdetail}"
stats += details
n = n + 1
else:
stats += "\nNo channels connected in current group!!"
await message.reply_text(stats)
return
total = await countfilters(group_id)
if total:
stats += f"\n\n<b>Total number of filters</b> : {total}"
await message.reply_text(stats)
@Client.on_message(filters.channel & (filters.document | filters.video | filters.audio | filters.photo))
async def addnewfiles(client: Bot, message: Message):
media = message.document or message.video or message.audio or message.photo
if media == message.photo:
channel_id = message.chat.id
file_name = message.caption.split("\n")[0]
file_size = "📺"
file_id = media.file_id
link = message.link
else:
channel_id = message.chat.id
file_name = media.file_name.replace("_"," ").replace("S01","S01 ").replace("S02","S02 ").replace("S03","S03 ").replace("S04","S04 ").replace("S05","S05 ").replace("S06","S06 ").replace("S07","S07 ").replace("S08","S08 ").replace("S09","S09 ").replace("S10","S10 ")
file_size = media.file_size
file_id = media.file_id
link = message.link
docs = []
data = {
'_id': file_id,
'channel_id' : channel_id,
'file_name': file_name,
'file_size': file_size,
'link': link
}
docs.append(data)
groupids = await findgroupid(channel_id)
if groupids:
for group_id in groupids:
await savefiles(docs, group_id)