2022-01-19 16:16:56 +08:00
|
|
|
"""本模块定义了依赖注入的各类参数。
|
|
|
|
|
2022-01-16 11:30:09 +08:00
|
|
|
FrontMatter:
|
|
|
|
sidebar_position: 4
|
|
|
|
description: nonebot.params 模块
|
|
|
|
"""
|
|
|
|
|
2022-08-24 09:54:08 +08:00
|
|
|
from typing import Any, Dict, List, Tuple, Union, Optional
|
2022-02-06 14:52:50 +08:00
|
|
|
|
|
|
|
from nonebot.typing import T_State
|
|
|
|
from nonebot.matcher import Matcher
|
|
|
|
from nonebot.internal.params import Arg as Arg
|
|
|
|
from nonebot.internal.params import ArgStr as ArgStr
|
|
|
|
from nonebot.internal.params import Depends as Depends
|
|
|
|
from nonebot.internal.params import ArgParam as ArgParam
|
|
|
|
from nonebot.internal.params import BotParam as BotParam
|
2022-08-24 09:54:08 +08:00
|
|
|
from nonebot.adapters import Event, Message, MessageSegment
|
2022-02-06 14:52:50 +08:00
|
|
|
from nonebot.internal.params import EventParam as EventParam
|
|
|
|
from nonebot.internal.params import StateParam as StateParam
|
|
|
|
from nonebot.internal.params import DependParam as DependParam
|
|
|
|
from nonebot.internal.params import ArgPlainText as ArgPlainText
|
|
|
|
from nonebot.internal.params import DefaultParam as DefaultParam
|
|
|
|
from nonebot.internal.params import MatcherParam as MatcherParam
|
|
|
|
from nonebot.internal.params import ExceptionParam as ExceptionParam
|
2021-12-14 01:08:48 +08:00
|
|
|
from nonebot.consts import (
|
|
|
|
CMD_KEY,
|
|
|
|
PREFIX_KEY,
|
|
|
|
REGEX_DICT,
|
|
|
|
SHELL_ARGS,
|
|
|
|
SHELL_ARGV,
|
|
|
|
CMD_ARG_KEY,
|
2022-10-12 13:41:28 +08:00
|
|
|
KEYWORD_KEY,
|
2021-12-14 01:08:48 +08:00
|
|
|
RAW_CMD_KEY,
|
|
|
|
REGEX_GROUP,
|
2022-10-12 13:41:28 +08:00
|
|
|
ENDSWITH_KEY,
|
2022-04-20 14:43:29 +08:00
|
|
|
CMD_START_KEY,
|
2022-10-12 13:41:28 +08:00
|
|
|
FULLMATCH_KEY,
|
2021-12-14 01:08:48 +08:00
|
|
|
REGEX_MATCHED,
|
2022-10-12 13:41:28 +08:00
|
|
|
STARTSWITH_KEY,
|
2021-12-14 01:08:48 +08:00
|
|
|
)
|
2021-11-15 21:44:24 +08:00
|
|
|
|
|
|
|
|
2021-12-14 01:08:48 +08:00
|
|
|
async def _event_type(event: Event) -> str:
|
|
|
|
return event.get_type()
|
|
|
|
|
|
|
|
|
|
|
|
def EventType() -> str:
|
2022-02-06 14:52:50 +08:00
|
|
|
"""{ref}`nonebot.adapters.Event` 类型参数"""
|
2021-12-14 01:08:48 +08:00
|
|
|
return Depends(_event_type)
|
|
|
|
|
|
|
|
|
|
|
|
async def _event_message(event: Event) -> Message:
|
|
|
|
return event.get_message()
|
|
|
|
|
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
def EventMessage() -> Any:
|
2022-02-06 14:52:50 +08:00
|
|
|
"""{ref}`nonebot.adapters.Event` 消息参数"""
|
2021-12-14 01:08:48 +08:00
|
|
|
return Depends(_event_message)
|
|
|
|
|
|
|
|
|
|
|
|
async def _event_plain_text(event: Event) -> str:
|
|
|
|
return event.get_plaintext()
|
|
|
|
|
|
|
|
|
|
|
|
def EventPlainText() -> str:
|
2022-02-06 14:52:50 +08:00
|
|
|
"""{ref}`nonebot.adapters.Event` 纯文本消息参数"""
|
2021-12-14 01:08:48 +08:00
|
|
|
return Depends(_event_plain_text)
|
|
|
|
|
|
|
|
|
|
|
|
async def _event_to_me(event: Event) -> bool:
|
|
|
|
return event.is_tome()
|
|
|
|
|
|
|
|
|
|
|
|
def EventToMe() -> bool:
|
2022-02-06 14:52:50 +08:00
|
|
|
"""{ref}`nonebot.adapters.Event` `to_me` 参数"""
|
2021-12-14 01:08:48 +08:00
|
|
|
return Depends(_event_to_me)
|
|
|
|
|
|
|
|
|
2022-01-10 11:20:06 +08:00
|
|
|
def _command(state: T_State) -> Message:
|
2021-12-14 01:08:48 +08:00
|
|
|
return state[PREFIX_KEY][CMD_KEY]
|
|
|
|
|
|
|
|
|
|
|
|
def Command() -> Tuple[str, ...]:
|
2022-01-19 16:16:56 +08:00
|
|
|
"""消息命令元组"""
|
|
|
|
return Depends(_command)
|
2021-12-14 01:08:48 +08:00
|
|
|
|
|
|
|
|
2022-01-10 11:20:06 +08:00
|
|
|
def _raw_command(state: T_State) -> Message:
|
2021-12-14 01:08:48 +08:00
|
|
|
return state[PREFIX_KEY][RAW_CMD_KEY]
|
|
|
|
|
|
|
|
|
|
|
|
def RawCommand() -> str:
|
2022-01-19 16:16:56 +08:00
|
|
|
"""消息命令文本"""
|
|
|
|
return Depends(_raw_command)
|
2021-12-14 01:08:48 +08:00
|
|
|
|
|
|
|
|
2022-01-10 11:20:06 +08:00
|
|
|
def _command_arg(state: T_State) -> Message:
|
2021-12-14 01:08:48 +08:00
|
|
|
return state[PREFIX_KEY][CMD_ARG_KEY]
|
|
|
|
|
|
|
|
|
2021-12-20 00:28:02 +08:00
|
|
|
def CommandArg() -> Any:
|
2022-01-19 16:16:56 +08:00
|
|
|
"""消息命令参数"""
|
|
|
|
return Depends(_command_arg)
|
2021-12-14 01:08:48 +08:00
|
|
|
|
|
|
|
|
2022-04-20 14:43:29 +08:00
|
|
|
def _command_start(state: T_State) -> str:
|
|
|
|
return state[PREFIX_KEY][CMD_START_KEY]
|
|
|
|
|
|
|
|
|
|
|
|
def CommandStart() -> str:
|
|
|
|
"""消息命令开头"""
|
|
|
|
return Depends(_command_start)
|
|
|
|
|
|
|
|
|
2022-01-10 11:20:06 +08:00
|
|
|
def _shell_command_args(state: T_State) -> Any:
|
2022-08-24 09:54:08 +08:00
|
|
|
return state[SHELL_ARGS] # Namespace or ParserExit
|
2021-12-14 22:40:47 +08:00
|
|
|
|
|
|
|
|
2022-08-24 09:54:08 +08:00
|
|
|
def ShellCommandArgs() -> Any:
|
2022-01-19 16:16:56 +08:00
|
|
|
"""shell 命令解析后的参数字典"""
|
2021-12-16 23:22:25 +08:00
|
|
|
return Depends(_shell_command_args, use_cache=False)
|
2021-12-14 22:40:47 +08:00
|
|
|
|
|
|
|
|
2022-08-24 09:54:08 +08:00
|
|
|
def _shell_command_argv(state: T_State) -> List[Union[str, MessageSegment]]:
|
2021-12-14 22:40:47 +08:00
|
|
|
return state[SHELL_ARGV]
|
|
|
|
|
|
|
|
|
|
|
|
def ShellCommandArgv() -> Any:
|
2022-01-19 16:16:56 +08:00
|
|
|
"""shell 命令原始参数列表"""
|
2021-12-16 23:22:25 +08:00
|
|
|
return Depends(_shell_command_argv, use_cache=False)
|
2021-12-14 22:40:47 +08:00
|
|
|
|
|
|
|
|
2022-01-10 11:20:06 +08:00
|
|
|
def _regex_matched(state: T_State) -> str:
|
2021-12-14 22:40:47 +08:00
|
|
|
return state[REGEX_MATCHED]
|
|
|
|
|
|
|
|
|
|
|
|
def RegexMatched() -> str:
|
2022-01-19 16:16:56 +08:00
|
|
|
"""正则匹配结果"""
|
2021-12-16 23:22:25 +08:00
|
|
|
return Depends(_regex_matched, use_cache=False)
|
2021-12-14 22:40:47 +08:00
|
|
|
|
|
|
|
|
2022-01-10 11:20:06 +08:00
|
|
|
def _regex_group(state: T_State):
|
2021-12-14 22:40:47 +08:00
|
|
|
return state[REGEX_GROUP]
|
|
|
|
|
|
|
|
|
|
|
|
def RegexGroup() -> Tuple[Any, ...]:
|
2022-01-19 16:16:56 +08:00
|
|
|
"""正则匹配结果 group 元组"""
|
2021-12-16 23:22:25 +08:00
|
|
|
return Depends(_regex_group, use_cache=False)
|
2021-12-14 22:40:47 +08:00
|
|
|
|
|
|
|
|
2022-01-10 11:20:06 +08:00
|
|
|
def _regex_dict(state: T_State):
|
2021-12-14 22:40:47 +08:00
|
|
|
return state[REGEX_DICT]
|
|
|
|
|
|
|
|
|
|
|
|
def RegexDict() -> Dict[str, Any]:
|
2022-01-19 16:16:56 +08:00
|
|
|
"""正则匹配结果 group 字典"""
|
2021-12-16 23:22:25 +08:00
|
|
|
return Depends(_regex_dict, use_cache=False)
|
2021-12-14 22:40:47 +08:00
|
|
|
|
|
|
|
|
2022-10-12 13:41:28 +08:00
|
|
|
def _startswith(state: T_State) -> str:
|
|
|
|
return state[STARTSWITH_KEY]
|
|
|
|
|
|
|
|
|
|
|
|
def Startswith() -> str:
|
|
|
|
"""响应触发前缀"""
|
|
|
|
return Depends(_startswith, use_cache=False)
|
|
|
|
|
|
|
|
|
|
|
|
def _endswith(state: T_State) -> str:
|
|
|
|
return state[ENDSWITH_KEY]
|
|
|
|
|
|
|
|
|
|
|
|
def Endswith() -> str:
|
|
|
|
"""响应触发后缀"""
|
|
|
|
return Depends(_endswith, use_cache=False)
|
|
|
|
|
|
|
|
|
|
|
|
def _fullmatch(state: T_State) -> str:
|
|
|
|
return state[FULLMATCH_KEY]
|
|
|
|
|
|
|
|
|
|
|
|
def Fullmatch() -> str:
|
|
|
|
"""响应触发完整消息"""
|
|
|
|
return Depends(_fullmatch, use_cache=False)
|
|
|
|
|
|
|
|
|
|
|
|
def _keyword(state: T_State) -> str:
|
|
|
|
return state[KEYWORD_KEY]
|
|
|
|
|
|
|
|
|
|
|
|
def Keyword() -> str:
|
|
|
|
"""响应触发关键字"""
|
|
|
|
return Depends(_keyword, use_cache=False)
|
|
|
|
|
|
|
|
|
2021-12-21 00:39:12 +08:00
|
|
|
def Received(id: Optional[str] = None, default: Any = None) -> Any:
|
2022-01-19 16:16:56 +08:00
|
|
|
"""`receive` 事件参数"""
|
|
|
|
|
2021-12-14 22:40:47 +08:00
|
|
|
def _received(matcher: "Matcher"):
|
2021-12-21 00:39:12 +08:00
|
|
|
return matcher.get_receive(id or "", default)
|
2021-12-14 01:08:48 +08:00
|
|
|
|
2021-12-16 23:22:25 +08:00
|
|
|
return Depends(_received, use_cache=False)
|
2021-12-14 01:08:48 +08:00
|
|
|
|
2021-12-14 22:40:47 +08:00
|
|
|
|
|
|
|
def LastReceived(default: Any = None) -> Any:
|
2022-01-19 16:16:56 +08:00
|
|
|
"""`last_receive` 事件参数"""
|
|
|
|
|
2021-12-14 22:40:47 +08:00
|
|
|
def _last_received(matcher: "Matcher") -> Any:
|
2021-12-21 00:39:12 +08:00
|
|
|
return matcher.get_last_receive(default)
|
2021-12-14 22:40:47 +08:00
|
|
|
|
2021-12-16 23:22:25 +08:00
|
|
|
return Depends(_last_received, use_cache=False)
|
2021-12-14 01:08:48 +08:00
|
|
|
|
|
|
|
|
2022-01-19 16:16:56 +08:00
|
|
|
__autodoc__ = {
|
2022-02-06 14:52:50 +08:00
|
|
|
"Arg": True,
|
|
|
|
"ArgStr": True,
|
|
|
|
"Depends": True,
|
|
|
|
"ArgParam": True,
|
|
|
|
"BotParam": True,
|
|
|
|
"EventParam": True,
|
|
|
|
"StateParam": True,
|
|
|
|
"DependParam": True,
|
|
|
|
"ArgPlainText": True,
|
|
|
|
"DefaultParam": True,
|
|
|
|
"MatcherParam": True,
|
|
|
|
"ExceptionParam": True,
|
2022-01-19 16:16:56 +08:00
|
|
|
}
|