From 26a93c5ea1c24a5a52b35578e5456cd3e8f7dd1f Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 15 Nov 2021 20:34:15 +0100 Subject: [PATCH] Added functionality to connect to discord web api --- src/gismo/__init__.py | 4 +++- src/gismo/application.py | 4 ++-- src/gismo/appsettings.development.json | 8 +++++++ src/gismo/appsettings.edrafts-pc.json | 3 --- src/gismo/appsettings.production.json | 8 +++++++ src/gismo/gismo.json | 3 ++- src/gismo/main.py | 8 +++---- src/gismo_core/abc/bot_service_abc.py | 8 +++---- src/gismo_core/configuration/bot_settings.py | 23 ++++++++++++++++++ src/gismo_core/services/bot_service.py | 25 ++++++++++++++------ 10 files changed, 72 insertions(+), 22 deletions(-) create mode 100644 src/gismo_core/configuration/bot_settings.py diff --git a/src/gismo/__init__.py b/src/gismo/__init__.py index 0242bb0..5411e9b 100644 --- a/src/gismo/__init__.py +++ b/src/gismo/__init__.py @@ -19,7 +19,9 @@ __version__ = '0.1.0' from collections import namedtuple -# imports: +# imports: +import nest_asyncio +nest_asyncio.apply() VersionInfo = namedtuple('VersionInfo', 'major minor micro') version_info = VersionInfo(major='0', minor='1', micro='0') diff --git a/src/gismo/application.py b/src/gismo/application.py index 573a1e5..c5bf158 100644 --- a/src/gismo/application.py +++ b/src/gismo/application.py @@ -20,6 +20,6 @@ class Application(ApplicationABC): async def main(self): try: - await self._bot.run() + await self._bot.start_async() except Exception as e: - self._logger.error(__name__, 'A fatal error occured starting the bot', e) + self._logger.error(__name__, 'Start failed', e) diff --git a/src/gismo/appsettings.development.json b/src/gismo/appsettings.development.json index c18c91e..f48a355 100644 --- a/src/gismo/appsettings.development.json +++ b/src/gismo/appsettings.development.json @@ -11,5 +11,13 @@ "Filename": "log_$start_time.log", "ConsoleLogLevel": "TRACE", "FileLogLevel": "TRACE" + }, + + "Discord": { + "Token": "OTA5ODc4NDcyNzExNzU3ODQ1.YZKsXA.BXBszIF3z3wHpoe9s3pKfO3Yd5c" + }, + + "Bot": { + "Prefix": "!dev-g" } } diff --git a/src/gismo/appsettings.edrafts-pc.json b/src/gismo/appsettings.edrafts-pc.json index 07e013f..2c63c08 100644 --- a/src/gismo/appsettings.edrafts-pc.json +++ b/src/gismo/appsettings.edrafts-pc.json @@ -1,5 +1,2 @@ { - "Discord": { - "Token": "" - } } diff --git a/src/gismo/appsettings.production.json b/src/gismo/appsettings.production.json index 629e6eb..c31e974 100644 --- a/src/gismo/appsettings.production.json +++ b/src/gismo/appsettings.production.json @@ -11,5 +11,13 @@ "Filename": "log_$start_time.log", "ConsoleLogLevel": "ERROR", "FileLogLevel": "WARN" + }, + + "Discord": { + "Token": "OTA5ODc3NDg3MjEzODk5ODQ3.YZKrcQ.qwfDWBFdkOzxZZT10jUWG5fY2RA" + }, + + "Bot": { + "Prefix": "!g" } } diff --git a/src/gismo/gismo.json b/src/gismo/gismo.json index b8a447a..95a329a 100644 --- a/src/gismo/gismo.json +++ b/src/gismo/gismo.json @@ -18,7 +18,8 @@ "Dependencies": [ "sh_cpl-core>=2021.10.2", "sh_cpl-query>=2021.10.2", - "discord.py==1.7.3" + "discord.py==1.7.3", + "nest-asyncio==1.5.1" ], "PythonVersion": ">=3.9.2", "PythonPath": { diff --git a/src/gismo/main.py b/src/gismo/main.py index 808c102..ad64e2d 100644 --- a/src/gismo/main.py +++ b/src/gismo/main.py @@ -1,15 +1,15 @@ import asyncio -from cpl_core.application import ApplicationBuilder +from cpl_core.application import ApplicationBuilder, ApplicationABC -from application import Application -from startup import Startup +from gismo.application import Application +from gismo.startup import Startup async def main(): app_builder = ApplicationBuilder(Application) app_builder.use_startup(Startup) - app: Application = await app_builder.build_async() + app: ApplicationABC = await app_builder.build_async() await app.run_async() diff --git a/src/gismo_core/abc/bot_service_abc.py b/src/gismo_core/abc/bot_service_abc.py index 62972ca..458cd95 100644 --- a/src/gismo_core/abc/bot_service_abc.py +++ b/src/gismo_core/abc/bot_service_abc.py @@ -5,9 +5,9 @@ class BotServiceABC(ABC): @abstractmethod def __init__(self): pass - + @abstractmethod - async def run(self): pass - + async def start_async(self): pass + @abstractmethod - async def exit(self): pass + async def stop_async(self): pass diff --git a/src/gismo_core/configuration/bot_settings.py b/src/gismo_core/configuration/bot_settings.py new file mode 100644 index 0000000..e682e25 --- /dev/null +++ b/src/gismo_core/configuration/bot_settings.py @@ -0,0 +1,23 @@ +import traceback + +from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC +from cpl_core.console import Console + + +class BotSettings(ConfigurationModelABC): + + def __init__(self): + ConfigurationModelABC.__init__(self) + + self._prefix: str = '' + + @property + def prefix(self) -> str: + return self._prefix + + def from_dict(self, settings: dict): + try: + self._prefix = settings['Prefix'] + except Exception as e: + Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in {self.__name__} settings') + Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') diff --git a/src/gismo_core/services/bot_service.py b/src/gismo_core/services/bot_service.py index e70f973..ceff6de 100644 --- a/src/gismo_core/services/bot_service.py +++ b/src/gismo_core/services/bot_service.py @@ -1,15 +1,26 @@ +from cpl_core.logging import LoggerABC +from discord.ext import commands + from gismo_core.abc.bot_service_abc import BotServiceABC +from gismo_core.configuration.bot_settings import BotSettings from gismo_core.configuration.discord_settings import DiscordSettings -class BotService(BotServiceABC): +class BotService(BotServiceABC, commands.Bot): - def __init__(self, discord_settings: DiscordSettings): + def __init__(self, logger: LoggerABC, discord_settings: DiscordSettings, bot_settings: BotSettings): + self._logger = logger + self._discord_settings = discord_settings + self._bot_settings: BotSettings = bot_settings # setup self - commands.Bot.__init__(self, command_prefix=discord_settings.bot.prefix, help_command=None) + commands.Bot.__init__(self, command_prefix=bot_settings.prefix, help_command=None) - async def run(self): - pass + async def start_async(self): + self.run(self._discord_settings.token) - async def exit(self): - pass + async def stop_async(self): + try: + pass + # save data + except Exception as e: + self._logger.error(__name__, 'Stop failed', e)