antichrist_bot/main.py

37 lines
891 B
Python

import asyncio
import signal
from core.bot import Bot
from core.config import ConfigManager
from core.logger import logger
async def shutdown(signal, loop, bot):
logger.warning(f"Received exit signal {signal.name}...")
await bot.stop()
tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
for task in tasks:
task.cancel()
await asyncio.gather(*tasks, return_exceptions=True)
loop.stop()
async def main():
config = ConfigManager("configs/config.toml")
bot = Bot(config)
loop = asyncio.get_running_loop()
for sig in (signal.SIGINT, signal.SIGTERM):
loop.add_signal_handler(
sig,
lambda s=sig: asyncio.create_task(shutdown(s, loop, bot))
)
try:
await bot.start()
except asyncio.CancelledError:
pass
if __name__ == '__main__':
asyncio.run(main())