Skip to content

Commit

Permalink
✨ dynamic inject param
Browse files Browse the repository at this point in the history
  • Loading branch information
RF-Tar-Railt committed Oct 20, 2023
1 parent 83aa22b commit 5f42106
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
8 changes: 0 additions & 8 deletions src/nonebot_plugin_alconna/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
from .tools import image_fetch as image_fetch
from .params import match_value as match_value
from .params import AlconnaMatch as AlconnaMatch
from .params import AlconnaParam as AlconnaParam
from .params import AlconnaQuery as AlconnaQuery
from .model import CommandResult as CommandResult
from .params import AlcExecResult as AlcExecResult
Expand Down Expand Up @@ -103,16 +102,9 @@
},
)


_use_param = True

with contextlib.suppress(ValueError, LookupError):
global_config = get_driver().config
_config = Config.parse_obj(global_config)
_use_param = _config.alconna_use_param
for path in _config.alconna_global_extensions:
log("DEBUG", lang.require("nbp-alc", "log.load_global_extensions").format(path=path))
load_from_path(path)

if _use_param:
AlconnaMatcher.extend_param(AlconnaParam)
35 changes: 23 additions & 12 deletions src/nonebot_plugin_alconna/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
from typing import Any, Union, Callable, ClassVar, Iterable, NoReturn, Protocol

from nonebot.rule import Rule
from nonebot import get_driver
from nonebot.params import Depends
from _weakref import _remove_dead_weakref
from nonebot.permission import Permission
from nonebot.dependencies import Dependent
from nonebot.message import run_postprocessor
from nepattern import STRING, AnyOne, AnyString
from nonebot.consts import ARG_KEY, RECEIVE_KEY
from nonebot.internal.params import DefaultParam
from nonebot.dependencies import Param, Dependent
from tarina import lang, is_awaitable, run_always_await
from arclet.alconna.tools import AlconnaFormat, AlconnaString
from arclet.alconna.tools.construct import FuncMounter, MountConfig
Expand All @@ -25,13 +26,24 @@
from nonebot.matcher import Matcher, matchers, current_bot, current_event, current_matcher

from .rule import alconna
from .config import Config
from .typings import MReturn
from .model import CompConfig
from .uniseg import Segment, UniMessage
from .uniseg.template import UniMessageTemplate
from .extension import Extension, ExtensionExecutor
from .consts import ALCONNA_RESULT, ALCONNA_ARG_KEY, log
from .params import CHECK, MIDDLEWARE, Check, AlcExecResult, assign, _seminal, _Dispatch, merge_path
from .params import (
CHECK,
MIDDLEWARE,
Check,
AlconnaParam,
AlcExecResult,
assign,
_seminal,
_Dispatch,
merge_path,
)

_M = Union[str, Message, MessageSegment, MessageTemplate, Segment, UniMessage, UniMessageTemplate]

Expand Down Expand Up @@ -76,15 +88,6 @@ class AlconnaMatcher(Matcher):
basepath: ClassVar[str]
executor: ClassVar[ExtensionExecutor]

@classmethod
def extend_param(cls, *params: type[Param]):
"""扩展 AlconnaMatcher 的 Param 种类"""
cls.HANDLER_PARAM_TYPES = (
*cls.HANDLER_PARAM_TYPES[:-1],
*params,
DefaultParam,
)

@classmethod
def shortcut(cls, key: str, args: ShortcutArgs | None = None, delete: bool = False):
return cls.command.shortcut(key, args, delete)
Expand Down Expand Up @@ -266,7 +269,7 @@ def dispatch(
fn = _Dispatch(merge_path(path, cls.basepath), value, or_not, additional)
cls.handle()(fn.set)

matcher: type[AlconnaMatcher] = AlconnaMatcher.new(
matcher: type[AlconnaMatcher] = cls.new(
"",
rule & Rule(fn),
Permission() | permission,
Expand Down Expand Up @@ -554,6 +557,14 @@ def on_alconna(
use_cmd_start,
use_cmd_sep,
)
try:
global_config = get_driver().config
config = Config.parse_obj(global_config)
use_param = config.alconna_use_param
except ValueError:
use_param = True
if use_param:
AlconnaMatcher.HANDLER_PARAM_TYPES = Matcher.HANDLER_PARAM_TYPES[:-1] + (AlconnaParam, DefaultParam)
matcher: type[AlconnaMatcher] = AlconnaMatcher.new(
"",
rule & _rule,
Expand Down

0 comments on commit 5f42106

Please sign in to comment.