A-0.3 - Basismodul #36

Merged
edraft merged 48 commits from 0.3 into Alpha 2021-12-27 18:17:42 +01:00
4 changed files with 82 additions and 9 deletions
Showing only changes of commit 026dfe0dfb - Show all commits

View File

@ -22,6 +22,9 @@ class ClientRepositoryABC(ABC):
@abstractmethod
def find_client_by_discord_id(self, discord_id: int) -> Optional[Client]: pass
@abstractmethod
def find_client_by_server_id(self, discord_id: int) -> Optional[Client]: pass
@abstractmethod
def add_client(self, client: Client): pass

View File

@ -37,7 +37,7 @@ class Client(TableABC):
@property
def discord_id(self) -> int:
return self._discord_id
return self._discord_client_id
@property
def sent_message_count(self) -> int:
@ -78,7 +78,7 @@ class Client(TableABC):
`CreatedAt` DATETIME(6),
`LastModifiedAt` DATETIME(6),
FOREIGN KEY (`ServerId`) REFERENCES Servers(`ServerId`),
PRIMARY KEY(`UserId`)
PRIMARY KEY(`ClientId`)
);
""")
@ -102,10 +102,18 @@ class Client(TableABC):
WHERE `DiscordClientId` = {id};
""")
@staticmethod
def get_select_by_server_id_string(id: int) -> str:
return str(f"""
SELECT * FROM `Clients`
WHERE `ServerId` = {id};
""")
@property
def insert_string(self) -> str:
return str(f"""
INSERT INTO `Clients` (
`DiscordClientId`,
`SentMessageCount`,
`ReceivedMessageCount`,
`DeletedMessageCount`,
@ -115,12 +123,13 @@ class Client(TableABC):
`CreatedAt`,
`LastModifiedAt`
) VALUES (
{self._discord_client_id},
{self._sent_message_count},
{self._received_message_count},
{self._deleted_message_count},
{self._received_message_count},
{self._moved_users_count},
{self._server.id},
{self._server.server_id},
'{self._created_at}',
'{self._modified_at}'
);

View File

@ -83,6 +83,25 @@ class ClientRepositoryService(ClientRepositoryABC):
self._servers.get_server_by_id(result[7]),
id=result[0]
)
def find_client_by_server_id(self, discord_id: int) -> Optional[Client]:
self._logger.trace(__name__, f'Send SQL command: {Client.get_select_by_server_id_string(discord_id)}')
result = self._context.select(Client.get_select_by_server_id_string(discord_id))
if result is None or len(result) == 0:
return None
result = result[0]
return Client(
result[1],
result[2],
result[3],
result[4],
result[5],
result[6],
self._servers.get_server_by_id(result[7]),
id=result[0]
)
def add_client(self, client: Client):
self._logger.trace(__name__, f'Send SQL command: {client.insert_string}')

View File

@ -1,16 +1,19 @@
import asyncio
from datetime import datetime
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.user_repository_abc import UserRepositoryABC
from gismo_data.model.client import Client
from gismo_data.model.server import Server
from gismo_data.model.user import User
from gismo_data.service.user_repository_service import ServerRepositoryABC
@ -27,7 +30,8 @@ class Database(ModuleABC, OnReadyABC):
bot: BotServiceABC,
db_context: DatabaseContextABC,
server_repo: ServerRepositoryABC,
user_repo: UserRepositoryABC
user_repo: UserRepositoryABC,
client_repo: ClientRepositoryABC,
):
self._config = config
@ -36,6 +40,7 @@ class Database(ModuleABC, OnReadyABC):
self._db_context = db_context
self._servers = server_repo
self._users = user_repo
self._clients = client_repo
ModuleABC.__init__(self)
self._priorities[OnReadyABC] = 0
@ -66,12 +71,16 @@ class Database(ModuleABC, OnReadyABC):
try:
server = self._servers.find_server_by_discord_id(g.id)
if server is not None:
return
break
self._logger.warn(__name__, f'Server not found in database: {g.id}')
self._logger.debug(__name__, f'Add server: {g.id}')
self._servers.add_server(Server(g.id))
self._db_context.save_changes()
server = self._servers.find_server_by_discord_id(g.id)
if server is None:
self._logger.fatal(__name__, f'Cannot add server: {g.id}')
self._logger.debug(__name__, f'Added server: {g.id}')
except Exception as e:
@ -80,6 +89,35 @@ class Database(ModuleABC, OnReadyABC):
results = self._servers.get_servers()
if results is None or len(results) == 0:
self._logger.error(__name__, f'Table Servers is empty!')
def _check_clients(self):
for g in self._bot.guilds:
g: discord.Guild = g
try:
server: 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}')
client = self._clients.find_client_by_server_id(server.server_id)
if client is not None:
break
self._logger.warn(__name__, f'Client for server {g.id} not found in database: {self._bot.user.id}')
self._logger.debug(__name__, f'Add client: {self._bot.user.id}')
self._clients.add_client(Client(self._bot.user.id, 0, 0, 0, 0, 0, server))
self._db_context.save_changes()
client = self._clients.find_client_by_server_id(server.server_id)
if client is None:
self._logger.fatal(__name__, f'Cannot add client {self._bot.user.id} for server {g.id}')
self._logger.debug(__name__, f'Added client: {g.id}')
except Exception as e:
self._logger.error(__name__, f'Cannot get client', 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_users(self):
for g in self._bot.guilds:
@ -89,10 +127,13 @@ class Database(ModuleABC, OnReadyABC):
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}')
break
for u in g.members:
u: discord.Member = u
if u.bot:
self._logger.trace(__name__, f'User {u.id} is ignored, because its a bot')
break
user = self._users.find_user_by_discord_id(u.id)
if user is not None:
break
@ -114,7 +155,8 @@ class Database(ModuleABC, OnReadyABC):
self._logger.debug(__name__, f'Module {type(self)} started')
self._check_servers()
self._check_clients()
self._check_users()
self._validate_init_time()
self._logger.trace(__name__, f'Module {type(self)} stopped')
self._logger.trace(__name__, f'Module {type(self)} stopped')