Improved user message per hour check handling #446

This commit is contained in:
Sven Heidemann 2023-12-03 11:59:16 +01:00
parent 0aa690b984
commit 4233e089f8
4 changed files with 43 additions and 27 deletions

View File

@ -48,6 +48,16 @@ class ClientUtilsABC(ABC):
def get_auto_complete_list(self, _l: List, current: str, select: Callable = None) -> List: def get_auto_complete_list(self, _l: List, current: str, select: Callable = None) -> List:
pass pass
@abstractmethod
def update_user_message_xp_count_by_hour(
self,
created_at: datetime,
user: User,
settings: ServerConfig,
is_reaction: bool = False,
):
pass
@abstractmethod @abstractmethod
def is_message_xp_count_by_hour_higher_that_max_message_count_per_hour( def is_message_xp_count_by_hour_higher_that_max_message_count_per_hour(
self, self,

View File

@ -1,5 +1,5 @@
from datetime import datetime from datetime import datetime
from typing import Callable, Union from typing import Callable
import discord import discord
from cpl_core.configuration import ConfigurationABC 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 ( from bot_data.abc.user_message_count_per_hour_repository_abc import (
UserMessageCountPerHourRepositoryABC, UserMessageCountPerHourRepositoryABC,
) )
from bot_data.model.auto_role_rule import AutoRoleRule
from bot_data.model.server_config import ServerConfig from bot_data.model.server_config import ServerConfig
from bot_data.model.user import User from bot_data.model.user import User
from bot_data.model.user_message_count_per_hour import UserMessageCountPerHour from bot_data.model.user_message_count_per_hour import UserMessageCountPerHour
@ -143,14 +142,13 @@ class ClientUtilsService(ClientUtilsABC):
return _l.take(25) 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, self,
created_at: datetime, created_at: datetime,
user: User, user: User,
settings: ServerConfig, settings: ServerConfig,
is_reaction: bool = False, is_reaction: bool = False,
) -> bool: ):
umcph = None
try: try:
umcph = self._umcphs.find_user_message_count_per_hour_by_user_id_and_date(user.id, created_at) umcph = self._umcphs.find_user_message_count_per_hour_by_user_id_and_date(user.id, created_at)
if umcph is None: if umcph is None:
@ -162,44 +160,50 @@ class ClientUtilsService(ClientUtilsABC):
user, user,
) )
) )
self._db.save_changes() self._db.save_changes()
umcph = self._umcphs.get_user_message_count_per_hour_by_user_id_and_date(user.id, created_at) 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._umcphs.update_user_message_count_per_hour(umcph)
self._db.save_changes() self._db.save_changes()
except Exception as e: except Exception as e:
self._logger.error( self._logger.error(
__name__, __name__,
f"Cannot update user message count per hour with id {umcph.id}", f"Cannot update user message count per hour {created_at}",
e, e,
) )
return False return False
if umcph.xp_count is None: 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 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: def get_ontime_for_user(self, user: User) -> float:
return round( return round(
self._user_joined_voice_channel.get_user_joined_voice_channels_by_user_id(user.id) sum(
.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), (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, 2,
) )

View File

@ -67,6 +67,7 @@ class BaseOnMessageEvent(OnMessageABC):
return return
settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{server.discord_id}") 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( if self._client_utils.is_message_xp_count_by_hour_higher_that_max_message_count_per_hour(
message.created_at, user, settings message.created_at, user, settings
): ):

View File

@ -73,6 +73,7 @@ class BaseReactionHandler:
settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{guild.id}") settings: ServerConfig = self._config.get_configuration(f"ServerConfig_{guild.id}")
if r_type == "add": 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( if self._client_utils.is_message_xp_count_by_hour_higher_that_max_message_count_per_hour(
datetime.now(), user, settings, is_reaction=True datetime.now(), user, settings, is_reaction=True
): ):