kd_discord_bot/src/bot/main.py

65 lines
1.8 KiB
Python
Raw Normal View History

2022-07-14 16:32:46 +02:00
import asyncio
2022-07-16 19:47:04 +02:00
from typing import Optional
2022-07-14 16:32:46 +02:00
from cpl_core.application import ApplicationBuilder
2022-07-19 11:58:33 +02:00
from cpl_core.console import Console
2022-07-14 16:32:46 +02:00
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
2022-07-14 16:32:46 +02:00
2022-07-19 11:58:33 +02:00
class Program:
2022-07-16 19:47:04 +02:00
def __init__(self):
2022-07-19 11:58:33 +02:00
self.app: Optional[Application] = None
2022-07-16 19:47:04 +02:00
2022-07-19 11:58:33 +02:00
async def start(self):
2022-07-16 19:47:04 +02:00
app_builder = ApplicationBuilder(Application)
app_builder.use_extension(StartupDiscordExtension)
app_builder.use_extension(StartupMigrationExtension)
app_builder.use_extension(BootLogExtension)
app_builder.use_extension(DatabaseExtension)
2022-07-16 19:47:04 +02:00
app_builder.use_startup(Startup)
2022-07-19 11:58:33 +02:00
self.app: Application = await app_builder.build_async()
await self.app.run_async()
2022-07-16 19:47:04 +02:00
async def stop(self):
2022-07-19 11:58:33 +02:00
await self.app.stop_async()
2022-07-14 16:32:46 +02:00
2022-07-19 11:58:33 +02:00
def main():
program = Program()
2022-07-16 19:47:04 +02:00
try:
2022-07-19 11:58:33 +02:00
asyncio.run(program.start())
2022-07-16 19:47:04 +02:00
except KeyboardInterrupt:
2022-07-19 11:58:33 +02:00
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()
2022-07-16 19:47:04 +02:00
# ((
# ( `)
# ; / ,
# / \/
# / |
# / ~/
# / ) ) ~ edraft
# ___// | /
# `--' \_~-,