63 lines
1.8 KiB
Django/Jinja
63 lines
1.8 KiB
Django/Jinja
import asyncio
|
|
import contextlib
|
|
|
|
import anyio
|
|
import uvloop
|
|
|
|
{% if use_dishka %}from dependencies.container import container
|
|
{% endif %}from userbot.modules.client import PyroClient
|
|
{% if database != 'none' %}from utils.db import init_db
|
|
{% endif %}from utils.logging import logger, setup_logging
|
|
|
|
setup_logging()
|
|
|
|
|
|
async def runner() -> None:
|
|
{% if database != 'none' %} await init_db()
|
|
|
|
{% endif %} sessions_dir = anyio.Path("sessions")
|
|
await sessions_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
started: set[str] = set()
|
|
clients: dict[int, PyroClient] = {}
|
|
|
|
try:
|
|
while True:
|
|
current = {path.name async for path in sessions_dir.glob("*.session")}
|
|
|
|
for session_file in current - started:
|
|
started.add(session_file)
|
|
name = session_file.removesuffix(".session")
|
|
try:
|
|
client = PyroClient(name)
|
|
await client.start()
|
|
except Exception as e:
|
|
logger.warning(f"[red]Client {name} failed to start: {e}[/]")
|
|
continue
|
|
|
|
me = client.me
|
|
logger.info(
|
|
f"[green]Client started as[/] "
|
|
f"{me.full_name if me else 'unknown'} ({me.id if me else '?'})"
|
|
)
|
|
if me:
|
|
clients[me.id] = client
|
|
|
|
await asyncio.sleep(10)
|
|
finally:
|
|
for client in clients.values():
|
|
with contextlib.suppress(Exception):
|
|
await client.stop()
|
|
{% if use_dishka %} await container.close()
|
|
{% endif %}
|
|
|
|
def main() -> None:
|
|
uvloop.install()
|
|
|
|
logger.info("Starting...")
|
|
|
|
with contextlib.suppress(KeyboardInterrupt):
|
|
asyncio.run(runner())
|
|
|
|
logger.info("[red]Stopped.[/]")
|