Initial commit
This commit is contained in:
60
modules/app.py
Normal file
60
modules/app.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import logging
|
||||
from os import getpid
|
||||
from time import time
|
||||
|
||||
import pyrogram
|
||||
from pyrogram.client import Client
|
||||
from pyrogram.errors import BadRequest
|
||||
from pyrogram.raw.all import layer
|
||||
from ujson import loads
|
||||
|
||||
from modules.utils import config_get
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
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()
|
||||
logger.warning(f"Bot stopped with PID {getpid()}.")
|
35
modules/utils.py
Normal file
35
modules/utils.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from typing import Any
|
||||
import aiofiles
|
||||
from ujson import loads, dumps
|
||||
|
||||
|
||||
async def json_read(path: str) -> Any:
|
||||
async with aiofiles.open(path, mode="r", encoding="utf-8") as f:
|
||||
data = await f.read()
|
||||
return loads(data)
|
||||
|
||||
|
||||
async def json_write(data: Any, path: str) -> None:
|
||||
async with aiofiles.open(path, mode="w", encoding="utf-8") as f:
|
||||
await f.write(dumps(data, ensure_ascii=False, escape_forward_slashes=False))
|
||||
|
||||
|
||||
async def config_get(key: str, *path: str) -> Any:
|
||||
this_key = await json_read("config.json")
|
||||
for dict_key in path:
|
||||
this_key = this_key[dict_key]
|
||||
return this_key[key]
|
||||
|
||||
|
||||
async def config_set(key: str, value: Any, *path: str) -> None:
|
||||
this_dict = await json_read("config.json")
|
||||
string = "this_dict"
|
||||
for arg in path:
|
||||
string += f'["{arg}"]'
|
||||
if type(value) in [str]:
|
||||
string += f'["{key}"] = "{value}"'
|
||||
else:
|
||||
string += f'["{key}"] = {value}'
|
||||
exec(string)
|
||||
await json_write(this_dict, "config.json")
|
||||
return
|
Reference in New Issue
Block a user