2021-11-15 21:44:24 +08:00
|
|
|
import inspect
|
|
|
|
from typing import Any, Dict, Optional
|
|
|
|
|
2021-11-22 11:38:42 +08:00
|
|
|
from pydantic.fields import Undefined
|
|
|
|
|
2021-11-15 21:44:24 +08:00
|
|
|
from nonebot.typing import T_State
|
2021-11-16 18:30:16 +08:00
|
|
|
from nonebot.dependencies import Param
|
2021-11-15 21:44:24 +08:00
|
|
|
from nonebot.adapters import Bot, Event
|
2021-11-16 18:30:16 +08:00
|
|
|
from nonebot.utils import generic_check_issubclass
|
2021-11-15 21:44:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BotParam(Param):
|
|
|
|
@classmethod
|
|
|
|
def _check(cls, name: str, param: inspect.Parameter) -> bool:
|
2021-11-22 23:21:26 +08:00
|
|
|
return generic_check_issubclass(param.annotation, Bot) or (
|
|
|
|
param.annotation == param.empty and name == "bot"
|
|
|
|
)
|
2021-11-15 21:44:24 +08:00
|
|
|
|
|
|
|
def _solve(self, bot: Bot, **kwargs: Any) -> Any:
|
|
|
|
return bot
|
|
|
|
|
|
|
|
|
|
|
|
class EventParam(Param):
|
|
|
|
@classmethod
|
|
|
|
def _check(cls, name: str, param: inspect.Parameter) -> bool:
|
2021-11-22 23:21:26 +08:00
|
|
|
return generic_check_issubclass(param.annotation, Event) or (
|
|
|
|
param.annotation == param.empty and name == "event"
|
|
|
|
)
|
2021-11-15 21:44:24 +08:00
|
|
|
|
|
|
|
def _solve(self, event: Event, **kwargs: Any) -> Any:
|
|
|
|
return event
|
|
|
|
|
|
|
|
|
|
|
|
class StateParam(Param):
|
|
|
|
@classmethod
|
|
|
|
def _check(cls, name: str, param: inspect.Parameter) -> bool:
|
2021-11-22 23:21:26 +08:00
|
|
|
return generic_check_issubclass(param.annotation, Dict) or (
|
|
|
|
param.annotation == param.empty and name == "state"
|
|
|
|
)
|
2021-11-15 21:44:24 +08:00
|
|
|
|
|
|
|
def _solve(self, state: T_State, **kwargs: Any) -> Any:
|
|
|
|
return state
|
|
|
|
|
|
|
|
|
|
|
|
class MatcherParam(Param):
|
|
|
|
@classmethod
|
|
|
|
def _check(cls, name: str, param: inspect.Parameter) -> bool:
|
2021-11-22 23:21:26 +08:00
|
|
|
return generic_check_issubclass(param.annotation, Matcher) or (
|
|
|
|
param.annotation == param.empty and name == "matcher"
|
|
|
|
)
|
2021-11-15 21:44:24 +08:00
|
|
|
|
|
|
|
def _solve(self, matcher: Optional["Matcher"] = None, **kwargs: Any) -> Any:
|
|
|
|
return matcher
|
|
|
|
|
|
|
|
|
|
|
|
class ExceptionParam(Param):
|
|
|
|
@classmethod
|
|
|
|
def _check(cls, name: str, param: inspect.Parameter) -> bool:
|
2021-11-22 23:21:26 +08:00
|
|
|
return generic_check_issubclass(param.annotation, Exception) or (
|
|
|
|
param.annotation == param.empty and name == "exception"
|
|
|
|
)
|
2021-11-15 21:44:24 +08:00
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
def _solve(self, exception: Optional[Exception] = None, **kwargs: Any) -> Any:
|
2021-11-15 21:44:24 +08:00
|
|
|
return exception
|
|
|
|
|
|
|
|
|
2021-11-22 11:38:42 +08:00
|
|
|
class DefaultParam(Param):
|
|
|
|
@classmethod
|
|
|
|
def _check(cls, name: str, param: inspect.Parameter) -> bool:
|
|
|
|
return param.default != param.empty
|
|
|
|
|
|
|
|
def _solve(self, **kwargs: Any) -> Any:
|
|
|
|
return Undefined
|
|
|
|
|
|
|
|
|
2021-11-16 18:30:16 +08:00
|
|
|
from nonebot.matcher import Matcher
|