PagerMaid-Pyro/pagermaid/__init__.py

83 lines
2.3 KiB
Python
Raw Normal View History

2022-06-20 13:55:14 +00:00
import contextlib
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
2023-01-31 16:24:56 +00:00
pgm_version = "1.2.30"
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-11-14 14:11:27 +00:00
"startup": set(), "shutdown": set(), "command_pre": set(), "command_post": set(), "process_error": set(),
"load_plugins_finished": 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
2022-10-21 16:51:56 +00:00
if not Config.API_ID or not Config.API_HASH:
logs.warning("Api-ID or Api-HASH Not Found!")
Config.API_ID = Config.DEFAULT_API_ID
Config.API_HASH = Config.DEFAULT_API_HASH
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-12-08 05:46:38 +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,
app_version=f"PagerMaid {pgm_version}",
)
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(
2022-12-08 05:46:38 +00:00
Config.LOG_ID,
message
2022-06-26 12:59:36 +00:00
)
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"