-
-
Notifications
You must be signed in to change notification settings - Fork 930
feat: 一个指令只允许一个handler处理, 匹配最长指令序列, 同时兼容前缀指令, 增加指令冲突检查 #2746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
3cfca78
65a3774
dc2e81a
392fbfc
b8a90df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,6 @@ | ||||||||||||||||||||||
import re | ||||||||||||||||||||||
from collections import defaultdict | ||||||||||||||||||||||
from typing import AsyncGenerator, Union | ||||||||||||||||||||||
|
||||||||||||||||||||||
from astrbot import logger | ||||||||||||||||||||||
from astrbot.core.message.components import At, AtAll, Reply | ||||||||||||||||||||||
from astrbot.core.message.message_event_result import MessageChain, MessageEventResult | ||||||||||||||||||||||
|
@@ -8,7 +9,6 @@ | |||||||||||||||||||||
from astrbot.core.star.session_plugin_manager import SessionPluginManager | ||||||||||||||||||||||
from astrbot.core.star.star import star_map | ||||||||||||||||||||||
from astrbot.core.star.star_handler import EventType, star_handlers_registry | ||||||||||||||||||||||
|
||||||||||||||||||||||
from ..context import PipelineContext | ||||||||||||||||||||||
from ..stage import Stage, register_stage | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -46,6 +46,220 @@ async def initialize(self, ctx: PipelineContext) -> None: | |||||||||||||||||||||
"ignore_at_all", False | ||||||||||||||||||||||
) | ||||||||||||||||||||||
|
||||||||||||||||||||||
def _find_command_filter(self, handler): | ||||||||||||||||||||||
"""查找command filter | ||||||||||||||||||||||
|
||||||||||||||||||||||
Args: | ||||||||||||||||||||||
handler (StarHandlerMetadata): handler 元数据对象 | ||||||||||||||||||||||
Returns: | ||||||||||||||||||||||
CommandFilter | None: 找到则返回 CommandFilter 对象,否则返回 None | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
for f in handler.event_filters: | ||||||||||||||||||||||
if hasattr(f, "command_name"): | ||||||||||||||||||||||
return f | ||||||||||||||||||||||
return None | ||||||||||||||||||||||
|
||||||||||||||||||||||
async def _find_best_command_handlers(self, command_handlers, event): | ||||||||||||||||||||||
"""查找最长匹配的handler | ||||||||||||||||||||||
|
||||||||||||||||||||||
Args: | ||||||||||||||||||||||
command_handlers (List[Tuple[StarHandlerMetadata, CommandFilter]]): 候选的指令 handler 列表 | ||||||||||||||||||||||
event (AstrMessageEvent): 消息事件对象 | ||||||||||||||||||||||
Returns: | ||||||||||||||||||||||
StarHandlerMetadata | None: 找到则返回 handler 元数据对象,否则返回 None | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
|
||||||||||||||||||||||
if not event.is_at_or_wake_command: | ||||||||||||||||||||||
return None | ||||||||||||||||||||||
|
||||||||||||||||||||||
message_str = re.sub(r"\s+", " ", event.get_message_str().strip()) | ||||||||||||||||||||||
best_match = None | ||||||||||||||||||||||
best_length = 0 | ||||||||||||||||||||||
best_params = None | ||||||||||||||||||||||
|
||||||||||||||||||||||
# 找到所有可能的匹配 | ||||||||||||||||||||||
for handler, command_filter in command_handlers: | ||||||||||||||||||||||
match_result = self._match_command(command_filter, message_str, event) | ||||||||||||||||||||||
if match_result: | ||||||||||||||||||||||
Comment on lines
+82
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): 使用命名表达式简化赋值和条件 (
Suggested change
Original comment in Englishsuggestion (code-quality): Use named expression to simplify assignment and conditional (
Suggested change
|
||||||||||||||||||||||
match_length, parsed_params = match_result | ||||||||||||||||||||||
if match_length > best_length: | ||||||||||||||||||||||
best_length = match_length | ||||||||||||||||||||||
best_match = handler | ||||||||||||||||||||||
best_params = parsed_params | ||||||||||||||||||||||
|
||||||||||||||||||||||
if best_match: | ||||||||||||||||||||||
# 解析的参数 | ||||||||||||||||||||||
if best_params is not None: | ||||||||||||||||||||||
event.set_extra("parsed_params", best_params) | ||||||||||||||||||||||
|
||||||||||||||||||||||
# 还需要执行完整的filter检查 | ||||||||||||||||||||||
result = await self._check_handler_filters(best_match, event) | ||||||||||||||||||||||
if result == "activate": | ||||||||||||||||||||||
return best_match | ||||||||||||||||||||||
elif result == "permission_error": | ||||||||||||||||||||||
raise PermissionError("权限不足") | ||||||||||||||||||||||
|
||||||||||||||||||||||
return None | ||||||||||||||||||||||
|
||||||||||||||||||||||
def _match_command(self, command_filter, message_str, event): | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): 考虑提取一个辅助函数来生成所有完整的命令字符串,并重构命令匹配和冲突检测以使用它,从而减少重复。 这里有两个小的重构,可以在不改变行为的情况下,折叠
def _full_command_list(command_filter):
parts = []
for base in [command_filter.command_name] + list(command_filter.alias):
for parent in command_filter.parent_command_names:
parts.append(f"{parent} {base}" if parent else base)
return parts
import re
def _match_command(self, command_filter, message_str, event):
if not command_filter.custom_filter_ok(event, self.ctx.astrbot_config):
return None
commands = _full_command_list(command_filter)
# build: ^(?:cmd1|cmd2)(?:\s+(.*))?$
pat = re.compile(rf"^(?:{'|'.join(map(re.escape, commands))})(?:\s+(.*))?$")
m = pat.match(message_str)
if not m:
return None
raw_args = m.group(1) or ""
params = raw_args.split() if raw_args.strip() else []
try:
parsed = command_filter.validate_and_convert_params(params, command_filter.handler_params)
return (len(m.group(0).split()[0]), parsed)
except ValueError:
return None
def _detect_command_conflicts(self, command_handlers):
command_map = defaultdict(list)
for handler, cf in command_handlers:
plugin = star_map.get(handler.handler_module_path, {}).name or "unknown"
for full_cmd in _full_command_list(cf):
command_map[full_cmd].append((handler, plugin))
for cmd, lst in command_map.items():
if len(lst) > 1:
logger.warning(f"冲突: '{cmd}' -> " +
", ".join(f"{p}.{h.handler_full_name}" for h, p in lst)) 这些更改
—同时保持了相同的行为。 Original comment in Englishissue (complexity): Consider extracting a helper to generate all full command strings and refactoring command matching and conflict detection to use it for reduced duplication. Here are two small refactorings that can collapse the nested loops and repeated logic in both
def _full_command_list(command_filter):
parts = []
for base in [command_filter.command_name] + list(command_filter.alias):
for parent in command_filter.parent_command_names:
parts.append(f"{parent} {base}" if parent else base)
return parts
import re
def _match_command(self, command_filter, message_str, event):
if not command_filter.custom_filter_ok(event, self.ctx.astrbot_config):
return None
commands = _full_command_list(command_filter)
# build: ^(?:cmd1|cmd2)(?:\s+(.*))?$
pat = re.compile(rf"^(?:{'|'.join(map(re.escape, commands))})(?:\s+(.*))?$")
m = pat.match(message_str)
if not m:
return None
raw_args = m.group(1) or ""
params = raw_args.split() if raw_args.strip() else []
try:
parsed = command_filter.validate_and_convert_params(params, command_filter.handler_params)
return (len(m.group(0).split()[0]), parsed)
except ValueError:
return None
def _detect_command_conflicts(self, command_handlers):
command_map = defaultdict(list)
for handler, cf in command_handlers:
plugin = star_map.get(handler.handler_module_path, {}).name or "unknown"
for full_cmd in _full_command_list(cf):
command_map[full_cmd].append((handler, plugin))
for cmd, lst in command_map.items():
if len(lst) > 1:
logger.warning(f"冲突: '{cmd}' -> " +
", ".join(f"{p}.{h.handler_full_name}" for h, p in lst)) These two changes
—while keeping identical behavior. |
||||||||||||||||||||||
"""匹配指令 | ||||||||||||||||||||||
|
||||||||||||||||||||||
Args: | ||||||||||||||||||||||
command_filter (CommandFilter): 指令过滤器对象 | ||||||||||||||||||||||
message_str (str): 消息字符串 | ||||||||||||||||||||||
event (AstrMessageEvent): 消息事件对象 | ||||||||||||||||||||||
Returns: | ||||||||||||||||||||||
Tuple[int, dict] | None: 如果匹配成功,返回 (匹配长度, 解析参数);否则返回 None | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
# 检查自定义过滤器 | ||||||||||||||||||||||
if not command_filter.custom_filter_ok(event, self.ctx.astrbot_config): | ||||||||||||||||||||||
return None | ||||||||||||||||||||||
|
||||||||||||||||||||||
candidates = [command_filter.command_name] + list(command_filter.alias) | ||||||||||||||||||||||
|
||||||||||||||||||||||
for candidate in candidates: | ||||||||||||||||||||||
for parent_command_name in command_filter.parent_command_names: | ||||||||||||||||||||||
if parent_command_name: | ||||||||||||||||||||||
full_command = f"{parent_command_name} {candidate}" | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
full_command = candidate | ||||||||||||||||||||||
|
||||||||||||||||||||||
matched_params = None | ||||||||||||||||||||||
if message_str == full_command: | ||||||||||||||||||||||
# 完全匹配,无参数 | ||||||||||||||||||||||
try: | ||||||||||||||||||||||
matched_params = command_filter.validate_and_convert_params( | ||||||||||||||||||||||
[], command_filter.handler_params | ||||||||||||||||||||||
) | ||||||||||||||||||||||
return (len(full_command), matched_params) | ||||||||||||||||||||||
except ValueError: | ||||||||||||||||||||||
continue | ||||||||||||||||||||||
elif message_str.startswith(full_command + " "): | ||||||||||||||||||||||
# 前缀匹配,有参数 | ||||||||||||||||||||||
param_str = message_str[len(full_command) :].strip() | ||||||||||||||||||||||
params_list = [p for p in param_str.split(" ") if p] | ||||||||||||||||||||||
try: | ||||||||||||||||||||||
matched_params = command_filter.validate_and_convert_params( | ||||||||||||||||||||||
params_list, command_filter.handler_params | ||||||||||||||||||||||
) | ||||||||||||||||||||||
return (len(full_command), matched_params) | ||||||||||||||||||||||
except ValueError: | ||||||||||||||||||||||
continue | ||||||||||||||||||||||
elif message_str.startswith(full_command) and len(message_str) > len( | ||||||||||||||||||||||
full_command | ||||||||||||||||||||||
): | ||||||||||||||||||||||
# 前缀匹配 忘了加空格的情况 | ||||||||||||||||||||||
param_str = message_str[len(full_command) :] | ||||||||||||||||||||||
# 将整个剩余部分作为一个参数 | ||||||||||||||||||||||
params_list = [param_str] if param_str else [] | ||||||||||||||||||||||
try: | ||||||||||||||||||||||
matched_params = command_filter.validate_and_convert_params( | ||||||||||||||||||||||
params_list, command_filter.handler_params | ||||||||||||||||||||||
) | ||||||||||||||||||||||
return (len(full_command), matched_params) | ||||||||||||||||||||||
except ValueError: | ||||||||||||||||||||||
continue | ||||||||||||||||||||||
|
||||||||||||||||||||||
return None | ||||||||||||||||||||||
|
||||||||||||||||||||||
async def _check_handler_filters(self, handler, event): | ||||||||||||||||||||||
"""检查处理器的所有过滤器 | ||||||||||||||||||||||
|
||||||||||||||||||||||
Args: | ||||||||||||||||||||||
handler (StarHandlerMetadata): handler 元数据对象 | ||||||||||||||||||||||
event (AstrMessageEvent): 消息事件对象 | ||||||||||||||||||||||
Returns: | ||||||||||||||||||||||
"activate": 通过所有检查,可以激活 | ||||||||||||||||||||||
"skip": 跳过(权限不足但不报错,或其他过滤器不通过) | ||||||||||||||||||||||
"permission_error": 权限不足且需要报错 | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
permission_not_pass = False | ||||||||||||||||||||||
permission_filter_raise_error = False | ||||||||||||||||||||||
|
||||||||||||||||||||||
for filter_obj in handler.event_filters: | ||||||||||||||||||||||
try: | ||||||||||||||||||||||
if isinstance(filter_obj, PermissionTypeFilter): | ||||||||||||||||||||||
if not filter_obj.filter(event, self.ctx.astrbot_config): | ||||||||||||||||||||||
permission_not_pass = True | ||||||||||||||||||||||
permission_filter_raise_error = filter_obj.raise_error | ||||||||||||||||||||||
elif hasattr(filter_obj, "command_name"): | ||||||||||||||||||||||
# 不需要再检查 command filter | ||||||||||||||||||||||
continue | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
Comment on lines
+179
to
+188
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): 过滤器检查中的异常处理可能会掩盖底层问题。 记录带有堆栈跟踪的异常将有助于识别过滤器实现中的问题。对于关键错误,请考虑重新抛出而不是仅仅通知用户。 Original comment in Englishsuggestion (bug_risk): Exception handling in filter checks may mask underlying issues. Logging exceptions with traceback will help identify issues in filter implementations. For critical errors, consider re-raising instead of only notifying the user. |
||||||||||||||||||||||
if not filter_obj.filter(event, self.ctx.astrbot_config): | ||||||||||||||||||||||
return "skip" | ||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||
await event.send( | ||||||||||||||||||||||
MessageEventResult().message( | ||||||||||||||||||||||
f"插件 {star_map[handler.handler_module_path].name}: {e}" | ||||||||||||||||||||||
) | ||||||||||||||||||||||
) | ||||||||||||||||||||||
event.stop_event() | ||||||||||||||||||||||
return "skip" | ||||||||||||||||||||||
|
||||||||||||||||||||||
# 处理权限检查结果 | ||||||||||||||||||||||
if permission_not_pass: | ||||||||||||||||||||||
if not permission_filter_raise_error: | ||||||||||||||||||||||
return "skip" | ||||||||||||||||||||||
|
||||||||||||||||||||||
if self.no_permission_reply: | ||||||||||||||||||||||
await event.send( | ||||||||||||||||||||||
MessageChain().message( | ||||||||||||||||||||||
f"您(ID: {event.get_sender_id()})的权限不足以使用此指令。通过 /sid 获取 ID 并请管理员添加。" | ||||||||||||||||||||||
) | ||||||||||||||||||||||
) | ||||||||||||||||||||||
logger.info( | ||||||||||||||||||||||
f"触发 {star_map[handler.handler_module_path].name} 时, 用户(ID={event.get_sender_id()}) 权限不足。" | ||||||||||||||||||||||
) | ||||||||||||||||||||||
return "permission_error" | ||||||||||||||||||||||
|
||||||||||||||||||||||
return "activate" | ||||||||||||||||||||||
|
||||||||||||||||||||||
def _detect_command_conflicts(self, command_handlers): | ||||||||||||||||||||||
"""检测指令冲突 | ||||||||||||||||||||||
|
||||||||||||||||||||||
Args: | ||||||||||||||||||||||
command_handlers (List[Tuple[StarHandlerMetadata, CommandFilter]]): 候选的指令 handler 列表 | ||||||||||||||||||||||
""" | ||||||||||||||||||||||
# 完整指令名 -> [(handler, plugin_name)] 映射 | ||||||||||||||||||||||
command_map = defaultdict(list) | ||||||||||||||||||||||
|
||||||||||||||||||||||
for handler, command_filter in command_handlers: | ||||||||||||||||||||||
star_metadata = star_map.get(handler.handler_module_path) | ||||||||||||||||||||||
if star_metadata: | ||||||||||||||||||||||
Comment on lines
+228
to
+229
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): 使用命名表达式简化赋值和条件 (
Suggested change
Original comment in Englishsuggestion (code-quality): Use named expression to simplify assignment and conditional (
Suggested change
|
||||||||||||||||||||||
plugin_name = star_metadata.name | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
plugin_name = "不知道是哪个插件" | ||||||||||||||||||||||
|
||||||||||||||||||||||
# 所有可能的指令名 | ||||||||||||||||||||||
candidates = [command_filter.command_name] + list(command_filter.alias) | ||||||||||||||||||||||
|
||||||||||||||||||||||
for candidate in candidates: | ||||||||||||||||||||||
for parent_command_name in command_filter.parent_command_names: | ||||||||||||||||||||||
if parent_command_name: | ||||||||||||||||||||||
full_command = f"{parent_command_name} {candidate}" | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
full_command = candidate | ||||||||||||||||||||||
|
||||||||||||||||||||||
command_map[full_command].append((handler, plugin_name, candidate)) | ||||||||||||||||||||||
|
||||||||||||||||||||||
# 检查冲突 | ||||||||||||||||||||||
conflicts_detected = False | ||||||||||||||||||||||
for command_name, handlers_list in command_map.items(): | ||||||||||||||||||||||
if len(handlers_list) > 1: | ||||||||||||||||||||||
if not conflicts_detected: | ||||||||||||||||||||||
logger.warning("检测到指令名冲突!") | ||||||||||||||||||||||
conflicts_detected = True | ||||||||||||||||||||||
|
||||||||||||||||||||||
conflict_info = [] | ||||||||||||||||||||||
for handler, plugin_name, original_command in handlers_list: | ||||||||||||||||||||||
conflict_info.append( | ||||||||||||||||||||||
f"插件 '{plugin_name}' 的指令 '{original_command}'" | ||||||||||||||||||||||
) | ||||||||||||||||||||||
logger.warning(f"指令 '{command_name}' 存在冲突:") | ||||||||||||||||||||||
for info in conflict_info: | ||||||||||||||||||||||
logger.warning(f" - {info}") | ||||||||||||||||||||||
|
||||||||||||||||||||||
async def process( | ||||||||||||||||||||||
self, event: AstrMessageEvent | ||||||||||||||||||||||
) -> Union[None, AsyncGenerator[None, None]]: | ||||||||||||||||||||||
|
@@ -110,73 +324,72 @@ async def process( | |||||||||||||||||||||
|
||||||||||||||||||||||
# 检查插件的 handler filter | ||||||||||||||||||||||
activated_handlers = [] | ||||||||||||||||||||||
handlers_parsed_params = {} # 注册了指令的 handler | ||||||||||||||||||||||
handlers_parsed_params = {} | ||||||||||||||||||||||
|
||||||||||||||||||||||
# 将 plugins_name 设置到 event 中 | ||||||||||||||||||||||
enabled_plugins_name = self.ctx.astrbot_config.get("plugin_set", ["*"]) | ||||||||||||||||||||||
if enabled_plugins_name == ["*"]: | ||||||||||||||||||||||
# 如果是 *,则表示所有插件都启用 | ||||||||||||||||||||||
event.plugins_name = None | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
event.plugins_name = enabled_plugins_name | ||||||||||||||||||||||
logger.debug(f"enabled_plugins_name: {enabled_plugins_name}") | ||||||||||||||||||||||
|
||||||||||||||||||||||
command_handlers = [] | ||||||||||||||||||||||
non_command_handlers = [] | ||||||||||||||||||||||
|
||||||||||||||||||||||
for handler in star_handlers_registry.get_handlers_by_event_type( | ||||||||||||||||||||||
EventType.AdapterMessageEvent, plugins_name=event.plugins_name | ||||||||||||||||||||||
): | ||||||||||||||||||||||
# filter 需满足 AND 逻辑关系 | ||||||||||||||||||||||
passed = True | ||||||||||||||||||||||
permission_not_pass = False | ||||||||||||||||||||||
permission_filter_raise_error = False | ||||||||||||||||||||||
if len(handler.event_filters) == 0: | ||||||||||||||||||||||
continue | ||||||||||||||||||||||
|
||||||||||||||||||||||
for filter in handler.event_filters: | ||||||||||||||||||||||
try: | ||||||||||||||||||||||
if isinstance(filter, PermissionTypeFilter): | ||||||||||||||||||||||
if not filter.filter(event, self.ctx.astrbot_config): | ||||||||||||||||||||||
permission_not_pass = True | ||||||||||||||||||||||
permission_filter_raise_error = filter.raise_error | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
if not filter.filter(event, self.ctx.astrbot_config): | ||||||||||||||||||||||
passed = False | ||||||||||||||||||||||
break | ||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||
await event.send( | ||||||||||||||||||||||
MessageEventResult().message( | ||||||||||||||||||||||
f"插件 {star_map[handler.handler_module_path].name}: {e}" | ||||||||||||||||||||||
) | ||||||||||||||||||||||
) | ||||||||||||||||||||||
event.stop_event() | ||||||||||||||||||||||
passed = False | ||||||||||||||||||||||
break | ||||||||||||||||||||||
if passed: | ||||||||||||||||||||||
if permission_not_pass: | ||||||||||||||||||||||
if not permission_filter_raise_error: | ||||||||||||||||||||||
# 跳过 | ||||||||||||||||||||||
continue | ||||||||||||||||||||||
if self.no_permission_reply: | ||||||||||||||||||||||
await event.send( | ||||||||||||||||||||||
MessageChain().message( | ||||||||||||||||||||||
f"您(ID: {event.get_sender_id()})的权限不足以使用此指令。通过 /sid 获取 ID 并请管理员添加。" | ||||||||||||||||||||||
) | ||||||||||||||||||||||
) | ||||||||||||||||||||||
logger.info( | ||||||||||||||||||||||
f"触发 {star_map[handler.handler_module_path].name} 时, 用户(ID={event.get_sender_id()}) 权限不足。" | ||||||||||||||||||||||
) | ||||||||||||||||||||||
event.stop_event() | ||||||||||||||||||||||
return | ||||||||||||||||||||||
# 检查是否为指令 | ||||||||||||||||||||||
command_filter = self._find_command_filter(handler) | ||||||||||||||||||||||
if command_filter: | ||||||||||||||||||||||
Comment on lines
+346
to
+348
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (code-quality): 我们发现了这些问题:
Suggested change
解释此函数的质量得分低于 25% 的质量阈值。 此得分是方法长度、认知复杂度和工作内存的组合。 您如何解决此问题? 重构此函数以使其更短、更易读可能是有益的。
Original comment in Englishsuggestion (code-quality): We've found these issues:
Suggested change
ExplanationThe quality score for this function is below the quality threshold of 25%. This score is a combination of the method length, cognitive complexity and working memory. How can you solve this? It might be worth refactoring this function to make it shorter and more readable.
|
||||||||||||||||||||||
command_handlers.append((handler, command_filter)) | ||||||||||||||||||||||
else: | ||||||||||||||||||||||
non_command_handlers.append(handler) | ||||||||||||||||||||||
|
||||||||||||||||||||||
is_wake = True | ||||||||||||||||||||||
event.is_wake = True | ||||||||||||||||||||||
self._detect_command_conflicts(command_handlers) | ||||||||||||||||||||||
|
||||||||||||||||||||||
activated_handlers.append(handler) | ||||||||||||||||||||||
# 指令 | ||||||||||||||||||||||
command_matched = False | ||||||||||||||||||||||
try: | ||||||||||||||||||||||
best_command_handler = await self._find_best_command_handlers( | ||||||||||||||||||||||
command_handlers, event | ||||||||||||||||||||||
) | ||||||||||||||||||||||
if best_command_handler: | ||||||||||||||||||||||
activated_handlers.append(best_command_handler) | ||||||||||||||||||||||
if "parsed_params" in event.get_extra(): | ||||||||||||||||||||||
handlers_parsed_params[handler.handler_full_name] = event.get_extra( | ||||||||||||||||||||||
"parsed_params" | ||||||||||||||||||||||
handlers_parsed_params[best_command_handler.handler_full_name] = ( | ||||||||||||||||||||||
event.get_extra("parsed_params") | ||||||||||||||||||||||
) | ||||||||||||||||||||||
command_matched = True | ||||||||||||||||||||||
is_wake = True | ||||||||||||||||||||||
event.is_wake = True | ||||||||||||||||||||||
except PermissionError: | ||||||||||||||||||||||
event.stop_event() | ||||||||||||||||||||||
return | ||||||||||||||||||||||
|
||||||||||||||||||||||
event._extras.pop("parsed_params", None) | ||||||||||||||||||||||
|
||||||||||||||||||||||
# 非指令 | ||||||||||||||||||||||
if not command_matched: | ||||||||||||||||||||||
for handler in non_command_handlers: | ||||||||||||||||||||||
result = await self._check_handler_filters(handler, event) | ||||||||||||||||||||||
if result == "activate": | ||||||||||||||||||||||
activated_handlers.append(handler) | ||||||||||||||||||||||
if "parsed_params" in event.get_extra(): | ||||||||||||||||||||||
handlers_parsed_params[handler.handler_full_name] = ( | ||||||||||||||||||||||
event.get_extra("parsed_params") | ||||||||||||||||||||||
) | ||||||||||||||||||||||
is_wake = True | ||||||||||||||||||||||
event.is_wake = True | ||||||||||||||||||||||
elif result == "permission_error": | ||||||||||||||||||||||
event.stop_event() | ||||||||||||||||||||||
return | ||||||||||||||||||||||
|
||||||||||||||||||||||
event._extras.pop("parsed_params", None) | ||||||||||||||||||||||
event._extras.pop("parsed_params", None) | ||||||||||||||||||||||
|
||||||||||||||||||||||
# 根据会话配置过滤插件处理器 | ||||||||||||||||||||||
activated_handlers = SessionPluginManager.filter_handlers_by_session( | ||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (code-quality): 使用内置函数
next
而不是 for 循环 (use-next
)Original comment in English
suggestion (code-quality): Use the built-in function
next
instead of a for-loop (use-next
)