PagerMaid-Pyro/pagermaid/__init__.py

82 lines
2.3 KiB
Python
Raw Normal View History

2022-06-20 13:55:14 +00:00
import contextlib
2022-08-14 04:44:43 +00:00
import sys
2022-08-02 04:57:08 +00:00
from typing import Callable, Awaitable, Set, Dict
2022-06-27 13:42:24 +00:00
2022-05-23 12:40:30 +00:00
from coloredlogs import ColoredFormatter
2022-06-20 13:55:14 +00:00
from datetime import datetime, timezone
2022-08-02 04:57:08 +00:00
from logging import getLogger, StreamHandler, CRITICAL, INFO, basicConfig, DEBUG, Formatter, FileHandler
2022-05-23 12:40:30 +00:00
from os import getcwd
from pagermaid.config import Config
2022-06-20 13:55:14 +00:00
from pagermaid.scheduler import scheduler
2022-05-23 12:40:30 +00:00
import pyromod.listen
from pyrogram import Client
2022-09-01 08:35:19 +00:00
pgm_version = "1.2.9"
2022-05-23 12:40:30 +00:00
CMD_LIST = {}
module_dir = __path__[0]
working_dir = getcwd()
# solve same process
read_context = {}
help_messages = {}
hook_functions: Dict[str, Set[Callable[[], Awaitable[None]]]] = {
2022-07-08 14:26:36 +00:00
"startup": set(), "shutdown": set(), "command_pre": set(), "command_post": set(), "process_error": set(), }
2022-05-23 12:40:30 +00:00
all_permissions = []
2022-06-20 13:55:14 +00:00
2022-05-23 12:40:30 +00:00
logs = getLogger(__name__)
logging_format = "%(levelname)s [%(asctime)s] [%(name)s] %(message)s"
logging_handler = StreamHandler()
logging_handler.setFormatter(ColoredFormatter(logging_format))
root_logger = getLogger()
2022-05-27 15:09:24 +00:00
root_logger.setLevel(DEBUG if Config.DEBUG else CRITICAL)
2022-05-23 12:40:30 +00:00
root_logger.addHandler(logging_handler)
2022-06-20 13:55:14 +00:00
pyro_logger = getLogger("pyrogram")
pyro_logger.setLevel(CRITICAL)
pyro_logger.addHandler(logging_handler)
2022-08-02 04:57:08 +00:00
file_handler = FileHandler(filename="pagermaid.log.txt", mode="w", encoding="utf-8")
file_handler.setFormatter(Formatter(logging_format))
root_logger.addHandler(file_handler)
2022-05-27 15:09:24 +00:00
basicConfig(level=DEBUG if Config.DEBUG else INFO)
logs.setLevel(DEBUG if Config.DEBUG else INFO)
2022-05-23 12:40:30 +00:00
# easy check
if not Config.API_ID:
logs.error("Api-ID Not Found!")
2022-05-25 04:19:25 +00:00
sys.exit(1)
2022-05-23 12:40:30 +00:00
elif not Config.API_HASH:
logs.error("Api-Hash Not Found!")
2022-05-25 04:19:25 +00:00
sys.exit(1)
2022-05-23 12:40:30 +00:00
2022-06-20 13:55:14 +00:00
start_time = datetime.now(timezone.utc)
2022-05-27 15:09:24 +00:00
2022-06-20 13:55:14 +00:00
with contextlib.suppress(ImportError):
import uvloop # noqa
2022-05-27 15:09:24 +00:00
uvloop.install()
2022-06-20 13:55:14 +00:00
if not scheduler.running:
scheduler.start()
2022-05-25 11:26:50 +00:00
bot = Client("pagermaid",
session_string=Config.STRING_SESSION,
api_id=Config.API_ID,
api_hash=Config.API_HASH,
ipv6=Config.IPV6,
proxy=Config.PROXY)
2022-06-20 13:55:14 +00:00
bot.job = scheduler
2022-05-23 12:40:30 +00:00
async def log(message):
logs.info(
message.replace('`', '\"')
)
if not Config.LOG:
return
2022-06-26 12:59:36 +00:00
try:
await bot.send_message(
Config.LOG_ID,
message
)
2022-08-14 04:44:43 +00:00
except Exception:
2022-06-26 12:59:36 +00:00
Config.LOG = False
2022-08-14 04:44:43 +00:00
Config.LOG_ID = "me"