Put welcome and goodbye messages into config

This commit is contained in:
Sven Heidemann 2021-12-08 18:34:32 +01:00
parent 199c269804
commit 49aa2f3aa3
3 changed files with 22 additions and 5 deletions

View File

@ -13,6 +13,8 @@ class ServerSettings(ConfigurationModelABC):
self._message_delete_timer: int = 0 self._message_delete_timer: int = 0
self._login_message_channel_id: int = 0 self._login_message_channel_id: int = 0
self._login_message: str = '' self._login_message: str = ''
self._welcome_message: str = ''
self._goodbye_message: str = ''
@property @property
def id(self) -> str: def id(self) -> str:
@ -30,12 +32,22 @@ class ServerSettings(ConfigurationModelABC):
def login_message(self) -> str: def login_message(self) -> str:
return self._login_message return self._login_message
@property
def welcome_message(self) -> str:
return self._welcome_message
@property
def goodbye_message(self) -> str:
return self._goodbye_message
def from_dict(self, settings: dict): def from_dict(self, settings: dict):
try: try:
self._id = int(settings['Id']) self._id = int(settings['Id'])
self._message_delete_timer = int(settings['MessageDeleteTimer']) self._message_delete_timer = int(settings['MessageDeleteTimer'])
self._login_message_channel_id = int(settings['LoginMessageChannelId']) self._login_message_channel_id = int(settings['LoginMessageChannelId'])
self._login_message = settings['LoginMessage'] self._login_message = settings['LoginMessage']
self._welcome_message = settings['WelcomeMessage']
self._goodbye_message = settings['GoodbyeMessage']
except Exception as e: except Exception as e:
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in settings') Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in settings')
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}') Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')

View File

@ -2,11 +2,13 @@ from datetime import datetime
from typing import Union from typing import Union
import discord import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging.logger_abc import LoggerABC from cpl_core.logging.logger_abc import LoggerABC
from gismo_core.abc.bot_service_abc import BotServiceABC from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.message_service_abc import MessageServiceABC from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.configuration.server_settings import ServerSettings
from gismo_data.abc.client_repository_abc import ClientRepositoryABC from gismo_data.abc.client_repository_abc import ClientRepositoryABC
from gismo_data.abc.known_user_repository_abc import KnownUserRepositoryABC from gismo_data.abc.known_user_repository_abc import KnownUserRepositoryABC
from gismo_data.abc.server_repository_abc import ServerRepositoryABC from gismo_data.abc.server_repository_abc import ServerRepositoryABC
@ -28,6 +30,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
def __init__( def __init__(
self, self,
config: ConfigurationABC,
logger: LoggerABC, logger: LoggerABC,
clients: ClientRepositoryABC, clients: ClientRepositoryABC,
servers: ServerRepositoryABC, servers: ServerRepositoryABC,
@ -38,7 +41,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
db: DatabaseContextABC, db: DatabaseContextABC,
messenger: MessageServiceABC messenger: MessageServiceABC
): ):
self._config = config
self._logger = logger self._logger = logger
self._clients = clients self._clients = clients
self._servers = servers self._servers = servers
@ -85,8 +88,8 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
async def _add_if_not_exists_user(self, member: Union[discord.User, discord.Member]): async def _add_if_not_exists_user(self, member: Union[discord.User, discord.Member]):
self._logger.debug(__name__, f'Check if user exists {member}') self._logger.debug(__name__, f'Check if user exists {member}')
# todo content to config settings: ServerSettings = self._config.get_configuration(f'DSERVER_{member.guild.id}')
await self._messenger.send_dm_message(f'Welcome to {member.guild.name}', member) await self._messenger.send_dm_message(settings.welcome_message.format(member.guild.name), member)
try: try:
server = self._servers.get_server_by_discord_id(member.guild.id) server = self._servers.get_server_by_discord_id(member.guild.id)
@ -107,8 +110,8 @@ class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceS
async def _remove_user(self, member: Union[discord.User, discord.Member]): async def _remove_user(self, member: Union[discord.User, discord.Member]):
self._logger.debug(__name__, f'Remove user {member}') self._logger.debug(__name__, f'Remove user {member}')
# todo content to config settings: ServerSettings = self._config.get_configuration(f'DSERVER_{member.guild.id}')
await self._messenger.send_dm_message(f'Bye bye', member) await self._messenger.send_dm_message(settings.goodbye_message, member)
try: try:
server = self._servers.get_server_by_discord_id(member.guild.id) server = self._servers.get_server_by_discord_id(member.guild.id)

View File

@ -222,6 +222,8 @@ class Database(ModuleABC, OnReadyABC):
results = self._users.get_users() results = self._users.get_users()
if results is None or len(results) == 0: if results is None or len(results) == 0:
self._logger.error(__name__, f'Table Users is empty!') self._logger.error(__name__, f'Table Users is empty!')
# todo check for users that arent on the server anymore
async def on_ready(self): async def on_ready(self):
self._logger.debug(__name__, f'Module {type(self)} started') self._logger.debug(__name__, f'Module {type(self)} started')