Skip to content

Commit 57b64de

Browse files
committed
⬆️ version 0.2.0
1 parent a90fae4 commit 57b64de

7 files changed

Lines changed: 278 additions & 262 deletions

File tree

arclet/entari/command.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from copy import deepcopy
33
from dataclasses import dataclass, field
44
import inspect
5-
from typing import Any, Callable, Generic, Optional, TypeVar, Union, cast, get_args, overload
5+
from typing import Any, Callable, Generic, Literal, Optional, TypeVar, Union, cast, get_args, overload
66

77
from arclet.alconna import (
88
Alconna,
@@ -34,7 +34,8 @@
3434
)
3535
from arclet.letoderea.handler import depend_handler
3636
from arclet.letoderea.provider import ProviderFactory
37-
from nepattern import DirectPattern, main
37+
from nepattern import DirectPattern
38+
from nepattern.util import CUnionType
3839
from pygtrie import CharTrie
3940
from satori.client import Account
4041
from satori.element import At, Text
@@ -174,7 +175,7 @@ async def __call__(self, scope: Scope, context: Contexts) -> Optional[Union[bool
174175
context["alc_result"] = CommandResult(self.cmd, _res, may_help_text)
175176
return context
176177
elif may_help_text:
177-
await account.send(context["$event"], may_help_text)
178+
await account.send(context["$event"], MessageChain(may_help_text))
178179
return False
179180

180181
@property
@@ -183,9 +184,9 @@ def scopes(self) -> set[Scope]:
183184

184185

185186
class AlconnaProvider(Provider[Any]):
186-
def __init__(self, type: str, extra: Optional[dict] = None):
187+
def __init__(self, type_: str, extra: Optional[dict] = None):
187188
super().__init__()
188-
self.type = type
189+
self.type = type_
189190
self.extra = extra or {}
190191

191192
async def __call__(self, context: Contexts):
@@ -221,7 +222,7 @@ async def __call__(self, context: Contexts):
221222
class AlconnaProviderFactory(ProviderFactory):
222223
def validate(self, param: Param):
223224
annotation = get_origin(param.annotation)
224-
if annotation in main._Contents:
225+
if annotation in (Union, CUnionType, Literal):
225226
annotation = get_origin(get_args(param.annotation)[0])
226227
if annotation is CommandResult:
227228
return AlconnaProvider("result")
@@ -254,7 +255,11 @@ def __init__(self, need_tome: bool = False, remove_tome: bool = False):
254255
plugins["~command.EntariCommands"] = self.publisher
255256
self.need_tome = need_tome
256257
self.remove_tome = remove_tome
257-
config.namespaces["Entari"] = Namespace(self.__namespace__)
258+
config.namespaces["Entari"] = Namespace(
259+
self.__namespace__,
260+
to_text=lambda x: x.text if x.__class__ is Text else None,
261+
converter=lambda x: MessageChain(x),
262+
)
258263

259264
@self.publisher.register(auxiliaries=[MessageJudger()])
260265
async def listener(event: MessageEvent):
@@ -340,8 +345,7 @@ def on(
340345
remove_tome: bool = False,
341346
auxiliaries: Optional[list[BaseAuxiliary]] = None,
342347
providers: Optional[list[Provider, type[Provider], ProviderFactory, type[ProviderFactory]]] = None,
343-
) -> Callable[[TCallable], TCallable]:
344-
...
348+
) -> Callable[[TCallable], TCallable]: ...
345349

346350
@overload
347351
def on(
@@ -354,8 +358,7 @@ def on(
354358
*,
355359
args: Optional[dict[str, Union[TAValue, Args, Arg]]] = None,
356360
meta: Optional[CommandMeta] = None,
357-
) -> Callable[[TCallable], TCallable]:
358-
...
361+
) -> Callable[[TCallable], TCallable]: ...
359362

360363
def on(
361364
self,

arclet/entari/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ async def event_parse_task(connection: Account, raw: Event):
5151

5252
logger.warning(f"received unsupported event {raw.type}: {raw}")
5353

54-
asyncio.create_task(event_parse_task(account, event))
54+
await event_parse_task(account, event)
5555

5656
# async def handle_lifecycle(self, account: Account, state: LoginStatus):
5757
# if state == LoginStatus.ONLINE:

arclet/entari/event.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class MessageEvent(Event):
218218
providers = [MessageContentProvider, QuoteProvider]
219219

220220
def __post_init__(self):
221-
self.content = MessageChain(self.message.content)
221+
self.content = MessageChain(self.message.message)
222222
if self.content.has(Quote):
223223
self.quote = self.content.get(Quote, 1)[0]
224224
self.content = self.content.exclude(Quote)
@@ -260,7 +260,7 @@ class ReactionEvent(NoticeEvent):
260260
providers = [MessageContentProvider, QuoteProvider]
261261

262262
def __post_init__(self):
263-
self.content = MessageChain(self.message.content)
263+
self.content = MessageChain(self.message.message)
264264
if self.content.has(Quote):
265265
self.quote = self.content.get(Quote, 1)[0]
266266
self.content = self.content.exclude(Quote)
@@ -338,7 +338,7 @@ class InteractionCommandMessageEvent(InteractionCommandEvent):
338338
providers = [MessageContentProvider, QuoteProvider]
339339

340340
def __post_init__(self):
341-
self.content = MessageChain(self.message.content)
341+
self.content = MessageChain(self.message.message)
342342
if self.content.has(Quote):
343343
self.quote = self.content.get(Quote, 1)[0]
344344
self.content = self.content.exclude(Quote)

arclet/entari/message.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,13 @@ def __repr__(self) -> str:
3939
return "[" + ", ".join(repr(seg) for seg in self) + "]"
4040

4141
@overload
42-
def __add__(self, other: str) -> MessageChain[TE | Text]:
43-
...
42+
def __add__(self, other: str) -> MessageChain[TE | Text]: ...
4443

4544
@overload
46-
def __add__(self, other: TE | Iterable[TE]) -> MessageChain[TE]:
47-
...
45+
def __add__(self, other: TE | Iterable[TE]) -> MessageChain[TE]: ...
4846

4947
@overload
50-
def __add__(self, other: TE1 | Iterable[TE1]) -> MessageChain[TE | TE1]:
51-
...
48+
def __add__(self, other: TE1 | Iterable[TE1]) -> MessageChain[TE | TE1]: ...
5249

5350
def __add__(self, other: str | TE | TE1 | Iterable[TE | TE1]) -> MessageChain:
5451
result: MessageChain = self.copy()
@@ -70,16 +67,13 @@ def __add__(self, other: str | TE | TE1 | Iterable[TE | TE1]) -> MessageChain:
7067
return result
7168

7269
@overload
73-
def __radd__(self, other: str) -> MessageChain[Text | TE]:
74-
...
70+
def __radd__(self, other: str) -> MessageChain[Text | TE]: ...
7571

7672
@overload
77-
def __radd__(self, other: TE | Iterable[TE]) -> MessageChain[TE]:
78-
...
73+
def __radd__(self, other: TE | Iterable[TE]) -> MessageChain[TE]: ...
7974

8075
@overload
81-
def __radd__(self, other: TE1 | Iterable[TE1]) -> MessageChain[TE1 | TE]:
82-
...
76+
def __radd__(self, other: TE1 | Iterable[TE1]) -> MessageChain[TE1 | TE]: ...
8377

8478
def __radd__(self, other: str | TE1 | Iterable[TE1]) -> MessageChain:
8579
result = MessageChain(other)
@@ -160,7 +154,7 @@ def __getitem__(self, args: slice) -> Self:
160154

161155
def __getitem__(
162156
self,
163-
args: (type[TE1] | tuple[type[TE1], int] | tuple[type[TE1], slice] | int | slice),
157+
args: type[TE1] | tuple[type[TE1], int] | tuple[type[TE1], slice] | int | slice,
164158
) -> TE | TE1 | MessageChain[TE1] | Self:
165159
arg1, arg2 = args if isinstance(args, tuple) else (args, None)
166160
if isinstance(arg1, int) and arg2 is None:

main.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
from satori import Image
12
from arclet.entari import ContextSession, Entari, EntariCommands, MessageCreatedEvent, Plugin, WebsocketsInfo
23

34
command = EntariCommands()
45

56

7+
@command.on("echoimg {img}")
8+
async def echoimg(img: Image, session: ContextSession):
9+
await session.send_message([img])
10+
11+
612
@command.on("add {a} {b}")
713
async def add(a: int, b: int, session: ContextSession):
814
await session.send_message(f"{a + b =}")
@@ -19,8 +25,8 @@ async def _(event: MessageCreatedEvent):
1925
app = Entari()
2026
app.apply(
2127
WebsocketsInfo(
22-
port=5500,
23-
token="9491ee65f2e5322d050021d4ceaca05d42c3ff2fc2a457fdffeb315619bf3f91",
28+
port=7777,
29+
token="fa1ccfd6a9fcac523f3af2f67575e54230b1aef5df69a6886a3bae140e39a13b",
2430
)
2531
)
2632

0 commit comments

Comments
 (0)