From 8aee72856c31d35e3d6522a423914360b13a9c43 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 6 Nov 2022 19:51:43 +0100 Subject: [PATCH] Added level service #25 --- kdb-bot/src/modules/level/level_module.py | 2 + kdb-bot/src/modules/level/service/__init__.py | 1 + .../modules/level/service/level_service.py | 53 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 kdb-bot/src/modules/level/service/__init__.py create mode 100644 kdb-bot/src/modules/level/service/level_service.py diff --git a/kdb-bot/src/modules/level/level_module.py b/kdb-bot/src/modules/level/level_module.py index 565297436f..760544babf 100644 --- a/kdb-bot/src/modules/level/level_module.py +++ b/kdb-bot/src/modules/level/level_module.py @@ -8,6 +8,7 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from modules.level.level_seeder import LevelSeeder +from modules.level.service.level_service import LevelService class LevelModule(ModuleABC): @@ -23,3 +24,4 @@ class LevelModule(ModuleABC): def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): services.add_transient(LevelSeeder) + services.add_transient(LevelService) diff --git a/kdb-bot/src/modules/level/service/__init__.py b/kdb-bot/src/modules/level/service/__init__.py new file mode 100644 index 0000000000..425ab6c146 --- /dev/null +++ b/kdb-bot/src/modules/level/service/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/kdb-bot/src/modules/level/service/level_service.py b/kdb-bot/src/modules/level/service/level_service.py new file mode 100644 index 0000000000..b3d2f664f4 --- /dev/null +++ b/kdb-bot/src/modules/level/service/level_service.py @@ -0,0 +1,53 @@ +from cpl_core.database.context import DatabaseContextABC +from cpl_core.logging import LoggerABC +from cpl_discord.container import Guild, Role, Member +from cpl_discord.service import DiscordBotServiceABC + +from bot_data.model.level import Level +from bot_data.model.user import User +from bot_data.service.level_repository_service import LevelRepositoryService +from bot_data.service.user_repository_service import UserRepositoryService + + +class LevelService: + + def __init__( + self, + logger: LoggerABC, + db: DatabaseContextABC, + levels: LevelRepositoryService, + users: UserRepositoryService, + bot: DiscordBotServiceABC + ): + self._logger = logger + self._db = db + self._levels = levels + self._users = users + self._bot = bot + + def get_level(self, user: User) -> Level: + levels = self._levels.get_levels_by_server_id(user.server.server_id).order_by(lambda l: l.min_xp) + return levels.where(lambda l: l.min_xp > user.xp).first() + + async def set_level(self, user: User): + level_names = self._levels.get_levels_by_server_id(user.server.server_id).select(lambda l: l.name) + guild: Guild = self._bot.guilds.where(lambda g: g.id == user.server.discord_server_id).single() + level_role: Role = guild.roles.where(lambda r: r.name == self.get_level(user).name).single() + member: Member = guild.members.where(lambda m: m.id == user.discord_id).single() + for role in member.roles: + if role.name not in level_names.to_list(): + continue + + try: + self._logger.debug(__name__, f'Try to remove role {role.name} from {member.name}') + await member.remove_roles(role) + self._logger.info(__name__, f'Removed role {role.name} from {member.name}') + except Exception as e: + self._logger.error(__name__, f'Removing role {role.name} from {member.name} failed!', e) + + try: + self._logger.debug(__name__, f'Try to add role {level_role.name} to {member.name}') + await member.add_roles(level_role) + self._logger.info(__name__, f'Add role {level_role.name} to {member.name}') + except Exception as e: + self._logger.error(__name__, f'Adding role {level_role.name} to {member.name} failed!', e)