forked from sh-edraft.de/sh_discord_bot
Added configuration
This commit is contained in:
parent
f8cd150f72
commit
5093275053
1
.gitignore
vendored
1
.gitignore
vendored
@ -140,7 +140,6 @@ cython_debug/
|
|||||||
|
|
||||||
# custom vs code
|
# custom vs code
|
||||||
PythonImportHelper-v2-Completion.json
|
PythonImportHelper-v2-Completion.json
|
||||||
appsettings.*.json
|
|
||||||
|
|
||||||
# idea
|
# idea
|
||||||
.idea/
|
.idea/
|
@ -1,16 +1,44 @@
|
|||||||
from cpl_core.application import ApplicationABC
|
|
||||||
from cpl_core.configuration import ConfigurationABC
|
from cpl_core.configuration import ConfigurationABC
|
||||||
from cpl_core.console import Console
|
from cpl_core.console import Console
|
||||||
from cpl_core.dependency_injection import ServiceProviderABC
|
from cpl_core.dependency_injection import ServiceProviderABC
|
||||||
|
from cpl_core.logging import LoggerABC
|
||||||
|
from cpl_discord.application import DiscordBotApplicationABC
|
||||||
|
from cpl_discord.configuration import DiscordBotSettings
|
||||||
|
from cpl_discord.service import DiscordBotServiceABC, DiscordBotService
|
||||||
|
from cpl_translation import TranslatePipe, TranslationServiceABC, TranslationSettings
|
||||||
|
|
||||||
|
|
||||||
class Application(ApplicationABC):
|
class Application(DiscordBotApplicationABC):
|
||||||
|
|
||||||
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
|
def __init__(self, config: ConfigurationABC, services: ServiceProviderABC):
|
||||||
ApplicationABC.__init__(self, config, services)
|
DiscordBotApplicationABC.__init__(self, config, services)
|
||||||
|
|
||||||
|
# cpl-core
|
||||||
|
self._logger: LoggerABC = services.get_service(LoggerABC)
|
||||||
|
# cpl-discord
|
||||||
|
self._bot: DiscordBotServiceABC = services.get_service(DiscordBotServiceABC)
|
||||||
|
self._bot_settings: DiscordBotSettings = config.get_configuration(DiscordBotSettings)
|
||||||
|
# cpl-translation
|
||||||
|
self._translation: TranslationServiceABC = services.get_service(TranslationServiceABC)
|
||||||
|
self._translate: TranslatePipe = services.get_service(TranslatePipe)
|
||||||
|
|
||||||
async def configure(self):
|
async def configure(self):
|
||||||
pass
|
self._translation.load_by_settings(self._configuration.get_configuration(TranslationSettings))
|
||||||
|
|
||||||
async def main(self):
|
async def main(self):
|
||||||
Console.write_line('Hello World')
|
try:
|
||||||
|
self._logger.debug(__name__, f'Starting...\n')
|
||||||
|
self._logger.trace(__name__, f'Try to start {DiscordBotService.__name__}')
|
||||||
|
await self._bot.start_async()
|
||||||
|
except Exception as e:
|
||||||
|
self._logger.error(__name__, 'Start failed', e)
|
||||||
|
|
||||||
|
async def stop_async(self):
|
||||||
|
try:
|
||||||
|
self._logger.trace(__name__, f'Try to stop {DiscordBotService.__name__}')
|
||||||
|
await self._bot.close()
|
||||||
|
self._logger.trace(__name__, f'Stopped {DiscordBotService.__name__}')
|
||||||
|
except Exception as e:
|
||||||
|
self._logger.error(__name__, 'stop failed', e)
|
||||||
|
|
||||||
|
Console.write_line()
|
||||||
|
24
src/bot/bot
Normal file
24
src/bot/bot
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
path="`dirname \"$0\"`" # relative
|
||||||
|
path="`( cd \"$path\" && pwd)`" # absolutized and normalized
|
||||||
|
|
||||||
|
cd "$path/../"
|
||||||
|
|
||||||
|
if [[ $1 == "-dev" ]]; then
|
||||||
|
export KDB_ENVIRONMENT=development
|
||||||
|
export KDB_NAME=KDB-dev
|
||||||
|
elif [[ $1 == "-stage" ]]; then
|
||||||
|
export KDB_ENVIRONMENT=staging
|
||||||
|
export KDB_NAME=KDB-test
|
||||||
|
elif [[ $1 == "-prod" ]]; then
|
||||||
|
export KDB_ENVIRONMENT=production
|
||||||
|
export KDB_NAME=KDB
|
||||||
|
else
|
||||||
|
export KDB_ENVIRONMENT=production
|
||||||
|
export KDB_NAME=KDB-prod
|
||||||
|
fi
|
||||||
|
|
||||||
|
export PYTHONPATH=./:$PYTHONPATH
|
||||||
|
|
||||||
|
python3.10 bot/main.py
|
@ -17,9 +17,9 @@
|
|||||||
"LicenseDescription": "MIT, see LICENSE for more details.",
|
"LicenseDescription": "MIT, see LICENSE for more details.",
|
||||||
"Dependencies": [
|
"Dependencies": [
|
||||||
"cpl-core>=2022.7.0.post1",
|
"cpl-core>=2022.7.0.post1",
|
||||||
"cpl-translation==2022.7.0",
|
"cpl-translation==2022.7.0.post1",
|
||||||
"cpl-query==2022.7.0",
|
"cpl-query==2022.7.0",
|
||||||
"cpl-discord==2022.7.0"
|
"cpl-discord==2022.7.0.post1"
|
||||||
],
|
],
|
||||||
"DevDependencies": [
|
"DevDependencies": [
|
||||||
"cpl-cli>=2022.7.0.post1"
|
"cpl-cli>=2022.7.0.post1"
|
||||||
|
33
src/bot/config/appsettings.development.json
Normal file
33
src/bot/config/appsettings.development.json
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
{
|
||||||
|
"TimeFormatSettings": {
|
||||||
|
"DateFormat": "%Y-%m-%d",
|
||||||
|
"TimeFormat": "%H:%M:%S",
|
||||||
|
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
||||||
|
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
||||||
|
},
|
||||||
|
"LoggingSettings": {
|
||||||
|
"Path": "logs/",
|
||||||
|
"Filename": "log_dev.log",
|
||||||
|
"ConsoleLogLevel": "DEBUG",
|
||||||
|
"FileLogLevel": "DEBUG"
|
||||||
|
},
|
||||||
|
"Translation": {
|
||||||
|
"DefaultLanguage": "de",
|
||||||
|
"Languages": [
|
||||||
|
"de"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"DiscordBot": {
|
||||||
|
"Token": "OTA5ODc4NDcyNzExNzU3ODQ1.YZKsXA.8p-g1c37EBZzYYW09Fvr7egQzhE",
|
||||||
|
"Prefix": "!kd "
|
||||||
|
},
|
||||||
|
"Bot": {
|
||||||
|
"Servers": [
|
||||||
|
{
|
||||||
|
"Id": "910199451145076828",
|
||||||
|
"MessageDeleteTimer": 2,
|
||||||
|
"BotHasNoPermissionMessage": "Ich habe keine Berechtigungen :("
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
31
src/bot/config/appsettings.edrafts-lapi.json
Normal file
31
src/bot/config/appsettings.edrafts-lapi.json
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"LoggingSettings": {
|
||||||
|
"Path": "logs/",
|
||||||
|
"Filename": "log_dev.log",
|
||||||
|
"ConsoleLogLevel": "TRACE",
|
||||||
|
"FileLogLevel": "TRACE"
|
||||||
|
},
|
||||||
|
"DatabaseSettings": {
|
||||||
|
"Host": "localhost",
|
||||||
|
"User": "sh_gismo",
|
||||||
|
"Password": "c2hfZ2lzbW8=",
|
||||||
|
"Database": "sh_gismo_dev",
|
||||||
|
"Charset": "utf8mb4",
|
||||||
|
"UseUnicode": "true",
|
||||||
|
"Buffered": "true",
|
||||||
|
"AuthPlugin": "mysql_native_password"
|
||||||
|
},
|
||||||
|
"DiscordBot": {
|
||||||
|
"Token": "OTExNTc0NDQyMzMxNzM0MDI2.YZjX2w.k7N2qTkvkDD7j9bT4Nrdl4qBHlI",
|
||||||
|
"Prefix": "!kde "
|
||||||
|
},
|
||||||
|
"Bot": {
|
||||||
|
"Servers": [
|
||||||
|
{
|
||||||
|
"Id": "910199451145076828",
|
||||||
|
"MessageDeleteTimer": 2,
|
||||||
|
"BotHasNoPermissionMessage": "Ich habe keine Berechtigungen :("
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
18
src/bot/config/appsettings.example.json
Normal file
18
src/bot/config/appsettings.example.json
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"TimeFormatSettings": {
|
||||||
|
"DateFormat": "%Y-%m-%d",
|
||||||
|
"TimeFormat": "%H:%M:%S",
|
||||||
|
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
||||||
|
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
||||||
|
},
|
||||||
|
"LoggingSettings": {
|
||||||
|
"Path": "logs/",
|
||||||
|
"Filename": "log_$start_time.log",
|
||||||
|
"ConsoleLogLevel": "ERROR",
|
||||||
|
"FileLogLevel": "WARN"
|
||||||
|
},
|
||||||
|
"DiscordBot": {
|
||||||
|
"Token": "",
|
||||||
|
"Prefix": "! "
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,6 @@
|
|||||||
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
||||||
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
||||||
},
|
},
|
||||||
|
|
||||||
"LoggingSettings": {
|
"LoggingSettings": {
|
||||||
"Path": "logs/",
|
"Path": "logs/",
|
||||||
"Filename": "log_$start_time.log",
|
"Filename": "log_$start_time.log",
|
24
src/bot/config/appsettings.production.json
Normal file
24
src/bot/config/appsettings.production.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"TimeFormatSettings": {
|
||||||
|
"DateFormat": "%Y-%m-%d",
|
||||||
|
"TimeFormat": "%H:%M:%S",
|
||||||
|
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
||||||
|
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
||||||
|
},
|
||||||
|
"LoggingSettings": {
|
||||||
|
"Path": "logs/",
|
||||||
|
"Filename": "log_$start_time.log",
|
||||||
|
"ConsoleLogLevel": "ERROR",
|
||||||
|
"FileLogLevel": "WARN"
|
||||||
|
},
|
||||||
|
"Translation": {
|
||||||
|
"DefaultLanguage": "de",
|
||||||
|
"Languages": [
|
||||||
|
"de"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"Discord": {
|
||||||
|
"Token": "OTA5ODc3NDg3MjEzODk5ODQ3.YZKrcQ.JLfdP_X0xtpfXKiBqP49HQe6z9Y",
|
||||||
|
"Prefix": "!kb "
|
||||||
|
}
|
||||||
|
}
|
33
src/bot/config/appsettings.staging.json
Normal file
33
src/bot/config/appsettings.staging.json
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
{
|
||||||
|
"TimeFormatSettings": {
|
||||||
|
"DateFormat": "%Y-%m-%d",
|
||||||
|
"TimeFormat": "%H:%M:%S",
|
||||||
|
"DateTimeFormat": "%Y-%m-%d %H:%M:%S.%f",
|
||||||
|
"DateTimeLogFormat": "%Y-%m-%d_%H-%M-%S"
|
||||||
|
},
|
||||||
|
"LoggingSettings": {
|
||||||
|
"Path": "logs/",
|
||||||
|
"Filename": "log_staging_$start_time.log",
|
||||||
|
"ConsoleLogLevel": "INFO",
|
||||||
|
"FileLogLevel": "DEBUG"
|
||||||
|
},
|
||||||
|
"Translation": {
|
||||||
|
"DefaultLanguage": "de",
|
||||||
|
"Languages": [
|
||||||
|
"de"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"Discord": {
|
||||||
|
"Token": "OTEwMjAxNjA1NDkwNjEwMjA3.YZPZTQ.lUj2Bs5pQAv4AJ_tb_mHMTpIRls"
|
||||||
|
},
|
||||||
|
"Bot": {
|
||||||
|
"Prefix": "!kt ",
|
||||||
|
"Servers": [
|
||||||
|
{
|
||||||
|
"Id": "910199451145076828",
|
||||||
|
"MessageDeleteTimer": 2,
|
||||||
|
"BotHasNoPermissionMessage": "Ich habe keine Berechtigungen :("
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from cpl_core.application import ApplicationBuilder
|
from cpl_core.application import ApplicationBuilder
|
||||||
|
|
||||||
@ -6,12 +7,34 @@ from bot.application import Application
|
|||||||
from bot.startup import Startup
|
from bot.startup import Startup
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
class Main:
|
||||||
app_builder = ApplicationBuilder(Application)
|
|
||||||
app_builder.use_startup(Startup)
|
def __init__(self):
|
||||||
app: Application = await app_builder.build_async()
|
self._bot: Optional[Application] = None
|
||||||
await app.run_async()
|
|
||||||
|
async def main(self):
|
||||||
|
app_builder = ApplicationBuilder(Application)
|
||||||
|
app_builder.use_startup(Startup)
|
||||||
|
app: Application = await app_builder.build_async()
|
||||||
|
await app.run_async()
|
||||||
|
|
||||||
|
async def stop(self):
|
||||||
|
await self._bot.stop_async()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
asyncio.run(main())
|
main = Main()
|
||||||
|
try:
|
||||||
|
asyncio.run(main.main())
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
asyncio.run(main.stop())
|
||||||
|
|
||||||
|
# ((
|
||||||
|
# ( `)
|
||||||
|
# ; / ,
|
||||||
|
# / \/
|
||||||
|
# / |
|
||||||
|
# / ~/
|
||||||
|
# / ) ) ~ edraft
|
||||||
|
# ___// | /
|
||||||
|
# `--' \_~-,
|
||||||
|
@ -1,16 +1,40 @@
|
|||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from cpl_core.application import StartupABC
|
from cpl_core.application import StartupABC
|
||||||
from cpl_core.configuration import ConfigurationABC
|
from cpl_core.configuration import ConfigurationABC
|
||||||
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
|
from cpl_core.dependency_injection import ServiceProviderABC, ServiceCollectionABC
|
||||||
from cpl_core.environment import ApplicationEnvironment
|
from cpl_core.environment import ApplicationEnvironment
|
||||||
|
from cpl_discord import get_discord_collection
|
||||||
|
|
||||||
|
|
||||||
class Startup(StartupABC):
|
class Startup(StartupABC):
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
StartupABC.__init__(self)
|
StartupABC.__init__(self)
|
||||||
|
self._start_time = datetime.now()
|
||||||
|
|
||||||
|
self._config: Optional[ConfigurationABC] = None
|
||||||
|
|
||||||
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC:
|
def configure_configuration(self, configuration: ConfigurationABC, environment: ApplicationEnvironment) -> ConfigurationABC:
|
||||||
|
environment.set_working_directory(os.path.dirname(os.path.realpath(__file__)))
|
||||||
|
configuration.add_environment_variables('KDB_')
|
||||||
|
configuration.add_environment_variables('DISCORD_')
|
||||||
|
|
||||||
|
configuration.add_json_file(f'config/appsettings.json', optional=False)
|
||||||
|
configuration.add_json_file(f'config/appsettings.{environment.environment_name}.json', optional=True)
|
||||||
|
configuration.add_json_file(f'config/appsettings.{environment.host_name}.json', optional=True)
|
||||||
|
|
||||||
|
configuration.add_configuration('Startup_StartTime', str(self._start_time))
|
||||||
|
|
||||||
|
self._config = configuration
|
||||||
return configuration
|
return configuration
|
||||||
|
|
||||||
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
|
def configure_services(self, services: ServiceCollectionABC, environment: ApplicationEnvironment) -> ServiceProviderABC:
|
||||||
|
services.add_logging()
|
||||||
|
services.add_translation()
|
||||||
|
services.add_discord()
|
||||||
|
discord_collection = get_discord_collection(services)
|
||||||
|
|
||||||
return services.build_service_provider()
|
return services.build_service_provider()
|
||||||
|
8
src/bot/translation/de.json
Normal file
8
src/bot/translation/de.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"common": {
|
||||||
|
"hello-world": "Hallo Welt"
|
||||||
|
},
|
||||||
|
"commands": {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user