Guild & Level can be null
All checks were successful
Deploy prod on push / pre-build (push) Successful in 2s
Deploy prod on push / build-bot (push) Successful in 3m19s
Deploy prod on push / build-web (push) Successful in 1m58s
Deploy prod on push / deploy (push) Successful in 23s

This commit is contained in:
Sven Heidemann 2024-09-06 19:39:50 +02:00
parent b361a7b685
commit e785bddf2e
17 changed files with 250 additions and 195 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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,

View File

@ -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()

View File

@ -251,7 +251,7 @@
placeholder="{{'common.level' | translate}}"></p-dropdown>
</ng-template>
<ng-template pTemplate="output">
{{member.level.name}}
{{ member.level?.name }}
</ng-template>
</p-cellEditor>
</td>