Improved message service & added logic to send login message

This commit is contained in:
Sven Heidemann 2021-11-16 20:37:51 +01:00
parent c254114987
commit 5e1caf713c
4 changed files with 57 additions and 17 deletions

View File

@ -22,10 +22,14 @@
"Servers": [
{
"Id": "511824600884051979",
"LoginMessageChannelId": "521260270757347328",
"LoginMessage": "Ich bin on the line :D\nDer Start hat {} Sekunden gedauert",
"MessageDeleteTimer": 2
},
{
"Id": "910199451145076828",
"LoginMessageChannelId": "910199452915093588",
"LoginMessage": "Ich bin on the line :D\nDer Start hat {} Sekunden gedauert",
"MessageDeleteTimer": 2
}
]

View File

@ -11,6 +11,8 @@ class ServerSettings(ConfigurationModelABC):
self._id: int = 0
self._message_delete_timer: int = 0
self._login_message_channel_id: int = 0
self._login_message: str = ''
@property
def id(self) -> str:
@ -20,10 +22,20 @@ class ServerSettings(ConfigurationModelABC):
def message_delete_timer(self) -> int:
return self._message_delete_timer
@property
def login_message_channel_id(self) -> int:
return self._login_message_channel_id
@property
def login_message(self) -> str:
return self._login_message
def from_dict(self, settings: dict):
try:
self._id = int(settings['Id'])
self._message_delete_timer = int(settings['MessageDeleteTimer'])
self._login_message_channel_id = int(settings['LoginMessageChannelId'])
self._login_message = settings['LoginMessage']
except Exception as e:
Console.error(f'[ ERROR ] [ {__name__} ]: Reading error in settings')
Console.error(f'[ EXCEPTION ] [ {__name__} ]: {e} -> {traceback.format_exc()}')

View File

@ -19,14 +19,14 @@ class MessageService(MessageServiceABC):
self._logger = logger
self._bot = bot
async def delete_message(self, messages: List[discord.Message]):
async def delete_messages(self, messages: List[discord.Message]):
self._logger.debug(__name__, f'Try to delete {messages.count()} messages')
for message in messages:
await self.delete_message(message)
self._logger.debug(__name__, 'Deleting messages finished')
async def delete_message(self, message: discord.Message):
server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{message.g.id}')
server_st: ServerSettings = self._config.get_configuration(f'DSERVER_{message.guild.id}')
await asyncio.sleep(server_st.message_delete_timer)
self._logger.debug(__name__, f'Try to delete message: {message.content}')
try:
@ -34,7 +34,7 @@ class MessageService(MessageServiceABC):
except Exception as e:
self._logger.error(__name__, f'Deleting message failed', e)
else:
self._logger.debug(__name__, f'Deleted message {message.content}')
self._logger.info(__name__, f'Deleted message {message.content}')
async def send_channel_message(self, channel: discord.TextChannel, message: str):
self._logger.debug(__name__, f'Try to send message {message} to channel {channel.id}')
@ -43,7 +43,7 @@ class MessageService(MessageServiceABC):
except Exception as e:
self._logger.error(__name__, f'Send message to channel {channel.id} failed', e)
else:
self._logger.debug(__name__, f'Send message to channel {channel.id}')
self._logger.info(__name__, f'Sent message to channel {channel.id}')
async def send_dm_message(self, message: str, receiver: Union[discord.User, discord.Member]):
self._logger.debug(__name__, f'Try to send message {message} to user {receiver.id}')
@ -52,7 +52,7 @@ class MessageService(MessageServiceABC):
except Exception as e:
self._logger.error(__name__, f'Send message to user {receiver.id} failed', e)
else:
self._logger.debug(__name__, f'Send message to user {receiver.id}')
self._logger.info(__name__, f'Sent message to user {receiver.id}')
async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.File]):
if ctx is None:
@ -69,4 +69,4 @@ class MessageService(MessageServiceABC):
except Exception as e:
self._logger.error(__name__, f'Send message to channel {ctx.channel.id} failed', e)
else:
self._logger.debug(__name__, f'Send message to channel {ctx.channel.id}')
self._logger.info(__name__, f'Sent message to channel {ctx.channel.id}')

View File

@ -4,17 +4,30 @@ from cpl_core.configuration import ConfigurationABC
from cpl_core.console import Console
from cpl_core.logging import LoggerABC, LoggingLevelEnum, LoggingSettings
from discord import guild
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.configuration.server_settings import ServerSettings
from modules_core.abc.module_abc import ModuleABC
class BootLog(ModuleABC):
def __init__(self, config: ConfigurationABC, logging_st: LoggingSettings, logger: LoggerABC, bot: BotServiceABC):
def __init__(
self,
config: ConfigurationABC,
logging_st: LoggingSettings,
logger: LoggerABC,
bot: BotServiceABC,
message_service: MessageServiceABC
):
self._config = config
self._logging_st = logging_st
self._logger = logger
self._bot = bot
self._message_service = message_service
ModuleABC.__init__(self)
self._logger.trace(__name__, f'Module {type(self)} loaded')
@ -27,11 +40,13 @@ class BootLog(ModuleABC):
self._logger.debug(__name__, f'Bot Init time: {init_time}s')
# print warning if initialisation took too long
if init_time >= 30:
self._logger.warn(__name__, 'It takes long time to start the bot!')
self._logger.warn(
__name__, 'It takes long time to start the bot!')
# print error if initialisation took way too long
elif init_time >= 90:
self._logger.error(__name__, 'It takes very long time to start the bot!!!')
self._logger.error(
__name__, 'It takes very long time to start the bot!!!')
except Exception as e:
self._logger.error(__name__, 'Init time calculation failed', e)
return
@ -44,9 +59,18 @@ class BootLog(ModuleABC):
g: guild = g
self._logger.debug(__name__, f'Server detected: {g.id}')
server_config = self._config.get_configuration(f'DSERVER_{g.id}')
if server_config is None:
self._logger.error(__name__, f'Config for server {g.id} not found!')
server_settings: ServerSettings = self._config.get_configuration(
f'DSERVER_{g.id}')
if server_settings is None:
self._logger.error(
__name__, f'Config for server {g.id} not found!')
await self._bot.close()
return
await self._message_service.send_channel_message(
self._bot.get_channel(
server_settings.login_message_channel_id),
server_settings.login_message.format(init_time)
)
self._logger.trace(__name__, f'Module {type(self)} stopped')