mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-06-06 12:26:38 +00:00
parent
8cda1b5417
commit
f332199baa
@ -313,10 +313,19 @@ class BaseMessageSegment(abc.ABC):
|
|||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
"""该消息段所代表的 str,在命令匹配部分使用"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
|
"""你需要在这里实现不同消息段的合并:
|
||||||
|
比如:
|
||||||
|
if isinstance(other, str):
|
||||||
|
...
|
||||||
|
elif isinstance(other, MessageSegment):
|
||||||
|
...
|
||||||
|
注意:不能返回 self,需要返回一个新生成的对象
|
||||||
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
@ -390,10 +399,7 @@ class BaseMessage(list, abc.ABC):
|
|||||||
* ``obj: Union[str, MessageSegment]``: 要添加的消息段
|
* ``obj: Union[str, MessageSegment]``: 要添加的消息段
|
||||||
"""
|
"""
|
||||||
if isinstance(obj, BaseMessageSegment):
|
if isinstance(obj, BaseMessageSegment):
|
||||||
if obj.type == "text" and self and self[-1].type == "text":
|
super().append(obj)
|
||||||
self[-1].data["text"] += obj.data["text"]
|
|
||||||
else:
|
|
||||||
super().append(obj)
|
|
||||||
elif isinstance(obj, str):
|
elif isinstance(obj, str):
|
||||||
self.extend(self._construct(obj))
|
self.extend(self._construct(obj))
|
||||||
else:
|
else:
|
||||||
@ -420,13 +426,13 @@ class BaseMessage(list, abc.ABC):
|
|||||||
"""
|
"""
|
||||||
:说明:
|
:说明:
|
||||||
|
|
||||||
缩减消息数组,即拼接相邻纯文本消息段
|
缩减消息数组,即按 MessageSegment 的实现拼接相邻消息段
|
||||||
"""
|
"""
|
||||||
index = 0
|
index = 0
|
||||||
while index < len(self):
|
while index < len(self):
|
||||||
if index > 0 and self[
|
if index > 0 and self[
|
||||||
index - 1].type == "text" and self[index].type == "text":
|
index - 1].type == "text" and self[index].type == "text":
|
||||||
self[index - 1].data["text"] += self[index].data["text"]
|
self[index - 1] += self[index]
|
||||||
del self[index]
|
del self[index]
|
||||||
else:
|
else:
|
||||||
index += 1
|
index += 1
|
||||||
@ -439,7 +445,7 @@ class BaseMessage(list, abc.ABC):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def _concat(x: str, y: BaseMessageSegment) -> str:
|
def _concat(x: str, y: BaseMessageSegment) -> str:
|
||||||
return f"{x} {y.data['text']}" if y.type == "text" else x
|
return f"{x} {y}" if y.type == "text" else x
|
||||||
|
|
||||||
plain_text = reduce(_concat, self, "")
|
plain_text = reduce(_concat, self, "")
|
||||||
return plain_text[1:] if plain_text else plain_text
|
return plain_text[1:] if plain_text else plain_text
|
||||||
|
@ -14,7 +14,7 @@ async def say_unescape(bot: Bot, event: Event, state: dict):
|
|||||||
|
|
||||||
def _unescape(message: Message, segment: MessageSegment):
|
def _unescape(message: Message, segment: MessageSegment):
|
||||||
if segment.type == "text":
|
if segment.type == "text":
|
||||||
return message.append(segment.data["text"])
|
return message.append(str(segment))
|
||||||
return message.append(segment)
|
return message.append(segment)
|
||||||
|
|
||||||
message = reduce(_unescape, event.message, Message()) # type: ignore
|
message = reduce(_unescape, event.message, Message()) # type: ignore
|
||||||
|
@ -130,11 +130,10 @@ class TrieRule:
|
|||||||
suffix = None
|
suffix = None
|
||||||
message = event.message[0]
|
message = event.message[0]
|
||||||
if message.type == "text":
|
if message.type == "text":
|
||||||
prefix = cls.prefix.longest_prefix(message.data["text"].lstrip())
|
prefix = cls.prefix.longest_prefix(str(message).lstrip())
|
||||||
message_r = event.message[-1]
|
message_r = event.message[-1]
|
||||||
if message_r.type == "text":
|
if message_r.type == "text":
|
||||||
suffix = cls.suffix.longest_prefix(
|
suffix = cls.suffix.longest_prefix(str(message_r).rstrip()[::-1])
|
||||||
message_r.data["text"].rstrip()[::-1])
|
|
||||||
|
|
||||||
state["_prefix"] = {
|
state["_prefix"] = {
|
||||||
"raw_command": prefix.key,
|
"raw_command": prefix.key,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user