diff --git a/kdb-bot/src/bot/config/feature-flags.json b/kdb-bot/src/bot/config/feature-flags.json index 2d214f7f..68bf6e00 100644 --- a/kdb-bot/src/bot/config/feature-flags.json +++ b/kdb-bot/src/bot/config/feature-flags.json @@ -10,7 +10,6 @@ "DatabaseModule": true, "ModeratorModule": true, "PermissionModule": true, - "PresenceModule": true, - "ApiOnly": true + "PresenceModule": true } } diff --git a/kdb-bot/src/bot_api/controller/discord/__init__.py b/kdb-bot/src/bot_api/controller/discord/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/kdb-bot/src/bot_api/controller/discord/server_controller.py b/kdb-bot/src/bot_api/controller/discord/server_controller.py new file mode 100644 index 00000000..957624a4 --- /dev/null +++ b/kdb-bot/src/bot_api/controller/discord/server_controller.py @@ -0,0 +1,39 @@ +from cpl_core.configuration import ConfigurationABC +from cpl_core.environment import ApplicationEnvironmentABC +from cpl_core.mailing import EMailClientABC, EMailClientSettings +from cpl_translation import TranslatePipe +from flask import Response, jsonify + +from bot_api.api import Api +from bot_api.logging.api_logger import ApiLogger +from bot_api.route.route import Route +from bot_api.service.discord_service import DiscordService + + +class ServerController: + BasePath = f'/api/discord/server' + + def __init__( + self, + config: ConfigurationABC, + env: ApplicationEnvironmentABC, + logger: ApiLogger, + t: TranslatePipe, + api: Api, + mail_settings: EMailClientSettings, + mailer: EMailClientABC, + discord_service: DiscordService + ): + self._config = config + self._env = env + self._logger = logger + self._t = t + self._api = api + self._mail_settings = mail_settings + self._mailer = mailer + self._discord_service = discord_service + + @Route.get(f'{BasePath}/servers') + @Route.authorize + async def get_all_servers(self) -> Response: + return jsonify(self._discord_service.get_all_servers().select(lambda x: x.to_dict())) diff --git a/kdb-bot/src/bot_api/event/__init__.py b/kdb-bot/src/bot_api/event/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/kdb-bot/src/bot_api/model/discord/__init__.py b/kdb-bot/src/bot_api/model/discord/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/kdb-bot/src/bot_api/model/discord/server_dto.py b/kdb-bot/src/bot_api/model/discord/server_dto.py new file mode 100644 index 00000000..e7115dd2 --- /dev/null +++ b/kdb-bot/src/bot_api/model/discord/server_dto.py @@ -0,0 +1,49 @@ +from bot_api.abc.dto_abc import DtoABC + + +class ServerDTO(DtoABC): + + def __init__( + self, + server_id: int, + discord_id: int, + name: str, + member_count: int + + ): + DtoABC.__init__(self) + + self._server_id = server_id + self._discord_id = discord_id + self._name = name + self._member_count = member_count + + @property + def server_id(self) -> int: + return self._server_id + + @property + def discord_id(self) -> int: + return self._discord_id + + @property + def name(self) -> str: + return self._name + + @property + def member_count(self) -> int: + return self._member_count + + def from_dict(self, values: dict): + self._server_id = int(values['serverId']) + self._discord_id = int(values['discordId']) + self._name = values['name'] + self._member_count = int(values['memberCount']) + + def to_dict(self) -> dict: + return { + 'serverId': self._server_id, + 'discordId': self._discord_id, + 'name': self._name, + 'memberCount': self._member_count, + } diff --git a/kdb-bot/src/bot_api/service/discord_service.py b/kdb-bot/src/bot_api/service/discord_service.py new file mode 100644 index 00000000..448d4d7f --- /dev/null +++ b/kdb-bot/src/bot_api/service/discord_service.py @@ -0,0 +1,23 @@ +from cpl_discord.service import DiscordBotServiceABC +from cpl_query.extension import List + +from bot_api.model.discord.server_dto import ServerDTO +from bot_api.transformer.server_transformer import ServerTransformer +from bot_data.abc.server_repository_abc import ServerRepositoryABC + + +class DiscordService: + + def __init__( + self, + bot: DiscordBotServiceABC, + servers: ServerRepositoryABC, + ): + self._bot = bot + self._servers = servers + + def get_all_servers(self) -> List[ServerDTO]: + servers = self._servers.get_servers().select() + return servers.select( + lambda x: ServerTransformer.to_dto(x, self._bot.get_guild(x.discord_server_id).name, self._bot.get_guild(x.discord_server_id).member_count) + ) diff --git a/kdb-bot/src/bot_api/transformer/server_transformer.py b/kdb-bot/src/bot_api/transformer/server_transformer.py new file mode 100644 index 00000000..920fd506 --- /dev/null +++ b/kdb-bot/src/bot_api/transformer/server_transformer.py @@ -0,0 +1,19 @@ +from bot_api.abc.transformer_abc import TransformerABC +from bot_api.model.discord.server_dto import ServerDTO +from bot_data.model.server import Server + + +class ServerTransformer(TransformerABC): + + @staticmethod + def to_db(dto: ServerDTO) -> Server: + return Server(dto.discord_id) + + @staticmethod + def to_dto(db: Server, name: str, member_count: int) -> ServerDTO: + return ServerDTO( + db.server_id, + db.discord_server_id, + name, + member_count, + )