Improved database on_ready

This commit is contained in:
Sven Heidemann 2021-12-07 17:36:19 +01:00
parent e73ebeb052
commit 9bae6c3c13
2 changed files with 70 additions and 13 deletions

View File

@ -65,13 +65,25 @@ class UserJoinedServer(TableABC):
@property @property
def insert_string(self) -> str: def insert_string(self) -> str:
if self._leaved_on is not None:
return str(f""" return str(f"""
INSERT INTO `UserJoinedServers` ( INSERT INTO `UserJoinedServers` (
`UserId`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt` `UserId`, `JoinedOn`, `LeavedOn`, `CreatedAt`, `LastModifiedAt`
) VALUES ( ) VALUES (
{self._user.user_id}, {self._user.user_id},
{self._joined_on}, '{self._joined_on}',
{self._leaved_on}, '{self._leaved_on}',
'{self._created_at}',
'{self._modified_at}'
);
""")
else:
return str(f"""
INSERT INTO `UserJoinedServers` (
`UserId`, `JoinedOn`, `CreatedAt`, `LastModifiedAt`
) VALUES (
{self._user.user_id},
'{self._joined_on}',
'{self._created_at}', '{self._created_at}',
'{self._modified_at}' '{self._modified_at}'
); );

View File

@ -4,14 +4,18 @@ import discord
from cpl_core.configuration import ConfigurationABC from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC from cpl_core.logging 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.known_user_repository_abc import KnownUserRepositoryABC
from gismo_data.abc.user_joined_server_repository_abc import \
UserJoinedServerRepositoryABC
from gismo_data.abc.user_repository_abc import UserRepositoryABC from gismo_data.abc.user_repository_abc import UserRepositoryABC
from gismo_data.model.client import Client from gismo_data.model.client import Client
from gismo_data.model.known_user import KnownUser from gismo_data.model.known_user import KnownUser
from gismo_data.model.server import Server from gismo_data.model.server import Server
from gismo_data.model.user import User from gismo_data.model.user import User
from gismo_data.model.user_joined_server import UserJoinedServer
from gismo_data.service.user_repository_service import ServerRepositoryABC from gismo_data.service.user_repository_service import ServerRepositoryABC
from modules_core.abc.events.on_ready_abc import OnReadyABC from modules_core.abc.events.on_ready_abc import OnReadyABC
from modules_core.abc.module_abc import ModuleABC from modules_core.abc.module_abc import ModuleABC
@ -28,7 +32,8 @@ class Database(ModuleABC, OnReadyABC):
server_repo: ServerRepositoryABC, server_repo: ServerRepositoryABC,
user_repo: UserRepositoryABC, user_repo: UserRepositoryABC,
client_repo: ClientRepositoryABC, client_repo: ClientRepositoryABC,
known_users: KnownUserRepositoryABC known_users: KnownUserRepositoryABC,
user_joins: UserJoinedServerRepositoryABC
): ):
self._config = config self._config = config
@ -39,6 +44,7 @@ class Database(ModuleABC, OnReadyABC):
self._users = user_repo self._users = user_repo
self._clients = client_repo self._clients = client_repo
self._known_users = known_users self._known_users = known_users
self._user_joins = user_joins
ModuleABC.__init__(self) ModuleABC.__init__(self)
self._priorities[OnReadyABC] = 0 self._priorities[OnReadyABC] = 0
@ -178,6 +184,44 @@ 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!')
def _check_user_joins(self):
self._logger.debug(__name__, f'Start checking UserJoinedServers table')
for g in self._bot.guilds:
g: discord.Guild = g
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:
u: discord.User = u
if u.bot:
self._logger.trace(__name__, f'User {u.id} is ignored, because its a bot')
continue
user = self._users.find_user_by_discord_id(u.id)
if user is None:
self._logger.fatal(__name__, f'User not found in database: {u.id}')
join = self._user_joins.find_active_user_joined_server_by_user_id(user.user_id)
if join is not None:
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._user_joins.add_user_joined_server(UserJoinedServer(user, m.joined_at, None))
self._db_context.save_changes()
self._logger.debug(__name__, f'Added UserJoinedServer: {u.id}')
except Exception as e:
self._logger.error(__name__, f'Cannot get UserJoinedServer', e)
results = self._users.get_users()
if results is None or len(results) == 0:
self._logger.error(__name__, f'Table Users is empty!')
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')
@ -185,6 +229,7 @@ class Database(ModuleABC, OnReadyABC):
self._check_servers() self._check_servers()
self._check_clients() self._check_clients()
self._check_users() self._check_users()
self._check_user_joins()
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')