Added logic to check in db if user already left before starting

This commit is contained in:
Sven Heidemann 2021-12-08 18:53:25 +01:00
parent 7e6485459b
commit f800a11f41

View File

@ -187,15 +187,15 @@ class Database(ModuleABC, OnReadyABC):
def _check_user_joins(self): def _check_user_joins(self):
self._logger.debug(__name__, f'Start checking UserJoinedServers table') self._logger.debug(__name__, f'Start checking UserJoinedServers table')
for g in self._bot.guilds: for guild in self._bot.guilds:
g: discord.Guild = g guild: discord.Guild = guild
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: try:
server = self._servers.find_server_by_discord_id(g.id) for u in guild.members:
if server is None:
self._logger.fatal(__name__, f'Server not found in database: {g.id}')
for u in g.members:
u: discord.User = u u: discord.User = u
if u.bot: if u.bot:
self._logger.trace(__name__, f'User {u.id} is ignored, because its a bot') self._logger.trace(__name__, f'User {u.id} is ignored, because its a bot')
@ -210,8 +210,8 @@ class Database(ModuleABC, OnReadyABC):
continue continue
m: discord.Member = u m: discord.Member = u
self._logger.warn(__name__, f'Active UserJoinedServer not found in database: {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: {g.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._user_joins.add_user_joined_server(UserJoinedServer(user, m.joined_at, None))
self._db_context.save_changes() self._db_context.save_changes()
@ -223,6 +223,24 @@ class Database(ModuleABC, OnReadyABC):
if results is None or len(results) == 0: if results is None or len(results) == 0:
self._logger.error(__name__, f'Table Users is empty!') 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): async def on_ready(self):
self._logger.debug(__name__, f'Module {type(self)} started') self._logger.debug(__name__, f'Module {type(self)} started')
@ -234,3 +252,4 @@ class Database(ModuleABC, OnReadyABC):
self._validate_init_time() self._validate_init_time()
self._logger.trace(__name__, f'Module {type(self)} stopped') self._logger.trace(__name__, f'Module {type(self)} stopped')
exit()