Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions docs/.vuepress/public/plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,61 @@
"desc": "多语种翻译插件",
"name": "translator",
"repo": "Lancercmd/nonebot_plugin_translator"
},
{
"id": "nonebot_plugin_simdraw",
"link": "nonebot-plugin-simdraw",
"author": "abrahum",
"desc": "根据配置的手游卡池信息模拟抽卡",
"name": "模拟抽卡",
"repo": "abrahum/nonebot_plugin_simdraw"
},
{
"id": "nonebot-plugin-wordbank",
"link": "nonebot-plugin-wordbank",
"author": "Joenothing-lst",
"desc": "无数据库的轻量问答插件,支持模糊问答",
"name": "nonebot-plugin-wordbank",
"repo": "Joenothing-lst/word-bank"
},
{
"id": "nonebot_plugin_cooldown",
"link": "nonebot-plugin-cooldown",
"author": "jks15satoshi",
"desc": "为用户调用功能添加冷却时间(调用频率限制)功能",
"name": "冷却事件",
"repo": "jks15satoshi/nonebot-plugin-cooldown"
},
{
"id": "nonebot_plugin_mqtt",
"link": "nonebot-plugin-mqtt",
"author": "synodriver",
"desc": "接入mqtt网络,订阅和发布消息",
"name": "mqtt接入",
"repo": "synodriver/nonebot_plugin_mqtt"
},
{
"id": "nonebot-plugin-ipypreter",
"link": "nonebot-plugin-ipypreter",
"author": "iyume",
"desc": "消息交互式 python 解释器",
"name": "消息交互式 python 解释器",
"repo": "iyume/nonebot-plugin-ipypreter"
},
{
"id": "nonebot_plugin_songpicker2",
"link": "nonebot-plugin-songpicker2",
"author": "maxesisn",
"desc": "点播歌曲,支持候选菜单、热评显示,数据源为网易云",
"name": "songpicker2",
"repo": "maxesisn/nonebot_plugin_songpicker2"
},
{
"id": "nonebot_plugin_styledstr",
"link": "nonebot-plugin-styledstr",
"author": "jks15satoshi",
"desc": "通过字符串标签管理字符串资源",
"name": "风格化字符串管理",
"repo": "jks15satoshi/nonebot_plugin_styledstr"
}
]
21 changes: 21 additions & 0 deletions docs/api/exception.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ sidebarDepth: 0



## _exception_ `ParserExit`

基类:`nonebot.exception.NoneBotException`


* **说明**

`shell command` 处理消息失败时返回的异常



* **参数**


* `status`


* `message`



## _exception_ `PausedException`

基类:`nonebot.exception.NoneBotException`
Expand Down
3 changes: 3 additions & 0 deletions docs/api/nonebot.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ sidebarDepth: 0
* `on_command` => `nonebot.plugin.on_command`


* `on_shell_command` => `nonebot.plugin.on_shell_command`


* `on_regex` => `nonebot.plugin.on_regex`


Expand Down
140 changes: 140 additions & 0 deletions docs/api/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,63 @@ def something_else():



## `on_shell_command(cmd, rule=None, aliases=None, parser=None, **kwargs)`


* **说明**

注册一个支持 `shell_like` 解析参数的命令消息事件响应器。

与普通的 `on_command` 不同的是,在添加 `parser` 参数时, 响应器会自动处理消息。

并将用户输入的原始参数列表保存在 `state["argv"]`, `parser` 处理的参数保存在 `state["args"]` 中



* **参数**


* `cmd: Union[str, Tuple[str, ...]]`: 指定命令内容


* `rule: Optional[Union[Rule, T_RuleChecker]]`: 事件响应规则


* `aliases: Optional[Set[Union[str, Tuple[str, ...]]]]`: 命令别名


* `parser: Optional[ArgumentParser]`: `nonebot.rule.ArgumentParser` 对象


* `permission: Optional[Permission]`: 事件响应权限


* `handlers: Optional[List[T_Handler]]`: 事件处理函数列表


* `temp: bool`: 是否为临时事件响应器(仅执行一次)


* `priority: int`: 事件响应器优先级


* `block: bool`: 是否阻止事件向更低优先级传递


* `state: Optional[T_State]`: 默认 state


* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数



* **返回**


* `Type[Matcher]`



## `on_regex(pattern, flags=0, rule=None, **kwargs)`


Expand Down Expand Up @@ -623,6 +680,32 @@ def something_else():



### `shell_command(cmd, **kwargs)`


* **说明**

注册一个新的命令。



* **参数**


* `cmd: Union[str, Tuple[str, ...]]`: 命令前缀


* `**kwargs`: 其他传递给 `on_command` 的参数,将会覆盖命令组默认值



