mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-02-17 16:20:05 +08:00
🐛 Fix cqhttp Message
null parameter
This commit is contained in:
parent
2218fa2b33
commit
877fa1a75a
@ -222,29 +222,29 @@ class Message(BaseMessage):
|
||||
for seg in msg:
|
||||
yield MessageSegment(seg["type"], seg.get("data") or {})
|
||||
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]]:
|
||||
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:]
|
||||
|
||||
for type_, data in _iter_message(msg):
|
||||
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)
|
||||
for type_, data in _iter_message(msg):
|
||||
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user