Skip to content

Commit 33a68b3

Browse files
committed
✨ escape & unescape in echo
1 parent d6c74ba commit 33a68b3

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

arclet/entari/builtins/echo.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from arclet.alconna import Alconna, AllParam, Args, CommandMeta
1+
from arclet.alconna import Arparma
22

3-
from arclet.entari import MessageChain, Session, command, metadata
3+
from arclet.entari import MessageChain, command, metadata
44
from arclet.entari.command import Match
55

66
metadata(
@@ -10,16 +10,16 @@
1010
)
1111

1212

13-
cmd = command.mount(Alconna("echo", Args["content?", AllParam], meta=CommandMeta("显示消息", compact=True)))
14-
15-
16-
@cmd.handle
17-
async def echo_handle(content: Match[MessageChain], session: Session):
18-
if content.available:
19-
return await session.send(content.result)
20-
21-
22-
@cmd.on_execute()
23-
async def echo_exec(content: Match[MessageChain]):
24-
if content.available:
13+
@(
14+
command.command("echo <...content>", "显示消息")
15+
.option("escape", "-e|--escape # 发送转义消息")
16+
.option("unescape", "-E|--unescape # 发送反转义消息")
17+
.config(compact=True)
18+
)
19+
def echo(content: Match[MessageChain], arp: Arparma):
20+
if arp.find("unescape"):
21+
return MessageChain.of(content.result.extract_plain_text())
22+
elif arp.find("escape"):
23+
return str(content.result)
24+
else:
2525
return content.result

arclet/entari/command/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from .plugin import mount
2525
from .provider import AlconnaProviderFactory, AlconnaSuppiler, MessageJudges
2626

27-
TM = TypeVar("TM", str, MessageChain)
27+
TM = TypeVar("TM", bound=Union[str, MessageChain])
2828

2929

3030
def get_cmd(target: Subscriber):

arclet/entari/message.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
from typing_extensions import Self, SupportsIndex, TypeAlias
77

88
from satori import select as satori_select
9-
from satori.element import At, Element, Link, Sharp, Style, Text
9+
from satori.element import At, Element, Link, Sharp, Style, Text, transform
10+
from satori.parser import parse
1011

1112
T = TypeVar("T")
1213
TE = TypeVar("TE", bound=Element)
@@ -615,3 +616,7 @@ def display(self):
615616
return "".join(
616617
str(elem) if isinstance(elem, (Text, Style, At, Sharp, Link)) else elem.__class__.__name__ for elem in self
617618
)
619+
620+
@staticmethod
621+
def of(text: str):
622+
return MessageChain(transform(parse(text)))

0 commit comments

Comments
 (0)