Added auto role filter #162

This commit is contained in:
2023-02-07 17:52:38 +01:00
parent e3c0a0dea3
commit eb58c34c4d
15 changed files with 109 additions and 56 deletions

View File

@@ -3,11 +3,13 @@ from typing import Optional
from cpl_core.database import TableABC
from bot_data.model.server import Server
class AutoRole(TableABC):
def __init__(
self,
server_id: int,
server: Optional[Server],
dc_channel_id: int,
dc_message_id: int,
created_at: datetime = None,
@@ -15,7 +17,7 @@ class AutoRole(TableABC):
id=0,
):
self._auto_role_id = id
self._server_id = server_id
self._server = server
self._discord_channel_id = dc_channel_id
self._discord_message_id = dc_message_id
@@ -28,8 +30,8 @@ class AutoRole(TableABC):
return self._auto_role_id
@property
def server_id(self) -> int:
return self._server_id
def server(self) -> Server:
return self._server
@property
def discord_channel_id(self) -> int:
@@ -81,7 +83,7 @@ class AutoRole(TableABC):
INSERT INTO `AutoRoles` (
`ServerId`, `DiscordChannelId`, `DiscordMessageId`, `CreatedAt`, `LastModifiedAt`
) VALUES (
{self._server_id},
{self._server.server_id},
{self._discord_channel_id},
{self._discord_message_id},
'{self._created_at}',
@@ -95,7 +97,7 @@ class AutoRole(TableABC):
return str(
f"""
UPDATE `AutoRoles`
SET `ServerId` = {self._server_id},
SET `ServerId` = {self._server.server_id},
`DiscordChannelId` = {self._discord_channel_id},
`DiscordMessageId` = {self._discord_message_id},
`LastModifiedAt` = '{self._modified_at}'

View File

@@ -5,14 +5,16 @@ from cpl_query.extension import List
from bot_core.logging.database_logger import DatabaseLogger
from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.model.auto_role import AutoRole
from bot_data.model.auto_role_rule import AutoRoleRule
class AutoRoleRepositoryService(AutoRoleRepositoryABC):
def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC):
def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC, servers: ServerRepositoryABC):
self._logger = logger
self._context = db_context
self._servers = servers
AutoRoleRepositoryABC.__init__(self)
@@ -21,14 +23,20 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC):
self._logger.trace(__name__, f"Send SQL command: {AutoRole.get_select_all_string()}")
results = self._context.select(AutoRole.get_select_all_string())
for result in results:
auto_roles.append(AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0]))
auto_roles.append(
AutoRole(
self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0]
)
)
return auto_roles
def get_auto_role_by_id(self, id: int) -> AutoRole:
self._logger.trace(__name__, f"Send SQL command: {AutoRole.get_select_by_id_string(id)}")
result = self._context.select(AutoRole.get_select_by_id_string(id))[0]
return AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0])
return AutoRole(
self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0]
)
def find_auto_role_by_id(self, id: int) -> Optional[AutoRole]:
self._logger.trace(__name__, f"Send SQL command: {AutoRole.get_select_by_id_string(id)}")
@@ -38,14 +46,20 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC):
result = result[0]
return AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0])
return AutoRole(
self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0]
)
def get_auto_roles_by_server_id(self, id: int) -> List[AutoRole]:
self._logger.trace(__name__, f"Send SQL command: {AutoRole.get_select_by_server_id_string(id)}")
auto_roles = List(AutoRole)
results = self._context.select(AutoRole.get_select_by_server_id_string(id))
for result in results:
auto_roles.append(AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0]))
auto_roles.append(
AutoRole(
self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0]
)
)
return auto_roles
@@ -55,7 +69,9 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC):
f"Send SQL command: {AutoRole.get_select_by_message_id_string(id)}",
)
result = self._context.select(AutoRole.get_select_by_message_id_string(id))[0]
return AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0])
return AutoRole(
self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0]
)
def find_auto_role_by_message_id(self, id: int) -> Optional[AutoRole]:
self._logger.trace(
@@ -68,7 +84,9 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC):
result = result[0]
return AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0])
return AutoRole(
self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0]
)
def add_auto_role(self, auto_role: AutoRole):
self._logger.trace(__name__, f"Send SQL command: {auto_role.insert_string}")