Update bot.py
Browse files
bot.py
CHANGED
@@ -3,16 +3,14 @@ import os, math, logging, datetime, pytz, logging.config
|
|
3 |
from aiohttp import web
|
4 |
from pyrogram import Client, types
|
5 |
from database.users_chats_db import db
|
6 |
-
from database.ia_filterdb import
|
7 |
from typing import Union, Optional, AsyncGenerator
|
8 |
from utils import temp, __repo__, __license__, __copyright__, __version__
|
9 |
from info import API_ID, API_HASH, BOT_TOKEN, LOG_CHANNEL, UPTIME, WEB_SUPPORT, LOG_MSG
|
10 |
|
11 |
# Get logging configurations
|
12 |
logging.config.fileConfig("logging.conf")
|
13 |
-
logging.getLogger(__name__)
|
14 |
-
logging.getLogger("cinemagoer").setLevel(logging.ERROR)
|
15 |
-
|
16 |
|
17 |
class Bot(Client):
|
18 |
def __init__(self):
|
@@ -23,14 +21,21 @@ class Bot(Client):
|
|
23 |
bot_token=BOT_TOKEN,
|
24 |
plugins=dict(root="plugins")
|
25 |
)
|
|
|
26 |
|
27 |
async def start(self):
|
|
|
28 |
b_users, b_chats = await db.get_banned()
|
29 |
temp.BANNED_USERS = b_users
|
30 |
-
temp.BANNED_CHATS = b_chats
|
31 |
-
|
|
|
32 |
await super().start()
|
|
|
|
|
33 |
await Media.ensure_indexes()
|
|
|
|
|
34 |
me = await self.get_me()
|
35 |
temp.U_NAME = me.username
|
36 |
temp.B_NAME = me.first_name
|
@@ -43,37 +48,39 @@ class Bot(Client):
|
|
43 |
curr = datetime.datetime.now(pytz.timezone("Asia/Kolkata"))
|
44 |
date = curr.strftime('%d %B, %Y')
|
45 |
tame = curr.strftime('%I:%M:%S %p')
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
if bool(WEB_SUPPORT) is True:
|
52 |
app = web.AppRunner(web.Application(client_max_size=30000000))
|
53 |
await app.setup()
|
54 |
await web.TCPSite(app, "0.0.0.0", 8080).start()
|
55 |
-
|
56 |
-
|
57 |
async def stop(self, *args):
|
|
|
58 |
await super().stop()
|
59 |
-
|
60 |
|
61 |
-
async def iter_messages(self, chat_id: Union[int, str], limit: int, offset: int = 0) -> Optional[AsyncGenerator["types.Message", None]]:
|
|
|
62 |
current = offset
|
63 |
while True:
|
64 |
new_diff = min(200, limit - current)
|
65 |
if new_diff <= 0:
|
|
|
66 |
return
|
67 |
messages = await self.get_messages(chat_id, list(range(current, current+new_diff+1)))
|
|
|
68 |
for message in messages:
|
69 |
yield message
|
70 |
current += 1
|
|
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
Bot().run()
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
3 |
from aiohttp import web
|
4 |
from pyrogram import Client, types
|
5 |
from database.users_chats_db import db
|
6 |
+
from database.ia_filterdb import Media
|
7 |
from typing import Union, Optional, AsyncGenerator
|
8 |
from utils import temp, __repo__, __license__, __copyright__, __version__
|
9 |
from info import API_ID, API_HASH, BOT_TOKEN, LOG_CHANNEL, UPTIME, WEB_SUPPORT, LOG_MSG
|
10 |
|
11 |
# Get logging configurations
|
12 |
logging.config.fileConfig("logging.conf")
|
13 |
+
logger = logging.getLogger(__name__)
|
|
|
|
|
14 |
|
15 |
class Bot(Client):
|
16 |
def __init__(self):
|
|
|
21 |
bot_token=BOT_TOKEN,
|
22 |
plugins=dict(root="plugins")
|
23 |
)
|
24 |
+
logger.info("Bot initialized.")
|
25 |
|
26 |
async def start(self):
|
27 |
+
logger.info("Starting bot...")
|
28 |
b_users, b_chats = await db.get_banned()
|
29 |
temp.BANNED_USERS = b_users
|
30 |
+
temp.BANNED_CHATS = b_chats
|
31 |
+
logger.info("Banned users and chats loaded.")
|
32 |
+
|
33 |
await super().start()
|
34 |
+
logger.info("Pyrogram client started.")
|
35 |
+
|
36 |
await Media.ensure_indexes()
|
37 |
+
logger.info("Indexes ensured for Media collection.")
|
38 |
+
|
39 |
me = await self.get_me()
|
40 |
temp.U_NAME = me.username
|
41 |
temp.B_NAME = me.first_name
|
|
|
48 |
curr = datetime.datetime.now(pytz.timezone("Asia/Kolkata"))
|
49 |
date = curr.strftime('%d %B, %Y')
|
50 |
tame = curr.strftime('%I:%M:%S %p')
|
51 |
+
log_message = LOG_MSG.format(me.first_name, date, tame, __repo__, __version__, __license__, __copyright__)
|
52 |
+
logger.info(log_message)
|
53 |
+
|
54 |
+
try:
|
55 |
+
await self.send_message(LOG_CHANNEL, text=log_message, disable_web_page_preview=True)
|
56 |
+
logger.info("Log message sent to LOG_CHANNEL.")
|
57 |
+
except Exception as e:
|
58 |
+
logger.warning(f"Bot Isn't Able To Send Message To LOG_CHANNEL \n{e}")
|
59 |
+
|
60 |
if bool(WEB_SUPPORT) is True:
|
61 |
app = web.AppRunner(web.Application(client_max_size=30000000))
|
62 |
await app.setup()
|
63 |
await web.TCPSite(app, "0.0.0.0", 8080).start()
|
64 |
+
logger.info("Web Response Is Running......🕸️")
|
65 |
+
|
66 |
async def stop(self, *args):
|
67 |
+
logger.info("Stopping bot...")
|
68 |
await super().stop()
|
69 |
+
logger.info(f"Bot Is Restarting ⟳...")
|
70 |
|
71 |
+
async def iter_messages(self, chat_id: Union[int, str], limit: int, offset: int = 0) -> Optional[AsyncGenerator["types.Message", None]]:
|
72 |
+
logger.info(f"Iterating messages in chat_id: {chat_id}, limit: {limit}, offset: {offset}")
|
73 |
current = offset
|
74 |
while True:
|
75 |
new_diff = min(200, limit - current)
|
76 |
if new_diff <= 0:
|
77 |
+
logger.info("No more messages to iterate.")
|
78 |
return
|
79 |
messages = await self.get_messages(chat_id, list(range(current, current+new_diff+1)))
|
80 |
+
logger.info(f"Retrieved {len(messages)} messages.")
|
81 |
for message in messages:
|
82 |
yield message
|
83 |
current += 1
|
84 |
+
logger.info(f"Yielding message with ID: {message.id}")
|
85 |
|
86 |
+
Bot().run()
|
|
|
|
|
|
|
|
|
|
|
|
|
|