From f417eca64456413689a683878529661352a7c60e Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Dec 2021 17:32:55 +0100 Subject: [PATCH] Added check for known user at on_member_join --- src/modules/base/base.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/modules/base/base.py b/src/modules/base/base.py index 0e315ee..c05a1df 100644 --- a/src/modules/base/base.py +++ b/src/modules/base/base.py @@ -1,10 +1,14 @@ +from typing import Union + 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.known_user_repository_abc import KnownUserRepositoryABC from gismo_data.abc.server_repository_abc import ServerRepositoryABC +from gismo_data.model.known_user import KnownUser 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 \ @@ -19,6 +23,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC): logger: LoggerABC, clients: ClientRepositoryABC, servers: ServerRepositoryABC, + known_users: KnownUserRepositoryABC, bot: BotServiceABC, db: DatabaseContextABC ): @@ -26,6 +31,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC): self._logger = logger self._clients = clients self._servers = servers + self._known_users = known_users self._bot = bot self._db = db @@ -49,7 +55,22 @@ class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC): 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): + def _check_for_known_user(self, member: Union[discord.User, discord.Member]): + self._logger.debug(__name__, f'Check if user is already known {member}') + try: + user = self._known_users.find_user_by_discord_id(member.id) + if user is not None: + return + + self._logger.debug(__name__, f'Add user: {member.id}') + 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}') + + 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') async def on_message(self, message: discord.Message): @@ -57,4 +78,6 @@ class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC): 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): + # todo: save ontime + # todo: add xp to user when he goes offline self._logger.debug(__name__, f'Module {type(self)} started')