From e34fbb60aa08e3bc29d41372fba3ed81fd4639f7 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Dec 2021 07:36:54 +0100 Subject: [PATCH] Added logic to append received messages --- src/gismo_data/abc/client_repository_abc.py | 3 ++ src/gismo_data/model/client.py | 29 +++++++++++++++++-- .../service/client_repository_service.py | 18 ++++++++++-- .../user_joined_server_repository_service.py | 2 +- .../service/user_repository_service.py | 5 ++-- src/modules/base/base.py | 28 ++++++++++++++++-- 6 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/gismo_data/abc/client_repository_abc.py b/src/gismo_data/abc/client_repository_abc.py index 2c84839..53e0c31 100644 --- a/src/gismo_data/abc/client_repository_abc.py +++ b/src/gismo_data/abc/client_repository_abc.py @@ -36,3 +36,6 @@ class ClientRepositoryABC(ABC): @abstractmethod def delete_client(self, client: Client): pass + + @abstractmethod + def apppend_received_message_count(self, id: int, server_id: int, value: int): pass diff --git a/src/gismo_data/model/client.py b/src/gismo_data/model/client.py index dd918a6..0382cfc 100644 --- a/src/gismo_data/model/client.py +++ b/src/gismo_data/model/client.py @@ -43,22 +43,47 @@ class Client(TableABC): def sent_message_count(self) -> int: return self._sent_message_count + @sent_message_count.setter + def sent_message_count(self, value: int): + self._modified_at = datetime.now().isoformat() + self._sent_message_count = value + @property def received_message_count(self) -> int: return self._received_message_count + @received_message_count.setter + def received_message_count(self, value: int): + self._modified_at = datetime.now().isoformat() + self._received_message_count = value + @property def deleted_message_count(self) -> int: return self._deleted_message_count + @deleted_message_count.setter + def deleted_message_count(self, value: int): + self._modified_at = datetime.now().isoformat() + self._deleted_message_count = value + @property def received_command_count(self) -> int: return self._received_command_count + @received_command_count.setter + def received_command_count(self, value: int): + self._modified_at = datetime.now().isoformat() + self._received_command_count = value + @property def moved_users_count(self) -> int: return self._moved_users_count + @moved_users_count.setter + def moved_users_count(self, value: int): + self._modified_at = datetime.now().isoformat() + self._moved_users_count = value + @property def server(self) -> Server: return self._server @@ -134,12 +159,12 @@ class Client(TableABC): `ReceivedCommandsCount` = {self._received_command_count}, `MovedUsersCount` = {self._moved_users_count}, `LastModifiedAt` = '{self._modified_at}' - WHERE `Id` = {self._client_id}; + WHERE `ClientId` = {self._client_id}; """) @property def delete_string(self) -> str: return str(f""" DELETE FROM `Clients` - WHERE `Id` = {self._client_id}; + WHERE `ClientId` = {self._client_id}; """) diff --git a/src/gismo_data/service/client_repository_service.py b/src/gismo_data/service/client_repository_service.py index 1397ec9..9d297c6 100644 --- a/src/gismo_data/service/client_repository_service.py +++ b/src/gismo_data/service/client_repository_service.py @@ -104,8 +104,8 @@ class ClientRepositoryService(ClientRepositoryABC): ) def find_client_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> Optional[Client]: - self._logger.trace(__name__, f'Send SQL command: {Client.get_select_by_server_id_string(discord_id)}') - result = self._context.select(Client.get_select_by_server_id_string(discord_id)) + self._logger.trace(__name__, f'Send SQL command: {Client.get_select_by_discord_id_and_server_id_string(discord_id, server_id)}') + result = self._context.select(Client.get_select_by_discord_id_and_server_id_string(discord_id, server_id)) if result is None or len(result) == 0: return None @@ -133,3 +133,17 @@ class ClientRepositoryService(ClientRepositoryABC): def delete_client(self, client: Client): self._logger.trace(__name__, f'Send SQL command: {client.delete_string}') self._context.cursor.execute(client.delete_string) + + def apppend_received_message_count(self, id: int, server_id: int, value: int): + server = self._servers.find_server_by_discord_id(server_id) + if server is None: + self._logger.warn(__name__, f'Cannot find server by id {server_id}') + raise Exception('Value not found') + + client = self.find_client_by_discord_id_and_server_id(id, server.server_id) + if client is None: + self._logger.warn(__name__, f'Cannot find client by ids {id}@{server.server_id}') + raise Exception('Value not found') + + client.received_message_count += 1 + self.update_client(client) diff --git a/src/gismo_data/service/user_joined_server_repository_service.py b/src/gismo_data/service/user_joined_server_repository_service.py index 786ef57..91fcbed 100644 --- a/src/gismo_data/service/user_joined_server_repository_service.py +++ b/src/gismo_data/service/user_joined_server_repository_service.py @@ -86,7 +86,7 @@ class UserJoinedServerRepositoryService(UserJoinedServerRepositoryABC): result = self._context.select(UserJoinedServer.get_select_active_by_user_id_string(user_id)) if result is None or len(result) == 0: return None - + result = result[0] return UserJoinedServer( diff --git a/src/gismo_data/service/user_repository_service.py b/src/gismo_data/service/user_repository_service.py index 2644dfc..ba857be 100644 --- a/src/gismo_data/service/user_repository_service.py +++ b/src/gismo_data/service/user_repository_service.py @@ -35,7 +35,8 @@ class UserRepositoryService(UserRepositoryABC): def get_user_by_id(self, id: int) -> User: self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_id_string(id)}') - result = self._context.select(User.get_select_by_id_string(id)) + result = self._context.select(User.get_select_by_id_string(id))[0] + return User( result[1], result[2], @@ -47,8 +48,6 @@ class UserRepositoryService(UserRepositoryABC): self._logger.trace(__name__, f'Send SQL command: {User.get_select_by_discord_id_string(discord_id)}') result = self._context.select(User.get_select_by_discord_id_string(discord_id))[0] - result = result[0] - return User( result[1], result[2], diff --git a/src/modules/base/base.py b/src/modules/base/base.py index 530b3cd..79a4589 100644 --- a/src/modules/base/base.py +++ b/src/modules/base/base.py @@ -1,17 +1,33 @@ import discord +from cpl_core.database.context import DatabaseContextABC from cpl_core.logging.logger_abc import LoggerABC +from gismo_core.abc.bot_service_abc import BotServiceABC +from gismo_data.abc.client_repository_abc import ClientRepositoryABC +from gismo_data.abc.server_repository_abc import ServerRepositoryABC from modules_core.abc.events.on_member_join_abc import OnMemberJoinABC from modules_core.abc.events.on_message_abc import OnMessageABC -from modules_core.abc.events.on_voice_state_update_abc import OnVoiceStateUpdateABC +from modules_core.abc.events.on_voice_state_update_abc import \ + OnVoiceStateUpdateABC from modules_core.abc.module_abc import ModuleABC class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC): - def __init__(self, logger: LoggerABC): + def __init__( + self, + logger: LoggerABC, + clients: ClientRepositoryABC, + servers: ServerRepositoryABC, + bot: BotServiceABC, + db: DatabaseContextABC + ): self._logger = logger + self._clients = clients + self._servers = servers + self._bot = bot + self._db = db ModuleABC.__init__(self) self._priorities[OnMemberJoinABC] = 1 @@ -19,10 +35,18 @@ class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC): self._priorities[OnVoiceStateUpdateABC] = 10 self._logger.trace(__name__, f'Module {type(self)} loaded') + def _apppend_received_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') async def on_message(self, message: discord.Message): + self._apppend_received_message_count(message.guild.id) self._logger.debug(__name__, f'Module {type(self)} started') async def on_voice_state_update(self, member: discord.Member, before: discord.VoiceState, after: discord.VoiceState):