mirror of
https://github.com/nonebot/nonebot2.git
synced 2025-03-11 07:49:34 +08:00
💡 add matcher docstring
This commit is contained in:
parent
9f8d009309
commit
727eef9a34
@ -113,7 +113,7 @@ await bot.send_msg(message="hello world")
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### _abstract async_ `send(*args, **kwargs)`
|
### _abstract async_ `send(event, message, **kwargs)`
|
||||||
|
|
||||||
|
|
||||||
* **说明**
|
* **说明**
|
||||||
@ -125,7 +125,10 @@ await bot.send_msg(message="hello world")
|
|||||||
* **参数**
|
* **参数**
|
||||||
|
|
||||||
|
|
||||||
* `*args`
|
* `event: Event`: 上报事件
|
||||||
|
|
||||||
|
|
||||||
|
* `message: Union[str, Message, MessageSegment]`: 要发送的消息
|
||||||
|
|
||||||
|
|
||||||
* `**kwargs`
|
* `**kwargs`
|
||||||
|
@ -182,6 +182,11 @@ sidebarDepth: 0
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### `__init__()`
|
||||||
|
|
||||||
|
实例化 Matcher 以便运行
|
||||||
|
|
||||||
|
|
||||||
### `handlers`
|
### `handlers`
|
||||||
|
|
||||||
|
|
||||||
@ -307,7 +312,7 @@ sidebarDepth: 0
|
|||||||
|
|
||||||
* **说明**
|
* **说明**
|
||||||
|
|
||||||
用于装饰一个函数来更改当前事件响应器的默认参数解析函数
|
装饰一个函数来更改当前事件响应器的默认参数解析函数
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -320,9 +325,161 @@ sidebarDepth: 0
|
|||||||
|
|
||||||
### _classmethod_ `handle()`
|
### _classmethod_ `handle()`
|
||||||
|
|
||||||
直接处理消息事件
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
装饰一个函数来向事件响应器直接添加一个处理函数
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
|
||||||
|
* 无
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### _classmethod_ `receive()`
|
### _classmethod_ `receive()`
|
||||||
|
|
||||||
接收一条新消息并处理
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
装饰一个函数来指示 NoneBot 在接收用户新的一条消息后继续运行该函数
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
|
||||||
|
* 无
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### _classmethod_ `got(key, prompt=None, args_parser=None)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
装饰一个函数来指示 NoneBot 当要获取的 `key` 不存在时接收用户新的一条消息并经过 `ArgsParser` 处理后再运行该函数,如果 `key` 已存在则直接继续运行
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
|
||||||
|
* `key: str`: 参数名
|
||||||
|
|
||||||
|
|
||||||
|
* `prompt: Optional[Union[str, Message, MessageSegment]]`: 在参数不存在时向用户发送的消息
|
||||||
|
|
||||||
|
|
||||||
|
* `args_parser: Optional[ArgsParser]`: 可选参数解析函数,空则使用默认解析函数
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### _async classmethod_ `send(message)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
发送一条消息给当前交互用户
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
|
||||||
|
* `message: Union[str, Message, MessageSegment]`: 消息内容
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### _async classmethod_ `finish(message=None)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
发送一条消息给当前交互用户并结束当前事件响应器
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
|
||||||
|
* `message: Union[str, Message, MessageSegment]`: 消息内容
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### _async classmethod_ `pause(prompt=None)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
发送一条消息给当前交互用户并暂停事件响应器,在接收用户新的一条消息后继续下一个处理函数
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
|
||||||
|
* `prompt: Union[str, Message, MessageSegment]`: 消息内容
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### _async classmethod_ `reject(prompt=None)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
发送一条消息给当前交互用户并暂停事件响应器,在接收用户新的一条消息后重新运行当前处理函数
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **参数**
|
||||||
|
|
||||||
|
|
||||||
|
* `prompt: Union[str, Message, MessageSegment]`: 消息内容
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## _class_ `MatcherGroup`
|
||||||
|
|
||||||
|
基类:`object`
|
||||||
|
|
||||||
|
事件响应器组合,统一管理。用法同 `Matcher`
|
||||||
|
|
||||||
|
|
||||||
|
### `__init__(type_='', rule=None, permission=None, handlers=None, temp=False, priority=1, block=False, *, module=None, default_state=None, expire_time=None)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
创建一个事件响应器组合,参数为默认值,与 `Matcher.new` 一致
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### `matchers`
|
||||||
|
|
||||||
|
|
||||||
|
* **类型**
|
||||||
|
|
||||||
|
`List[Type[Matcher]]`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
组内事件响应器列表
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
### `new(type_='', rule=None, permission=None, handlers=None, temp=False, priority=1, block=False, *, module=None, default_state=None, expire_time=None)`
|
||||||
|
|
||||||
|
|
||||||
|
* **说明**
|
||||||
|
|
||||||
|
在组中创建一个新的事件响应器,参数留空则使用组合默认值
|
||||||
|
|
||||||
|
|
||||||
|
:::danger 警告
|
||||||
|
如果使用 handlers 参数覆盖组合默认值则该事件响应器不会随组合一起添加新的事件处理函数
|
||||||
|
:::
|
||||||
|
@ -9,4 +9,5 @@ NoneBot.matcher 模块
|
|||||||
.. automodule:: nonebot.matcher
|
.. automodule:: nonebot.matcher
|
||||||
:members:
|
:members:
|
||||||
:private-members:
|
:private-members:
|
||||||
|
:special-members: __init__
|
||||||
:show-inheritance:
|
:show-inheritance:
|
||||||
|
@ -85,12 +85,15 @@ class BaseBot(abc.ABC):
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
async def send(self, *args, **kwargs):
|
async def send(self, event: "BaseEvent",
|
||||||
|
message: Union[str, "BaseMessage",
|
||||||
|
"BaseMessageSegment"], **kwargs):
|
||||||
"""
|
"""
|
||||||
:说明:
|
:说明:
|
||||||
调用机器人基础发送消息接口
|
调用机器人基础发送消息接口
|
||||||
:参数:
|
:参数:
|
||||||
* ``*args``
|
* ``event: Event``: 上报事件
|
||||||
|
* ``message: Union[str, Message, MessageSegment]``: 要发送的消息
|
||||||
* ``**kwargs``
|
* ``**kwargs``
|
||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
@ -196,7 +196,7 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
def args_parser(cls, func: ArgsParser) -> ArgsParser:
|
def args_parser(cls, func: ArgsParser) -> ArgsParser:
|
||||||
"""
|
"""
|
||||||
:说明:
|
:说明:
|
||||||
用于装饰一个函数来更改当前事件响应器的默认参数解析函数
|
装饰一个函数来更改当前事件响应器的默认参数解析函数
|
||||||
:参数:
|
:参数:
|
||||||
* ``func: ArgsParser``: 参数解析函数
|
* ``func: ArgsParser``: 参数解析函数
|
||||||
"""
|
"""
|
||||||
@ -205,7 +205,12 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def handle(cls) -> Callable[[Handler], Handler]:
|
def handle(cls) -> Callable[[Handler], Handler]:
|
||||||
"""直接处理消息事件"""
|
"""
|
||||||
|
:说明:
|
||||||
|
装饰一个函数来向事件响应器直接添加一个处理函数
|
||||||
|
:参数:
|
||||||
|
* 无
|
||||||
|
"""
|
||||||
|
|
||||||
def _decorator(func: Handler) -> Handler:
|
def _decorator(func: Handler) -> Handler:
|
||||||
cls.handlers.append(func)
|
cls.handlers.append(func)
|
||||||
@ -215,7 +220,12 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def receive(cls) -> Callable[[Handler], Handler]:
|
def receive(cls) -> Callable[[Handler], Handler]:
|
||||||
"""接收一条新消息并处理"""
|
"""
|
||||||
|
:说明:
|
||||||
|
装饰一个函数来指示 NoneBot 在接收用户新的一条消息后继续运行该函数
|
||||||
|
:参数:
|
||||||
|
* 无
|
||||||
|
"""
|
||||||
|
|
||||||
async def _receive(bot: Bot, event: Event, state: dict) -> NoReturn:
|
async def _receive(bot: Bot, event: Event, state: dict) -> NoReturn:
|
||||||
raise PausedException
|
raise PausedException
|
||||||
@ -236,9 +246,17 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
def got(
|
def got(
|
||||||
cls,
|
cls,
|
||||||
key: str,
|
key: str,
|
||||||
prompt: Optional[str] = None,
|
prompt: Optional[Union[str, Message, MessageSegment]] = None,
|
||||||
args_parser: Optional[ArgsParser] = None
|
args_parser: Optional[ArgsParser] = None
|
||||||
) -> Callable[[Handler], Handler]:
|
) -> Callable[[Handler], Handler]:
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
装饰一个函数来指示 NoneBot 当要获取的 ``key`` 不存在时接收用户新的一条消息并经过 ``ArgsParser`` 处理后再运行该函数,如果 ``key`` 已存在则直接继续运行
|
||||||
|
:参数:
|
||||||
|
* ``key: str``: 参数名
|
||||||
|
* ``prompt: Optional[Union[str, Message, MessageSegment]]``: 在参数不存在时向用户发送的消息
|
||||||
|
* ``args_parser: Optional[ArgsParser]``: 可选参数解析函数,空则使用默认解析函数
|
||||||
|
"""
|
||||||
|
|
||||||
async def _key_getter(bot: Bot, event: Event, state: dict):
|
async def _key_getter(bot: Bot, event: Event, state: dict):
|
||||||
state["_current_key"] = key
|
state["_current_key"] = key
|
||||||
@ -281,6 +299,12 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def send(cls, message: Union[str, Message, MessageSegment]):
|
async def send(cls, message: Union[str, Message, MessageSegment]):
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
发送一条消息给当前交互用户
|
||||||
|
:参数:
|
||||||
|
* ``message: Union[str, Message, MessageSegment]``: 消息内容
|
||||||
|
"""
|
||||||
bot = current_bot.get()
|
bot = current_bot.get()
|
||||||
event = current_event.get()
|
event = current_event.get()
|
||||||
await bot.send(event=event, message=message)
|
await bot.send(event=event, message=message)
|
||||||
@ -288,12 +312,18 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
@classmethod
|
@classmethod
|
||||||
async def finish(
|
async def finish(
|
||||||
cls,
|
cls,
|
||||||
prompt: Optional[Union[str, Message,
|
message: Optional[Union[str, Message,
|
||||||
MessageSegment]] = None) -> NoReturn:
|
MessageSegment]] = None) -> NoReturn:
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
发送一条消息给当前交互用户并结束当前事件响应器
|
||||||
|
:参数:
|
||||||
|
* ``message: Union[str, Message, MessageSegment]``: 消息内容
|
||||||
|
"""
|
||||||
bot = current_bot.get()
|
bot = current_bot.get()
|
||||||
event = current_event.get()
|
event = current_event.get()
|
||||||
if prompt:
|
if message:
|
||||||
await bot.send(event=event, message=prompt)
|
await bot.send(event=event, message=message)
|
||||||
raise FinishedException
|
raise FinishedException
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@ -301,6 +331,12 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
cls,
|
cls,
|
||||||
prompt: Optional[Union[str, Message,
|
prompt: Optional[Union[str, Message,
|
||||||
MessageSegment]] = None) -> NoReturn:
|
MessageSegment]] = None) -> NoReturn:
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
发送一条消息给当前交互用户并暂停事件响应器,在接收用户新的一条消息后继续下一个处理函数
|
||||||
|
:参数:
|
||||||
|
* ``prompt: Union[str, Message, MessageSegment]``: 消息内容
|
||||||
|
"""
|
||||||
bot = current_bot.get()
|
bot = current_bot.get()
|
||||||
event = current_event.get()
|
event = current_event.get()
|
||||||
if prompt:
|
if prompt:
|
||||||
@ -312,6 +348,12 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
cls,
|
cls,
|
||||||
prompt: Optional[Union[str, Message,
|
prompt: Optional[Union[str, Message,
|
||||||
MessageSegment]] = None) -> NoReturn:
|
MessageSegment]] = None) -> NoReturn:
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
发送一条消息给当前交互用户并暂停事件响应器,在接收用户新的一条消息后重新运行当前处理函数
|
||||||
|
:参数:
|
||||||
|
* ``prompt: Union[str, Message, MessageSegment]``: 消息内容
|
||||||
|
"""
|
||||||
bot = current_bot.get()
|
bot = current_bot.get()
|
||||||
event = current_event.get()
|
event = current_event.get()
|
||||||
if prompt:
|
if prompt:
|
||||||
@ -369,11 +411,12 @@ class Matcher(metaclass=MatcherMeta):
|
|||||||
|
|
||||||
|
|
||||||
class MatcherGroup:
|
class MatcherGroup:
|
||||||
|
"""事件响应器组合,统一管理。用法同 ``Matcher``"""
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
type_: str = "",
|
type_: str = "",
|
||||||
rule: Rule = Rule(),
|
rule: Optional[Rule] = None,
|
||||||
permission: Permission = Permission(),
|
permission: Optional[Permission] = None,
|
||||||
handlers: Optional[list] = None,
|
handlers: Optional[list] = None,
|
||||||
temp: bool = False,
|
temp: bool = False,
|
||||||
priority: int = 1,
|
priority: int = 1,
|
||||||
@ -382,11 +425,19 @@ class MatcherGroup:
|
|||||||
module: Optional[str] = None,
|
module: Optional[str] = None,
|
||||||
default_state: Optional[dict] = None,
|
default_state: Optional[dict] = None,
|
||||||
expire_time: Optional[datetime] = None):
|
expire_time: Optional[datetime] = None):
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
创建一个事件响应器组合,参数为默认值,与 ``Matcher.new`` 一致
|
||||||
|
"""
|
||||||
self.matchers: List[Type[Matcher]] = []
|
self.matchers: List[Type[Matcher]] = []
|
||||||
|
"""
|
||||||
|
:类型: ``List[Type[Matcher]]``
|
||||||
|
:说明: 组内事件响应器列表
|
||||||
|
"""
|
||||||
|
|
||||||
self.type = type_
|
self.type = type_
|
||||||
self.rule = rule
|
self.rule = rule or Rule()
|
||||||
self.permission = permission
|
self.permission = permission or Permission()
|
||||||
self.handlers = handlers
|
self.handlers = handlers
|
||||||
self.temp = temp
|
self.temp = temp
|
||||||
self.priority = priority
|
self.priority = priority
|
||||||
@ -408,8 +459,8 @@ class MatcherGroup:
|
|||||||
|
|
||||||
def new(self,
|
def new(self,
|
||||||
type_: str = "",
|
type_: str = "",
|
||||||
rule: Rule = Rule(),
|
rule: Optional[Rule] = None,
|
||||||
permission: Permission = Permission(),
|
permission: Optional[Permission] = None,
|
||||||
handlers: Optional[list] = None,
|
handlers: Optional[list] = None,
|
||||||
temp: bool = False,
|
temp: bool = False,
|
||||||
priority: int = 1,
|
priority: int = 1,
|
||||||
@ -418,6 +469,14 @@ class MatcherGroup:
|
|||||||
module: Optional[str] = None,
|
module: Optional[str] = None,
|
||||||
default_state: Optional[dict] = None,
|
default_state: Optional[dict] = None,
|
||||||
expire_time: Optional[datetime] = None) -> Type[Matcher]:
|
expire_time: Optional[datetime] = None) -> Type[Matcher]:
|
||||||
|
"""
|
||||||
|
:说明:
|
||||||
|
在组中创建一个新的事件响应器,参数留空则使用组合默认值
|
||||||
|
|
||||||
|
\:\:\:danger 警告
|
||||||
|
如果使用 handlers 参数覆盖组合默认值则该事件响应器不会随组合一起添加新的事件处理函数
|
||||||
|
\:\:\:
|
||||||
|
"""
|
||||||
matcher = Matcher.new(type_=type_ or self.type,
|
matcher = Matcher.new(type_=type_ or self.type,
|
||||||
rule=self.rule & rule,
|
rule=self.rule & rule,
|
||||||
permission=permission or self.permission,
|
permission=permission or self.permission,
|
||||||
@ -438,7 +497,6 @@ class MatcherGroup:
|
|||||||
return func
|
return func
|
||||||
|
|
||||||
def handle(self) -> Callable[[Handler], Handler]:
|
def handle(self) -> Callable[[Handler], Handler]:
|
||||||
"""直接处理消息事件"""
|
|
||||||
|
|
||||||
def _decorator(func: Handler) -> Handler:
|
def _decorator(func: Handler) -> Handler:
|
||||||
self.handlers.append(func)
|
self.handlers.append(func)
|
||||||
@ -447,7 +505,6 @@ class MatcherGroup:
|
|||||||
return _decorator
|
return _decorator
|
||||||
|
|
||||||
def receive(self) -> Callable[[Handler], Handler]:
|
def receive(self) -> Callable[[Handler], Handler]:
|
||||||
"""接收一条新消息并处理"""
|
|
||||||
|
|
||||||
async def _receive(bot: Bot, event: Event, state: dict) -> NoReturn:
|
async def _receive(bot: Bot, event: Event, state: dict) -> NoReturn:
|
||||||
raise PausedException
|
raise PausedException
|
||||||
@ -510,14 +567,19 @@ class MatcherGroup:
|
|||||||
|
|
||||||
return _decorator
|
return _decorator
|
||||||
|
|
||||||
async def finish(
|
async def send(self, message: Union[str, Message, MessageSegment]):
|
||||||
self,
|
|
||||||
prompt: Optional[Union[str, Message,
|
|
||||||
MessageSegment]] = None) -> NoReturn:
|
|
||||||
bot = current_bot.get()
|
bot = current_bot.get()
|
||||||
event = current_event.get()
|
event = current_event.get()
|
||||||
if prompt:
|
await bot.send(event=event, message=message)
|
||||||
await bot.send(event=event, message=prompt)
|
|
||||||
|
async def finish(
|
||||||
|
self,
|
||||||
|
message: Optional[Union[str, Message,
|
||||||
|
MessageSegment]] = None) -> NoReturn:
|
||||||
|
bot = current_bot.get()
|
||||||
|
event = current_event.get()
|
||||||
|
if message:
|
||||||
|
await bot.send(event=event, message=message)
|
||||||
raise FinishedException
|
raise FinishedException
|
||||||
|
|
||||||
async def pause(
|
async def pause(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user