sh_discord_bot/src/bot/main.py

69 lines
1.9 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
2022-10-02 02:54:08 +02:00
from bot.startup_settings_extension import StartupSettingsExtension
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-10-02 02:54:08 +02:00
app_builder = ApplicationBuilder(Application) \
.use_extension(StartupSettingsExtension) \
.use_extension(StartupDiscordExtension) \
.use_extension(StartupMigrationExtension) \
.use_extension(BootLogExtension) \
.use_extension(DatabaseExtension) \
.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-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:
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
# ___// | /
# `--' \_~-,