Improved database module

This commit is contained in:
Sven Heidemann 2021-12-02 17:51:11 +01:00
parent 0fd99cc6ba
commit c949efea50
3 changed files with 41 additions and 10 deletions

View File

@ -29,7 +29,6 @@ class KnownUser(TableABC):
CREATE TABLE IF NOT EXISTS `KnownUsers` (
`KnownUserId` BIGINT NOT NULL AUTO_INCREMENT,
`DiscordId` BIGINT NOT NULL,
`ServerId` BIGINT,
`CreatedAt` DATETIME(6),
`LastModifiedAt` DATETIME(6),
PRIMARY KEY(`KnownUserId`)

View File

@ -2,13 +2,13 @@ from typing import Optional
from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC
from cpl_query.extension import List
from gismo_data.abc.known_user_repository_abc import KnownUserRepositoryABC
from gismo_data.abc.server_repository_abc import ServerRepositoryABC
from gismo_data.abc.user_repository_abc import UserRepositoryABC
from gismo_data.model.known_user import KnownUser
class KnownUserRepositoryService(UserRepositoryABC):
class KnownUserRepositoryService(KnownUserRepositoryABC):
def __init__(self, logger: LoggerABC, db_context: DatabaseContextABC, servers: ServerRepositoryABC):
self._logger = logger
@ -16,7 +16,7 @@ class KnownUserRepositoryService(UserRepositoryABC):
self._servers = servers
UserRepositoryABC.__init__(self)
KnownUserRepositoryABC.__init__(self)
def get_users(self) -> List[KnownUser]:
users = List(KnownUser)

View File

@ -1,19 +1,15 @@
import asyncio
import time
from datetime import datetime
from typing import Union
import discord
from cpl_core.configuration import ConfigurationABC
from cpl_core.database.context import DatabaseContextABC
from cpl_core.logging import LoggerABC
from discord.ext import commands
from gismo_core.abc.bot_service_abc import BotServiceABC
from gismo_core.abc.message_service_abc import MessageServiceABC
from gismo_core.configuration.server_settings import ServerSettings
from gismo_data.abc.client_repository_abc import ClientRepositoryABC
from gismo_data.abc.known_user_repository_abc import KnownUserRepositoryABC
from gismo_data.abc.user_repository_abc import UserRepositoryABC
from gismo_data.model.client import Client
from gismo_data.model.known_user import KnownUser
from gismo_data.model.server import Server
from gismo_data.model.user import User
from gismo_data.service.user_repository_service import ServerRepositoryABC
@ -32,6 +28,7 @@ class Database(ModuleABC, OnReadyABC):
server_repo: ServerRepositoryABC,
user_repo: UserRepositoryABC,
client_repo: ClientRepositoryABC,
known_users: KnownUserRepositoryABC
):
self._config = config
@ -41,6 +38,7 @@ class Database(ModuleABC, OnReadyABC):
self._servers = server_repo
self._users = user_repo
self._clients = client_repo
self._known_users = known_users
ModuleABC.__init__(self)
self._priorities[OnReadyABC] = 0
@ -65,7 +63,38 @@ class Database(ModuleABC, OnReadyABC):
self._logger.error(__name__, 'Database init time calculation failed', e)
return
def _check_known_users(self):
self._logger.debug(__name__, f'Start checking KnownUsers table')
for u in self._bot.users:
u: discord.User = u
try:
if u.bot:
self._logger.trace(__name__, f'User {u.id} is ignored, because its a bot')
break
user = self._known_users.find_user_by_discord_id(u.id)
if user is not None:
break
self._logger.warn(__name__, f'Unknown user: {u.id}')
self._logger.debug(__name__, f'Add user: {u.id}')
self._known_users.add_user(KnownUser(u.id))
self._db_context.save_changes()
user = self._known_users.find_user_by_discord_id(u.id)
if user is None:
self._logger.fatal(__name__, f'Cannot add user: {u.id}')
self._logger.debug(__name__, f'Added user: {u.id}')
except Exception as e:
self._logger.error(__name__, f'Cannot get user', e)
results = self._servers.get_servers()
if results is None or len(results) == 0:
self._logger.error(__name__, f'Table Servers is empty!')
def _check_servers(self):
self._logger.debug(__name__, f'Start checking Servers table')
for g in self._bot.guilds:
g: discord.Guild = g
try:
@ -91,6 +120,7 @@ class Database(ModuleABC, OnReadyABC):
self._logger.error(__name__, f'Table Servers is empty!')
def _check_clients(self):
self._logger.debug(__name__, f'Start checking Clients table')
for g in self._bot.guilds:
g: discord.Guild = g
try:
@ -120,6 +150,7 @@ class Database(ModuleABC, OnReadyABC):
self._logger.error(__name__, f'Table Servers is empty!')
def _check_users(self):
self._logger.debug(__name__, f'Start checking Users table')
for g in self._bot.guilds:
g: discord.Guild = g
@ -154,6 +185,7 @@ class Database(ModuleABC, OnReadyABC):
async def on_ready(self):
self._logger.debug(__name__, f'Module {type(self)} started')
self._check_known_users()
self._check_servers()
self._check_clients()
self._check_users()