Added logic to append received messages

This commit is contained in:
Sven Heidemann 2021-12-08 07:36:54 +01:00
parent f03ee54703
commit e34fbb60aa
6 changed files with 75 additions and 10 deletions

View File

@ -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

View File

@ -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};
""")

View File

@ -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)

View File

@ -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],

View File

@ -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):