⚗️ rewrite echo and say builtin plugin

This commit is contained in:
yanyongyu 2020-10-04 18:10:01 +08:00
parent 0a11bd3e8e
commit 781d0cf654
5 changed files with 39 additions and 8 deletions

View File

@ -265,7 +265,7 @@ class BaseMessageSegment(abc.ABC):
- 类型: ``str`` - 类型: ``str``
- 说明: 消息段类型 - 说明: 消息段类型
""" """
data: Dict[str, Union[str, list]] = field(default_factory=lambda: {}) data: Dict[str, Any] = field(default_factory=lambda: {})
""" """
- 类型: ``Dict[str, Union[str, list]]`` - 类型: ``Dict[str, Union[str, list]]``
- 说明: 消息段数据 - 说明: 消息段数据
@ -279,6 +279,11 @@ class BaseMessageSegment(abc.ABC):
def __add__(self, other): def __add__(self, other):
raise NotImplementedError raise NotImplementedError
@classmethod
@abc.abstractmethod
def text(cls, text: str) -> "BaseMessageSegment":
return cls("text", {"text": text})
class BaseMessage(list, abc.ABC): class BaseMessage(list, abc.ABC):
"""消息数组""" """消息数组"""

View File

@ -637,9 +637,9 @@ class Event(BaseEvent):
class MessageSegment(BaseMessageSegment): class MessageSegment(BaseMessageSegment):
@overrides(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": if type == "text":
data["text"] = unescape(data["text"]) # type: ignore data["text"] = unescape(data["text"])
super().__init__(type=type, data=data) super().__init__(type=type, data=data)
@overrides(BaseMessageSegment) @overrides(BaseMessageSegment)

View File

@ -858,7 +858,7 @@ class Event:
class MessageSegment: 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): def __str__(self):

View File

@ -1,10 +1,34 @@
from functools import reduce
from nonebot.rule import to_me from nonebot.rule import to_me
from nonebot.plugin import on_command 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() @say.handle()
async def repeat(bot: Bot, event: Event, state: dict): async def say_unescape(bot: Bot, event: Event, state: dict):
await bot.send(message=event.message, event=event) 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)

View File

@ -3,6 +3,8 @@ HOST=0.0.0.0
PORT=2333 PORT=2333
DEBUG=true DEBUG=true
SUPERUSERS=[123123123]
COMMAND_START=["", "/", "#"] COMMAND_START=["", "/", "#"]
COMMAND_SEP=["/", "."] COMMAND_SEP=["/", "."]