11import asyncio
22import weakref
3+ from contextlib import AsyncExitStack
34from typing import Any , Union , Literal , Optional
45
56import nonebot
67from tarina import lang
7- from nonebot .typing import T_State
88from nonebot .matcher import Matcher
99from nonebot .utils import escape_tag
1010from pydantic import ValidationError
1111from nonebot .adapters import Bot , Event
1212from nonebot .internal .rule import Rule as Rule
13+ from nonebot .internal .params import DependencyCache
1314from nonebot import require , get_driver , get_plugin_config
1415from arclet .alconna .exceptions import SpecialOptionTriggered
16+ from nonebot .typing import T_State , T_RuleChecker , _DependentCallable
1517from arclet .alconna import Alconna , Arparma , CompSession , output_manager , command_manager
1618
1719from .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