Initial commit
This commit is contained in:
60
main.py
Normal file
60
main.py
Normal file
@@ -0,0 +1,60 @@
|
||||
"""Application entry point."""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
from pathlib import Path
|
||||
import signal
|
||||
import traceback
|
||||
|
||||
from mab import MatrixBot
|
||||
|
||||
import logic
|
||||
import config
|
||||
|
||||
def signal_callback(event: asyncio.Event) -> None:
|
||||
logging.info("Signal is caught")
|
||||
event.set()
|
||||
|
||||
async def main() -> None:
|
||||
"""Application entry point."""
|
||||
# logging setup
|
||||
logging.basicConfig(
|
||||
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
level=logging.INFO
|
||||
)
|
||||
logging.getLogger("nio").setLevel(logging.CRITICAL + 1)
|
||||
logging.info("2026-matrix-down")
|
||||
# setup app termination logic
|
||||
stop_event = asyncio.Event()
|
||||
for s in (signal.SIGINT, signal.SIGTERM):
|
||||
asyncio.get_running_loop().add_signal_handler(
|
||||
s,
|
||||
signal_callback,
|
||||
stop_event
|
||||
)
|
||||
# load the config
|
||||
bot_config = await config.load_config()
|
||||
if bot_config is None:
|
||||
logging.critical("Please edit config.json and restart the bot")
|
||||
return
|
||||
|
||||
# setup the bot
|
||||
bot = MatrixBot(bot_config)
|
||||
await logic.initialize(bot)
|
||||
bot.start()
|
||||
|
||||
# wait for termination
|
||||
logging.info("You may use Ctrl+C to terminate the app")
|
||||
await stop_event.wait()
|
||||
|
||||
await bot.stop()
|
||||
await logic.deinitizalize(bot)
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
asyncio.run(main())
|
||||
except SystemExit:
|
||||
raise
|
||||
except:
|
||||
traceback.print_exc()
|
||||
Reference in New Issue
Block a user