diff --git a/nonebot/adapters/cqhttp/message.py b/nonebot/adapters/cqhttp/message.py index f8e57af7..ae7fe7b6 100644 --- a/nonebot/adapters/cqhttp/message.py +++ b/nonebot/adapters/cqhttp/message.py @@ -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" 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( diff --git a/nonebot/plugins/base.py b/nonebot/plugins/base.py index 1c74a7cc..bf4e9619 100644 --- a/nonebot/plugins/base.py +++ b/nonebot/plugins/base.py @@ -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) diff --git a/nonebot/typing.py b/nonebot/typing.py index 72e6f47a..4b1be10b 100644 --- a/nonebot/typing.py +++ b/nonebot/typing.py @@ -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