PagerMaid-Pyro/pagermaid/__main__.py
xtaodada 93ab93a5ec
All checks were successful
Docker Dev Build / docker build and publish (push) Successful in 6m34s
Docker Build / docker build and publish (push) Successful in 11m47s
🚑 fix: pyrogram bot PARENT_DIR
2024-09-29 12:39:32 +08:00

102 lines
2.5 KiB
Python

import asyncio
from os import sep
from pathlib import Path
from signal import signal as signal_fn, SIGINT, SIGTERM, SIGABRT
from sys import path, platform, exit
from pyrogram.errors import AuthKeyUnregistered
from pagermaid.common.reload import load_all
from pagermaid.config import Config
from pagermaid.dependence import scheduler
from pagermaid.services import bot
from pagermaid.static import working_dir
from pagermaid.utils import lang, safe_remove, logs
from pagermaid.utils.listener import process_exit
from pagermaid.web import web
from pagermaid.web.api.web_login import web_login
from pyromod.methods.sign_in_qrcode import start_client
bot.PARENT_DIR = Path(working_dir)
path.insert(1, f"{working_dir}{sep}plugins")
async def idle():
task = None
def signal_handler(_, __):
if web.web_server_task:
web.web_server_task.cancel()
task.cancel()
for s in (SIGINT, SIGTERM, SIGABRT):
signal_fn(s, signal_handler)
while True:
task = asyncio.create_task(asyncio.sleep(600))
web.bot_main_task = task
try:
await task
except asyncio.CancelledError:
break
async def console_bot():
try:
await start_client(bot)
except AuthKeyUnregistered:
safe_remove("pagermaid.session")
exit()
me = await bot.get_me()
if me.is_bot:
safe_remove("pagermaid.session")
exit()
logs.info(f"{lang('save_id')} {me.first_name}({me.id})")
await load_all()
await process_exit(start=True, _client=bot)
async def web_bot():
try:
await web_login.init()
except AuthKeyUnregistered:
safe_remove("pagermaid.session")
exit()
if bot.me is not None:
me = await bot.get_me()
if me.is_bot:
safe_remove("pagermaid.session")
exit()
else:
logs.info("Please use web to login, path: web_login .")
async def main():
logs.info(lang("platform") + platform + lang("platform_load"))
if not scheduler.running:
scheduler.start()
await web.start()
if not (Config.WEB_ENABLE and Config.WEB_LOGIN):
await console_bot()
logs.info(lang("start"))
else:
await web_bot()
try:
await idle()
finally:
if scheduler.running:
scheduler.shutdown()
try:
await bot.stop()
except ConnectionError:
pass
if web.web_server:
try:
await web.web_server.shutdown()
except AttributeError:
pass
bot.run(main())