From e54eaab842267eba26d8acbead0748cf3eab1746 Mon Sep 17 00:00:00 2001 From: Raven95676 Date: Sat, 5 Apr 2025 21:19:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E9=AA=8C=E8=AF=81=E5=99=A8=E5=AD=97?= =?UTF-8?q?=E5=85=B8=E7=A7=BB=E5=88=B0=E7=B1=BB=E7=BA=A7=E5=88=AB=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E9=87=8D=E5=A4=8D=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- astrbot/core/pipeline/respond/stage.py | 74 ++++++++++++-------------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/astrbot/core/pipeline/respond/stage.py b/astrbot/core/pipeline/respond/stage.py index e41e61e28..86c165945 100644 --- a/astrbot/core/pipeline/respond/stage.py +++ b/astrbot/core/pipeline/respond/stage.py @@ -16,6 +16,38 @@ from astrbot.core.star.star import star_map @register_stage class RespondStage(Stage): + # 组件类型到其非空判断函数的映射 + _component_validators = { + Comp.Plain: lambda comp: bool(comp.text and comp.text.strip()), # 纯文本消息需要strip + Comp.Face: lambda comp: comp.id is not None, # QQ表情 + Comp.Record: lambda comp: bool(comp.file), # 语音 + Comp.Video: lambda comp: bool(comp.file), # 视频 + Comp.At: lambda comp: bool(comp.qq) or bool(comp.name), # @ + Comp.AtAll: lambda comp: True, # @所有人 + Comp.RPS: lambda comp: True, # 不知道是啥(未完成) + Comp.Dice: lambda comp: True, # 骰子(未完成) + Comp.Shake: lambda comp: True, # 摇一摇(未完成) + Comp.Anonymous: lambda comp: True, # 匿名(未完成) + Comp.Share: lambda comp: bool(comp.url) and bool(comp.title), # 分享 + Comp.Contact: lambda comp: True, # 联系人(未完成) + Comp.Location: lambda comp: bool(comp.lat and comp.lon), # 位置 + Comp.Music: lambda comp: bool(comp._type) and bool(comp.url) and bool(comp.audio), # 音乐 + Comp.Image: lambda comp: bool(comp.file), # 图片 + Comp.Reply: lambda comp: bool(comp.id) and comp.sender_id is not None, # 回复 + Comp.RedBag: lambda comp: bool(comp.title), # 红包 + Comp.Poke: lambda comp: comp.id != 0 and comp.qq != 0, # 戳一戳 + Comp.Forward: lambda comp: bool(comp.id and comp.id.strip()), # 转发 + Comp.Node: lambda comp: bool(comp.name) and comp.uin != 0 and bool(comp.content), # 一个转发节点 + Comp.Nodes: lambda comp: bool(comp.nodes), # 多个转发节点 + Comp.Xml: lambda comp: bool(comp.data and comp.data.strip()), # XML + Comp.Json: lambda comp: bool(comp.data), # JSON + Comp.CardImage: lambda comp: bool(comp.file), # 卡片图片 + Comp.TTS: lambda comp: bool(comp.text and comp.text.strip()), # 语音合成 + Comp.Unknown: lambda comp: bool(comp.text and comp.text.strip()), # 未知消息 + Comp.File: lambda comp: bool(comp.file), # 文件 + Comp.WechatEmoji: lambda comp: bool(comp.md5), # 微信表情 + } + async def initialize(self, ctx: PipelineContext): self.ctx = ctx @@ -81,50 +113,12 @@ class RespondStage(Stage): if not chain: return True - # 组件类型到其非空判断函数的映射 - component_validators = { - Comp.Plain: lambda comp: bool( - comp.text and comp.text.strip() - ), # 纯文本消息需要strip - Comp.Face: lambda comp: comp.id is not None, # QQ表情 - Comp.Record: lambda comp: bool(comp.file), # 语音 - Comp.Video: lambda comp: bool(comp.file), # 视频 - Comp.At: lambda comp: bool(comp.qq) or bool(comp.name), # @ - Comp.AtAll: lambda comp: True, # @所有人 - Comp.RPS: lambda comp: True, # 不知道是啥(未完成) - Comp.Dice: lambda comp: True, # 骰子(未完成) - Comp.Shake: lambda comp: True, # 摇一摇(未完成) - Comp.Anonymous: lambda comp: True, # 匿名(未完成) - Comp.Share: lambda comp: bool(comp.url) and bool(comp.title), # 分享 - Comp.Contact: lambda comp: True, # 联系人(未完成) - Comp.Location: lambda comp: bool(comp.lat and comp.lon), # 位置 - Comp.Music: lambda comp: bool(comp._type) - and bool(comp.url) - and bool(comp.audio), # 音乐 - Comp.Image: lambda comp: bool(comp.file), # 图片 - Comp.Reply: lambda comp: bool(comp.id) and comp.sender_id is not None, # 回复 - Comp.RedBag: lambda comp: bool(comp.title), # 红包 - Comp.Poke: lambda comp: comp.id != 0 and comp.qq != 0, # 戳一戳 - Comp.Forward: lambda comp: bool(comp.id and comp.id.strip()), # 转发 - Comp.Node: lambda comp: bool(comp.name) - and comp.uin != 0 - and bool(comp.content), # 一个转发节点 - Comp.Nodes: lambda comp: bool(comp.nodes), # 多个转发节点 - Comp.Xml: lambda comp: bool(comp.data and comp.data.strip()), # XML - Comp.Json: lambda comp: bool(comp.data), # JSON - Comp.CardImage: lambda comp: bool(comp.file), # 卡片图片 - Comp.TTS: lambda comp: bool(comp.text and comp.text.strip()), # 语音合成 - Comp.Unknown: lambda comp: bool(comp.text and comp.text.strip()), # 未知消息 - Comp.File: lambda comp: bool(comp.file), # 文件 - Comp.WechatEmoji: lambda comp: bool(comp.md5), # 微信表情 - } - for comp in chain: comp_type = type(comp) # 检查组件类型是否在字典中 - if comp_type in component_validators: - if component_validators[comp_type](comp): + if comp_type in self._component_validators: + if self._component_validators[comp_type](comp): return False else: logger.info(f"空内容检查: 无法识别的组件类型: {comp_type.__name__}")