-
Notifications
You must be signed in to change notification settings - Fork 51
/
bot.py
515 lines (452 loc) · 19.2 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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
# Copyright (c) 2021 HEIMAN PICTURES
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import traceback
import logging
from pyrogram import Client, filters
from configs import Config as C
# LMAO, This Is Logging
# logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# logger = logging.getLogger(__name__)
# logging.getLogger("pyrogram").setLevel(logging.WARNING)
# Import From Framework
# from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery
from pyrogram.types import *
from database.broadcast import broadcast
from database.verifier import handle_user_status
from database.database import Database
LOG_CHANNEL = C.LOG_CHANNEL
AUTH_USERS = C.AUTH_USERS
DB_URL = C.DB_URL
DB_NAME = C.DB_NAME
db = Database(DB_URL, DB_NAME)
# Don't Change Anything, Except If You Want To Add Value
bot = Client('Feedback bot',
api_id=C.API_ID,
api_hash=C.API_HASH,
bot_token=C.BOT_TOKEN)
donate_link=C.DONATE_LINK
owner_id=C.OWNER_ID
LOG_TEXT = "ID: <code>{}</code>\nFirst Name: <a href='tg://user?id={}'>{}{}</a>\nDC ID: <code>{}</code>"
IF_TEXT = "<b>Message from:</b> {}\n<b>Name:</b> {}\n\n{}"
IF_CONTENT = "<b>Message from:</b> {} \n<b>Name:</b> {}"
# Callback
@bot.on_callback_query()
async def callback_handlers(bot: Client, cb: CallbackQuery):
user_id = cb.from_user.id
if "closeMeh" in cb.data:
await cb.message.delete(True)
elif "notifon" in cb.data:
notif = await db.get_notif(cb.from_user.id)
if notif is True:
#
await db.set_notif(user_id, notif=False)
else:
#
await db.set_notif(user_id, notif=True)
await cb.message.edit(
f"`Here You Can Set Your Settings:`\n\nSuccessfully setted notifications to **{await db.get_notif(user_id)}**",
reply_markup=InlineKeyboardMarkup(
[
[
InlineKeyboardButton(
f"NOTIFICATION {'🔔' if ((await db.get_notif(user_id)) is True) else '🔕'}",
callback_data="notifon",
)
],
[InlineKeyboardButton("CLOSE", callback_data="closeMeh")],
]
),
)
await cb.answer(
f"Successfully setted notifications to {await db.get_notif(user_id)}"
)
@bot.on_message((filters.private | filters.group))
async def _(bot, cmd):
await handle_user_status(bot, cmd)
@bot.on_message(filters.command('start') & (filters.private | filters.group))
async def start(bot, message):
chat_id = message.from_user.id
# Adding to DB
if not await db.is_user_exist(chat_id):
data = await bot.get_me()
BOT_USERNAME = data.username
await db.add_user(chat_id)
await bot.send_message(
LOG_CHANNEL,
f"#NEWUSER: \n\nNew User [{message.from_user.first_name}](tg://user?id={message.from_user.id}) started @{BOT_USERNAME} !!",
)
return
#
ban_status = await db.get_ban_status(chat_id)
is_banned = ban_status['is_banned']
ban_duration = ban_status['ban_duration']
ban_reason = ban_status['ban_reason']
if is_banned is True:
await message.reply_text(f"You are Banned 🚫 to use this bot for **{ban_duration}** day(s) for the reason __{ban_reason}__ \n\n**Message from the admin 🤠**")
return
await message.reply_text(
text="**Hi {}!**\n".format(message.chat.first_name)+C.START,
reply_markup=InlineKeyboardMarkup([
[ InlineKeyboardButton(text="🛠SUPPORT🛠", url=f"{C.SUPPORT_GROUP}"), InlineKeyboardButton(text="📮UPDATES📮", url=f"{C.UPDATE_CHANNEL}")]
])
)
@bot.on_message(filters.command('help') & (filters.group | filters.private))
async def help(bot, message):
chat_id = message.from_user.id
# Adding to DB
if not await db.is_user_exist(chat_id):
data = await bot.get_me()
BOT_USERNAME = data.username
await db.add_user(chat_id)
await bot.send_message(
LOG_CHANNEL,
f"#NEWUSER: \n\nNew User [{message.from_user.first_name}](tg://user?id={message.from_user.id}) started @{BOT_USERNAME} !!",
)
ban_status = await db.get_ban_status(chat_id)
is_banned = ban_status['is_banned']
ban_duration = ban_status['ban_duration']
ban_reason = ban_status['ban_reason']
if is_banned is True:
await message.reply_text(f"You are Banned 🚫 to use this bot for **{ban_duration}** day(s) for the reason __{ban_reason}__ \n\n**Message from the admin 🤠**")
return
await message.reply_text(
text=C.HELP,
reply_markup=InlineKeyboardMarkup([
[ InlineKeyboardButton(text="🛠SUPPORT🛠", url=f"{C.SUPPORT_GROUP}"), InlineKeyboardButton(text="📮UPDATES📮", url=f"{C.UPDATE_CHANNEL}")]
])
)
@bot.on_message(filters.command('donate') & (filters.group | filters.private))
async def donate(bot, message):
chat_id = message.from_user.id
# Adding to DB
if not await db.is_user_exist(chat_id):
data = await bot.get_me()
BOT_USERNAME = data.username
await db.add_user(chat_id)
await bot.send_message(
LOG_CHANNEL,
f"#NEWUSER: \n\nNew User [{message.from_user.first_name}](tg://user?id={message.from_user.id}) started @{BOT_USERNAME} !!",
)
ban_status = await db.get_ban_status(chat_id)
is_banned = ban_status['is_banned']
ban_duration = ban_status['ban_duration']
ban_reason = ban_status['ban_reason']
if is_banned is True:
await message.reply_text(f"You are Banned 🚫 to use this bot for **{ban_duration}** day(s) for the reason __{ban_reason}__ \n\n**Message from the admin 🤠**")
return
await message.reply_text(
text=C.DONATE + "If You Liked This Bot You Can Also Donate Creator through BTC `3AKE4bNwb9TsgaofLQxHAGCR9w2ftwFs2R`",
reply_markup=InlineKeyboardMarkup([
[ InlineKeyboardButton(text="DONATE", url=f"{donate_link}")]
])
)
@bot.on_message(filters.command("settings") & filters.private)
async def opensettings(bot, cmd):
user_id = cmd.from_user.id
# Adding to DB
if not await db.is_user_exist(user_id):
data = await bot.get_me()
BOT_USERNAME = data.username
await db.add_user(user_id)
await bot.send_message(
LOG_CHANNEL,
f"#NEWUSER: \n\nNew User [{cmd.from_user.first_name}](tg://user?id={cmd.from_user.id}) started @{BOT_USERNAME} !!",
)
try:
await cmd.reply_text(
text=f"⚙ `Here You Can Set Your Settings:` ⚙\n\nSuccessfully setted notifications to **{await db.get_notif(user_id)}**",
reply_markup=InlineKeyboardMarkup(
[
[InlineKeyboardButton(text=f"NOTIFICATION {'🔔' if ((await db.get_notif(user_id)) is True) else '🔕'}",callback_data="notifon")],
[InlineKeyboardButton(text="CLOSE", callback_data="closeMeh")],
]
)
)
except Exception as e:
await cmd.reply_text(e)
@bot.on_message(filters.private & filters.command("broadcast"))
async def broadcast_handler_open(_, m):
if m.from_user.id not in AUTH_USERS:
await m.delete()
return
if m.reply_to_message is None:
await m.delete()
return
await broadcast(m, db)
@bot.on_message((filters.group | filters.private) & filters.command("stats"))
async def sts(c, m):
if m.from_user.id not in AUTH_USERS:
await m.delete()
return
await m.reply_text(
text=f"**Total Users in Database 📂:** `{await db.total_users_count()}`\n\n**Total Users with Notification Enabled 🔔 :** `{await db.total_notif_users_count()}`",
quote=True,
)
@bot.on_message(filters.private & filters.command("ban_user"))
async def ban(c, m):
if m.from_user.id not in AUTH_USERS:
await m.delete()
return
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}."
if user_id == owner_id:
await m.reply_text("**You can Ban The Owner Vro")
return
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 BaseException:
traceback.print_exc()
ban_log_text += (
f"\n\n ⚠️ User 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 BaseException:
traceback.print_exc()
await m.reply_text(
f"Error occoured ⚠️! Traceback given below\n\n`{traceback.format_exc()}`",
quote=True,
)
@bot.on_message((filters.group | filters.private) & filters.command("unban_user"))
async def unban(c, m):
if m.from_user.id not in AUTH_USERS:
await m.delete()
return
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\n✅ User notified successfully! ✅"
except BaseException:
traceback.print_exc()
unban_log_text += (
f"\n\n⚠️ User 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 BaseException:
traceback.print_exc()
await m.reply_text(
f"⚠️ Error occoured ⚠️! Traceback given below\n\n`{traceback.format_exc()}`",
quote=True,
)
@bot.on_message((filters.group | filters.private) & filters.command("banned_users"))
async def _banned_usrs(c, m):
if m.from_user.id not in AUTH_USERS:
await m.delete()
return
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)
return
@bot.on_message((filters.group | filters.private) & filters.text)
async def pm_text(bot, message):
chat_id = message.from_user.id
# Adding to DB
if not await db.is_user_exist(chat_id):
data = await bot.get_me()
BOT_USERNAME = data.username
await db.add_user(chat_id)
await bot.send_message(
LOG_CHANNEL,
f"#NEWUSER: \n\nNew User [{message.from_user.first_name}](tg://user?id={message.from_user.id}) started @{BOT_USERNAME} !!",
)
ban_status = await db.get_ban_status(chat_id)
is_banned = ban_status['is_banned']
ban_duration = ban_status['ban_duration']
ban_reason = ban_status['ban_reason']
if is_banned is True:
await message.reply_text(f"You are Banned 🚫 to use this bot for **{ban_duration}** day(s) for the reason __{ban_reason}__ \n\n**Message from the admin 🤠**")
return
if message.from_user.id == owner_id:
await reply_text(bot, message)
return
info = await bot.get_users(user_ids=message.from_user.id)
reference_id = int(message.chat.id)
await bot.send_message(
chat_id=owner_id,
text=IF_TEXT.format(reference_id, info.first_name, message.text),
)
@bot.on_message((filters.group | filters.private) & filters.media_group)
async def pm_media_group(bot, message):
chat_id = message.from_user.id
# Adding to DB
if not await db.is_user_exist(chat_id):
data = await bot.get_me()
BOT_USERNAME = data.username
await db.add_user(chat_id)
await bot.send_message(
LOG_CHANNEL,
f"#NEWUSER: \n\nNew User [{message.from_user.first_name}](tg://user?id={message.from_user.id}) started @{BOT_USERNAME} !!",
)
ban_status = await db.get_ban_status(chat_id)
is_banned = ban_status['is_banned']
ban_duration = ban_status['ban_duration']
ban_reason = ban_status['ban_reason']
if is_banned is True:
await message.reply_text(f"You are Banned 🚫 to use this bot for **{ban_duration}** day(s) for the reason __{ban_reason}__ \n\n**Message from the admin 🤠**")
return
if message.from_user.id == owner_id:
await replay_media(bot, message)
return
reference_id = int(message.chat.id)
await bot.copy_media_group(chat_id=owner_id, from_chat_id=reference_id, message_id=message.message_id)
@bot.on_message((filters.group | filters.private) & filters.media)
async def pm_media(bot, message):
chat_id = message.from_user.id
# Adding to DB
if not await db.is_user_exist(chat_id):
data = await bot.get_me()
BOT_USERNAME = data.username
await db.add_user(chat_id)
await bot.send_message(
LOG_CHANNEL,
f"#NEWUSER: \n\nNew User [{message.from_user.first_name}](tg://user?id={message.from_user.id}) started @{BOT_USERNAME} !!",
)
ban_status = await db.get_ban_status(chat_id)
is_banned = ban_status['is_banned']
ban_duration = ban_status['ban_duration']
ban_reason = ban_status['ban_reason']
if is_banned is True:
await message.reply_text(f"You are Banned 🚫 to use this bot for **{ban_duration}** day(s) for the reason __{ban_reason}__ \n\n**Message from the admin 🤠**")
return
if message.from_user.id == owner_id:
await replay_media(bot, message)
return
info = await bot.get_users(user_ids=message.from_user.id)
reference_id = int(message.chat.id)
if message.media_group_id is not None:
# media = []
# async for m in bot.iter_history(message.chat.id, message.media_group_id):
# print(m)
# media.append(message.photo)
# bot.send_media_group(chat_id=owner_id, media=media)
return
else:
await bot.copy_message(
chat_id=owner_id,
from_chat_id=message.chat.id,
message_id=message.message_id,
caption=IF_CONTENT.format(reference_id, info.first_name),
)
@bot.on_message(filters.user(owner_id) & filters.text)
async def reply_text(bot, message):
chat_id = message.from_user.id
# Adding to DB
if not await db.is_user_exist(chat_id):
data = await bot.get_me()
BOT_USERNAME = data.username
await db.add_user(chat_id)
await bot.send_message(
LOG_CHANNEL,
f"#NEWUSER: \n\nNew User [{message.from_user.first_name}](tg://user?id={message.from_user.id}) started @{BOT_USERNAME} !!",
)
reference_id = True
if message.reply_to_message is not None:
file = message.reply_to_message
try:
reference_id = file.text.split()[2]
except Exception:
pass
try:
reference_id = file.caption.split()[2]
except Exception:
pass
await bot.send_message(
chat_id=int(reference_id),
#from_chat_id=message.chat.id,
#message_id=message.message_id,
text=message.text
)
@bot.on_message(filters.user(owner_id) & filters.media)
async def replay_media(bot, message):
chat_id = message.from_user.id
# Adding to DB
if not await db.is_user_exist(chat_id):
data = await bot.get_me()
BOT_USERNAME = data.username
await db.add_user(chat_id)
await bot.send_message(
LOG_CHANNEL,
f"#NEWUSER: \n\nNew User [{message.from_user.first_name}](tg://user?id={message.from_user.id}) started @{BOT_USERNAME} !!",
)
reference_id = True
if message.reply_to_message is not None:
file = message.reply_to_message
try:
reference_id = file.text.split()[2]
except Exception:
pass
try:
reference_id = file.caption.split()[2]
except Exception:
pass
# check if media is group of media
if message.media_group_id is not None:
await bot.copy_message(
chat_id=int(reference_id),
from_chat_id=message.chat.id,
message_id=message.message_id,
caption=message.caption,
)
else:
await bot.copy_message(
chat_id=int(reference_id),
from_chat_id=message.chat.id,
message_id=message.message_id,
)
if __name__ == "__main__":
print("---------- ** Bot Started ** ----------")
bot.run()