diff --git a/src/modules/database/database.py b/src/modules/database/database.py index 4e2910c..f655506 100644 --- a/src/modules/database/database.py +++ b/src/modules/database/database.py @@ -187,15 +187,15 @@ class Database(ModuleABC, OnReadyABC): def _check_user_joins(self): self._logger.debug(__name__, f'Start checking UserJoinedServers table') - for g in self._bot.guilds: - g: discord.Guild = g + for guild in self._bot.guilds: + guild: discord.Guild = guild - try: - server = self._servers.find_server_by_discord_id(g.id) - if server is None: - self._logger.fatal(__name__, f'Server not found in database: {g.id}') - - for u in g.members: + server = self._servers.find_server_by_discord_id(guild.id) + if server is None: + self._logger.fatal(__name__, f'Server not found in database: {guild.id}') + + try: + for u in guild.members: u: discord.User = u if u.bot: self._logger.trace(__name__, f'User {u.id} is ignored, because its a bot') @@ -210,8 +210,8 @@ class Database(ModuleABC, OnReadyABC): continue m: discord.Member = u - self._logger.warn(__name__, f'Active UserJoinedServer not found in database: {g.id}:{u.id}@{m.joined_at}') - self._logger.debug(__name__, f'Add UserJoinedServer: {g.id}:{u.id}@{m.joined_at}') + self._logger.warn(__name__, f'Active UserJoinedServer not found in database: {guild.id}:{u.id}@{m.joined_at}') + self._logger.debug(__name__, f'Add UserJoinedServer: {guild.id}:{u.id}@{m.joined_at}') self._user_joins.add_user_joined_server(UserJoinedServer(user, m.joined_at, None)) self._db_context.save_changes() @@ -223,6 +223,24 @@ class Database(ModuleABC, OnReadyABC): if results is None or len(results) == 0: self._logger.error(__name__, f'Table Users is empty!') + + joins = self._user_joins.get_user_joined_servers() + for join in joins: + join: UserJoinedServer = join + if join.user.server.discord_server_id != guild.id: + continue + + if join.leaved_on is not None: + continue + + dc_user = guild.get_member(join.user.discord_id) + if dc_user is None: + self._logger.warn(__name__, f'User {join.user.discord_id} already left the server.') + join.leaved_on = datetime.now() + self._user_joins.update_user_joined_server(join) + + self._db_context.save_changes() + async def on_ready(self): self._logger.debug(__name__, f'Module {type(self)} started') @@ -234,3 +252,4 @@ class Database(ModuleABC, OnReadyABC): self._validate_init_time() self._logger.trace(__name__, f'Module {type(self)} stopped') + exit()