diff --git a/kdb-bot/src/bot_data/model/auto_role_rule.py b/kdb-bot/src/bot_data/model/auto_role_rule.py index 01d45ebe..fc79f622 100644 --- a/kdb-bot/src/bot_data/model/auto_role_rule.py +++ b/kdb-bot/src/bot_data/model/auto_role_rule.py @@ -1,13 +1,14 @@ from datetime import datetime -from typing import Optional from cpl_core.database import TableABC +from bot_data.model.auto_role import AutoRole + class AutoRoleRule(TableABC): def __init__( self, - auto_role_id: int, + auto_role: AutoRole, discord_emoji_name: str, discord_role_id: int, created_at: datetime = None, @@ -15,7 +16,7 @@ class AutoRoleRule(TableABC): id=0, ): self._auto_role_rule_id = id - self._auto_role_id = auto_role_id + self._auto_role = auto_role self._discord_emoji_name = discord_emoji_name self._discord_role_id = discord_role_id @@ -28,8 +29,8 @@ class AutoRoleRule(TableABC): return self._auto_role_rule_id @property - def auto_role_id(self) -> int: - return self._auto_role_id + def auto_role(self) -> AutoRole: + return self._auto_role @property def emoji_name(self) -> str: @@ -72,7 +73,7 @@ class AutoRoleRule(TableABC): INSERT INTO `AutoRoleRules` ( `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `CreatedAt`, `LastModifiedAt` ) VALUES ( - {self._auto_role_id}, + {self._auto_role}, '{self._discord_emoji_name}', {self._discord_role_id}, '{self._created_at}', @@ -86,7 +87,7 @@ class AutoRoleRule(TableABC): return str( f""" UPDATE `AutoRoleRules` - SET `AutoRoleId` = {self._auto_role_id}, + SET `AutoRoleId` = {self._auto_role}, `DiscordEmojiName` = {self._discord_emoji_name}, `DiscordRoleId` = {self._discord_role_id}, `LastModifiedAt` = '{self._modified_at}' diff --git a/kdb-bot/src/bot_data/service/auto_role_repository_service.py b/kdb-bot/src/bot_data/service/auto_role_repository_service.py index 1e23916d..d1599341 100644 --- a/kdb-bot/src/bot_data/service/auto_role_repository_service.py +++ b/kdb-bot/src/bot_data/service/auto_role_repository_service.py @@ -105,14 +105,20 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): self._logger.trace(__name__, f"Send SQL command: {AutoRoleRule.get_select_all_string()}") results = self._context.select(AutoRoleRule.get_select_all_string()) for result in results: - auto_role_rules.append(AutoRoleRule(result[1], result[2], result[3], result[4], result[5], id=result[0])) + auto_role_rules.append( + AutoRoleRule( + self.get_auto_role_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0] + ) + ) return auto_role_rules def get_auto_role_rule_by_id(self, id: int) -> AutoRoleRule: self._logger.trace(__name__, f"Send SQL command: {AutoRoleRule.get_select_by_id_string(id)}") result = self._context.select(AutoRoleRule.get_select_by_id_string(id))[0] - return AutoRoleRule(result[1], result[2], result[3], result[4], result[5], id=result[0]) + return AutoRoleRule( + self.get_auto_role_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0] + ) def get_auto_role_rules_by_auto_role_id(self, id: int) -> List[AutoRoleRule]: auto_role_rules = List(AutoRoleRule) @@ -122,7 +128,11 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): ) results = self._context.select(AutoRoleRule.get_select_by_auto_role_id_string(id)) for result in results: - auto_role_rules.append(AutoRoleRule(result[1], result[2], result[3], result[4], result[5], id=result[0])) + auto_role_rules.append( + AutoRoleRule( + self.get_auto_role_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0] + ) + ) return auto_role_rules diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_filter.py index 288aa68d..8badeea9 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_filter.py @@ -42,7 +42,7 @@ class AutoRoleFilter(FilterABC): def filter(self, query: List[AutoRole]) -> List[AutoRole]: if self._id is not None: - query = query.where(lambda x: x.id == self._id) + query = query.where(lambda x: x.auto_role_id == self._id) if self._channel_id is not None: query = query.where(lambda x: x.discord_channel_id == self._channel_id) diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py index a1912fd7..65e54eb0 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py @@ -1,3 +1,4 @@ +from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List from bot_data.model.auto_role_rule import AutoRoleRule @@ -5,13 +6,15 @@ from bot_graphql.abc.filter_abc import FilterABC class AutoRoleRuleFilter(FilterABC): - def __init__(self): + def __init__(self, bot: DiscordBotServiceABC): FilterABC.__init__(self) + self._bot = bot self._id = None self._emoji_name = None self._role_id = None - self._auto_role_id = None + self._role_name = None + self._auto_role = None def from_dict(self, values: dict): if "id" in values: @@ -23,23 +26,37 @@ class AutoRoleRuleFilter(FilterABC): if "role_id" in values: self._role_id = int(values["role_id"]) - if "auto_role_id" in values: - self._auto_role_id = int(values["auto_role_id"]) + if "role_name" in values: + self._role_name = values["role_name"] + + if "auto_role" in values: + from bot_graphql.filter.auto_role_filter import AutoRoleFilter + + auto_role = AutoRoleFilter(self._bot) + auto_role.from_dict(values["auto_role"]) + self._auto_role = auto_role def filter(self, query: List[AutoRoleRule]) -> List[AutoRoleRule]: if self._id is not None: query = query.where(lambda x: x.auto_role_rule_id == self._id) - if self._auto_role_id is not None: - query = query.where(lambda x: x.auto_role_id == self._auto_role_id) - if self._emoji_name is not None: query = query.where(lambda x: x.emoji_name == self._emoji_name) if self._role_id is not None: query = query.where(lambda x: x.role_id == self._role_id) - # if self._server_id is not None: - # query = query.where(lambda x: x.server.server_id == self._server_id) + if self._role_name is not None: + + def get_role_name(x: AutoRoleRule): + guild = self._bot.get_guild(x.auto_role.server.discord_server_id) + name = guild.get_role(x.role_id).name + return name == self._role_name or self._role_name in name + + query = query.where(get_role_name) + + if self._auto_role is not None: + auto_roles = self._auto_role.filter(query.select(lambda x: x.auto_role)).select(lambda x: x.auto_role_id) + query = query.where(lambda x: x.auto_role.auto_role_id in auto_roles) return query diff --git a/kdb-bot/src/bot_graphql/model/auto_role.gql b/kdb-bot/src/bot_graphql/model/auto_role.gql index 47ee4e81..345f0df9 100644 --- a/kdb-bot/src/bot_graphql/model/auto_role.gql +++ b/kdb-bot/src/bot_graphql/model/auto_role.gql @@ -6,8 +6,8 @@ type AutoRole implements TableQuery { server: Server - auto_role_rules: [AutoRoleRule] auto_role_rule_count: Int + auto_role_rules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRoleRule] created_at: String modified_at: String diff --git a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql index 27e06ad0..34c9ccdd 100644 --- a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql +++ b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql @@ -2,6 +2,7 @@ type AutoRoleRule implements TableQuery { id: ID emoji_name: String role_id: String + role_name: String auto_role: AutoRole @@ -13,5 +14,6 @@ input AutoRoleRuleFilter { id: ID emoji_name: String role_id: String + role_name: String auto_role: AutoRoleFilter } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/query.gql b/kdb-bot/src/bot_graphql/model/query.gql index 665c72e0..02a63479 100644 --- a/kdb-bot/src/bot_graphql/model/query.gql +++ b/kdb-bot/src/bot_graphql/model/query.gql @@ -2,7 +2,7 @@ type Query { auto_roles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole] auto_role_count: Int - auto_role_rules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRole] + auto_role_rules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRoleRule] auto_role_rule_count: Int clients(filter: ClientFilter, page: Page, sort: Sort): [Client] diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py index 1cbd870c..943e4d8e 100644 --- a/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py +++ b/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py @@ -1,3 +1,5 @@ +from cpl_discord.service import DiscordBotServiceABC + from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.model.auto_role_rule import AutoRoleRule from bot_graphql.abc.data_query_abc import DataQueryABC @@ -6,15 +8,18 @@ from bot_graphql.abc.data_query_abc import DataQueryABC class AutoRoleRuleQuery(DataQueryABC): def __init__( self, + bot: DiscordBotServiceABC, auto_roles: AutoRoleRepositoryABC, ): DataQueryABC.__init__(self, "AutoRoleRule") + self._bot = bot self._auto_roles = auto_roles self.set_field("id", self.resolve_id) self.set_field("emoji_name", self.resolve_emoji_name) self.set_field("role_id", self.resolve_role_id) + self.set_field("role_name", self.resolve_role_name) self.set_field("auto_role", self.resolve_auto_role) @staticmethod @@ -29,5 +34,9 @@ class AutoRoleRuleQuery(DataQueryABC): def resolve_role_id(x: AutoRoleRule, *_): return x.role_id + def resolve_role_name(self, x: AutoRoleRule, *_): + guild = self._bot.get_guild(x.auto_role.server.discord_server_id) + return guild.get_role(x.role_id).name + def resolve_auto_role(self, x: AutoRoleRule, *_): - return self._auto_roles.get_auto_role_by_id(x.auto_role_id) + return self._auto_roles.get_auto_role_by_id(x.auto_role.auto_role_id)