Skip to content

Commit 7c2fde6

Browse files
committed
✨ after_rules
1 parent 6319995 commit 7c2fde6

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

src/nonebot_plugin_alconna/matcher.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,7 @@ def ensure_context(self, bot: Bot, event: Event):
943943
def on_alconna(
944944
command: Alconna | str,
945945
rule: None | Rule | T_RuleChecker = None,
946+
after_rule: None | Rule | T_RuleChecker = None,
946947
skip_for_unmatch: bool = True,
947948
auto_send_output: bool | None = None,
948949
aliases: set[str] | tuple[str, ...] | None = None,
@@ -968,6 +969,7 @@ def on_alconna(
968969
参数:
969970
command: Alconna 命令
970971
rule: 事件响应规则
972+
after_rule: 作用于 Alconna 解析后的事件响应规则
971973
skip_for_unmatch: 是否在解析失败时跳过
972974
auto_send_output: 是否自动发送输出信息并跳过
973975
aliases: 命令别名
@@ -1018,6 +1020,7 @@ def on_alconna(
10181020
response_self,
10191021
aliases,
10201022
rule,
1023+
after_rule,
10211024
)
10221025
executor = _rule.executor
10231026
params = (

src/nonebot_plugin_alconna/rule.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class AlconnaRule:
6565
"_path",
6666
"_tasks",
6767
"_waiter",
68+
"after_rules",
6869
"auto_send",
6970
"before_rules",
7071
"command",
@@ -89,6 +90,7 @@ def __init__(
8990
response_self: Optional[bool] = None,
9091
_aliases: Optional[Union[set[str], tuple[str, ...]]] = None,
9192
before_rule: Optional[Union[Rule, T_RuleChecker]] = None,
93+
after_rule: Optional[Union[Rule, T_RuleChecker]] = None,
9294
):
9395
if isinstance(comp_config, bool):
9496
self.comp_config = {} if comp_config else None
@@ -156,6 +158,7 @@ def _update(cmd_id: int):
156158
self._namespace = command.namespace
157159
self._tasks: dict[str, asyncio.Task] = {}
158160
self.before_rules = Rule() & before_rule
161+
self.after_rules = Rule() & after_rule
159162

160163
self._comp_help = ""
161164
if self.comp_config is not None:
@@ -357,6 +360,8 @@ async def __call__(
357360
state[ALCONNA_RESULT] = CommandResult(result=arp, output=may_help_text)
358361
state[ALCONNA_EXEC_RESULT] = cmd.exec_result
359362
state[ALCONNA_EXTENSION] = selected
363+
if not await self.after_rules(bot, event, state, stack, dependency_cache):
364+
return False
360365
return True
361366

362367
async def send(self, text: str, bot: Bot, event: Event, arp: Arparma) -> Any:

src/nonebot_plugin_alconna/shortcut.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def funcommand(
4343
use_cmd_sep: bool | None = None,
4444
response_self: bool | None = None,
4545
rule: Rule | T_RuleChecker | None = None,
46+
after_rule: Rule | T_RuleChecker | None = None,
4647
permission: Permission | T_PermissionChecker | None = None,
4748
*,
4849
handlers: list[T_Handler | Dependent] | None = None,
@@ -65,6 +66,7 @@ def wrapper(func: Callable[..., MReturn]) -> type[AlconnaMatcher]:
6566
matcher = on_alconna(
6667
FuncMounter(func, _config),
6768
rule,
69+
after_rule,
6870
skip_for_unmatch,
6971
auto_send_output,
7072
extensions=extensions,
@@ -105,6 +107,7 @@ def args_gen(pattern: str, types: dict):
105107
def build(
106108
self,
107109
rule: Rule | T_RuleChecker | None = None,
110+
after_rule: Rule | T_RuleChecker | None = None,
108111
skip_for_unmatch: bool = True,
109112
auto_send_output: bool | None = None,
110113
aliases: set[str] | tuple[str, ...] | None = None,

src/nonebot_plugin_alconna/uniseg/message.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from json import dumps, loads
77
from pathlib import Path
88
from types import FunctionType
9-
from typing import TYPE_CHECKING, Any, Callable, Literal, NoReturn, TypeVar, Union
9+
from typing import TYPE_CHECKING, Any, Callable, Literal, NoReturn, TypeVar, Union, Protocol
1010
from typing_extensions import Self, SupportsIndex, deprecated
1111

1212
from nonebot.exception import FinishedException
@@ -46,6 +46,7 @@
4646
from .template import UniMessageTemplate
4747

4848
TS = TypeVar("TS", bound=Segment)
49+
_TM = TypeVar("_TM", bound="str | Message | UniMessage")
4950

5051

5152
class _method:
@@ -58,7 +59,11 @@ def __get__(self, instance, owner):
5859
return self.__func__.__get__(instance, owner)
5960

6061

61-
current_send_wrapper = ContextModel("nonebot_plugin_alconna.uniseg.send_wrapper")
62+
class SendWrapper(Protocol):
63+
async def __call__(self, bot: Bot, event: Event, send: _TM) -> _TM: ...
64+
65+
66+
current_send_wrapper: ContextModel[SendWrapper] = ContextModel("nonebot_plugin_alconna.uniseg.send_wrapper")
6267
MessageContainer = Union[str, Segment, Sequence["MessageContainer"], "UniMessage"]
6368

6469

0 commit comments

Comments
 (0)