From 781d0cf65420f63dfb68afbdefceb062cb66de07 Mon Sep 17 00:00:00 2001 From: yanyongyu Date: Sun, 4 Oct 2020 18:10:01 +0800 Subject: [PATCH] :alembic: rewrite echo and say builtin plugin --- nonebot/adapters/__init__.py | 7 ++++++- nonebot/adapters/cqhttp.py | 4 ++-- nonebot/adapters/cqhttp.pyi | 2 +- nonebot/plugins/base.py | 32 ++++++++++++++++++++++++++++---- tests/.env.dev | 2 ++ 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/nonebot/adapters/__init__.py b/nonebot/adapters/__init__.py index 5f7ad810..a76dcc33 100644 --- a/nonebot/adapters/__init__.py +++ b/nonebot/adapters/__init__.py @@ -265,7 +265,7 @@ class BaseMessageSegment(abc.ABC): - 类型: ``str`` - 说明: 消息段类型 """ - data: Dict[str, Union[str, list]] = field(default_factory=lambda: {}) + data: Dict[str, Any] = field(default_factory=lambda: {}) """ - 类型: ``Dict[str, Union[str, list]]`` - 说明: 消息段数据 @@ -279,6 +279,11 @@ class BaseMessageSegment(abc.ABC): def __add__(self, other): raise NotImplementedError + @classmethod + @abc.abstractmethod + def text(cls, text: str) -> "BaseMessageSegment": + return cls("text", {"text": text}) + class BaseMessage(list, abc.ABC): """消息数组""" diff --git a/nonebot/adapters/cqhttp.py b/nonebot/adapters/cqhttp.py index e55b51bb..ecb15daa 100644 --- a/nonebot/adapters/cqhttp.py +++ b/nonebot/adapters/cqhttp.py @@ -637,9 +637,9 @@ class Event(BaseEvent): class MessageSegment(BaseMessageSegment): @overrides(BaseMessageSegment) - def __init__(self, type: str, data: Dict[str, Union[str, list]]) -> None: + def __init__(self, type: str, data: Dict[str, Any]) -> None: if type == "text": - data["text"] = unescape(data["text"]) # type: ignore + data["text"] = unescape(data["text"]) super().__init__(type=type, data=data) @overrides(BaseMessageSegment) diff --git a/nonebot/adapters/cqhttp.pyi b/nonebot/adapters/cqhttp.pyi index 604a08c2..25e8a7a3 100644 --- a/nonebot/adapters/cqhttp.pyi +++ b/nonebot/adapters/cqhttp.pyi @@ -858,7 +858,7 @@ class Event: class MessageSegment: - def __init__(self, type: str, data: Dict[str, Union[str, list]]) -> None: + def __init__(self, type: str, data: Dict[str, Any]) -> None: ... def __str__(self): diff --git a/nonebot/plugins/base.py b/nonebot/plugins/base.py index de743d14..075ddd20 100644 --- a/nonebot/plugins/base.py +++ b/nonebot/plugins/base.py @@ -1,10 +1,34 @@ +from functools import reduce + from nonebot.rule import to_me from nonebot.plugin import on_command -from nonebot.typing import Bot, Event +from nonebot.permission import SUPERUSER +from nonebot.typing import Bot, Event, MessageSegment -say = on_command("say", to_me()) +say = on_command("say", to_me(), permission=SUPERUSER) @say.handle() -async def repeat(bot: Bot, event: Event, state: dict): - await bot.send(message=event.message, event=event) +async def say_unescape(bot: Bot, event: Event, state: dict): + Message = event.message.__class__ + + def _unescape(message: Message, segment: MessageSegment): + if segment.type == "text": + return message.append(segment.data["text"]) + return message.append(segment) + + message = reduce(_unescape, event.message, Message()) # type: ignore + await bot.send(message=message, event=event) + + +echo = on_command("echo", to_me()) + + +@echo.handle() +async def echo_escape(bot: Bot, event: Event, state: dict): + Message = event.message.__class__ + MessageSegment = event.message[0].__class__ + + message = Message().append( # type: ignore + MessageSegment.text(str(event.message))) + await bot.send(message=message, event=event) diff --git a/tests/.env.dev b/tests/.env.dev index 2a129ee1..9e330159 100644 --- a/tests/.env.dev +++ b/tests/.env.dev @@ -3,6 +3,8 @@ HOST=0.0.0.0 PORT=2333 DEBUG=true +SUPERUSERS=[123123123] + COMMAND_START=["", "/", "#"] COMMAND_SEP=["/", "."]