Guild & Level can be null
This commit is contained in:
parent
b361a7b685
commit
e785bddf2e
@ -1,4 +1,5 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from cpl_core.database import TableABC
|
from cpl_core.database import TableABC
|
||||||
from cpl_core.dependency_injection import ServiceProviderABC
|
from cpl_core.dependency_injection import ServiceProviderABC
|
||||||
@ -48,8 +49,10 @@ class AutoRoleRule(TableABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@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)
|
guild = bot.get_guild(self.auto_role.server.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return None
|
||||||
return guild.get_role(self.role_id).name
|
return guild.get_role(self.role_id).name
|
||||||
|
|
||||||
@role_id.setter
|
@role_id.setter
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
from cpl_core.dependency_injection import ServiceProviderABC
|
from cpl_core.dependency_injection import ServiceProviderABC
|
||||||
from cpl_discord.service import DiscordBotServiceABC
|
from cpl_discord.service import DiscordBotServiceABC
|
||||||
|
|
||||||
@ -43,6 +45,8 @@ class AutoRoleRuleHistory(HistoryTableABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@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)
|
guild = bot.get_guild(self.auto_role.server.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return None
|
||||||
return guild.get_role(self.role_id).name
|
return guild.get_role(self.role_id).name
|
||||||
|
@ -39,8 +39,10 @@ class Level(TableABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@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)
|
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()
|
role = List(Role, guild.roles).where(lambda x: x.name == self._name).first_or_default()
|
||||||
return None if role is None else role.icon
|
return None if role is None else role.icon
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from cpl_core.database import TableABC
|
from cpl_core.database import TableABC
|
||||||
from cpl_core.dependency_injection import ServiceProviderABC
|
from cpl_core.dependency_injection import ServiceProviderABC
|
||||||
@ -30,14 +31,18 @@ class Server(TableABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@ServiceProviderABC.inject
|
||||||
def name(self, bot: DiscordBotServiceABC) -> str:
|
def name(self, bot: DiscordBotServiceABC) -> Optional[str]:
|
||||||
guild = bot.get_guild(self.discord_id)
|
guild = bot.get_guild(self.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return None
|
||||||
return None if guild is None else guild.name
|
return None if guild is None else guild.name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@ServiceProviderABC.inject
|
||||||
def icon_url(self, bot: DiscordBotServiceABC) -> str:
|
def icon_url(self, bot: DiscordBotServiceABC) -> Optional[str]:
|
||||||
guild = bot.get_guild(self.discord_id)
|
guild = bot.get_guild(self.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return None
|
||||||
return None if guild is None else guild.icon.url
|
return None if guild is None else guild.icon.url
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
from cpl_core.dependency_injection import ServiceProviderABC
|
from cpl_core.dependency_injection import ServiceProviderABC
|
||||||
from cpl_discord.service import DiscordBotServiceABC
|
from cpl_discord.service import DiscordBotServiceABC
|
||||||
|
|
||||||
@ -32,12 +34,16 @@ class ServerHistory(HistoryTableABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@ServiceProviderABC.inject
|
||||||
def name(self, bot: DiscordBotServiceABC) -> str:
|
def name(self, bot: DiscordBotServiceABC) -> Optional[str]:
|
||||||
guild = bot.get_guild(self.discord_id)
|
guild = bot.get_guild(self.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return None
|
||||||
return None if guild is None else guild.name
|
return None if guild is None else guild.name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@ServiceProviderABC.inject
|
||||||
def icon_url(self, bot: DiscordBotServiceABC) -> str:
|
def icon_url(self, bot: DiscordBotServiceABC) -> Optional[str]:
|
||||||
guild = bot.get_guild(self.discord_id)
|
guild = bot.get_guild(self.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return None
|
||||||
return None if guild is None else guild.icon.url
|
return None if guild is None else guild.icon.url
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from cpl_core.database import TableABC
|
from cpl_core.database import TableABC
|
||||||
from cpl_core.dependency_injection import ServiceProviderABC
|
from cpl_core.dependency_injection import ServiceProviderABC
|
||||||
@ -51,8 +52,10 @@ class ShortRoleName(TableABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@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)
|
guild = bot.get_guild(self._server.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return None
|
||||||
return guild.get_role(self.role_id).name
|
return guild.get_role(self.role_id).name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
from cpl_core.dependency_injection import ServiceProviderABC
|
from cpl_core.dependency_injection import ServiceProviderABC
|
||||||
from cpl_discord.service import DiscordBotServiceABC
|
from cpl_discord.service import DiscordBotServiceABC
|
||||||
|
|
||||||
@ -47,8 +49,10 @@ class ShortRoleNameHistory(HistoryTableABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@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)
|
guild = bot.get_guild(self._server.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return None
|
||||||
return guild.get_role(self.role_id).name
|
return guild.get_role(self.role_id).name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -53,15 +53,19 @@ class User(TableABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@ServiceProviderABC.inject
|
||||||
def name(self, bot: DiscordBotServiceABC) -> str:
|
def name(self, bot: DiscordBotServiceABC) -> Optional[str]:
|
||||||
guild = bot.get_guild(self.server.discord_id)
|
guild = bot.get_guild(self.server.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return None
|
||||||
user = guild.get_member(self.discord_id)
|
user = guild.get_member(self.discord_id)
|
||||||
return None if user is None else user.name
|
return None if user is None else user.name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@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)
|
guild = bot.get_guild(self.server.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return None
|
||||||
user = guild.get_member(self.discord_id)
|
user = guild.get_member(self.discord_id)
|
||||||
return None if user is None else user.display_icon
|
return None if user is None else user.display_icon
|
||||||
|
|
||||||
@ -137,7 +141,7 @@ class User(TableABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@ServiceProviderABC.inject
|
||||||
def level(self, services: ServiceProviderABC) -> Level:
|
def level(self, services: ServiceProviderABC) -> Optional[Level]:
|
||||||
from modules.level.service.level_service import LevelService
|
from modules.level.service.level_service import LevelService
|
||||||
|
|
||||||
levels: LevelService = services.get_service(LevelService)
|
levels: LevelService = services.get_service(LevelService)
|
||||||
@ -175,8 +179,10 @@ class User(TableABC):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
@ServiceProviderABC.inject
|
@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)
|
guild = bot.get_guild(self.server.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return None
|
||||||
user = guild.get_member(self._discord_id)
|
user = guild.get_member(self._discord_id)
|
||||||
return None if user is None or user.avatar is None else user.avatar.url
|
return None if user is None or user.avatar is None else user.avatar.url
|
||||||
|
|
||||||
|
@ -51,6 +51,8 @@ class AutoRoleRuleFilter(FilterABC):
|
|||||||
|
|
||||||
def get_role_name(x: AutoRoleRule):
|
def get_role_name(x: AutoRoleRule):
|
||||||
guild = self._bot.get_guild(x.auto_role.server.discord_id)
|
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
|
name = guild.get_role(x.role_id).name
|
||||||
return name == self._role_name or self._role_name in name
|
return name == self._role_name or self._role_name in name
|
||||||
|
|
||||||
|
@ -57,6 +57,8 @@ class ShortRoleNameFilter(FilterABC):
|
|||||||
|
|
||||||
def get_role_name(x: ShortRoleName):
|
def get_role_name(x: ShortRoleName):
|
||||||
guild = self._bot.get_guild(x.server.discord_id)
|
guild = self._bot.get_guild(x.server.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return False
|
||||||
name = guild.get_role(x.role_id).name
|
name = guild.get_role(x.role_id).name
|
||||||
return name == self._role_name or self._role_name in name
|
return name == self._role_name or self._role_name in name
|
||||||
|
|
||||||
|
@ -80,6 +80,8 @@ class UserFilter(FilterABC):
|
|||||||
|
|
||||||
def _get_member(user: User):
|
def _get_member(user: User):
|
||||||
guild = self._bot.get_guild(user.server.discord_id)
|
guild = self._bot.get_guild(user.server.discord_id)
|
||||||
|
if guild is None:
|
||||||
|
return False
|
||||||
member = guild.get_member(user.discord_id)
|
member = guild.get_member(user.discord_id)
|
||||||
return member is not None and (member.name == self._name or self._name in member.name)
|
return member is not None and (member.name == self._name or self._name in member.name)
|
||||||
|
|
||||||
|
@ -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.auto_role_repository_abc import AutoRoleRepositoryABC
|
||||||
from bot_data.abc.client_repository_abc import ClientRepositoryABC
|
from bot_data.abc.client_repository_abc import ClientRepositoryABC
|
||||||
from bot_data.abc.game_server_repository_abc import GameServerRepositoryABC
|
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.scheduled_event_repository_abc import ScheduledEventRepositoryABC
|
||||||
from bot_data.abc.server_config_repository_abc import ServerConfigRepositoryABC
|
from bot_data.abc.server_config_repository_abc import ServerConfigRepositoryABC
|
||||||
from bot_data.abc.short_role_name_repository_abc import ShortRoleNameRepositoryABC
|
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_filter import UserFilter
|
||||||
from bot_graphql.filter.user_warning_filter import UserWarningFilter
|
from bot_graphql.filter.user_warning_filter import UserWarningFilter
|
||||||
from bot_graphql.model.server_statistics import ServerStatistics
|
from bot_graphql.model.server_statistics import ServerStatistics
|
||||||
|
from modules.level.service.level_service import LevelService
|
||||||
|
|
||||||
|
|
||||||
class ServerQuery(DataQueryWithHistoryABC):
|
class ServerQuery(DataQueryWithHistoryABC):
|
||||||
@ -41,7 +41,7 @@ class ServerQuery(DataQueryWithHistoryABC):
|
|||||||
db: DatabaseContextABC,
|
db: DatabaseContextABC,
|
||||||
auto_roles: AutoRoleRepositoryABC,
|
auto_roles: AutoRoleRepositoryABC,
|
||||||
clients: ClientRepositoryABC,
|
clients: ClientRepositoryABC,
|
||||||
levels: LevelRepositoryABC,
|
levels: LevelService,
|
||||||
game_servers: GameServerRepositoryABC,
|
game_servers: GameServerRepositoryABC,
|
||||||
users: UserRepositoryABC,
|
users: UserRepositoryABC,
|
||||||
ujs: UserJoinedServerRepositoryABC,
|
ujs: UserJoinedServerRepositoryABC,
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
|
from typing import Optional
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
from cpl_core.configuration import ConfigurationABC
|
from cpl_core.configuration import ConfigurationABC
|
||||||
from cpl_core.database.context import DatabaseContextABC
|
from cpl_core.database.context import DatabaseContextABC
|
||||||
from cpl_core.logging import LoggerABC
|
from cpl_core.logging import LoggerABC
|
||||||
from cpl_discord.container import Guild, Role, Member
|
from cpl_discord.container import Guild, Role, Member
|
||||||
from cpl_discord.service import DiscordBotServiceABC
|
from cpl_discord.service import DiscordBotServiceABC
|
||||||
|
from cpl_query.extension import List
|
||||||
from cpl_translation import TranslatePipe
|
from cpl_translation import TranslatePipe
|
||||||
|
|
||||||
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
|
from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum
|
||||||
@ -40,13 +43,26 @@ class LevelService:
|
|||||||
self._message_service = message_service
|
self._message_service = message_service
|
||||||
self._t = t
|
self._t = t
|
||||||
|
|
||||||
def _check_for_feature(self, user: User):
|
def _check_for_feature(self, discord_id: int):
|
||||||
server_config: ServerConfig = self._config.get_configuration(f"ServerConfig_{user.server.discord_id}")
|
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):
|
if not FeatureFlagsSettings.get_flag_from_dict(server_config.feature_flags, FeatureFlagsEnum.level_module):
|
||||||
raise Exception(f"Feature {FeatureFlagsEnum.level_module.value} disabled")
|
raise Exception(f"Feature {FeatureFlagsEnum.level_module.value} disabled")
|
||||||
|
|
||||||
def get_level(self, user: User) -> Level:
|
def get_levels_by_server_id(self, server_id: int) -> List[Level]:
|
||||||
self._check_for_feature(user)
|
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)
|
levels_by_server = self._levels.get_levels_by_server_id(user.server.id)
|
||||||
if user.xp < 0:
|
if user.xp < 0:
|
||||||
@ -60,7 +76,7 @@ class LevelService:
|
|||||||
return levels.last()
|
return levels.last()
|
||||||
|
|
||||||
async def set_level(self, user: User):
|
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)
|
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()
|
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()
|
member: Member = guild.members.where(lambda m: m.id == user.discord_id).single()
|
||||||
|
@ -251,7 +251,7 @@
|
|||||||
placeholder="{{'common.level' | translate}}"></p-dropdown>
|
placeholder="{{'common.level' | translate}}"></p-dropdown>
|
||||||
</ng-template>
|
</ng-template>
|
||||||
<ng-template pTemplate="output">
|
<ng-template pTemplate="output">
|
||||||
{{member.level.name}}
|
{{ member.level?.name }}
|
||||||
</ng-template>
|
</ng-template>
|
||||||
</p-cellEditor>
|
</p-cellEditor>
|
||||||
</td>
|
</td>
|
||||||
|
Loading…
Reference in New Issue
Block a user