2023-04-01 15:25:41 +03:00
|
|
|
import logging
|
|
|
|
from os import getpid
|
|
|
|
from time import time
|
|
|
|
|
|
|
|
import pyrogram
|
2023-05-14 22:30:40 +03:00
|
|
|
from libbot import config_get
|
2023-04-01 15:25:41 +03:00
|
|
|
from pyrogram.client import Client
|
|
|
|
from pyrogram.errors import BadRequest
|
|
|
|
from pyrogram.raw.all import layer
|
|
|
|
from ujson import loads
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class PyroClient(Client):
|
|
|
|
def __init__(self):
|
|
|
|
with open("config.json", "r", encoding="utf-8") as f:
|
|
|
|
config = loads(f.read())
|
|
|
|
super().__init__(
|
|
|
|
name="bot_client",
|
|
|
|
api_id=config["bot"]["api_id"],
|
|
|
|
api_hash=config["bot"]["api_hash"],
|
|
|
|
bot_token=config["bot"]["bot_token"],
|
|
|
|
workers=config["bot"]["workers"],
|
|
|
|
plugins=dict(root="plugins", exclude=config["disabled_plugins"]),
|
|
|
|
sleep_threshold=120,
|
|
|
|
)
|
2023-05-23 16:14:50 +03:00
|
|
|
self.start_time = 0
|
2023-04-01 15:25:41 +03:00
|
|
|
|
|
|
|
async def start(self):
|
|
|
|
await super().start()
|
|
|
|
|
|
|
|
self.start_time = time()
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
"Bot is running with Pyrogram v%s (Layer %s) and has started as @%s on PID %s.",
|
|
|
|
pyrogram.__version__,
|
|
|
|
layer,
|
|
|
|
self.me.username,
|
|
|
|
getpid(),
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await self.send_message(
|
|
|
|
chat_id=await config_get("chat_id", "reports"),
|
|
|
|
text=f"Bot started PID `{getpid()}`",
|
|
|
|
)
|
|
|
|
except BadRequest:
|
|
|
|
logger.warning("Unable to send message to report chat.")
|
|
|
|
|
|
|
|
async def stop(self):
|
|
|
|
try:
|
|
|
|
await self.send_message(
|
|
|
|
chat_id=await config_get("chat_id", "reports"),
|
|
|
|
text=f"Bot stopped with PID `{getpid()}`",
|
|
|
|
)
|
|
|
|
except BadRequest:
|
|
|
|
logger.warning("Unable to send message to report chat.")
|
|
|
|
await super().stop()
|
2023-05-23 16:14:50 +03:00
|
|
|
logger.warning("Bot stopped with PID %s.", getpid())
|