nonebot2/nonebot/params.py

245 lines
5.7 KiB
Python
Raw Normal View History

2022-01-19 16:16:56 +08:00
"""本模块定义了依赖注入的各类参数。
2022-01-16 11:30:09 +08:00
FrontMatter:
sidebar_position: 4
description: nonebot.params 模块
"""
from typing import Any, Dict, List, Match, Tuple, Union, Optional
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
from nonebot.adapters import Event, Message, MessageSegment
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,
SHELL_ARGS,
SHELL_ARGV,
CMD_ARG_KEY,
KEYWORD_KEY,
2021-12-14 01:08:48 +08:00
RAW_CMD_KEY,
ENDSWITH_KEY,
CMD_START_KEY,
FULLMATCH_KEY,
2021-12-14 01:08:48 +08:00
REGEX_MATCHED,
STARTSWITH_KEY,
CMD_WHITESPACE_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:
"""{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:
"""{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:
"""{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:
"""{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
def _command_start(state: T_State) -> str:
return state[PREFIX_KEY][CMD_START_KEY]
def CommandStart() -> str:
"""消息命令开头"""
return Depends(_command_start)
def _command_whitespace(state: T_State) -> str:
return state[PREFIX_KEY][CMD_WHITESPACE_KEY]
def CommandWhitespace() -> str:
"""消息命令与参数之间的空白"""
return Depends(_command_whitespace)
2022-01-10 11:20:06 +08:00
def _shell_command_args(state: T_State) -> Any:
return state[SHELL_ARGS] # Namespace or ParserExit
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)
def _shell_command_argv(state: T_State) -> List[Union[str, MessageSegment]]:
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)
def _regex_matched(state: T_State) -> Match[str]:
return state[REGEX_MATCHED]
def RegexMatched() -> Match[str]:
2022-01-19 16:16:56 +08:00
"""正则匹配结果"""
2021-12-16 23:22:25 +08:00
return Depends(_regex_matched, use_cache=False)
def _regex_str(state: T_State) -> str:
return _regex_matched(state).group()
def RegexStr() -> str:
"""正则匹配结果文本"""
return Depends(_regex_str, use_cache=False)
def _regex_group(state: T_State) -> Tuple[Any, ...]:
return _regex_matched(state).groups()
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)
def _regex_dict(state: T_State) -> Dict[str, Any]:
return _regex_matched(state).groupdict()
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)
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)
def Received(id: Optional[str] = None, default: Any = None) -> Any:
2022-01-19 16:16:56 +08:00
"""`receive` 事件参数"""
def _received(matcher: "Matcher") -> Any:
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
def LastReceived(default: Any = None) -> Any:
2022-01-19 16:16:56 +08:00
"""`last_receive` 事件参数"""
def _last_received(matcher: "Matcher") -> Any:
return matcher.get_last_receive(default)
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__ = {
"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
}