fix: detect missed recipient names and expose QQ mention diagnostics
This commit is contained in:
1 parent
f89aadb5da
commit
9c5b87772f
9 files changed
+277
-9
No files matched your search
@@ -137,9 +137,11 @@ def official_mention(user_id):
|
||||
|
||||
def reminder_markdown(rem):
|
||||
# Keep the mention outside user-provided text/code blocks. Preserve reminder
|
||||
# text literally instead of interpreting its punctuation as Markdown syntax.
|
||||
# text literally instead of interpreting formatting delimiters as Markdown.
|
||||
# QQ exposes unnecessary backslashes before ordinary parentheses and the
|
||||
# inline reminder number, so do not escape every CommonMark punctuation mark.
|
||||
text = escape(reminder_text(rem), quote=False)
|
||||
text = re.sub(r"([\\`*_{}\[\]()#+.!|~\-])", r"\\\1", text)
|
||||
text = re.sub(r"([\\`*_{}\[\]|~])", r"\\\1", text)
|
||||
return official_mention(rem["target_id"]) + "\n\n" + text.replace("\n", "\n\n")
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
"""Show the actual incoming mention fields without exposing tokens or full IDs."""
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
|
||||
from .delivery import field
|
||||
from .targets import MARKUP, mention_data, resolve_target
|
||||
|
||||
VERSION = "v1.3.1"
|
||||
|
||||
|
||||
def identity(value):
|
||||
if not value:
|
||||
return ""
|
||||
return "id#" + hashlib.sha256(str(value).encode()).hexdigest()[:10]
|
||||
|
||||
|
||||
async def diagnose(event, args, store, scope):
|
||||
raw = event.message_obj.raw_message
|
||||
data = field(raw, "raw_data", raw)
|
||||
trusted, _, aliases, bot_ids, _, payload, sdk = mention_data(event)
|
||||
ids = set(aliases) | set(aliases.values()) | set(trusted) | bot_ids
|
||||
ids.add(str(event.get_sender_id()))
|
||||
|
||||
def redact(text):
|
||||
text = str(text or "")
|
||||
text = MARKUP.sub(
|
||||
lambda match: (
|
||||
"[QQ @ " + identity(next(g for g in match.groups() if g is not None)) + "]"
|
||||
),
|
||||
text,
|
||||
)
|
||||
for value in sorted(ids, key=len, reverse=True):
|
||||
if value:
|
||||
text = text.replace(value, identity(value))
|
||||
return text[:600]
|
||||
|
||||
def member(value):
|
||||
# Select only incoming member fields; never serialize bot/api/session objects.
|
||||
return {
|
||||
key: identity(field(value, key))
|
||||
if key in {"id", "member_openid"}
|
||||
else field(value, key)
|
||||
for key in (
|
||||
"id",
|
||||
"member_openid",
|
||||
"username",
|
||||
"nickname",
|
||||
"nick",
|
||||
"card",
|
||||
"is_you",
|
||||
"bot",
|
||||
)
|
||||
if field(value, key) is not None
|
||||
}
|
||||
|
||||
report = {
|
||||
"插件版本": VERSION,
|
||||
"适配器": event.get_platform_name(),
|
||||
"消息对象类型": type(raw).__name__,
|
||||
"发起人": identity(event.get_sender_id()),
|
||||
"AstrBot正文": redact(event.message_str),
|
||||
"QQ原始正文": redact(field(data, "content") or field(raw, "content", "")),
|
||||
"QQ原始mentions": [member(item) for item in payload[:10]],
|
||||
"SDK mentions": [member(item) for item in sdk[:10]],
|
||||
"At消息段": [
|
||||
{"id": identity(field(c, "qq")), "name": field(c, "name", "")}
|
||||
for c in event.get_messages()
|
||||
if field(c, "type") in {"At", "at"} or type(c).__name__ == "At"
|
||||
][:10],
|
||||
"本群已知名字数": len(await store.member_names(scope)),
|
||||
"原始author": member(field(data, "author", {})),
|
||||
}
|
||||
try:
|
||||
_, target, name = await resolve_target(event, args, store, scope)
|
||||
report["解析结果"] = {
|
||||
"对象": identity(target),
|
||||
"名字": name,
|
||||
"提醒自己": target == str(event.get_sender_id()),
|
||||
}
|
||||
except ValueError as exc:
|
||||
report["解析失败"] = redact(str(exc))
|
||||
return json.dumps(report, ensure_ascii=False, indent=2)
|
||||
+7
-1
@@ -107,7 +107,7 @@ def text_mentions(names):
|
||||
# reminder text after a shorter name. Quoted names work before registration.
|
||||
known = "|".join(re.escape(n) for n in sorted(set(names), key=len, reverse=True) if n)
|
||||
pattern = re.compile(
|
||||
r'(?<![\w@])@(?:"([^"\n]+)"|“([^”\n]+)”|'
|
||||
r'(?<![A-Za-z0-9_.@@])[@@](?:"([^"\n]+)"|“([^”\n]+)”|'
|
||||
+ (rf"({known})(?=\s|$)|" if known else "")
|
||||
+ r"([^\s@]+))"
|
||||
)
|
||||
@@ -131,6 +131,12 @@ async def resolve_target(event, args, store, scope):
|
||||
known_names + [n for values in names.values() for n in values] + list(bot_names)
|
||||
)
|
||||
textual = list(pattern.finditer(args))
|
||||
# A QQ mention can precede the command. Without native mention metadata,
|
||||
# tx()'s argument slicing previously discarded this explicit recipient.
|
||||
command = re.search(r"(?:^|\s)/?(?:tx|提醒|remind)(?=\s|$)", event.message_str, re.I)
|
||||
if command:
|
||||
prefix = MARKUP.sub(" ", event.message_str[: command.start()])
|
||||
textual.extend(pattern.finditer(prefix))
|
||||
for match in textual:
|
||||
value = next(g for g in match.groups() if g is not None)
|
||||
if value in bot_names or aliases.get(value, value) in bot_ids:
|
||||
|
||||
Reference in new issue
Block a user