forked from AbirHasan2005/PyroFilesStoreBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
433 lines (390 loc) · 18.3 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
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
# (c) @AbirHasan2005
import os
import asyncio
import traceback
from pyrogram import Client, filters
from pyrogram.errors import UserNotParticipant
from pyrogram.errors import FloodWait
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery, Message
from configs import Config
from handlers.check_user_status import handle_user_status
from handlers.force_sub_handler import handle_force_sub
from handlers.broadcast_handlers import main_broadcast_handler
from handlers.database import Database
db = Database(Config.DATABASE_URL, Config.BOT_USERNAME)
Bot = Client(Config.BOT_USERNAME, bot_token=Config.BOT_TOKEN, api_id=Config.API_ID, api_hash=Config.API_HASH)
@Bot.on_message(filters.private)
async def _(bot: Client, cmd: Message):
await handle_user_status(bot, cmd)
@Bot.on_message(filters.command("start") & filters.private)
async def start(bot: Client, cmd: Message):
if cmd.from_user.id in Config.BANNED_USERS:
await cmd.reply_text("Sorry, You are banned.")
return
if Config.UPDATES_CHANNEL is not None:
back = await handle_force_sub(bot, cmd)
if back == 400:
return
else:
pass
usr_cmd = cmd.text.split("_")[-1]
if usr_cmd == "/start":
chat_id = cmd.from_user.id
if not await db.is_user_exist(chat_id):
await db.add_user(chat_id)
await bot.send_message(
Config.LOG_CHANNEL,
f"#NEW_USER: \n\nNew User [{cmd.from_user.first_name}](tg://user?id={cmd.from_user.id}) started @{Config.BOT_USERNAME} !!"
)
await cmd.reply_text(
Config.HOME_TEXT.format(cmd.from_user.first_name, cmd.from_user.id),
parse_mode="Markdown",
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Updates Channel ⬆️", url="http://t.me/PremiumValleyUpdates"),
InlineKeyboardButton("Support Group 🆘", url="http://t.me/PremiumValleySupport")
],
[
InlineKeyboardButton("About Bot", callback_data="aboutbot"),
InlineKeyboardButton("About Dev", callback_data="aboutdevs")
]
]
)
)
else:
try:
file_id = int(usr_cmd)
send_stored_file = None
if Config.FORWARD_AS_COPY is True:
send_stored_file = await bot.copy_message(chat_id=cmd.from_user.id, from_chat_id=Config.DB_CHANNEL,
message_id=file_id)
elif Config.FORWARD_AS_COPY is False:
send_stored_file = await bot.forward_messages(chat_id=cmd.from_user.id, from_chat_id=Config.DB_CHANNEL,
message_ids=file_id)
await send_stored_file.reply_text(
f"**Here is Sharable Link of this file:** https://t.me/{Config.BOT_USERNAME}?start=AbirHasan2005_{file_id}\n\n__To Retrive the Stored File, just open the link!__",
disable_web_page_preview=True, quote=True)
except Exception as err:
await cmd.reply_text(f"Something went wrong!\n\n**Error:** `{err}`")
@Bot.on_message((filters.document | filters.video | filters.audio) & ~filters.edited)
async def main(bot: Client, message: Message):
if message.chat.type == "private":
chat_id = message.from_user.id
if not await db.is_user_exist(chat_id):
await db.add_user(chat_id)
await bot.send_message(
Config.LOG_CHANNEL,
f"#NEW_USER: \n\nNew User [{message.from_user.first_name}](tg://user?id={message.from_user.id}) started @{Config.BOT_USERNAME} !!"
)
if Config.UPDATES_CHANNEL is not None:
back = await handle_force_sub(bot, message)
if back == 400:
return
else:
pass
if message.from_user.id in Config.BANNED_USERS:
await message.reply_text("Sorry, You are banned!\n\nContact [Support Group 🆘](http://t.me/PremiumValleySupport)",
disable_web_page_preview=True)
return
if Config.OTHER_USERS_CAN_SAVE_FILE is False:
return
editable = await message.reply_text("Please wait ...")
try:
forwarded_msg = await message.forward(Config.DB_CHANNEL)
file_er_id = forwarded_msg.message_id
await forwarded_msg.reply_text(
f"#PRIVATE_FILE:\n\n[{message.from_user.first_name}](tg://user?id={message.from_user.id}) Got File Link!",
parse_mode="Markdown", disable_web_page_preview=True)
share_link = f"https://t.me/{Config.BOT_USERNAME}?start=AbirHasan2005_{file_er_id}"
await editable.edit(
f"**Your File Stored in my Database!**\n\nHere is the Permanent Link of your file: {share_link} \n\nJust Click the link to get your file!",
parse_mode="Markdown",
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton("Open Link", url=share_link)],
[InlineKeyboardButton("Updates Channel ⬆️", url="http://t.me/PremiumValleyUpdates"),
InlineKeyboardButton("Support Group 🆘", url="http://t.me/PremiumValleySupport")]]
),
disable_web_page_preview=True
)
except FloodWait as sl:
await asyncio.sleep(sl.x)
await bot.send_message(
chat_id=Config.LOG_CHANNEL,
text=f"#FloodWait:\nGot FloodWait of `{str(sl.x)}s` from `{str(message.chat.id)}` !!",
parse_mode="Markdown",
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(
[
[InlineKeyboardButton("Ban User", callback_data=f"ban_user_{str(message.chat.id)}")]
]
)
)
except Exception as err:
await editable.edit(f"Something Went Wrong!\n\n**Error:** `{err}`")
await bot.send_message(
chat_id=Config.LOG_CHANNEL,
text=f"#ERROR_TRACEBACK:\nGot Error from `{str(message.chat.id)}` !!\n\n**Traceback:** `{err}`",
parse_mode="Markdown",
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(
[
[InlineKeyboardButton("Ban User", callback_data=f"ban_user_{str(message.chat.id)}")]
]
)
)
elif message.chat.type == "channel":
if (message.chat.id == Config.LOG_CHANNEL) or (message.chat.id == int(Config.UPDATES_CHANNEL)) or message.forward_from_chat or message.forward_from:
return
elif int(message.chat.id) in Config.BANNED_CHAT_IDS:
await bot.leave_chat(message.chat.id)
return
else:
pass
try:
forwarded_msg = await message.forward(Config.DB_CHANNEL)
file_er_id = forwarded_msg.message_id
share_link = f"https://t.me/{Config.BOT_USERNAME}?start=AbirHasan2005_{file_er_id}"
CH_edit = await bot.edit_message_reply_markup(message.chat.id, message.message_id,
reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton(
"Get Sharable Stored Link", url=share_link)]]))
if message.chat.username:
await forwarded_msg.reply_text(
f"#CHANNEL_BUTTON:\n\n[{message.chat.title}](https://t.me/{message.chat.username}/{CH_edit.message_id}) Channel's Broadcasted File's Button Added!")
else:
private_ch = str(message.chat.id)[4:]
await forwarded_msg.reply_text(
f"#CHANNEL_BUTTON:\n\n[{message.chat.title}](https://t.me/c/{private_ch}/{CH_edit.message_id}) Channel's Broadcasted File's Button Added!")
except FloodWait as sl:
await asyncio.sleep(sl.x)
await bot.send_message(
chat_id=Config.LOG_CHANNEL,
text=f"#FloodWait:\nGot FloodWait of `{str(sl.x)}s` from `{str(message.chat.id)}` !!",
parse_mode="Markdown",
disable_web_page_preview=True
)
except Exception as err:
await bot.send_message(
chat_id=Config.LOG_CHANNEL,
text=f"#ERROR_TRACEBACK:\nGot Error from `{str(message.chat.id)}` !!\n\n**Traceback:** `{err}`",
parse_mode="Markdown",
disable_web_page_preview=True
)
@Bot.on_message(filters.private & filters.command("broadcast") & filters.user(Config.BOT_OWNER) & filters.reply)
async def broadcast_handler_open(_, m: Message):
await main_broadcast_handler(m, db)
@Bot.on_message(filters.private & filters.command("status") & filters.user(Config.BOT_OWNER))
async def sts(_, m: Message):
total_users = await db.total_users_count()
await m.reply_text(text=f"**Total Users in DB:** `{total_users}`", parse_mode="Markdown", quote=True)
@Bot.on_message(filters.private & filters.command("ban_user") & filters.user(Config.BOT_OWNER))
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\nUsage:\n\n`/ban_user user_id ban_duration ban_reason`\n\nEg: `/ban_user 1234567 28 You misused me.`\n 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**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
)
@Bot.on_message(filters.private & filters.command("unban_user") & filters.user(Config.BOT_OWNER))
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\nUsage:\n\n`/unban_user user_id`\n\nEg: `/unban_user 1234567`\n 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 occoured! Traceback given below\n\n`{traceback.format_exc()}`",
quote=True
)
@Bot.on_message(filters.private & filters.command("banned_users") & filters.user(Config.BOT_OWNER))
async def _banned_usrs(_, 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}`, **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)
@Bot.on_callback_query()
async def button(bot: Client, cmd: CallbackQuery):
cb_data = cmd.data
if "aboutbot" in cb_data:
await cmd.message.edit(
Config.ABOUT_BOT_TEXT,
parse_mode="Markdown",
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Main Channel",
url="https://t.me/ThePremiumValley")
],
[
InlineKeyboardButton("Go Home", callback_data="gotohome"),
InlineKeyboardButton("About Dev", callback_data="aboutdevs")
]
]
)
)
elif "aboutdevs" in cb_data:
await cmd.message.edit(
Config.ABOUT_DEV_TEXT,
parse_mode="Markdown",
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Main Channel",
url="https://t.me/ThePremiumValley")
],
[
InlineKeyboardButton("About Bot", callback_data="aboutbot"),
InlineKeyboardButton("Go Home", callback_data="gotohome")
]
]
)
)
elif "gotohome" in cb_data:
await cmd.message.edit(
Config.HOME_TEXT.format(cmd.message.chat.first_name, cmd.message.chat.id),
parse_mode="Markdown",
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Support Group", url="https://t.me/PremiumValleySupport"),
InlineKeyboardButton("Main Channel", url="https://t.me/ThePremiumValley")
],
[
InlineKeyboardButton("About Bot", callback_data="aboutbot"),
InlineKeyboardButton("About Dev", callback_data="aboutdevs")
]
]
)
)
elif "refreshmeh" in cb_data:
if Config.UPDATES_CHANNEL:
invite_link = await bot.create_chat_invite_link(int(Config.UPDATES_CHANNEL))
try:
user = await bot.get_chat_member(int(Config.UPDATES_CHANNEL), cmd.message.chat.id)
if user.status == "kicked":
await cmd.message.edit(
text="Sorry Sir, You are Banned to use me. Contact my [Support Group](https://t.me/PremiumValleySupport).",
parse_mode="markdown",
disable_web_page_preview=True
)
return
except UserNotParticipant:
await cmd.message.edit(
text="**You Still Didn't Join ☹️, Please Join My Updates Channel to use this Bot!**\n\nDue to Overload, Only Channel Subscribers can use the Bot!",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("🤖 Join Updates Channel", url=invite_link.invite_link)
],
[
InlineKeyboardButton("🔄 Refresh 🔄", callback_data="refreshmeh")
]
]
),
parse_mode="markdown"
)
return
except Exception:
await cmd.message.edit(
text="Something went Wrong. Contact my [Support Group](https://t.me/PremiumValleySupport).",
parse_mode="markdown",
disable_web_page_preview=True
)
return
await cmd.message.edit(
text=Config.HOME_TEXT.format(cmd.message.chat.first_name, cmd.message.chat.id),
parse_mode="Markdown",
disable_web_page_preview=True,
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Support Group", url="https://t.me/PremiumValleySupport"),
InlineKeyboardButton("Main Channel", url="https://t.me/ThePremiumValley")
],
[
InlineKeyboardButton("About Bot", callback_data="aboutbot"),
InlineKeyboardButton("About Dev", callback_data="aboutdevs")
]
]
)
)
elif cb_data.startswith("ban_user_"):
user_id = cb_data.split("_", 2)[-1]
if Config.UPDATES_CHANNEL is None:
await cmd.answer("Sorry Sir, You didn't Set any Updates Channel!", show_alert=True)
return
if not int(cmd.from_user.id) == Config.BOT_OWNER:
await cmd.answer("You are not allowed to do that!", show_alert=True)
return
try:
await bot.kick_chat_member(chat_id=int(Config.UPDATES_CHANNEL), user_id=int(user_id))
await cmd.answer("User Banned from Updates Channel!", show_alert=True)
except Exception as e:
await cmd.answer(f"Can't Ban Him!\n\nError: {e}", show_alert=True)
await cmd.answer()
Bot.run()