PagerMaid-Pyro/pagermaid/__main__.py

100 lines
2.5 KiB
Python
Raw Normal View History

2023-06-18 03:13:16 +00:00
import asyncio
2022-05-23 12:40:30 +00:00
from os import sep
2023-06-18 03:13:16 +00:00
from signal import signal as signal_fn, SIGINT, SIGTERM, SIGABRT
from sys import path, platform, exit
2022-05-23 12:40:30 +00:00
2022-12-22 04:00:10 +00:00
from pyrogram.errors import AuthKeyUnregistered
2022-05-23 12:40:30 +00:00
2023-06-18 03:13:16 +00:00
from pagermaid.common.reload import load_all
from pagermaid.config import Config
2024-09-29 03:51:00 +00:00
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
2023-06-18 03:13:16 +00:00
from pagermaid.web import web
from pagermaid.web.api.web_login import web_login
from pyromod.methods.sign_in_qrcode import start_client
2022-05-23 12:40:30 +00:00
path.insert(1, f"{working_dir}{sep}plugins")
2022-05-27 15:09:24 +00:00
2023-06-18 03:13:16 +00:00
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
2022-05-27 15:09:24 +00:00
2023-06-18 03:13:16 +00:00
async def console_bot():
2022-12-22 04:00:10 +00:00
try:
await start_client(bot)
2022-12-22 04:00:10 +00:00
except AuthKeyUnregistered:
safe_remove("pagermaid.session")
exit()
me = await bot.get_me()
2022-08-01 16:04:45 +00:00
if me.is_bot:
safe_remove("pagermaid.session")
exit()
logs.info(f"{lang('save_id')} {me.first_name}({me.id})")
2023-06-18 03:13:16 +00:00
await load_all()
await process_exit(start=True, _client=bot)
2022-05-27 15:09:24 +00:00
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 .")
2023-06-18 03:13:16 +00:00
async def main():
logs.info(lang("platform") + platform + lang("platform_load"))
2024-09-29 03:51:00 +00:00
if not scheduler.running:
scheduler.start()
2023-06-18 03:13:16 +00:00
await web.start()
if not (Config.WEB_ENABLE and Config.WEB_LOGIN):
await console_bot()
logs.info(lang("start"))
else:
await web_bot()
2022-06-27 13:42:24 +00:00
2023-06-18 03:13:16 +00:00
try:
await idle()
finally:
2024-09-29 03:51:00 +00:00
if scheduler.running:
scheduler.shutdown()
2023-06-18 03:13:16 +00:00
try:
await bot.stop()
except ConnectionError:
pass
if web.web_server:
try:
await web.web_server.shutdown()
except AttributeError:
pass
2022-05-27 15:09:24 +00:00
2023-03-12 03:56:01 +00:00
2022-05-27 15:09:24 +00:00
bot.run(main())