* **返回**


* `Type[Matcher]`



## _class_ `MatcherGroup`

基类:`object`
Expand Down Expand Up @@ -1070,6 +1153,63 @@ def something_else():



### `on_shell_command(cmd, rule=None, aliases=None, parser=None, **kwargs)`


* **说明**


注册一个支持 `shell_like` 解析参数的命令消息事件响应器。

与普通的 `on_command` 不同的是,在添加 `parser` 参数时, 响应器会自动处理消息。

并将用户输入的原始参数列表保存在 `state["argv"]`, `parser` 处理的参数保存在 `state["args"]` 中


* **参数**



* `cmd: Union[str, Tuple[str, ...]]`: 指定命令内容


* `rule: Optional[Union[Rule, T_RuleChecker]]`: 事件响应规则


* `aliases: Optional[Set[Union[str, Tuple[str, ...]]]]`: 命令别名


* `parser: Optional[ArgumentParser]`: `nonebot.rule.ArgumentParser` 对象


* `permission: Optional[Permission]`: 事件响应权限


* `handlers: Optional[List[T_Handler]]`: 事件处理函数列表


* `temp: bool`: 是否为临时事件响应器(仅执行一次)


* `priority: int`: 事件响应器优先级


* `block: bool`: 是否阻止事件向更低优先级传递


* `state: Optional[T_State]`: 默认 state


* `state_factory: Optional[T_StateFactory]`: 默认 state 的工厂函数


* **返回**



* `Type[Matcher]`


### `on_regex(pattern, flags=0, rule=None, **kwargs)`


Expand Down
55 changes: 55 additions & 0 deletions docs/api/rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,61 @@ Rule(async_function, run_sync(sync_function))
:::


## _class_ `ArgumentParser`

基类:`argparse.ArgumentParser`


* **说明**

`shell_like` 命令参数解析器,解析出错时不会退出程序。



## `shell_command(*cmds, parser=None)`


* **说明**

支持 `shell_like` 解析参数的命令形式匹配,根据配置里提供的 `command_start`, `command_sep` 判断消息是否为命令。

可以通过 `state["_prefix"]["command"]` 获取匹配成功的命令(例:`("test",)`),通过 `state["_prefix"]["raw_command"]` 获取匹配成功的原始命令文本(例:`"/test"`)。

可以通过 `state["argv"]` 获取用户输入的原始参数列表

添加 `parser` 参数后, 可以自动处理消息并将结果保存在 `state["args"]` 中。



* **参数**


* `*cmds: Union[str, Tuple[str, ...]]`: 命令内容


* `parser: Optional[ArgumentParser]`: `nonebot.rule.ArgumentParser` 对象



* **示例**

使用默认 `command_start`, `command_sep` 配置,更多示例参考 `argparse` 标准库文档。


```python
from nonebot.rule import ArgumentParser

parser = ArgumentParser()
parser.add_argument("-a", type=bool)

rule = shell_command("ls", parser=parser)
```

:::tip 提示
命令内容与后续消息间无需空格!
:::


## `regex(regex, flags=0)`


Expand Down
3 changes: 2 additions & 1 deletion nonebot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- ``on_endswith`` => ``nonebot.plugin.on_endswith``
- ``on_keyword`` => ``nonebot.plugin.on_keyword``
- ``on_command`` => ``nonebot.plugin.on_command``
- ``on_shell_command`` => ``nonebot.plugin.on_shell_command``
- ``on_regex`` => ``nonebot.plugin.on_regex``
- ``CommandGroup`` => ``nonebot.plugin.CommandGroup``
- ``Matchergroup`` => ``nonebot.plugin.MatcherGroup``
Expand Down Expand Up @@ -219,6 +220,6 @@ def run(host: Optional[str] = None,


from nonebot.plugin import on_message, on_notice, on_request, on_metaevent, CommandGroup, MatcherGroup
from nonebot.plugin import on_startswith, on_endswith, on_keyword, on_command, on_regex
from nonebot.plugin import on_startswith, on_endswith, on_keyword, on_command, on_shell_command, on_regex
from nonebot.plugin import load_plugin, load_plugins, load_builtin_plugins
from nonebot.plugin import export, require, get_plugin, get_loaded_plugins
17 changes: 17 additions & 0 deletions nonebot/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ def __str__(self):
return self.__repr__()


class ParserExit(NoneBotException):
"""
:说明:

``shell command`` 处理消息失败时返回的异常

:参数:

* ``status``
* ``message``
"""

def __init__(self, status=0, message=None):
self.status = status
self.message = message


class PausedException(NoneBotException):
"""
:说明:
Expand Down
Loading