From 4233e089f8ce5ee8aeb27d7001517cb74e3989d0 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 3 Dec 2023 11:59:16 +0100 Subject: [PATCH] Improved user message per hour check handling #446 --- bot/src/bot_core/abc/client_utils_abc.py | 10 ++++ .../bot_core/service/client_utils_service.py | 58 ++++++++++--------- .../base/events/base_on_message_event.py | 1 + .../base/helper/base_reaction_handler.py | 1 + 4 files changed, 43 insertions(+), 27 deletions(-) diff --git a/bot/src/bot_core/abc/client_utils_abc.py b/bot/src/bot_core/abc/client_utils_abc.py index 66e25500..4caefc2d 100644 --- a/bot/src/bot_core/abc/client_utils_abc.py +++ b/bot/src/bot_core/abc/client_utils_abc.py @@ -48,6 +48,16 @@ class ClientUtilsABC(ABC): def get_auto_complete_list(self, _l: List, current: str, select: Callable = None) -> List: pass + @abstractmethod + def update_user_message_xp_count_by_hour( + self, + created_at: datetime, + user: User, + settings: ServerConfig, + is_reaction: bool = False, + ): + pass + @abstractmethod def is_message_xp_count_by_hour_higher_that_max_message_count_per_hour( self, diff --git a/bot/src/bot_core/service/client_utils_service.py b/bot/src/bot_core/service/client_utils_service.py index e316bf03..86bc1f93 100644 --- a/bot/src/bot_core/service/client_utils_service.py +++ b/bot/src/bot_core/service/client_utils_service.py @@ -1,5 +1,5 @@ from datetime import datetime -from typing import Callable, Union +from typing import Callable import discord from cpl_core.configuration import ConfigurationABC @@ -25,7 +25,6 @@ from bot_data.abc.user_joined_voice_channel_repository_abc import ( from bot_data.abc.user_message_count_per_hour_repository_abc import ( UserMessageCountPerHourRepositoryABC, ) -from bot_data.model.auto_role_rule import AutoRoleRule from bot_data.model.server_config import ServerConfig from bot_data.model.user import User from bot_data.model.user_message_count_per_hour import UserMessageCountPerHour @@ -143,14 +142,13 @@ class ClientUtilsService(ClientUtilsABC): return _l.take(25) - def is_message_xp_count_by_hour_higher_that_max_message_count_per_hour( + def update_user_message_xp_count_by_hour( self, created_at: datetime, user: User, settings: ServerConfig, is_reaction: bool = False, - ) -> bool: - umcph = None + ): try: umcph = self._umcphs.find_user_message_count_per_hour_by_user_id_and_date(user.id, created_at) if umcph is None: @@ -162,44 +160,50 @@ class ClientUtilsService(ClientUtilsABC): user, ) ) - self._db.save_changes() - umcph = self._umcphs.get_user_message_count_per_hour_by_user_id_and_date(user.id, created_at) - except Exception as e: - self._logger.error( - __name__, - f"Cannot add user message count per hour with id {umcph.id}", - e, - ) - return False - - try: - if is_reaction: - umcph.xp_count += settings.xp_per_reaction - else: - umcph.xp_count += settings.xp_per_message + umcph.xp_count += settings.xp_per_reaction if is_reaction else settings.xp_per_message self._umcphs.update_user_message_count_per_hour(umcph) self._db.save_changes() except Exception as e: self._logger.error( __name__, - f"Cannot update user message count per hour with id {umcph.id}", + f"Cannot update user message count per hour {created_at}", e, ) return False - if umcph.xp_count is None: - return False + def is_message_xp_count_by_hour_higher_that_max_message_count_per_hour( + self, + created_at: datetime, + user: User, + settings: ServerConfig, + is_reaction: bool = False, + ) -> bool: + try: + umcph = self._umcphs.find_user_message_count_per_hour_by_user_id_and_date(user.id, created_at) + if umcph is None or umcph.xp_count is None: + return False - return umcph.xp_count > settings.max_message_xp_per_hour + return umcph.xp_count > settings.max_message_xp_per_hour + except Exception as e: + self._logger.error( + __name__, + f"Cannot add user message count per hour with", + e, + ) + return False def get_ontime_for_user(self, user: User) -> float: return round( - self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.id) - .where(lambda x: x.leaved_on is not None and x.joined_on is not None) - .sum(lambda join: (join.leaved_on - join.joined_on).total_seconds() / 3600), + sum( + [ + (join.leaved_on - join.joined_on).total_seconds() / 3600 + for join in self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.id) + if join.leaved_on is not None and join.joined_on is not None + ] + ), 2, ) diff --git a/bot/src/modules/base/events/base_on_message_event.py b/bot/src/modules/base/events/base_on_message_event.py index 18ba2bb3..beb9c4a6 100644 --- a/bot/src/modules/base/events/base_on_message_event.py +++ b/bot/src/modules/base/events/base_on_message_event.py @@ -67,6 +67,7 @@ class BaseOnMessageEvent(OnMessageABC): return settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{server.discord_id}") + self._client_utils.update_user_message_xp_count_by_hour(message.created_at, user, settings) if self._client_utils.is_message_xp_count_by_hour_higher_that_max_message_count_per_hour( message.created_at, user, settings ): diff --git a/bot/src/modules/base/helper/base_reaction_handler.py b/bot/src/modules/base/helper/base_reaction_handler.py index d2cb1581..f0e8d104 100644 --- a/bot/src/modules/base/helper/base_reaction_handler.py +++ b/bot/src/modules/base/helper/base_reaction_handler.py @@ -73,6 +73,7 @@ class BaseReactionHandler: settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{guild.id}") if r_type == "add": + self._client_utils.update_user_message_xp_count_by_hour(datetime.now(), user, settings, is_reaction=True) if self._client_utils.is_message_xp_count_by_hour_higher_that_max_message_count_per_hour( datetime.now(), user, settings, is_reaction=True ):