diff --git a/src/modules/base/events/base_on_command_event.py b/src/modules/base/events/base_on_command_event.py index bbe548e7..e1c46a20 100644 --- a/src/modules/base/events/base_on_command_event.py +++ b/src/modules/base/events/base_on_command_event.py @@ -1,3 +1,7 @@ +from typing import Optional + +import discord +from cpl_core.database.context import DatabaseContextABC from cpl_core.time import TimeFormatSettings from cpl_discord.events import OnCommandABC from cpl_discord.service import DiscordBotServiceABC @@ -7,6 +11,12 @@ from discord.ext.commands import Context from bot_core.abc.message_service_abc import MessageServiceABC from bot_core.configuration.bot_settings import BotSettings from bot_core.logging.command_logger import CommandLogger +from bot_data.abc.client_repository_abc import ClientRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.user import User +from modules.base.abc.base_helper_abc import BaseHelperABC +from modules.base.configuration.base_server_settings import BaseServerSettings class BaseOnCommandEvent(OnCommandABC): @@ -18,7 +28,12 @@ class BaseOnCommandEvent(OnCommandABC): messenger: MessageServiceABC, bot_settings: BotSettings, time_format_settings: TimeFormatSettings, - translate: TranslatePipe + translate: TranslatePipe, + bhs: BaseHelperABC, + db: DatabaseContextABC, + users: UserRepositoryABC, + clients: ClientRepositoryABC, + servers: ServerRepositoryABC, ): OnCommandABC.__init__(self) self._logger = logger @@ -27,7 +42,49 @@ class BaseOnCommandEvent(OnCommandABC): self._bot_settings = bot_settings self._time_format_settings = time_format_settings self._t = translate + self._base_helper = bhs + self._db = db + self._users = users + self._clients = clients + self._servers = servers + + def _append_received_message_count(self, g_id: int): + try: + self._clients.append_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}', e) + + def _handle_message_for_xp(self, message: discord.Message): + dc_user_id = message.author.id + try: + server = self._servers.get_server_by_discord_id(message.guild.id) + except Exception as e: + self._logger.error(__name__, f'Cannot get server {message.guild.id}', e) + return + + user: Optional[User] = None + try: + user = self._users.get_user_by_discord_id_and_server_id(dc_user_id, server.server_id) + except Exception as e: + self._logger.error(__name__, f'Cannot get user {dc_user_id}', e) + return + + if user is None: + self._logger.error(__name__, f'User not found {dc_user_id}') + return + + settings: BaseServerSettings = self._base_helper.get_config(message.guild.id) + old_xp = user.xp + user.xp += settings.xp_per_message + self._users.update_user(user) + self._db.save_changes() + + self._logger.debug(__name__, f'User {user} sent message. xp: from {old_xp} to {user.xp}') async def on_command(self, ctx: Context): - print('\n', ctx.command) - pass + self._logger.debug(__name__, f'Module {type(self)} started') + self._logger.info(__name__, f'Received command: {ctx.command} from {ctx.channel}') + if ctx is None or ctx.guild is None: + return + self._append_received_message_count(ctx.guild.id) diff --git a/src/modules/base/events/base_on_message_event.py b/src/modules/base/events/base_on_message_event.py index f5288c50..ee8fcdbc 100644 --- a/src/modules/base/events/base_on_message_event.py +++ b/src/modules/base/events/base_on_message_event.py @@ -72,7 +72,7 @@ class BaseOnMessageEvent(OnMessageABC): async def on_message(self, message: discord.Message): self._logger.debug(__name__, f'Module {type(self)} started') - self._logger.debug(__name__, f'Received message: {LogMessageHelper.get_log_string(message)}') + self._logger.info(__name__, f'Received message: {LogMessageHelper.get_log_string(message)}') if message is None or message.guild is None: return self._append_received_message_count(message.guild.id)