mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-27 18:45:05 +08:00
⚗️ rewrite echo and say builtin plugin
This commit is contained in:
parent
0a11bd3e8e
commit
781d0cf654
@ -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):
|
||||
"""消息数组"""
|
||||
|
@ -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)
|
||||
|
@ -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):
|
||||
|
@ -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)
|
||||
|
@ -3,6 +3,8 @@ HOST=0.0.0.0
|
||||
PORT=2333
|
||||
DEBUG=true
|
||||
|
||||
SUPERUSERS=[123123123]
|
||||
|
||||
COMMAND_START=["", "/", "#"]
|
||||
COMMAND_SEP=["/", "."]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user