From 68c1bc0bc3b8fbc15b9b66ff42b6522f5425869c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Dec 2021 07:48:55 +0100 Subject: [PATCH] Added logic to save client data --- src/gismo_core/service/message_service.py | 15 ++++++++++++++- src/modules/base/base.py | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/gismo_core/service/message_service.py b/src/gismo_core/service/message_service.py index 32ae1c3..a4156d3 100644 --- a/src/gismo_core/service/message_service.py +++ b/src/gismo_core/service/message_service.py @@ -3,6 +3,7 @@ from typing import Union import discord from cpl_core.configuration.configuration_abc import ConfigurationABC +from cpl_core.database.context.database_context_abc import DatabaseContextABC from cpl_core.logging import LoggerABC from cpl_query.extension import List from discord.ext.commands import Context @@ -10,14 +11,17 @@ from discord.ext.commands import Context 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 gismo_data.abc.client_repository_abc import ClientRepositoryABC class MessageService(MessageServiceABC): - def __init__(self, config: ConfigurationABC, logger: LoggerABC, bot: BotServiceABC): + def __init__(self, config: ConfigurationABC, logger: LoggerABC, bot: BotServiceABC, clients: ClientRepositoryABC, db: DatabaseContextABC): self._config = config self._logger = logger self._bot = bot + self._clients = clients + self._db = db async def delete_messages(self, messages: List[discord.Message]): self._logger.debug(__name__, f'Try to delete {messages.count()} messages') @@ -29,11 +33,14 @@ class MessageService(MessageServiceABC): 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:\n\t{message}\n\t{message.content}') + guild_id = message.guild.id try: await message.delete() except Exception as e: self._logger.error(__name__, f'Deleting message failed', e) else: + self._clients.apppend_deleted_message_count(self._bot.user.id, guild_id) + self._db.save_changes() self._logger.info(__name__, f'Deleted message {message}') async def send_channel_message(self, channel: discord.TextChannel, message: str): @@ -45,6 +52,8 @@ class MessageService(MessageServiceABC): self._logger.error(__name__, f'Send message to channel {channel.id} failed', e) else: self._logger.info(__name__, f'Sent message to channel {channel.id}') + self._clients.apppend_sent_message_count(self._bot.user.id, channel.guild.id) + self._db.save_changes() await self.delete_message(msg) async def send_dm_message(self, message: str, receiver: Union[discord.User, discord.Member]): @@ -54,6 +63,8 @@ class MessageService(MessageServiceABC): except Exception as e: self._logger.error(__name__, f'Send message to user {receiver.id} failed', e) else: + self._clients.apppend_sent_message_count(self._bot.user.id, receiver.guild.id) + self._db.save_changes() self._logger.info(__name__, f'Sent message to user {receiver.id}') async def send_ctx_msg(self, ctx: Context, message: Union[str, discord.File]): @@ -73,4 +84,6 @@ class MessageService(MessageServiceABC): self._logger.error(__name__, f'Send message to channel {ctx.channel.id} failed', e) else: self._logger.info(__name__, f'Sent message to channel {ctx.channel.id}') + self._clients.apppend_sent_message_count(self._bot.user.id, ctx.guild.id) + self._db.save_changes() await self.delete_message(msg) diff --git a/src/modules/base/base.py b/src/modules/base/base.py index 79a4589..0e315ee 100644 --- a/src/modules/base/base.py +++ b/src/modules/base/base.py @@ -42,6 +42,13 @@ class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC): except Exception as e: self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}') + def _apppend_deleted_message_count(self, g_id: int): + try: + self._clients.apppend_received_message_count(self._bot.user.id, g_id, 1) + self._db.save_changes() + except Exception as e: + self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}') + async def on_member_join(self, member: discord.Member): self._logger.debug(__name__, f'Module {type(self)} started')