Added check for known user at on_member_join

This commit is contained in:
Sven Heidemann 2021-12-08 17:32:55 +01:00
parent a10724a495
commit f417eca644

View File

@ -1,10 +1,14 @@
from typing import Union
import discord import discord
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging.logger_abc import LoggerABC from cpl_core.logging.logger_abc import LoggerABC
from gismo_core.abc.bot_service_abc import BotServiceABC from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_data.abc.client_repository_abc import ClientRepositoryABC 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.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_member_join_abc import OnMemberJoinABC
from modules_core.abc.events.on_message_abc import OnMessageABC from modules_core.abc.events.on_message_abc import OnMessageABC
from modules_core.abc.events.on_voice_state_update_abc import \ from modules_core.abc.events.on_voice_state_update_abc import \
@ -19,6 +23,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC):
logger: LoggerABC, logger: LoggerABC,
clients: ClientRepositoryABC, clients: ClientRepositoryABC,
servers: ServerRepositoryABC, servers: ServerRepositoryABC,
known_users: KnownUserRepositoryABC,
bot: BotServiceABC, bot: BotServiceABC,
db: DatabaseContextABC db: DatabaseContextABC
): ):
@ -26,6 +31,7 @@ class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC):
self._logger = logger self._logger = logger
self._clients = clients self._clients = clients
self._servers = servers self._servers = servers
self._known_users = known_users
self._bot = bot self._bot = bot
self._db = db self._db = db
@ -49,7 +55,22 @@ class Base(ModuleABC, OnMemberJoinABC, OnMessageABC, OnVoiceStateUpdateABC):
except Exception as e: except Exception as e:
self._logger.error(__name__, f'Cannot edit client {self._bot.user.id}@{g_id}') 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') self._logger.debug(__name__, f'Module {type(self)} started')
async def on_message(self, message: discord.Message): 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') 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): 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') self._logger.debug(__name__, f'Module {type(self)} started')