From dff7cc4ca5ae5b1a6e5d3fc08e8221efeb2e9995 Mon Sep 17 00:00:00 2001 From: Alero Date: Fri, 14 Feb 2025 20:34:31 +0800 Subject: [PATCH] feat: when custom filter cant pass, won't raise error anymore. and when you use a command group and dont have custom filter access, the return group tree wont contain the command that you dont have permisson. --- astrbot/core/star/filter/command.py | 12 +++-- astrbot/core/star/filter/command_group.py | 62 ++++++++++++++++------- 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/astrbot/core/star/filter/command.py b/astrbot/core/star/filter/command.py index 41fdb61dc..d34d19a00 100644 --- a/astrbot/core/star/filter/command.py +++ b/astrbot/core/star/filter/command.py @@ -8,6 +8,7 @@ from astrbot.core.config import AstrBotConfig from astrbot.core.utils.param_validation_mixin import ParameterValidationMixin from .custom_filter import CustomFilter from ..star_handler import StarHandlerMetadata +from ... import logger # 标准指令受到 wake_prefix 的制约。 class CommandFilter(HandlerFilter, ParameterValidationMixin): @@ -48,13 +49,18 @@ class CommandFilter(HandlerFilter, ParameterValidationMixin): def add_custom_filter(self, custom_filter: CustomFilter): self.custom_filter_list.append(custom_filter) + def custom_filter_ok(self, event: AstrMessageEvent, cfg: AstrBotConfig) -> bool: + for custom_filter in self.custom_filter_list: + if not custom_filter.filter(event, cfg): + return False + return True + def filter(self, event: AstrMessageEvent, cfg: AstrBotConfig) -> bool: if not event.is_at_or_wake_command: return False - for custom_filter in self.custom_filter_list: - if not custom_filter.filter(event, cfg): - raise ValueError(f"没有执行该指令(组)的权限\n") + if not self.custom_filter_ok(event, cfg): + return False if event.get_extra("parsing_command"): message_str = event.get_extra("parsing_command").strip() diff --git a/astrbot/core/star/filter/command_group.py b/astrbot/core/star/filter/command_group.py index 0bb298cca..e75efbfd5 100644 --- a/astrbot/core/star/filter/command_group.py +++ b/astrbot/core/star/filter/command_group.py @@ -8,6 +8,7 @@ from astrbot.core.platform.astr_message_event import AstrMessageEvent from astrbot.core.config import AstrBotConfig from .custom_filter import CustomFilter from ..star_handler import StarHandlerMetadata +from ... import logger # 指令组受到 wake_prefix 的制约。 class CommandGroupFilter(HandlerFilter): @@ -23,24 +24,52 @@ class CommandGroupFilter(HandlerFilter): self.custom_filter_list.append(custom_filter) # 以树的形式打印出来 - def print_cmd_tree(self, sub_command_filters: List[Union[CommandFilter, CommandGroupFilter]], prefix: str = "") -> str: + def print_cmd_tree(self, + sub_command_filters: List[Union[CommandFilter, + CommandGroupFilter]], + prefix: str = "", + event: AstrMessageEvent = None, + cfg: AstrBotConfig = None, + ) -> str: result = "" for sub_filter in sub_command_filters: if isinstance(sub_filter, CommandFilter): - cmd_th = sub_filter.print_types() - result += f"{prefix}├── {sub_filter.command_name}" - if cmd_th: - result += f" ({cmd_th})" + if event and cfg: + if sub_filter.custom_filter_ok(event, cfg): + permission_pass = True + else: + permission_pass = False else: - result += " (无参数指令)" - - result += "\n" + permission_pass = True + if permission_pass: + cmd_th = sub_filter.print_types() + result += f"{prefix}├── {sub_filter.command_name}" + if cmd_th: + result += f" ({cmd_th})" + else: + result += " (无参数指令)" + result += "\n" elif isinstance(sub_filter, CommandGroupFilter): - result += f"{prefix}├── {sub_filter.group_name}" - result += "\n" - result += sub_filter.print_cmd_tree(sub_filter.sub_command_filters, prefix+"│ ") + if event and cfg: + if sub_filter.custom_filter_ok(event, cfg): + permission_pass = True + else: + permission_pass = False + else: + permission_pass = True + if permission_pass: + result += f"{prefix}├── {sub_filter.group_name}" + result += "\n" + result += sub_filter.print_cmd_tree(sub_filter.sub_command_filters, prefix+"│ ", event=event, cfg=cfg) + return result - + + def custom_filter_ok(self, event: AstrMessageEvent, cfg: AstrBotConfig) -> bool: + for custom_filter in self.custom_filter_list: + if not custom_filter.filter(event, cfg): + return False + return True + def filter(self, event: AstrMessageEvent, cfg: AstrBotConfig) -> Tuple[bool, StarHandlerMetadata]: if not event.is_at_or_wake_command: return False, None @@ -64,13 +93,12 @@ class CommandGroupFilter(HandlerFilter): if parsing_command == "": # 当前还是指令组 - tree = self.group_name + "\n" + self.print_cmd_tree(self.sub_command_filters) + tree = self.group_name + "\n" + self.print_cmd_tree(self.sub_command_filters, event=event, cfg=cfg) raise ValueError(f"指令组 {self.group_name} 未填写完全。这个指令组下有如下指令:\n"+tree) # 判断当前指令组的自定义过滤器 - for custom_filter in self.custom_filter_list: - if not custom_filter.filter(event, cfg): - raise ValueError(f"没有执行该指令(组)的权限\n") + if not self.custom_filter_ok(event, cfg): + return False, None child_command_handler_md = None for sub_filter in self.sub_command_filters: @@ -83,5 +111,5 @@ class CommandGroupFilter(HandlerFilter): if ok: child_command_handler_md = handler return True, child_command_handler_md - tree = self.group_name + "\n" + self.print_cmd_tree(self.sub_command_filters) + tree = self.group_name + "\n" + self.print_cmd_tree(self.sub_command_filters, event=event, cfg=cfg) raise ValueError(f"指令组 {self.group_name} 下没有找到对应的指令。这个指令组下有如下指令:\n"+tree)