A-0.3 - Basismodul #36
@ -22,6 +22,9 @@ class ClientRepositoryABC(ABC):
|
|||||||
@abstractmethod
|
@abstractmethod
|
||||||
def find_client_by_discord_id(self, discord_id: int) -> Optional[Client]: pass
|
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
|
@abstractmethod
|
||||||
def add_client(self, client: Client): pass
|
def add_client(self, client: Client): pass
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@ class Client(TableABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def discord_id(self) -> int:
|
def discord_id(self) -> int:
|
||||||
return self._discord_id
|
return self._discord_client_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def sent_message_count(self) -> int:
|
def sent_message_count(self) -> int:
|
||||||
@ -78,7 +78,7 @@ class Client(TableABC):
|
|||||||
`CreatedAt` DATETIME(6),
|
`CreatedAt` DATETIME(6),
|
||||||
`LastModifiedAt` DATETIME(6),
|
`LastModifiedAt` DATETIME(6),
|
||||||
FOREIGN KEY (`ServerId`) REFERENCES Servers(`ServerId`),
|
FOREIGN KEY (`ServerId`) REFERENCES Servers(`ServerId`),
|
||||||
PRIMARY KEY(`UserId`)
|
PRIMARY KEY(`ClientId`)
|
||||||
);
|
);
|
||||||
""")
|
""")
|
||||||
|
|
||||||
@ -102,10 +102,18 @@ class Client(TableABC):
|
|||||||
WHERE `DiscordClientId` = {id};
|
WHERE `DiscordClientId` = {id};
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_select_by_server_id_string(id: int) -> str:
|
||||||
|
return str(f"""
|
||||||
|
SELECT * FROM `Clients`
|
||||||
|
WHERE `ServerId` = {id};
|
||||||
|
""")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def insert_string(self) -> str:
|
def insert_string(self) -> str:
|
||||||
return str(f"""
|
return str(f"""
|
||||||
INSERT INTO `Clients` (
|
INSERT INTO `Clients` (
|
||||||
|
`DiscordClientId`,
|
||||||
`SentMessageCount`,
|
`SentMessageCount`,
|
||||||
`ReceivedMessageCount`,
|
`ReceivedMessageCount`,
|
||||||
`DeletedMessageCount`,
|
`DeletedMessageCount`,
|
||||||
@ -115,12 +123,13 @@ class Client(TableABC):
|
|||||||
`CreatedAt`,
|
`CreatedAt`,
|
||||||
`LastModifiedAt`
|
`LastModifiedAt`
|
||||||
) VALUES (
|
) VALUES (
|
||||||
|
{self._discord_client_id},
|
||||||
{self._sent_message_count},
|
{self._sent_message_count},
|
||||||
{self._received_message_count},
|
{self._received_message_count},
|
||||||
{self._deleted_message_count},
|
{self._deleted_message_count},
|
||||||
{self._received_message_count},
|
{self._received_message_count},
|
||||||
{self._moved_users_count},
|
{self._moved_users_count},
|
||||||
{self._server.id},
|
{self._server.server_id},
|
||||||
'{self._created_at}',
|
'{self._created_at}',
|
||||||
'{self._modified_at}'
|
'{self._modified_at}'
|
||||||
);
|
);
|
||||||
|
@ -84,6 +84,25 @@ class ClientRepositoryService(ClientRepositoryABC):
|
|||||||
id=result[0]
|
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):
|
def add_client(self, client: Client):
|
||||||
self._logger.trace(__name__, f'Send SQL command: {client.insert_string}')
|
self._logger.trace(__name__, f'Send SQL command: {client.insert_string}')
|
||||||
self._context.cursor.execute(client.insert_string)
|
self._context.cursor.execute(client.insert_string)
|
||||||
|
@ -1,16 +1,19 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from datetime import datetime
|
|
||||||
import time
|
import time
|
||||||
|
from datetime import datetime
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
import discord
|
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 discord.ext import commands
|
||||||
from gismo_core.abc.bot_service_abc import BotServiceABC
|
from gismo_core.abc.bot_service_abc import BotServiceABC
|
||||||
from gismo_core.abc.message_service_abc import MessageServiceABC
|
from gismo_core.abc.message_service_abc import MessageServiceABC
|
||||||
from gismo_core.configuration.server_settings import ServerSettings
|
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.abc.user_repository_abc import UserRepositoryABC
|
||||||
|
from gismo_data.model.client import Client
|
||||||
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.service.user_repository_service import ServerRepositoryABC
|
from gismo_data.service.user_repository_service import ServerRepositoryABC
|
||||||
@ -27,7 +30,8 @@ class Database(ModuleABC, OnReadyABC):
|
|||||||
bot: BotServiceABC,
|
bot: BotServiceABC,
|
||||||
db_context: DatabaseContextABC,
|
db_context: DatabaseContextABC,
|
||||||
server_repo: ServerRepositoryABC,
|
server_repo: ServerRepositoryABC,
|
||||||
user_repo: UserRepositoryABC
|
user_repo: UserRepositoryABC,
|
||||||
|
client_repo: ClientRepositoryABC,
|
||||||
):
|
):
|
||||||
self._config = config
|
self._config = config
|
||||||
|
|
||||||
@ -36,6 +40,7 @@ class Database(ModuleABC, OnReadyABC):
|
|||||||
self._db_context = db_context
|
self._db_context = db_context
|
||||||
self._servers = server_repo
|
self._servers = server_repo
|
||||||
self._users = user_repo
|
self._users = user_repo
|
||||||
|
self._clients = client_repo
|
||||||
|
|
||||||
ModuleABC.__init__(self)
|
ModuleABC.__init__(self)
|
||||||
self._priorities[OnReadyABC] = 0
|
self._priorities[OnReadyABC] = 0
|
||||||
@ -66,13 +71,17 @@ class Database(ModuleABC, OnReadyABC):
|
|||||||
try:
|
try:
|
||||||
server = self._servers.find_server_by_discord_id(g.id)
|
server = self._servers.find_server_by_discord_id(g.id)
|
||||||
if server is not None:
|
if server is not None:
|
||||||
return
|
break
|
||||||
|
|
||||||
self._logger.warn(__name__, f'Server not found in database: {g.id}')
|
self._logger.warn(__name__, f'Server not found in database: {g.id}')
|
||||||
self._logger.debug(__name__, f'Add server: {g.id}')
|
self._logger.debug(__name__, f'Add server: {g.id}')
|
||||||
self._servers.add_server(Server(g.id))
|
self._servers.add_server(Server(g.id))
|
||||||
self._db_context.save_changes()
|
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}')
|
self._logger.debug(__name__, f'Added server: {g.id}')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._logger.error(__name__, f'Cannot get server', e)
|
self._logger.error(__name__, f'Cannot get server', e)
|
||||||
@ -81,6 +90,35 @@ 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 Servers is empty!')
|
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):
|
def _check_users(self):
|
||||||
for g in self._bot.guilds:
|
for g in self._bot.guilds:
|
||||||
g: discord.Guild = g
|
g: discord.Guild = g
|
||||||
@ -89,10 +127,13 @@ class Database(ModuleABC, OnReadyABC):
|
|||||||
server = self._servers.find_server_by_discord_id(g.id)
|
server = self._servers.find_server_by_discord_id(g.id)
|
||||||
if server is None:
|
if server is None:
|
||||||
self._logger.fatal(__name__, f'Server not found in database: {g.id}')
|
self._logger.fatal(__name__, f'Server not found in database: {g.id}')
|
||||||
break
|
|
||||||
|
|
||||||
for u in g.members:
|
for u in g.members:
|
||||||
u: discord.Member = u
|
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)
|
user = self._users.find_user_by_discord_id(u.id)
|
||||||
if user is not None:
|
if user is not None:
|
||||||
break
|
break
|
||||||
@ -114,6 +155,7 @@ class Database(ModuleABC, OnReadyABC):
|
|||||||
self._logger.debug(__name__, f'Module {type(self)} started')
|
self._logger.debug(__name__, f'Module {type(self)} started')
|
||||||
|
|
||||||
self._check_servers()
|
self._check_servers()
|
||||||
|
self._check_clients()
|
||||||
self._check_users()
|
self._check_users()
|
||||||
|
|
||||||
self._validate_init_time()
|
self._validate_init_time()
|
||||||
|
Reference in New Issue
Block a user