Skip to content

Commit 30375e9

Browse files
committed
🍻 before_rules
resolve #119
1 parent c9ef4b7 commit 30375e9

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/nonebot_plugin_alconna/matcher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,7 @@ def on_alconna(
10171017
use_cmd_sep,
10181018
response_self,
10191019
aliases,
1020+
rule,
10201021
)
10211022
executor = _rule.executor
10221023
params = (
@@ -1032,7 +1033,7 @@ def on_alconna(
10321033
{
10331034
"_source": source,
10341035
"type": "",
1035-
"rule": rule & _rule.rule,
1036+
"rule": _rule.rule,
10361037
"permission": Permission() | permission,
10371038
"handlers": (
10381039
[

src/nonebot_plugin_alconna/rule.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import asyncio
22
import weakref
3+
from contextlib import AsyncExitStack
34
from typing import Any, Union, Literal, Optional
45

56
import nonebot
67
from tarina import lang
7-
from nonebot.typing import T_State
88
from nonebot.matcher import Matcher
99
from nonebot.utils import escape_tag
1010
from pydantic import ValidationError
1111
from nonebot.adapters import Bot, Event
1212
from nonebot.internal.rule import Rule as Rule
13+
from nonebot.internal.params import DependencyCache
1314
from nonebot import require, get_driver, get_plugin_config
1415
from arclet.alconna.exceptions import SpecialOptionTriggered
16+
from nonebot.typing import T_State, T_RuleChecker, _DependentCallable
1517
from arclet.alconna import Alconna, Arparma, CompSession, output_manager, command_manager
1618

1719
from .i18n import Lang
@@ -64,6 +66,7 @@ class AlconnaRule:
6466
"_tasks",
6567
"_waiter",
6668
"auto_send",
69+
"before_rules",
6770
"command",
6871
"comp_config",
6972
"executor",
@@ -85,6 +88,7 @@ def __init__(
8588
use_cmd_sep: Optional[bool] = None,
8689
response_self: Optional[bool] = None,
8790
_aliases: Optional[Union[set[str], tuple[str, ...]]] = None,
91+
before_rule: Optional[Union[Rule, T_RuleChecker]] = None,
8892
):
8993
if isinstance(comp_config, bool):
9094
self.comp_config = {} if comp_config else None
@@ -151,6 +155,7 @@ def _update(cmd_id: int):
151155
self._path = command.path
152156
self._namespace = command.namespace
153157
self._tasks: dict[str, asyncio.Task] = {}
158+
self.before_rules = Rule() & before_rule
154159

155160
self._comp_help = ""
156161
if self.comp_config is not None:
@@ -281,7 +286,16 @@ def _checker(_event: Event):
281286
interface.exit()
282287
return res
283288

284-
async def __call__(self, event: Event, state: T_State, bot: Bot) -> bool:
289+
async def __call__(
290+
self,
291+
bot: Bot,
292+
event: Event,
293+
state: T_State,
294+
stack: Optional[AsyncExitStack] = None,
295+
dependency_cache: Optional[dict[_DependentCallable[Any], DependencyCache]] = None,
296+
) -> bool:
297+
if not await self.before_rules(bot, event, state, stack, dependency_cache):
298+
return False
285299
if event.get_type() == "meta_event":
286300
return False
287301
selected = self.executor.select(bot, event)

0 commit comments

Comments
 (0)