kd_discord_bot/src/bot/main.py

65 lines
1.8 KiB
Python

import asyncio
from typing import Optional
from cpl_core.application import ApplicationBuilder
from cpl_core.console import Console
from bot.application import Application
from bot.startup import Startup
from bot.startup_discord_extension import StartupDiscordExtension
from bot.startup_migration_extension import StartupMigrationExtension
from modules.boot_log.boot_log_extension import BootLogExtension
from modules.database.database_extension import DatabaseExtension
class Program:
def __init__(self):
self.app: Optional[Application] = None
async def start(self):
app_builder = ApplicationBuilder(Application)
app_builder.use_extension(StartupDiscordExtension)
app_builder.use_extension(StartupMigrationExtension)
app_builder.use_extension(BootLogExtension)
app_builder.use_extension(DatabaseExtension)
app_builder.use_startup(Startup)
self.app: Application = await app_builder.build_async()
await self.app.run_async()
async def stop(self):
await self.app.stop_async()
def main():
program = Program()
try:
asyncio.run(program.start())
except KeyboardInterrupt:
asyncio.run(program.stop())
except Exception as e:
Console.error(f'[ ERROR ] [ {__name__} ]: Cannot start the bot', str(e))
finally:
try:
asyncio.run(program.stop())
except Exception as e:
Console.error(f'[ ERROR ] [ {__name__} ]: Cannot stop the bot', str(e))
if program.app is not None and program.app.is_restart():
del program
main()
if __name__ == '__main__':
main()
# ((
# ( `)
# ; / ,
# / \/
# / |
# / ~/
# / ) ) ~ edraft
# ___// | /
# `--' \_~-,