🐛 fix missing escape in builtin plugin #117

This commit is contained in:
yanyongyu 2020-12-22 20:11:48 +08:00
parent 054df580eb
commit ab256e57b1
3 changed files with 13 additions and 16 deletions

View File

@ -14,8 +14,6 @@ class MessageSegment(BaseMessageSegment):
@overrides(BaseMessageSegment)
def __init__(self, type: str, data: Dict[str, Any]) -> None:
if type == "text":
data["text"] = unescape(data["text"])
super().__init__(type=type, data=data)
@overrides(BaseMessageSegment)
@ -215,17 +213,16 @@ class Message(BaseMessage):
r"(?P<params>"
r"(?:,[a-zA-Z0-9-_.]+=[^,\]]+)*"
r"),?\]", msg):
yield "text", unescape(msg[text_begin:cqcode.pos +
cqcode.start()])
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", unescape(msg[text_begin:])
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": data})
yield MessageSegment(type_, {"text": unescape(data)})
else:
data = {
k: unescape(v) for k, v in map(

View File

@ -1,24 +1,22 @@
from functools import reduce
from nonebot.rule import to_me
from nonebot.typing import T_State
from nonebot.plugin import on_command
from nonebot.permission import SUPERUSER
from nonebot.adapters import Bot, Event, Message, MessageSegment
from nonebot.adapters.cqhttp import Bot, unescape, MessageEvent, Message, MessageSegment
say = on_command("say", to_me(), permission=SUPERUSER)
@say.handle()
async def say_unescape(bot: Bot, event: Event, state: T_State):
MessageImpl = event.get_message().__class__
async def say_unescape(bot: Bot, event: MessageEvent):
def _unescape(message: Message, segment: MessageSegment):
if segment.is_text():
return message.append(str(segment))
return message.append(unescape(str(segment)))
return message.append(segment)
message = reduce(_unescape, event.get_message(), MessageImpl())
message = reduce(_unescape, event.get_message(), Message()) # type: ignore
await bot.send(message=message, event=event)
@ -26,5 +24,5 @@ echo = on_command("echo", to_me())
@echo.handle()
async def echo_escape(bot: Bot, event: Event, state: T_State):
async def echo_escape(bot: Bot, event: MessageEvent):
await bot.send(message=event.get_message(), event=event)

View File

@ -17,17 +17,19 @@
.. _typing:
https://docs.python.org/3/library/typing.html
"""
from typing import Any, Dict, Union, TypeVar, Protocol, overload, Optional, Callable, NoReturn, Awaitable, TYPE_CHECKING
from collections.abc import Callable as BaseCallable
from typing import Any, Dict, Union, TypeVar, Optional, Callable, NoReturn, Awaitable, TYPE_CHECKING
if TYPE_CHECKING:
from nonebot.adapters import Bot, Event
from nonebot.matcher import Matcher
T_Wrapped = TypeVar("T_Wrapped", bound=BaseCallable)
def overrides(InterfaceClass: object):
def overrider(func: Callable) -> Callable:
def overrider(func: T_Wrapped) -> T_Wrapped:
assert func.__name__ in dir(
InterfaceClass), f"Error method: {func.__name__}"
return func