mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-07-30 09:40:05 +00:00
🐛 Fix cqhttp Message
null parameter
This commit is contained in:
@ -222,29 +222,29 @@ class Message(BaseMessage):
|
|||||||
for seg in msg:
|
for seg in msg:
|
||||||
yield MessageSegment(seg["type"], seg.get("data") or {})
|
yield MessageSegment(seg["type"], seg.get("data") or {})
|
||||||
return
|
return
|
||||||
|
elif isinstance(msg, str):
|
||||||
|
def _iter_message(msg: str) -> Iterable[Tuple[str, str]]:
|
||||||
|
text_begin = 0
|
||||||
|
for cqcode in re.finditer(
|
||||||
|
r"\[CQ:(?P<type>[a-zA-Z0-9-_.]+)"
|
||||||
|
r"(?P<params>"
|
||||||
|
r"(?:,[a-zA-Z0-9-_.]+=[^,\]]+)*"
|
||||||
|
r"),?\]", msg):
|
||||||
|
yield "text", msg[text_begin:cqcode.pos + cqcode.start()]
|
||||||
|
text_begin = cqcode.pos + cqcode.end()
|
||||||
|
yield cqcode.group("type"), cqcode.group("params").lstrip(",")
|
||||||
|
yield "text", msg[text_begin:]
|
||||||
|
|
||||||
def _iter_message(msg: str) -> Iterable[Tuple[str, str]]:
|
for type_, data in _iter_message(msg):
|
||||||
text_begin = 0
|
if type_ == "text":
|
||||||
for cqcode in re.finditer(
|
if data:
|
||||||
r"\[CQ:(?P<type>[a-zA-Z0-9-_.]+)"
|
# only yield non-empty text segment
|
||||||
r"(?P<params>"
|
yield MessageSegment(type_, {"text": unescape(data)})
|
||||||
r"(?:,[a-zA-Z0-9-_.]+=[^,\]]+)*"
|
else:
|
||||||
r"),?\]", msg):
|
data = {
|
||||||
yield "text", msg[text_begin:cqcode.pos + cqcode.start()]
|
k: unescape(v) for k, v in map(
|
||||||
text_begin = cqcode.pos + cqcode.end()
|
lambda x: x.split("=", maxsplit=1),
|
||||||
yield cqcode.group("type"), cqcode.group("params").lstrip(",")
|
filter(lambda x: x, (
|
||||||
yield "text", msg[text_begin:]
|
x.lstrip() for x in data.split(","))))
|
||||||
|
}
|
||||||
for type_, data in _iter_message(msg):
|
yield MessageSegment(type_, data)
|
||||||
if type_ == "text":
|
|
||||||
if data:
|
|
||||||
# only yield non-empty text segment
|
|
||||||
yield MessageSegment(type_, {"text": unescape(data)})
|
|
||||||
else:
|
|
||||||
data = {
|
|
||||||
k: unescape(v) for k, v in map(
|
|
||||||
lambda x: x.split("=", maxsplit=1),
|
|
||||||
filter(lambda x: x, (
|
|
||||||
x.lstrip() for x in data.split(","))))
|
|
||||||
}
|
|
||||||
yield MessageSegment(type_, data)
|
|
||||||
|
Reference in New Issue
Block a user