sh_discord_bot/kdb-bot/src/bot/main.py

89 lines
2.8 KiB
Python
Raw Normal View History

2022-07-14 16:32:46 +02:00
import asyncio
2022-10-03 13:39:09 +02:00
import traceback
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.extension.init_bot_extension import InitBotExtension
2022-07-14 16:32:46 +02:00
from bot.startup import Startup
from bot.startup_discord_extension import StartupDiscordExtension
from bot.startup_migration_extension import StartupMigrationExtension
2022-10-03 00:28:22 +02:00
from bot.startup_module_extension import StartupModuleExtension
2022-10-02 02:54:08 +02:00
from bot.startup_settings_extension import StartupSettingsExtension
from bot_api.app_api_extension import AppApiExtension
2022-11-13 11:56:42 +01:00
from bot_core.core_extension.core_extension import CoreExtension
from modules.boot_log.boot_log_extension import BootLogExtension
from modules.config.config_extension import ConfigExtension
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-10-03 01:05:34 +02:00
# discord extension has to be loaded before modules (modules depends on discord stuff)
2023-01-13 23:35:42 +01:00
app_builder = (
ApplicationBuilder(Application)
.use_extension(StartupSettingsExtension)
.use_extension(StartupDiscordExtension)
.use_extension(StartupModuleExtension)
.use_extension(StartupMigrationExtension)
.use_extension(DatabaseExtension)
.use_extension(ConfigExtension)
2023-01-13 23:35:42 +01:00
.use_extension(InitBotExtension)
.use_extension(BootLogExtension)
.use_extension(AppApiExtension)
.use_extension(CoreExtension)
2022-10-02 02:54:08 +02:00
.use_startup(Startup)
2023-01-13 23:35:42 +01:00
)
2022-07-19 11:58:33 +02:00
self.app: Application = await app_builder.build_async()
await self.app.run_async()
2023-03-30 14:43:21 +02:00
Console.write_line(f"[ INFO ] [ {__name__} ]: Finished app.run_async")
2022-07-16 19:47:04 +02:00
async def stop(self):
2022-09-30 17:03:44 +02:00
if self.app is None:
return
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:
2023-01-13 23:35:42 +01:00
Console.error(
f"[ ERROR ] [ {__name__} ]: Cannot start the bot",
f"{e} -> {traceback.format_exc()}",
)
2022-07-19 11:58:33 +02:00
finally:
try:
asyncio.run(program.stop())
except Exception as e:
2023-01-13 23:35:42 +01:00
Console.error(
f"[ ERROR ] [ {__name__} ]: Cannot stop the bot",
f"{e} -> {traceback.format_exc()}",
)
2022-07-19 11:58:33 +02:00
if program.app is not None and program.app.is_restart():
del program
main()
2023-01-13 23:35:42 +01:00
if __name__ == "__main__":
2022-07-19 11:58:33 +02:00
main()
2022-07-16 19:47:04 +02:00
# ((
# ( `)
# ; / ,
# / \/
# / |
# / ~/
# / ) ) ~ edraft
# ___// | /
# `--' \_~-,