2022-01-22 15:23:07 +08:00
|
|
|
|
"""本模块定义事件响应器便携定义函数。
|
|
|
|
|
|
|
|
|
|
FrontMatter:
|
2024-10-22 10:33:48 +08:00
|
|
|
|
mdx:
|
|
|
|
|
format: md
|
2022-01-22 15:23:07 +08:00
|
|
|
|
sidebar_position: 2
|
|
|
|
|
description: nonebot.plugin.on 模块
|
|
|
|
|
"""
|
2023-06-24 14:47:35 +08:00
|
|
|
|
|
2021-11-08 01:02:35 +08:00
|
|
|
|
import re
|
2021-11-09 00:57:59 +08:00
|
|
|
|
import inspect
|
2023-09-09 13:46:09 +08:00
|
|
|
|
import warnings
|
2021-11-09 00:57:59 +08:00
|
|
|
|
from types import ModuleType
|
2024-04-16 00:33:48 +08:00
|
|
|
|
from typing import Any, Union, Optional
|
2022-07-20 10:21:31 +08:00
|
|
|
|
from datetime import datetime, timedelta
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-08-30 09:54:09 +08:00
|
|
|
|
from nonebot.adapters import Event
|
2021-11-08 01:02:35 +08:00
|
|
|
|
from nonebot.permission import Permission
|
2021-12-12 18:19:08 +08:00
|
|
|
|
from nonebot.dependencies import Dependent
|
2023-09-09 13:46:09 +08:00
|
|
|
|
from nonebot.matcher import Matcher, MatcherSource
|
2022-01-28 14:04:57 +08:00
|
|
|
|
from nonebot.typing import T_State, T_Handler, T_RuleChecker, T_PermissionChecker
|
2021-11-22 23:21:26 +08:00
|
|
|
|
from nonebot.rule import (
|
|
|
|
|
Rule,
|
|
|
|
|
ArgumentParser,
|
|
|
|
|
regex,
|
|
|
|
|
command,
|
2022-08-30 09:54:09 +08:00
|
|
|
|
is_type,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
keyword,
|
|
|
|
|
endswith,
|
2022-02-15 08:20:29 +08:00
|
|
|
|
fullmatch,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
startswith,
|
|
|
|
|
shell_command,
|
|
|
|
|
)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2023-09-27 16:00:26 +08:00
|
|
|
|
from .model import Plugin
|
2024-04-20 14:47:12 +08:00
|
|
|
|
from .manager import _current_plugin
|
2023-03-29 12:22:50 +08:00
|
|
|
|
from . import get_plugin_by_module_name
|
2022-01-15 21:27:43 +08:00
|
|
|
|
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def store_matcher(matcher: type[Matcher]) -> None:
|
2023-03-29 23:09:33 +08:00
|
|
|
|
"""存储一个事件响应器到插件。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
matcher: 事件响应器
|
|
|
|
|
"""
|
2023-03-29 12:22:50 +08:00
|
|
|
|
# only store the matcher defined when plugin loading
|
2024-04-20 14:47:12 +08:00
|
|
|
|
if plugin := _current_plugin.get():
|
|
|
|
|
plugin.matcher.add(matcher)
|
2023-03-29 12:22:50 +08:00
|
|
|
|
|
|
|
|
|
|
2023-09-09 13:46:09 +08:00
|
|
|
|
def get_matcher_plugin(depth: int = 1) -> Optional[Plugin]: # pragma: no cover
|
2023-03-29 23:09:33 +08:00
|
|
|
|
"""获取事件响应器定义所在插件。
|
|
|
|
|
|
2023-09-09 13:46:09 +08:00
|
|
|
|
**Deprecated**, 请使用 {ref}`nonebot.plugin.on.get_matcher_source` 获取信息。
|
|
|
|
|
|
2023-03-29 23:09:33 +08:00
|
|
|
|
参数:
|
|
|
|
|
depth: 调用栈深度
|
|
|
|
|
"""
|
2023-09-09 13:46:09 +08:00
|
|
|
|
warnings.warn(
|
|
|
|
|
"`get_matcher_plugin` is deprecated, please use `get_matcher_source` instead",
|
|
|
|
|
DeprecationWarning,
|
|
|
|
|
)
|
|
|
|
|
return (source := get_matcher_source(depth + 1)) and source.plugin
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
2023-09-09 13:46:09 +08:00
|
|
|
|
def get_matcher_module(depth: int = 1) -> Optional[ModuleType]: # pragma: no cover
|
2023-03-29 23:09:33 +08:00
|
|
|
|
"""获取事件响应器定义所在模块。
|
|
|
|
|
|
2023-09-09 13:46:09 +08:00
|
|
|
|
**Deprecated**, 请使用 {ref}`nonebot.plugin.on.get_matcher_source` 获取信息。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
depth: 调用栈深度
|
|
|
|
|
"""
|
|
|
|
|
warnings.warn(
|
|
|
|
|
"`get_matcher_module` is deprecated, please use `get_matcher_source` instead",
|
|
|
|
|
DeprecationWarning,
|
|
|
|
|
)
|
|
|
|
|
return (source := get_matcher_source(depth + 1)) and source.module
|
|
|
|
|
|
|
|
|
|
|
2024-04-17 17:24:38 +08:00
|
|
|
|
def get_matcher_source(depth: int = 0) -> Optional[MatcherSource]:
|
2023-09-09 13:46:09 +08:00
|
|
|
|
"""获取事件响应器定义所在源码信息。
|
|
|
|
|
|
2023-03-29 23:09:33 +08:00
|
|
|
|
参数:
|
|
|
|
|
depth: 调用栈深度
|
|
|
|
|
"""
|
2021-11-09 00:57:59 +08:00
|
|
|
|
current_frame = inspect.currentframe()
|
|
|
|
|
if current_frame is None:
|
|
|
|
|
return None
|
2024-04-17 17:24:38 +08:00
|
|
|
|
|
|
|
|
|
frame = current_frame
|
|
|
|
|
d = depth + 1
|
|
|
|
|
while d > 0:
|
|
|
|
|
frame = frame.f_back
|
|
|
|
|
if frame is None:
|
|
|
|
|
raise ValueError("Depth out of range")
|
|
|
|
|
d -= 1
|
2023-09-09 13:46:09 +08:00
|
|
|
|
|
|
|
|
|
module_name = (module := inspect.getmodule(frame)) and module.__name__
|
|
|
|
|
|
|
|
|
|
# matcher defined when plugin loading
|
2024-04-20 14:47:12 +08:00
|
|
|
|
plugin: Optional["Plugin"] = _current_plugin.get()
|
2023-09-09 13:46:09 +08:00
|
|
|
|
# matcher defined when plugin running
|
2024-04-20 14:47:12 +08:00
|
|
|
|
if plugin is None and module_name:
|
2023-09-09 13:46:09 +08:00
|
|
|
|
plugin = get_plugin_by_module_name(module_name)
|
|
|
|
|
|
|
|
|
|
return MatcherSource(
|
2024-04-20 14:47:12 +08:00
|
|
|
|
plugin_id=plugin and plugin.id_,
|
2023-09-09 13:46:09 +08:00
|
|
|
|
module_name=module_name,
|
|
|
|
|
lineno=frame.f_lineno,
|
|
|
|
|
)
|
2021-11-09 00:57:59 +08:00
|
|
|
|
|
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
def on(
|
|
|
|
|
type: str = "",
|
|
|
|
|
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
2021-12-12 18:19:08 +08:00
|
|
|
|
permission: Optional[Union[Permission, T_PermissionChecker]] = None,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
*,
|
2024-08-06 14:19:17 +08:00
|
|
|
|
handlers: Optional[list[Union[T_Handler, Dependent[Any]]]] = None,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
temp: bool = False,
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: Optional[Union[datetime, timedelta]] = None,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
priority: int = 1,
|
|
|
|
|
block: bool = False,
|
|
|
|
|
state: Optional[T_State] = None,
|
|
|
|
|
_depth: int = 0,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个基础事件响应器,可自定义类型。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
type: 事件响应器类型
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2021-11-22 23:21:26 +08:00
|
|
|
|
matcher = Matcher.new(
|
|
|
|
|
type,
|
|
|
|
|
Rule() & rule,
|
2021-12-12 18:19:08 +08:00
|
|
|
|
Permission() | permission,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
temp=temp,
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time=expire_time,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
priority=priority,
|
|
|
|
|
block=block,
|
|
|
|
|
handlers=handlers,
|
2023-09-09 13:46:09 +08:00
|
|
|
|
source=get_matcher_source(_depth + 1),
|
2021-11-22 23:21:26 +08:00
|
|
|
|
default_state=state,
|
|
|
|
|
)
|
2023-03-29 23:09:33 +08:00
|
|
|
|
store_matcher(matcher)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
return matcher
|
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def on_metaevent(*args, _depth: int = 0, **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个元事件响应器。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
rule: 事件响应规则
|
2023-03-17 15:48:48 +08:00
|
|
|
|
permission: 事件响应权限
|
2022-01-12 19:10:29 +08:00
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2023-03-17 15:48:48 +08:00
|
|
|
|
return on("meta_event", *args, **kwargs, _depth=_depth + 1)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def on_message(*args, _depth: int = 0, **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2023-03-29 10:38:39 +08:00
|
|
|
|
kwargs.setdefault("block", True)
|
2023-03-17 15:48:48 +08:00
|
|
|
|
return on("message", *args, **kwargs, _depth=_depth + 1)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def on_notice(*args, _depth: int = 0, **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个通知事件响应器。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
rule: 事件响应规则
|
2023-03-17 15:48:48 +08:00
|
|
|
|
permission: 事件响应权限
|
2022-01-12 19:10:29 +08:00
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2023-03-17 15:48:48 +08:00
|
|
|
|
return on("notice", *args, **kwargs, _depth=_depth + 1)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def on_request(*args, _depth: int = 0, **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个请求事件响应器。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
rule: 事件响应规则
|
2023-03-17 15:48:48 +08:00
|
|
|
|
permission: 事件响应权限
|
2022-01-12 19:10:29 +08:00
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2023-03-17 15:48:48 +08:00
|
|
|
|
return on("request", *args, **kwargs, _depth=_depth + 1)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
def on_startswith(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
msg: Union[str, tuple[str, ...]],
|
2022-04-01 16:29:44 +08:00
|
|
|
|
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
ignorecase: bool = False,
|
|
|
|
|
_depth: int = 0,
|
|
|
|
|
**kwargs,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器,并且当消息的**文本部分**以指定内容开头时响应。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
msg: 指定消息开头内容
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
ignorecase: 是否忽略大小写
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2021-11-22 23:21:26 +08:00
|
|
|
|
return on_message(startswith(msg, ignorecase) & rule, **kwargs, _depth=_depth + 1)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
def on_endswith(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
msg: Union[str, tuple[str, ...]],
|
2022-04-01 16:29:44 +08:00
|
|
|
|
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
ignorecase: bool = False,
|
|
|
|
|
_depth: int = 0,
|
|
|
|
|
**kwargs,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器,并且当消息的**文本部分**以指定内容结尾时响应。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
msg: 指定消息结尾内容
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
ignorecase: 是否忽略大小写
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2021-11-22 23:21:26 +08:00
|
|
|
|
return on_message(endswith(msg, ignorecase) & rule, **kwargs, _depth=_depth + 1)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
2022-02-15 08:20:29 +08:00
|
|
|
|
def on_fullmatch(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
msg: Union[str, tuple[str, ...]],
|
2022-04-01 16:29:44 +08:00
|
|
|
|
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
2022-02-15 08:20:29 +08:00
|
|
|
|
ignorecase: bool = False,
|
|
|
|
|
_depth: int = 0,
|
|
|
|
|
**kwargs,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器,并且当消息的**文本部分**与指定内容完全一致时响应。
|
2022-02-15 08:20:29 +08:00
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
msg: 指定消息全匹配内容
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
ignorecase: 是否忽略大小写
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-02-15 08:20:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
|
|
|
|
"""
|
|
|
|
|
return on_message(fullmatch(msg, ignorecase) & rule, **kwargs, _depth=_depth + 1)
|
|
|
|
|
|
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
def on_keyword(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
keywords: set[str],
|
2021-11-22 23:21:26 +08:00
|
|
|
|
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
|
|
|
|
_depth: int = 0,
|
|
|
|
|
**kwargs,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器,并且当消息纯文本部分包含关键词时响应。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
keywords: 关键词列表
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2021-11-11 17:33:30 +08:00
|
|
|
|
return on_message(keyword(*keywords) & rule, **kwargs, _depth=_depth + 1)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
def on_command(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cmd: Union[str, tuple[str, ...]],
|
2021-11-22 23:21:26 +08:00
|
|
|
|
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
aliases: Optional[set[Union[str, tuple[str, ...]]]] = None,
|
2023-02-27 00:11:24 +08:00
|
|
|
|
force_whitespace: Optional[Union[str, bool]] = None,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
_depth: int = 0,
|
|
|
|
|
**kwargs,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器,并且当消息以指定命令开头时响应。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 19:10:29 +08:00
|
|
|
|
命令匹配规则参考: `命令形式匹配 <rule.md#command-command>`_
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
cmd: 指定命令内容
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
aliases: 命令别名
|
2023-02-27 00:11:24 +08:00
|
|
|
|
force_whitespace: 是否强制命令后必须有指定空白符
|
2022-01-12 19:10:29 +08:00
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
commands = {cmd} | (aliases or set())
|
2023-03-29 10:38:39 +08:00
|
|
|
|
kwargs.setdefault("block", False)
|
2021-12-23 22:16:55 +08:00
|
|
|
|
return on_message(
|
2023-02-27 00:11:24 +08:00
|
|
|
|
command(*commands, force_whitespace=force_whitespace) & rule,
|
|
|
|
|
**kwargs,
|
|
|
|
|
_depth=_depth + 1,
|
2021-12-23 22:16:55 +08:00
|
|
|
|
)
|
2021-11-22 23:21:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_shell_command(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cmd: Union[str, tuple[str, ...]],
|
2021-11-22 23:21:26 +08:00
|
|
|
|
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
aliases: Optional[set[Union[str, tuple[str, ...]]]] = None,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
parser: Optional[ArgumentParser] = None,
|
|
|
|
|
_depth: int = 0,
|
|
|
|
|
**kwargs,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个支持 `shell_like` 解析参数的命令消息事件响应器。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 19:10:29 +08:00
|
|
|
|
与普通的 `on_command` 不同的是,在添加 `parser` 参数时, 响应器会自动处理消息。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
可以通过 {ref}`nonebot.params.ShellCommandArgv` 获取原始参数列表,
|
|
|
|
|
通过 {ref}`nonebot.params.ShellCommandArgs` 获取解析后的参数字典。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
cmd: 指定命令内容
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
aliases: 命令别名
|
|
|
|
|
parser: `nonebot.rule.ArgumentParser` 对象
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
commands = {cmd} | (aliases or set())
|
2021-11-22 23:21:26 +08:00
|
|
|
|
return on_message(
|
|
|
|
|
shell_command(*commands, parser=parser) & rule,
|
|
|
|
|
**kwargs,
|
|
|
|
|
_depth=_depth + 1,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_regex(
|
|
|
|
|
pattern: str,
|
|
|
|
|
flags: Union[int, re.RegexFlag] = 0,
|
|
|
|
|
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
|
|
|
|
_depth: int = 0,
|
|
|
|
|
**kwargs,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器,并且当消息匹配正则表达式时响应。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 19:10:29 +08:00
|
|
|
|
命令匹配规则参考: `正则匹配 <rule.md#regex-regex-flags-0>`_
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
pattern: 正则表达式
|
|
|
|
|
flags: 正则匹配标志
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2021-11-11 17:33:30 +08:00
|
|
|
|
return on_message(regex(pattern, flags) & rule, **kwargs, _depth=_depth + 1)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
2022-08-30 09:54:09 +08:00
|
|
|
|
def on_type(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
types: Union[type[Event], tuple[type[Event], ...]],
|
2022-08-30 09:54:09 +08:00
|
|
|
|
rule: Optional[Union[Rule, T_RuleChecker]] = None,
|
|
|
|
|
*,
|
|
|
|
|
_depth: int = 0,
|
|
|
|
|
**kwargs,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> type[Matcher]:
|
2022-08-30 09:54:09 +08:00
|
|
|
|
"""注册一个事件响应器,并且当事件为指定类型时响应。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
types: 事件类型
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
|
|
|
|
"""
|
|
|
|
|
event_types = types if isinstance(types, tuple) else (types,)
|
|
|
|
|
return on(rule=is_type(*event_types) & rule, **kwargs, _depth=_depth + 1)
|
|
|
|
|
|
|
|
|
|
|
2022-09-10 20:54:49 +08:00
|
|
|
|
class _Group:
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
|
"""创建一个事件响应器组合,参数为默认值,与 `on` 一致"""
|
2024-04-16 00:33:48 +08:00
|
|
|
|
self.matchers: list[type[Matcher]] = []
|
2022-09-10 20:54:49 +08:00
|
|
|
|
"""组内事件响应器列表"""
|
2024-04-16 00:33:48 +08:00
|
|
|
|
self.base_kwargs: dict[str, Any] = kwargs
|
2022-09-10 20:54:49 +08:00
|
|
|
|
"""其他传递给 `on` 的参数默认值"""
|
|
|
|
|
|
|
|
|
|
def _get_final_kwargs(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
self, update: dict[str, Any], *, exclude: Optional[set[str]] = None
|
|
|
|
|
) -> dict[str, Any]:
|
2022-09-10 20:54:49 +08:00
|
|
|
|
"""获取最终传递给 `on` 的参数
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
update: 更新的关键字参数
|
|
|
|
|
exclude: 需要排除的参数
|
|
|
|
|
"""
|
|
|
|
|
final_kwargs = self.base_kwargs.copy()
|
|
|
|
|
final_kwargs.update(update)
|
|
|
|
|
if exclude:
|
|
|
|
|
for key in exclude:
|
|
|
|
|
final_kwargs.pop(key, None)
|
|
|
|
|
final_kwargs["_depth"] = 1
|
|
|
|
|
return final_kwargs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CommandGroup(_Group):
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""命令组,用于声明一组有相同名称前缀的命令。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
cmd: 指定命令内容
|
2023-07-01 15:40:30 +08:00
|
|
|
|
prefix_aliases: 是否影响命令别名,给命令别名加前缀
|
2022-07-20 10:21:31 +08:00
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
|
|
|
|
"""
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2023-07-01 15:40:30 +08:00
|
|
|
|
def __init__(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
self, cmd: Union[str, tuple[str, ...]], prefix_aliases: bool = False, **kwargs
|
2023-07-01 15:40:30 +08:00
|
|
|
|
):
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""命令前缀"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
super().__init__(**kwargs)
|
2024-04-16 00:33:48 +08:00
|
|
|
|
self.basecmd: tuple[str, ...] = (cmd,) if isinstance(cmd, str) else cmd
|
2022-09-10 20:54:49 +08:00
|
|
|
|
self.base_kwargs.pop("aliases", None)
|
2023-07-01 15:40:30 +08:00
|
|
|
|
self.prefix_aliases = prefix_aliases
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
2022-09-10 20:54:49 +08:00
|
|
|
|
return f"CommandGroup(cmd={self.basecmd}, matchers={len(self.matchers)})"
|
2022-09-09 11:52:57 +08:00
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def command(self, cmd: Union[str, tuple[str, ...]], **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个新的命令。新参数将会覆盖命令组默认值
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
cmd: 指定命令内容
|
|
|
|
|
aliases: 命令别名
|
2023-02-27 00:11:24 +08:00
|
|
|
|
force_whitespace: 是否强制命令后必须有指定空白符
|
2022-07-20 10:21:31 +08:00
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
|
|
|
|
sub_cmd = (cmd,) if isinstance(cmd, str) else cmd
|
|
|
|
|
cmd = self.basecmd + sub_cmd
|
2023-07-01 15:40:30 +08:00
|
|
|
|
if self.prefix_aliases and (aliases := kwargs.get("aliases")):
|
|
|
|
|
kwargs["aliases"] = {
|
|
|
|
|
self.basecmd + ((alias,) if isinstance(alias, str) else alias)
|
|
|
|
|
for alias in aliases
|
|
|
|
|
}
|
2022-09-10 20:54:49 +08:00
|
|
|
|
matcher = on_command(cmd, **self._get_final_kwargs(kwargs))
|
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
def shell_command(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
self, cmd: Union[str, tuple[str, ...]], **kwargs
|
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个新的 `shell_like` 命令。新参数将会覆盖命令组默认值
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
cmd: 指定命令内容
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
aliases: 命令别名
|
|
|
|
|
parser: `nonebot.rule.ArgumentParser` 对象
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
|
|
|
|
sub_cmd = (cmd,) if isinstance(cmd, str) else cmd
|
|
|
|
|
cmd = self.basecmd + sub_cmd
|
2023-07-01 15:40:30 +08:00
|
|
|
|
if self.prefix_aliases and (aliases := kwargs.get("aliases")):
|
|
|
|
|
kwargs["aliases"] = {
|
|
|
|
|
self.basecmd + ((alias,) if isinstance(alias, str) else alias)
|
|
|
|
|
for alias in aliases
|
|
|
|
|
}
|
2022-09-10 20:54:49 +08:00
|
|
|
|
matcher = on_shell_command(cmd, **self._get_final_kwargs(kwargs))
|
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
|
|
|
|
|
2022-09-10 20:54:49 +08:00
|
|
|
|
class MatcherGroup(_Group):
|
2022-01-12 18:19:21 +08:00
|
|
|
|
"""事件响应器组合,统一管理。为 `Matcher` 创建提供默认属性。"""
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-09-09 11:52:57 +08:00
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
return f"MatcherGroup(matchers={len(self.matchers)})"
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def on(self, **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个基础事件响应器,可自定义类型。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
type: 事件响应器类型
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
matcher = on(**self._get_final_kwargs(kwargs))
|
2021-11-08 01:02:35 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def on_metaevent(self, **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个元事件响应器。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
rule: 事件响应规则
|
2023-03-17 15:48:48 +08:00
|
|
|
|
permission: 事件响应权限
|
2022-01-12 19:10:29 +08:00
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type", "permission"})
|
|
|
|
|
matcher = on_metaevent(**final_kwargs)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def on_message(self, **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
|
|
|
|
|
matcher = on_message(**final_kwargs)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def on_notice(self, **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个通知事件响应器。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
rule: 事件响应规则
|
2023-03-17 15:48:48 +08:00
|
|
|
|
permission: 事件响应权限
|
2022-01-12 19:10:29 +08:00
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type", "permission"})
|
|
|
|
|
matcher = on_notice(**final_kwargs)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def on_request(self, **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个请求事件响应器。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
rule: 事件响应规则
|
2023-03-17 15:48:48 +08:00
|
|
|
|
permission: 事件响应权限
|
2022-01-12 19:10:29 +08:00
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type", "permission"})
|
|
|
|
|
matcher = on_request(**final_kwargs)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
def on_startswith(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
self, msg: Union[str, tuple[str, ...]], **kwargs
|
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器,并且当消息的**文本部分**以指定内容开头时响应。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
msg: 指定消息开头内容
|
|
|
|
|
ignorecase: 是否忽略大小写
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
|
|
|
|
|
matcher = on_startswith(msg, **final_kwargs)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def on_endswith(self, msg: Union[str, tuple[str, ...]], **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器,并且当消息的**文本部分**以指定内容结尾时响应。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
msg: 指定消息结尾内容
|
|
|
|
|
ignorecase: 是否忽略大小写
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
|
|
|
|
|
matcher = on_endswith(msg, **final_kwargs)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def on_fullmatch(self, msg: Union[str, tuple[str, ...]], **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器,并且当消息的**文本部分**与指定内容完全一致时响应。
|
2022-02-15 08:20:29 +08:00
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
msg: 指定消息全匹配内容
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
ignorecase: 是否忽略大小写
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-02-15 08:20:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
|
|
|
|
|
matcher = on_fullmatch(msg, **final_kwargs)
|
2022-02-15 08:20:29 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
|
|
|
|
|
2024-04-16 00:33:48 +08:00
|
|
|
|
def on_keyword(self, keywords: set[str], **kwargs) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器,并且当消息纯文本部分包含关键词时响应。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
keywords: 关键词列表
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
|
|
|
|
|
matcher = on_keyword(keywords, **final_kwargs)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
def on_command(
|
|
|
|
|
self,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cmd: Union[str, tuple[str, ...]],
|
|
|
|
|
aliases: Optional[set[Union[str, tuple[str, ...]]]] = None,
|
2023-02-27 00:11:24 +08:00
|
|
|
|
force_whitespace: Optional[Union[str, bool]] = None,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
**kwargs,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器,并且当消息以指定命令开头时响应。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:16:05 +08:00
|
|
|
|
命令匹配规则参考: `命令形式匹配 <rule.md#command-command>`_
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
cmd: 指定命令内容
|
|
|
|
|
aliases: 命令别名
|
2023-02-27 00:11:24 +08:00
|
|
|
|
force_whitespace: 是否强制命令后必须有指定空白符
|
2022-01-12 19:10:29 +08:00
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
|
2023-02-27 00:11:24 +08:00
|
|
|
|
matcher = on_command(
|
|
|
|
|
cmd, aliases=aliases, force_whitespace=force_whitespace, **final_kwargs
|
|
|
|
|
)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
def on_shell_command(
|
|
|
|
|
self,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
cmd: Union[str, tuple[str, ...]],
|
|
|
|
|
aliases: Optional[set[Union[str, tuple[str, ...]]]] = None,
|
2021-11-22 23:21:26 +08:00
|
|
|
|
parser: Optional[ArgumentParser] = None,
|
|
|
|
|
**kwargs,
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个支持 `shell_like` 解析参数的命令消息事件响应器。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:19:21 +08:00
|
|
|
|
与普通的 `on_command` 不同的是,在添加 `parser` 参数时, 响应器会自动处理消息。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2023-06-24 14:47:35 +08:00
|
|
|
|
可以通过 {ref}`nonebot.params.ShellCommandArgv` 获取原始参数列表,
|
|
|
|
|
通过 {ref}`nonebot.params.ShellCommandArgs` 获取解析后的参数字典。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
cmd: 指定命令内容
|
|
|
|
|
aliases: 命令别名
|
|
|
|
|
parser: `nonebot.rule.ArgumentParser` 对象
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
|
|
|
|
|
matcher = on_shell_command(cmd, aliases=aliases, parser=parser, **final_kwargs)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
|
|
|
|
|
2021-11-22 23:21:26 +08:00
|
|
|
|
def on_regex(
|
|
|
|
|
self, pattern: str, flags: Union[int, re.RegexFlag] = 0, **kwargs
|
2024-04-16 00:33:48 +08:00
|
|
|
|
) -> type[Matcher]:
|
2022-07-20 10:21:31 +08:00
|
|
|
|
"""注册一个消息事件响应器,并且当消息匹配正则表达式时响应。
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:16:05 +08:00
|
|
|
|
命令匹配规则参考: `正则匹配 <rule.md#regex-regex-flags-0>`_
|
2021-11-08 01:02:35 +08:00
|
|
|
|
|
2022-01-12 18:31:12 +08:00
|
|
|
|
参数:
|
2022-01-12 19:10:29 +08:00
|
|
|
|
pattern: 正则表达式
|
|
|
|
|
flags: 正则匹配标志
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
2022-07-20 10:21:31 +08:00
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
2022-01-12 19:10:29 +08:00
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
2021-11-08 01:02:35 +08:00
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
|
|
|
|
|
matcher = on_regex(pattern, flags=flags, **final_kwargs)
|
2021-11-08 01:02:35 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|
2022-08-30 09:54:09 +08:00
|
|
|
|
|
|
|
|
|
def on_type(
|
2024-04-16 00:33:48 +08:00
|
|
|
|
self, types: Union[type[Event], tuple[type[Event]]], **kwargs
|
|
|
|
|
) -> type[Matcher]:
|
2022-08-30 09:54:09 +08:00
|
|
|
|
"""注册一个事件响应器,并且当事件为指定类型时响应。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
types: 事件类型
|
|
|
|
|
rule: 事件响应规则
|
|
|
|
|
permission: 事件响应权限
|
|
|
|
|
handlers: 事件处理函数列表
|
|
|
|
|
temp: 是否为临时事件响应器(仅执行一次)
|
|
|
|
|
expire_time: 事件响应器最终有效时间点,过时即被删除
|
|
|
|
|
priority: 事件响应器优先级
|
|
|
|
|
block: 是否阻止事件向更低优先级传递
|
|
|
|
|
state: 默认 state
|
|
|
|
|
"""
|
2022-09-10 20:54:49 +08:00
|
|
|
|
final_kwargs = self._get_final_kwargs(kwargs, exclude={"type"})
|
|
|
|
|
matcher = on_type(types, **final_kwargs)
|
2022-08-30 09:54:09 +08:00
|
|
|
|
self.matchers.append(matcher)
|
|
|
|
|
return matcher
|