From 199c269804894067814930334cfe0f2772e7229f Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Dec 2021 18:26:51 +0100 Subject: [PATCH] Added logic to handle on_member_join & remove --- src/gismo_data/model/user_joined_server.py | 12 +++- src/modules/base/base.py | 77 ++++++++++++++++++++-- 2 files changed, 81 insertions(+), 8 deletions(-) diff --git a/src/gismo_data/model/user_joined_server.py b/src/gismo_data/model/user_joined_server.py index be73141..7ec4806 100644 --- a/src/gismo_data/model/user_joined_server.py +++ b/src/gismo_data/model/user_joined_server.py @@ -31,9 +31,19 @@ class UserJoinedServer(TableABC): def joined_on(self) -> datetime: return self._joined_on + @joined_on.setter + def joined_on(self, value: datetime): + self._modified_at = datetime.now() + self.joined_on = value + @property def leaved_on(self) -> datetime: return self._leaved_on + + @leaved_on.setter + def leaved_on(self, value: datetime): + self._modified_at = datetime.now() + self._leaved_on = value @staticmethod def get_select_all_string() -> str: @@ -93,7 +103,7 @@ class UserJoinedServer(TableABC): def udpate_string(self) -> str: return str(f""" UPDATE `UserJoinedServers` - SET `LeavedOn` = {self._leaved_on}, + SET `LeavedOn` = '{self._leaved_on}', `LastModifiedAt` = '{self._modified_at}' WHERE `UserId` = {self._user.user_id}; """) diff --git a/src/modules/base/base.py b/src/modules/base/base.py index c05a1df..5522cf2 100644 --- a/src/modules/base/base.py +++ b/src/modules/base/base.py @@ -1,3 +1,4 @@ +from datetime import datetime from typing import Union import discord @@ -5,18 +6,25 @@ 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_core.abc.message_service_abc import MessageServiceABC from gismo_data.abc.client_repository_abc import ClientRepositoryABC from gismo_data.abc.known_user_repository_abc import KnownUserRepositoryABC from gismo_data.abc.server_repository_abc import ServerRepositoryABC +from gismo_data.abc.user_joined_server_repository_abc import \ + UserJoinedServerRepositoryABC +from gismo_data.abc.user_repository_abc import UserRepositoryABC from gismo_data.model.known_user import KnownUser +from gismo_data.model.user import User +from gismo_data.model.user_joined_server import UserJoinedServer from modules_core.abc.events.on_member_join_abc import OnMemberJoinABC +from modules_core.abc.events.on_member_remove_abc import OnMemberRemoveABC 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.module_abc import ModuleABC -class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC): +class Base(ModuleABC, OnMemberJoinABC, OnMemberRemoveABC, OnMessageABC, OnVoiceStateUpdateABC): def __init__( self, @@ -24,19 +32,26 @@ class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC): clients: ClientRepositoryABC, servers: ServerRepositoryABC, known_users: KnownUserRepositoryABC, + users: UserRepositoryABC, + user_joins: UserJoinedServerRepositoryABC, bot: BotServiceABC, - db: DatabaseContextABC + db: DatabaseContextABC, + messenger: MessageServiceABC ): self._logger = logger self._clients = clients self._servers = servers self._known_users = known_users + self._users = users + self._user_joins = user_joins self._bot = bot self._db = db + self._messenger = messenger ModuleABC.__init__(self) self._priorities[OnMemberJoinABC] = 1 + self._priorities[OnMemberRemoveABC] = 1 self._priorities[OnMessageABC] = 30 self._priorities[OnVoiceStateUpdateABC] = 10 self._logger.trace(__name__, f'Module {type(self)} loaded') @@ -66,18 +81,66 @@ class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC): self._known_users.add_user(KnownUser(member.id)) self._db.save_changes() except Exception as e: - self._logger.error(__name__, f'Cannot get user {member.id}') + self._logger.error(__name__, f'Cannot get user {member.id}', e) + + async def _add_if_not_exists_user(self, member: Union[discord.User, discord.Member]): + self._logger.debug(__name__, f'Check if user exists {member}') + # todo content to config + await self._messenger.send_dm_message(f'Welcome to {member.guild.name}', member) + + try: + server = self._servers.get_server_by_discord_id(member.guild.id) + + user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id) + if user is not None: + self._user_joins.add_user_joined_server(UserJoinedServer(user, datetime.now())) + return + + self._logger.debug(__name__, f'Add user: {member.id}') + self._users.add_user(User(member.id, 0, server)) + self._db.save_changes() + user = self._users.get_user_by_discord_id_and_server_id(member.id, server.server_id) + self._user_joins.add_user_joined_server(UserJoinedServer(user, datetime.now())) + self._db.save_changes() + except Exception as e: + self._logger.error(__name__, f'Cannot get user {member.id}', e) + + async def _remove_user(self, member: Union[discord.User, discord.Member]): + self._logger.debug(__name__, f'Remove user {member}') + # todo content to config + await self._messenger.send_dm_message(f'Bye bye', member) + + try: + server = self._servers.get_server_by_discord_id(member.guild.id) + + user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id) + if user is None: + self._logger.error(__name__, f'Cannot find user {member}') + return + + join = self._user_joins.get_active_user_joined_server_by_user_id(user.user_id) + join.leaved_on = datetime.now() + self._user_joins.update_user_joined_server(join) + self._db.save_changes() + except Exception as e: + self._logger.error(__name__, f'Cannot get user {member.id}', e) async def on_member_join(self, member: Union[discord.User, discord.Member]): - self._check_for_known_user(member) - # self._check_for_user(member) self._logger.debug(__name__, f'Module {type(self)} started') + self._check_for_known_user(member) + await self._add_if_not_exists_user(member) + + async def on_member_remove(self, member: Union[discord.User, discord.Member]): + self._logger.debug(__name__, f'Module {type(self)} started') + await self._remove_user(member) 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') + if message is None or message.guild is None: + return + self._apppend_received_message_count(message.guild.id) async def on_voice_state_update(self, member: discord.Member, before: discord.VoiceState, after: discord.VoiceState): + self._logger.debug(__name__, f'Module {type(self)} started') # todo: save ontime # todo: add xp to user when he goes offline - self._logger.debug(__name__, f'Module {type(self)} started')