Feature: 支持响应更多类型的喵 #52
Reference in New Issue
Block a user
No description provided.
Delete Branch "feature/nya-more"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
PR Type
Enhancement
Description
新增多字符"喵"消息的响应逻辑
定义符号映射表转换喵语句
重命名变量避免名称遮蔽
原单字"喵"响应保持不变
Diagram Walkthrough
File Walkthrough
__init__.py
新增多字符喵语句匹配与响应逻辑konabot/plugins/nya_echo/init.py
evt重命名为evt_nya避免变量名遮蔽NYA_SYMBOL_MAPPING字典定义喵语句中允许的符号及其映射has_nya规则函数,校验消息是否为纯文本且包含"喵"且仅含映射表中的字符evt_nya_v2处理器,将匹配的喵语句通过映射表转换后回复PR Reviewer Guide 🔍
(Review updated until commit
5e01e086f2)Here are some key observations to aid the review process:
规则冲突
evt_nya使用match_keyword("喵")匹配包含"喵"的消息,而evt_nya_v2的has_nya同样匹配包含"喵"且长度大于1的消息。当用户发送多字喵语句时,两个处理器可能同时触发,导致重复响应。需要确认match_keyword是否为精确匹配(仅匹配单个"喵"字),否则应为其中一个处理器设置优先级或互斥条件。符号映射遗漏
NYA_SYMBOL_MAPPING中"?"和"?"都映射到了"!"/"!",即问号被转换为感叹号。这看起来可能是有意为之的设计,但也可能是复制粘贴时的笔误,建议确认是否应该将问号映射为问号本身。函数命名
evt_nya和evt_nya_v2的 handler 函数都命名为_,虽然在 nonebot 中这是常见写法,但在同一模块中两个同名_函数会导致后者覆盖前者的引用。如果后续需要直接引用这些函数(如测试),可能会产生问题。PR Code Suggestions ✨
Latest suggestions up to
5e01e08Explore these optional code suggestions:
两个处理器可能重复响应同一消息
evt_nya和evt_nya_v2都会匹配包含"喵"且仅由映射字符组成的消息(长度>1时)。当用户发送如"喵喵"这样的消息时,两个处理器都会触发并各自发送一条回复,导致重复响应。应降低
evt_nya_v2的优先级,或在has_nya中排除纯"喵"单字的情况已经不够——需要排除evt_nya也会匹配的场景,例如通过设置优先级并使用event.stop_propagation()。konabot/plugins/nya_echo/init.py [50-52]
Suggestion importance[1-10]: 6
__
Why: The concern is valid:
evt_nyamatches any message containing "喵" viamatch_keyword("喵"), whilehas_nyaalso requires "喵" to be present. A message like "喵喵" would trigger both handlers, causing duplicate replies. However, the suggested fix of only settingpriority=2onevt_nya_v2is incomplete — withoutevent.stop_propagation()onevt_nya, both handlers would still fire. The suggestion correctly identifies the issue but theimproved_codealone doesn't fully resolve it.Previous suggestions
Suggestions up to commit
5e01e08变量名重复导致处理器被覆盖
变量
evt_nya_v2在文件上方已经被赋值为on_message(rule=match_keyword("喵")),这里再次赋值会覆盖前面的matcher,导致第一个
@evt_nya_v2.handle()注册的处理函数绑定在一个被丢弃的对象上,永远不会被触发。应该使用不同的变量名来注册第二个matcher。
konabot/plugins/nya_echo/init.py [50]
Suggestion importance[1-10]: 9
__
Why: This is a genuine bug.
evt_nya_v2is assigned at line 7 withon_message(rule=match_keyword("喵"))and its handler is registered at line 9. Then at line 50,evt_nya_v2is reassigned to a newon_message(rule=has_nya), which means the first matcher object (and its registered handler from line 9-11) becomes orphaned and will never trigger. Using a distinct variable name likeevt_nya_v3for the second matcher is the correct fix.Suggestions up to commit
5e01e08缺少对核心关键字的存在性校验
当
text为空字符串时(即消息中只有非Text段被过滤后无内容),len(text) <= 1会返回False,但空字符串实际上也应该被排除。此外,仅包含标点符号(如"!!!")而不含"喵"的消息也会匹配。建议增加对"喵"字符存在性的检查,确保消息确实包含"喵"。
konabot/plugins/nya_echo/init.py [40-41]
Suggestion importance[1-10]: 7
__
Why: This is a valid logic concern. The
has_nyafunction checks that all characters are inNYA_SYMBOL_MAPPING, but a message like"!!!"(only punctuation, no"喵") would pass the check and trigger the handler unnecessarily. Adding a"喵" not in textcheck ensures the plugin only responds to messages that actually contain the core keyword, which is a meaningful correctness improvement.参数名遮蔽外部变量导致潜在错误
参数
evt与外部作用域中的evt(on_message返回的Matcher实例)同名,会遮蔽(shadow)外层变量。更严重的是,在函数体内调用UniMessage.text(...).send(evt)时,传入的evt实际上是Event对象而非Matcher,这可能导致运行时行为异常或报错。建议将参数重命名以避免遮蔽。konabot/plugins/nya_echo/init.py [49-53]
Suggestion importance[1-10]: 6
__
Why: The parameter
evtdoes shadow the outerevtvariable (theMatcherinstance). However, in NoneBot2's framework,UniMessage.text(...).send()typically accepts anEventobject, so passing theEventparameter is likely correct behavior. The shadowing is still a code quality concern that could cause confusion, but it may not cause a runtime error as the suggestion claims. The rename toeventis a reasonable improvement for clarity.Persistent review updated to latest commit
5e01e086f2Persistent review updated to latest commit
5e01e086f2