PagerMaid-Pyro/pagermaid/__init__.py

101 lines
2.6 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
2023-11-07 12:05:06 +00:00
from datetime import datetime, timezone, timedelta
2023-03-12 03:56:01 +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
import pagermaid.update
2022-05-23 12:40:30 +00:00
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
pgm_version = "1.4.10"
pgm_version_code = 1410
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]]]] = {
2023-03-12 03:56:01 +00:00
"startup": set(),
"shutdown": set(),
"command_pre": set(),
"command_post": set(),
"process_error": set(),
2022-11-14 14:11:27 +00:00
"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(INFO if Config.DEBUG else CRITICAL)
2022-06-20 13:55:14 +00:00
pyro_logger.addHandler(logging_handler)
file_handler = FileHandler(
filename="data/pagermaid.log.txt", mode="w", encoding="utf-8"
)
2022-08-02 04:57:08 +00:00
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
2023-03-12 03:56:01 +00:00
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,
2023-06-18 03:13:16 +00:00
app_version=f"PGP {pgm_version}",
workdir="data",
2022-12-08 05:46:38 +00:00
)
2022-06-20 13:55:14 +00:00
bot.job = scheduler
2022-05-23 12:40:30 +00:00
2023-11-07 12:05:06 +00:00
async def log(message: str, notice: bool = False):
2023-03-12 03:56:01 +00:00
logs.info(message.replace("`", '"'))
2022-05-23 12:40:30 +00:00
if not Config.LOG:
return
2022-06-26 12:59:36 +00:00
try:
2023-11-07 12:05:06 +00:00
await bot.send_message(
Config.LOG_ID,
message,
schedule_date=(datetime.now() + timedelta(seconds=3)) if notice else None,
)
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"