Skip to content

Commit 45536d5

Browse files
committed
🐛 fix rule order
resolve #89
1 parent 33f27ab commit 45536d5

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ async def _():
103103
- ⬇️: 支持接收
104104
- ⬆️: 支持发送
105105
- ➖: 情况不存在
106-
- ❌: 插件未支持
106+
- ❌: 插件/适配器未支持
107107
- 🚫: 协议未支持
108108
- (🚧): 计划中或部分支持或为实验性支持
109109

src/nonebot_plugin_alconna/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import contextlib
2+
from typing_extensions import deprecated
23

34
import nonebot
45

@@ -62,7 +63,6 @@
6263
from .uniseg import at_me as at_me
6364
from .typings import Style as Style
6465
from .params import assign as assign
65-
from .rule import alconna as alconna
6666
from .uniseg import Button as Button
6767
from .uniseg import FORBID as FORBID
6868
from .uniseg import IGNORE as IGNORE
@@ -91,6 +91,7 @@
9191
from .params import AlcMatches as AlcMatches
9292
from .params import AlconnaArg as AlconnaArg
9393
from .params import match_path as match_path
94+
from .rule import AlconnaRule as AlconnaRule
9495
from .uniseg import CustomNode as CustomNode
9596
from .uniseg import UniMessage as UniMessage
9697
from .extension import Extension as Extension
@@ -194,3 +195,8 @@ def load_builtin_plugins(*plugins: str):
194195
apply_fetch_targets()
195196
if _config.alconna_builtin_plugins:
196197
load_builtin_plugins(*_config.alconna_builtin_plugins)
198+
199+
200+
@deprecated("`alconna(...)` are no longer supported and will be removed in the future. Use `AlconnaRule(...)` instead.")
201+
def alconna(*args, **kwargs):
202+
return AlconnaRule(*args, **kwargs).rule

src/nonebot_plugin_alconna/matcher.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
from nonebot.matcher import Matcher, matchers, current_bot, current_event, current_matcher
3434

3535
from .i18n import Lang
36-
from .rule import alconna
3736
from .config import Config
3837
from .util import annotation
3938
from .model import CompConfig
39+
from .rule import AlconnaRule
4040
from .uniseg import Text, Segment, UniMessage
4141
from .uniseg.fallback import FallbackStrategy
4242
from .uniseg.template import UniMessageTemplate
@@ -113,11 +113,11 @@ class AlconnaMatcher(Matcher):
113113
executor: ClassVar[ExtensionExecutor]
114114
_command_path: ClassVar[str]
115115
_tests: ClassVar[list[tuple[UniMessage, dict[str, Any] | None, bool]]]
116-
_rule: ClassVar[Rule]
116+
_rule: ClassVar[AlconnaRule]
117117

118118
@classmethod
119119
def command(cls) -> Alconna:
120-
return list(cls._rule.checkers)[0].call.command() # noqa: RUF015
120+
return cls._rule.command() # type: ignore
121121

122122
@classmethod
123123
@overload
@@ -972,7 +972,7 @@ def on_alconna(
972972
command.formatter.add(command)
973973
except ValueError:
974974
pass
975-
_rule = alconna(
975+
_rule = AlconnaRule(
976976
command,
977977
skip_for_unmatch,
978978
auto_send_output,
@@ -985,7 +985,7 @@ def on_alconna(
985985
response_self,
986986
aliases,
987987
)
988-
executor = cast(ExtensionExecutor, next(iter(_rule.checkers)).call.executor) # type: ignore
988+
executor = _rule.executor
989989
params = (
990990
(ExtensionParam.new(executor),)
991991
+ Matcher.HANDLER_PARAM_TYPES[:-1]
@@ -1001,7 +1001,7 @@ def on_alconna(
10011001
{
10021002
"_source": source,
10031003
"type": "",
1004-
"rule": _rule & rule,
1004+
"rule": rule & _rule.rule,
10051005
"permission": Permission() | permission,
10061006
"handlers": (
10071007
[

src/nonebot_plugin_alconna/rule.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from typing import Any, Union, Literal, Optional
44

55
import nonebot
6+
from tarina import lang
67
from nonebot.typing import T_State
7-
from tarina import lang, init_spec
88
from nonebot.matcher import Matcher
99
from nonebot.utils import escape_tag
1010
from pydantic import ValidationError
@@ -198,6 +198,10 @@ async def _waiter_handle(_event: Event, _matcher: Matcher, content: UniMsg):
198198

199199
self._waiter = _waiter_handle
200200

201+
@property
202+
def rule(self) -> Rule:
203+
return Rule(self)
204+
201205
def __repr__(self) -> str:
202206
return f"Alconna(command={self.command()!r})"
203207

@@ -348,8 +352,3 @@ async def send(self, text: str, bot: Bot, event: Event, arp: Arparma) -> Any:
348352
return await bot.send(event, await msg.export(bot, fallback=True)) # type: ignore
349353
except NotImplementedError:
350354
return await bot.send(event, event.get_message().__class__(text))
351-
352-
353-
@init_spec(AlconnaRule)
354-
def alconna(rule: AlconnaRule) -> Rule:
355-
return Rule(rule)

src/nonebot_plugin_alconna/uniseg/params.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,40 @@
22

33
from nonebot.typing import T_State
44
from nonebot.internal.params import Depends
5+
from nonebot.exception import SkippedException
56
from nonebot.internal.adapter import Bot, Event
67

7-
from .exporter import Target
88
from .message import TS, UniMessage
9+
from .exporter import Target, SerializeFailed
910
from .constraint import UNISEG_TARGET, UNISEG_MESSAGE, UNISEG_MESSAGE_ID
1011

1112

1213
async def _uni_msg(bot: Bot, event: Event, state: T_State) -> UniMessage:
1314
if UNISEG_MESSAGE in state:
1415
return state[UNISEG_MESSAGE]
16+
try:
17+
event.get_message()
18+
except ValueError:
19+
raise SkippedException from None
1520
return await UniMessage.generate(event=event, bot=bot)
1621

1722

1823
def _target(bot: Bot, event: Event, state: T_State) -> Target:
1924
if UNISEG_TARGET in state:
2025
return state[UNISEG_TARGET]
21-
return UniMessage.get_target(event=event, bot=bot)
26+
try:
27+
return UniMessage.get_target(event=event, bot=bot)
28+
except (SerializeFailed, NotImplementedError, ValueError):
29+
raise SkippedException from None
2230

2331

2432
def _msg_id(bot: Bot, event: Event, state: T_State) -> str:
2533
if UNISEG_MESSAGE_ID in state:
2634
return state[UNISEG_MESSAGE_ID]
35+
try:
36+
event.get_message()
37+
except ValueError:
38+
raise SkippedException from None
2739
return UniMessage.get_message_id(event=event, bot=bot)
2840

2941

0 commit comments

Comments
 (0)