bug优化
This commit is contained in:
parent
0d04b8288d
commit
4de10d02d7
50
main.py
50
main.py
@ -2,10 +2,10 @@ from astrbot.api.event import AstrMessageEvent
|
||||
from astrbot.api.event.filter import event_message_type, EventMessageType
|
||||
from astrbot.api.star import Context, Star, register
|
||||
from astrbot.api import logger
|
||||
import astrbot.api.message_components as Comp
|
||||
from astrbot.core.platform import AstrBotMessage
|
||||
from astrbot.core.message.components import At
|
||||
|
||||
|
||||
def _check_at_bot(message_obj) -> bool:
|
||||
def check_at_bot(message_obj:AstrBotMessage) -> bool:
|
||||
"""
|
||||
检测群聊消息是否@了当前机器人。
|
||||
依据: plugin.md 中 AstrBotMessage 及消息链定义:
|
||||
@ -14,45 +14,19 @@ def _check_at_bot(message_obj) -> bool:
|
||||
当消息链中存在 At 且其 qq 等于 self_id 时,判定为@了机器人。
|
||||
忽略 AtAll(@全体)。
|
||||
"""
|
||||
try:
|
||||
self_id = str(getattr(message_obj, "self_id", "")) or ""
|
||||
segments = getattr(message_obj, "message", []) or []
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
if not self_id or not isinstance(segments, list):
|
||||
return False
|
||||
|
||||
for seg in segments:
|
||||
# 适配对象消息段
|
||||
try:
|
||||
if isinstance(seg, Comp.At):
|
||||
# 排除 @all
|
||||
if isinstance(seg, Comp.AtAll):
|
||||
continue
|
||||
if str(seg.qq) == self_id:
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 适配字典形式的消息段(稳健性处理)
|
||||
try:
|
||||
if isinstance(seg, dict):
|
||||
seg_type = str(seg.get("type", "")).lower()
|
||||
data = seg.get("data", {}) or {}
|
||||
if seg_type == "at":
|
||||
qq = str(data.get("qq", ""))
|
||||
if qq and qq.lower() != "all" and qq == self_id:
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
self_id = message_obj.self_id
|
||||
segments = message_obj.message
|
||||
for segment in segments:
|
||||
if isinstance(segment, At) and str(segment.qq) == self_id:
|
||||
logger.info(f"[check_at_bot] 消息 {message_obj.message_id} @了机器人 {self_id}")
|
||||
return True
|
||||
logger.info(f"[check_at_bot] 消息 {message_obj.message_id} 未@机器人 {self_id}")
|
||||
return False
|
||||
|
||||
|
||||
|
||||
|
||||
@register("relay", "YourName", "消息中转插件", "1.0.0")
|
||||
@register("qqfight-bot", "hxuanyu", "消息中转插件", "1.0.0")
|
||||
class RelayPlugin(Star):
|
||||
def __init__(self, context: Context, config=None):
|
||||
super().__init__(context)
|
||||
@ -67,7 +41,7 @@ class RelayPlugin(Star):
|
||||
async def on_group_message(self, event: AstrMessageEvent):
|
||||
|
||||
group_id = str(event.message_obj.group_id)
|
||||
at_bot = _check_at_bot(event.message_obj)
|
||||
at_bot = check_at_bot(event.message_obj)
|
||||
|
||||
if group_id not in self.group_whitelist:
|
||||
logger.info(f"[RelayPlugin] 群聊 {group_id} 不在白名单,忽略消息")
|
||||
|
Loading…
x
Reference in New Issue
Block a user