diff --git a/bot/src/bot_data/model/auto_role_rule.py b/bot/src/bot_data/model/auto_role_rule.py index 8729b6ec..992fdfe0 100644 --- a/bot/src/bot_data/model/auto_role_rule.py +++ b/bot/src/bot_data/model/auto_role_rule.py @@ -1,4 +1,5 @@ from datetime import datetime +from typing import Optional from cpl_core.database import TableABC from cpl_core.dependency_injection import ServiceProviderABC @@ -48,8 +49,10 @@ class AutoRoleRule(TableABC): @property @ServiceProviderABC.inject - def role_name(self, bot: DiscordBotServiceABC) -> str: + def role_name(self, bot: DiscordBotServiceABC) -> Optional[str]: guild = bot.get_guild(self.auto_role.server.discord_id) + if guild is None: + return None return guild.get_role(self.role_id).name @role_id.setter diff --git a/bot/src/bot_data/model/auto_role_rule_history.py b/bot/src/bot_data/model/auto_role_rule_history.py index c5a5ccd9..1acc86a2 100644 --- a/bot/src/bot_data/model/auto_role_rule_history.py +++ b/bot/src/bot_data/model/auto_role_rule_history.py @@ -1,3 +1,5 @@ +from typing import Optional + from cpl_core.dependency_injection import ServiceProviderABC from cpl_discord.service import DiscordBotServiceABC @@ -43,6 +45,8 @@ class AutoRoleRuleHistory(HistoryTableABC): @property @ServiceProviderABC.inject - def role_name(self, bot: DiscordBotServiceABC) -> str: + def role_name(self, bot: DiscordBotServiceABC) -> Optional[str]: guild = bot.get_guild(self.auto_role.server.discord_id) + if guild is None: + return None return guild.get_role(self.role_id).name diff --git a/bot/src/bot_data/model/level.py b/bot/src/bot_data/model/level.py index 378d7fc6..6002e407 100644 --- a/bot/src/bot_data/model/level.py +++ b/bot/src/bot_data/model/level.py @@ -39,8 +39,10 @@ class Level(TableABC): @property @ServiceProviderABC.inject - def icon_url(self, bot: DiscordBotServiceABC) -> str: + def icon_url(self, bot: DiscordBotServiceABC) -> Optional[str]: guild = bot.get_guild(self.server.discord_id) + if guild is None: + return None role = List(Role, guild.roles).where(lambda x: x.name == self._name).first_or_default() return None if role is None else role.icon diff --git a/bot/src/bot_data/model/server.py b/bot/src/bot_data/model/server.py index d86c6e97..de9d5b78 100644 --- a/bot/src/bot_data/model/server.py +++ b/bot/src/bot_data/model/server.py @@ -1,4 +1,5 @@ from datetime import datetime +from typing import Optional from cpl_core.database import TableABC from cpl_core.dependency_injection import ServiceProviderABC @@ -30,14 +31,18 @@ class Server(TableABC): @property @ServiceProviderABC.inject - def name(self, bot: DiscordBotServiceABC) -> str: + def name(self, bot: DiscordBotServiceABC) -> Optional[str]: guild = bot.get_guild(self.discord_id) + if guild is None: + return None return None if guild is None else guild.name @property @ServiceProviderABC.inject - def icon_url(self, bot: DiscordBotServiceABC) -> str: + def icon_url(self, bot: DiscordBotServiceABC) -> Optional[str]: guild = bot.get_guild(self.discord_id) + if guild is None: + return None return None if guild is None else guild.icon.url @staticmethod diff --git a/bot/src/bot_data/model/server_history.py b/bot/src/bot_data/model/server_history.py index e8c95934..f34ab64e 100644 --- a/bot/src/bot_data/model/server_history.py +++ b/bot/src/bot_data/model/server_history.py @@ -1,3 +1,5 @@ +from typing import Optional + from cpl_core.dependency_injection import ServiceProviderABC from cpl_discord.service import DiscordBotServiceABC @@ -32,12 +34,16 @@ class ServerHistory(HistoryTableABC): @property @ServiceProviderABC.inject - def name(self, bot: DiscordBotServiceABC) -> str: + def name(self, bot: DiscordBotServiceABC) -> Optional[str]: guild = bot.get_guild(self.discord_id) + if guild is None: + return None return None if guild is None else guild.name @property @ServiceProviderABC.inject - def icon_url(self, bot: DiscordBotServiceABC) -> str: + def icon_url(self, bot: DiscordBotServiceABC) -> Optional[str]: guild = bot.get_guild(self.discord_id) + if guild is None: + return None return None if guild is None else guild.icon.url diff --git a/bot/src/bot_data/model/short_role_name.py b/bot/src/bot_data/model/short_role_name.py index 4ddef4f1..d987f062 100644 --- a/bot/src/bot_data/model/short_role_name.py +++ b/bot/src/bot_data/model/short_role_name.py @@ -1,4 +1,5 @@ from datetime import datetime +from typing import Optional from cpl_core.database import TableABC from cpl_core.dependency_injection import ServiceProviderABC @@ -51,8 +52,10 @@ class ShortRoleName(TableABC): @property @ServiceProviderABC.inject - def role_name(self, bot: DiscordBotServiceABC) -> str: + def role_name(self, bot: DiscordBotServiceABC) -> Optional[str]: guild = bot.get_guild(self._server.discord_id) + if guild is None: + return None return guild.get_role(self.role_id).name @property diff --git a/bot/src/bot_data/model/short_role_name_history.py b/bot/src/bot_data/model/short_role_name_history.py index 90bbdc00..9c718af3 100644 --- a/bot/src/bot_data/model/short_role_name_history.py +++ b/bot/src/bot_data/model/short_role_name_history.py @@ -1,3 +1,5 @@ +from typing import Optional + from cpl_core.dependency_injection import ServiceProviderABC from cpl_discord.service import DiscordBotServiceABC @@ -47,8 +49,10 @@ class ShortRoleNameHistory(HistoryTableABC): @property @ServiceProviderABC.inject - def role_name(self, bot: DiscordBotServiceABC) -> str: + def role_name(self, bot: DiscordBotServiceABC) -> Optional[str]: guild = bot.get_guild(self._server.discord_id) + if guild is None: + return None return guild.get_role(self.role_id).name @property diff --git a/bot/src/bot_data/model/user.py b/bot/src/bot_data/model/user.py index e9c3b30b..1343df1b 100644 --- a/bot/src/bot_data/model/user.py +++ b/bot/src/bot_data/model/user.py @@ -53,15 +53,19 @@ class User(TableABC): @property @ServiceProviderABC.inject - def name(self, bot: DiscordBotServiceABC) -> str: + def name(self, bot: DiscordBotServiceABC) -> Optional[str]: guild = bot.get_guild(self.server.discord_id) + if guild is None: + return None user = guild.get_member(self.discord_id) return None if user is None else user.name @property @ServiceProviderABC.inject - def icon_url(self, bot: DiscordBotServiceABC) -> str: + def icon_url(self, bot: DiscordBotServiceABC) -> Optional[str]: guild = bot.get_guild(self.server.discord_id) + if guild is None: + return None user = guild.get_member(self.discord_id) return None if user is None else user.display_icon @@ -137,7 +141,7 @@ class User(TableABC): @property @ServiceProviderABC.inject - def level(self, services: ServiceProviderABC) -> Level: + def level(self, services: ServiceProviderABC) -> Optional[Level]: from modules.level.service.level_service import LevelService levels: LevelService = services.get_service(LevelService) @@ -175,8 +179,10 @@ class User(TableABC): @property @ServiceProviderABC.inject - def profile_picture_url(self, bot: DiscordBotServiceABC) -> str: + def profile_picture_url(self, bot: DiscordBotServiceABC) -> Optional[str]: guild = bot.get_guild(self.server.discord_id) + if guild is None: + return None user = guild.get_member(self._discord_id) return None if user is None or user.avatar is None else user.avatar.url diff --git a/bot/src/bot_graphql/filter/auto_role_rule_filter.py b/bot/src/bot_graphql/filter/auto_role_rule_filter.py index b9571d8f..f5360b5f 100644 --- a/bot/src/bot_graphql/filter/auto_role_rule_filter.py +++ b/bot/src/bot_graphql/filter/auto_role_rule_filter.py @@ -51,6 +51,8 @@ class AutoRoleRuleFilter(FilterABC): def get_role_name(x: AutoRoleRule): guild = self._bot.get_guild(x.auto_role.server.discord_id) + if guild is None: + return False name = guild.get_role(x.role_id).name return name == self._role_name or self._role_name in name diff --git a/bot/src/bot_graphql/filter/short_role_name_filter.py b/bot/src/bot_graphql/filter/short_role_name_filter.py index ea5a8bba..94b56153 100644 --- a/bot/src/bot_graphql/filter/short_role_name_filter.py +++ b/bot/src/bot_graphql/filter/short_role_name_filter.py @@ -57,6 +57,8 @@ class ShortRoleNameFilter(FilterABC): def get_role_name(x: ShortRoleName): guild = self._bot.get_guild(x.server.discord_id) + if guild is None: + return False name = guild.get_role(x.role_id).name return name == self._role_name or self._role_name in name diff --git a/bot/src/bot_graphql/filter/user_filter.py b/bot/src/bot_graphql/filter/user_filter.py index 53c130d5..90909de6 100644 --- a/bot/src/bot_graphql/filter/user_filter.py +++ b/bot/src/bot_graphql/filter/user_filter.py @@ -80,6 +80,8 @@ class UserFilter(FilterABC): def _get_member(user: User): guild = self._bot.get_guild(user.server.discord_id) + if guild is None: + return False member = guild.get_member(user.discord_id) return member is not None and (member.name == self._name or self._name in member.name) diff --git a/bot/src/bot_graphql/queries/server_query.py b/bot/src/bot_graphql/queries/server_query.py index 112a4555..355b6576 100644 --- a/bot/src/bot_graphql/queries/server_query.py +++ b/bot/src/bot_graphql/queries/server_query.py @@ -8,7 +8,6 @@ from bot_data.abc.achievement_repository_abc import AchievementRepositoryABC from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC -from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.scheduled_event_repository_abc import ScheduledEventRepositoryABC from bot_data.abc.server_config_repository_abc import ServerConfigRepositoryABC from bot_data.abc.short_role_name_repository_abc import ShortRoleNameRepositoryABC @@ -31,6 +30,7 @@ from bot_graphql.filter.short_role_name_filter import ShortRoleNameFilter from bot_graphql.filter.user_filter import UserFilter from bot_graphql.filter.user_warning_filter import UserWarningFilter from bot_graphql.model.server_statistics import ServerStatistics +from modules.level.service.level_service import LevelService class ServerQuery(DataQueryWithHistoryABC): @@ -41,7 +41,7 @@ class ServerQuery(DataQueryWithHistoryABC): db: DatabaseContextABC, auto_roles: AutoRoleRepositoryABC, clients: ClientRepositoryABC, - levels: LevelRepositoryABC, + levels: LevelService, game_servers: GameServerRepositoryABC, users: UserRepositoryABC, ujs: UserJoinedServerRepositoryABC, diff --git a/bot/src/modules/level/service/level_service.py b/bot/src/modules/level/service/level_service.py index 8f05d4d5..821d82ab 100644 --- a/bot/src/modules/level/service/level_service.py +++ b/bot/src/modules/level/service/level_service.py @@ -1,9 +1,12 @@ +from typing import Optional + import discord from cpl_core.configuration import ConfigurationABC 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 cpl_query.extension import List from cpl_translation import TranslatePipe from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum @@ -40,13 +43,26 @@ class LevelService: self._message_service = message_service self._t = t - def _check_for_feature(self, user: User): - server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{user.server.discord_id}") + def _check_for_feature(self, discord_id: int): + server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{discord_id}") if not FeatureFlagsSettings.get_flag_from_dict(server_config.feature_flags, FeatureFlagsEnum.level_module): raise Exception(f"Feature {FeatureFlagsEnum.level_module.value} disabled") - def get_level(self, user: User) -> Level: - self._check_for_feature(user) + def get_levels_by_server_id(self, server_id: int) -> List[Level]: + try: + self._check_for_feature(self._servers.get_server_by_id(server_id).discord_id) + except Exception as e: + self._logger.warn(__name__, f"Feature {FeatureFlagsEnum.level_module.value} disabled\n{e}") + return List(Level) + + return self._levels.get_levels() + + def get_level(self, user: User) -> Optional[Level]: + try: + self._check_for_feature(user.server.discord_id) + except Exception as e: + self._logger.warn(__name__, f"Feature {FeatureFlagsEnum.level_module.value} disabled\n{e}") + return None levels_by_server = self._levels.get_levels_by_server_id(user.server.id) if user.xp < 0: @@ -60,7 +76,7 @@ class LevelService: return levels.last() async def set_level(self, user: User): - self._check_for_feature(user) + self._check_for_feature(user.server.discord_id) level_names = self._levels.get_levels_by_server_id(user.server.id).select(lambda l: l.name) guild: Guild = self._bot.guilds.where(lambda g: g.id == user.server.discord_id).single() member: Member = guild.members.where(lambda m: m.id == user.discord_id).single() diff --git a/web/package.json b/web/package.json index e9c46ef6..7e1d96e5 100644 --- a/web/package.json +++ b/web/package.json @@ -1,56 +1,56 @@ { - "name": "web", - "version": "1.2.7", - "scripts": { - "ng": "ng", - "update-version": "ts-node update-version.ts", - "prestart": "npm run update-version", - "start": "ng serve", - "prebuild": "npm run update-version", - "build": "ng build", - "watch": "ng build --watch --configuration development", - "test": "ng test", - "gv": "echo $npm_package_version", - "predocker-build": "npm run update-version", - "docker-build": "export VERSION=$npm_package_version; ng build; docker build -t sh-edraft.de/sdb-web:$VERSION .", - "docker-build-dev": "export VERSION=$npm_package_version; ng build --configuration development; docker build -t sh-edraft.de/sdb-web:$VERSION .", - "docker-build-stage": "export VERSION=$npm_package_version; ng build --configuration staging; docker build -t sh-edraft.de/sdb-web:$VERSION ." - }, - "private": true, - "dependencies": { - "@angular/animations": "^15.1.4", - "@angular/common": "^15.1.4", - "@angular/compiler": "^15.1.4", - "@angular/core": "^15.1.4", - "@angular/forms": "^15.1.4", - "@angular/platform-browser": "^15.1.4", - "@angular/platform-browser-dynamic": "^15.1.4", - "@angular/router": "^15.1.4", - "@auth0/angular-jwt": "^5.1.0", - "@microsoft/signalr": "^6.0.9", - "@ngx-translate/core": "^14.0.0", - "@ngx-translate/http-loader": "^7.0.0", - "@types/socket.io-client": "^3.0.0", - "moment": "^2.29.4", - "primeicons": "^6.0.1", - "primeng": "^15.2.0", - "rxjs": "~7.5.0", - "socket.io-client": "^4.5.3", - "zone.js": "~0.11.4" - }, - "devDependencies": { - "@angular-devkit/build-angular": "^15.1.5", - "@angular/cli": "~15.1.5", - "@angular/compiler-cli": "^15.1.4", - "@types/jasmine": "~4.0.0", - "@types/node": "^18.11.9", - "jasmine-core": "~4.1.0", - "karma": "~6.3.0", - "karma-chrome-launcher": "~3.1.0", - "karma-coverage": "~2.2.0", - "karma-jasmine": "~5.0.0", - "karma-jasmine-html-reporter": "~1.7.0", - "tslib": "^2.4.1", - "typescript": "~4.9.5" - } -} + "name": "web", + "version": "1.2.7", + "scripts": { + "ng": "ng", + "update-version": "ts-node update-version.ts", + "prestart": "npm run update-version", + "start": "ng serve", + "prebuild": "npm run update-version", + "build": "ng build", + "watch": "ng build --watch --configuration development", + "test": "ng test", + "gv": "echo $npm_package_version", + "predocker-build": "npm run update-version", + "docker-build": "export VERSION=$npm_package_version; ng build; docker build -t sh-edraft.de/sdb-web:$VERSION .", + "docker-build-dev": "export VERSION=$npm_package_version; ng build --configuration development; docker build -t sh-edraft.de/sdb-web:$VERSION .", + "docker-build-stage": "export VERSION=$npm_package_version; ng build --configuration staging; docker build -t sh-edraft.de/sdb-web:$VERSION ." + }, + "private": true, + "dependencies": { + "@angular/animations": "^15.1.4", + "@angular/common": "^15.1.4", + "@angular/compiler": "^15.1.4", + "@angular/core": "^15.1.4", + "@angular/forms": "^15.1.4", + "@angular/platform-browser": "^15.1.4", + "@angular/platform-browser-dynamic": "^15.1.4", + "@angular/router": "^15.1.4", + "@auth0/angular-jwt": "^5.1.0", + "@microsoft/signalr": "^6.0.9", + "@ngx-translate/core": "^14.0.0", + "@ngx-translate/http-loader": "^7.0.0", + "@types/socket.io-client": "^3.0.0", + "moment": "^2.29.4", + "primeicons": "^6.0.1", + "primeng": "^15.2.0", + "rxjs": "~7.5.0", + "socket.io-client": "^4.5.3", + "zone.js": "~0.11.4" + }, + "devDependencies": { + "@angular-devkit/build-angular": "^15.1.5", + "@angular/cli": "~15.1.5", + "@angular/compiler-cli": "^15.1.4", + "@types/jasmine": "~4.0.0", + "@types/node": "^18.11.9", + "jasmine-core": "~4.1.0", + "karma": "~6.3.0", + "karma-chrome-launcher": "~3.1.0", + "karma-coverage": "~2.2.0", + "karma-jasmine": "~5.0.0", + "karma-jasmine-html-reporter": "~1.7.0", + "tslib": "^2.4.1", + "typescript": "~4.9.5" + } +} \ No newline at end of file diff --git a/web/src/app/modules/view/server/members/members.component.html b/web/src/app/modules/view/server/members/members.component.html index da87353e..e060c58e 100644 --- a/web/src/app/modules/view/server/members/members.component.html +++ b/web/src/app/modules/view/server/members/members.component.html @@ -1,5 +1,5 @@