71 lines
1.8 KiB
Python
71 lines
1.8 KiB
Python
"""2026-matrix-sumka entry point."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import logging
|
|
import signal
|
|
from pathlib import Path
|
|
|
|
from mab import MatrixBot, MatrixBotConfig
|
|
|
|
from bot import SumkaBotController
|
|
from config import ConfigError, load_config
|
|
|
|
|
|
LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def run() -> int:
|
|
config_path = Path("config.json").resolve()
|
|
config = load_config(config_path)
|
|
if config is None:
|
|
LOGGER.error("Created %s; edit it and start the bot again", config_path)
|
|
return 2
|
|
|
|
matrix_config = MatrixBotConfig(
|
|
matrix_homeserver_url=config.matrix_homeserver,
|
|
matrix_username_localpart=config.matrix_user,
|
|
storage_directory=config.store_dir,
|
|
)
|
|
matrix_bot = MatrixBot(matrix_config)
|
|
controller = SumkaBotController(matrix_bot, config)
|
|
controller.setup_callback()
|
|
|
|
stop_event = asyncio.Event()
|
|
loop = asyncio.get_running_loop()
|
|
for caught_signal in (signal.SIGINT, signal.SIGTERM):
|
|
loop.add_signal_handler(caught_signal, stop_event.set)
|
|
|
|
started = False
|
|
try:
|
|
await matrix_bot.start()
|
|
started = True
|
|
LOGGER.info("Bot started")
|
|
await stop_event.wait()
|
|
finally:
|
|
await controller.shutdown()
|
|
if started:
|
|
await matrix_bot.stop()
|
|
LOGGER.info("Bot stopped")
|
|
return 0
|
|
|
|
|
|
def main() -> int:
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
|
)
|
|
logging.getLogger("nio").setLevel(logging.WARNING)
|
|
try:
|
|
return asyncio.run(run())
|
|
except ConfigError as error:
|
|
LOGGER.error("Invalid config: %s", error)
|
|
return 2
|
|
except KeyboardInterrupt:
|
|
return 130
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|