Added functionality to connect to discord web api

This commit is contained in:
Sven Heidemann 2021-11-15 20:34:15 +01:00
parent abe9a7b472
commit 26a93c5ea1
10 changed files with 72 additions and 22 deletions

View File

@ -20,6 +20,8 @@ __version__ = '0.1.0'
from collections import namedtuple
# imports:
import nest_asyncio
nest_asyncio.apply()
VersionInfo = namedtuple('VersionInfo', 'major minor micro')
version_info = VersionInfo(major='0', minor='1', micro='0')

View File

@ -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)

View File

@ -11,5 +11,13 @@
"Filename": "log_$start_time.log",
"ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE"
},
"Discord": {
"Token": "OTA5ODc4NDcyNzExNzU3ODQ1.YZKsXA.BXBszIF3z3wHpoe9s3pKfO3Yd5c"
},
"Bot": {
"Prefix": "!dev-g"
}
}

View File

@ -1,5 +1,2 @@
{
"Discord": {
"Token": ""
}
}

View File

@ -11,5 +11,13 @@
"Filename": "log_$start_time.log",
"ConsoleLogLevel": "ERROR",
"FileLogLevel": "WARN"
},
"Discord": {
"Token": "OTA5ODc3NDg3MjEzODk5ODQ3.YZKrcQ.qwfDWBFdkOzxZZT10jUWG5fY2RA"
},
"Bot": {
"Prefix": "!g"
}
}

View File

@ -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": {

View File

@ -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()

View File

@ -7,7 +7,7 @@ class BotServiceABC(ABC):
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

View File

@ -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()}')

View File

@ -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):
async def stop_async(self):
try:
pass
# save data
except Exception as e:
self._logger.error(__name__, 'Stop failed', e)