diff --git a/archive/2.0.0a9.post1/README.md b/archive/2.0.0a10/README.md similarity index 100% rename from archive/2.0.0a9.post1/README.md rename to archive/2.0.0a10/README.md diff --git a/archive/2.0.0a9.post1/advanced/README.md b/archive/2.0.0a10/advanced/README.md similarity index 100% rename from archive/2.0.0a9.post1/advanced/README.md rename to archive/2.0.0a10/advanced/README.md diff --git a/archive/2.0.0a10/advanced/export-and-require.md b/archive/2.0.0a10/advanced/export-and-require.md new file mode 100644 index 00000000..cb24c428 --- /dev/null +++ b/archive/2.0.0a10/advanced/export-and-require.md @@ -0,0 +1,117 @@ +# 跨插件访问 + +由于 `nonebot2` 独特的插件加载机制,在使用 python 原有的 import 机制来进行插件之间的访问时,很可能会有奇怪的或者意料以外的情况发生。为了避免这种情况的发生,您可以有两种方法来实现跨插件访问: + +1. 将插件间的要使用的公共代码剥离出来,作为公共文件或者文件夹,提供给插件加以调用。 +2. 使用 `nonebot2` 提供的 `export` 和 `require` 机制,来实现插件间的互相调用。 + +第一种方法比较容易理解和实现,这里不再赘述,但需要注意的是,请不要将公共文件或者公共文件夹作为**插件**被 `nonebot2` 加载。 + +下面将介绍第二种方法—— `export` 和 `require` 机制: + +## 使用 export and require + +现在,假定有两个插件 `pluginA` 和 `pluginB`,需要在 `pluginB` 中调用 `pluginA` 中的一个变量 `varA` 和一个函数 `funcA`。 + +在上面的条件中涉及到了两种操作:一种是在 `pluginA` 的 `导出对象` 操作;而另一种是在 `pluginB` 的 `导入对象` 操作。在 `nonebot2` 中,`导出对象` 的操作用 `export` 机制来实现,`导入对象` 的操作用 `require` 机制来实现。下面,我们将逐一进行介绍。 + +:::warning 警告 + +使用这个方法进行跨插件访问时,**需要先加载`导出对象`的插件,再加载`导入对象`的插件。** + +::: + +### 使用 export + +在 `pluginA` 中,我们调用 `export` 机制 `导出对象`。 + +在 `export` 机制调用前,我们需要保证导出的对象已经被定义,比如: + +```python +varA = "varA" + + +def funcA(): + return "funcA" +``` + +在确保定义之后,我们可以从 `nonebot.plugin` 导入 `export()` 方法, `export()` 方法会返回一个特殊的字典 `export`: + +```python +from nonebot.plugin import export + +export=export() +``` + +这个字典可以用来装载导出的对象,它的 key 是对象导出后的命名,value 是对象本身,我们可以直接创建新的 `key` - `value` 对导出对象: + +```python +export.vA = varA +export.fA = funcA +``` + +除此之外,也支持 `嵌套` 导出对象: + +```python +export.sub.vA = varA +export.sub.fA = funcA +``` + +特别地,对于 `函数对象` 而言,`export` 支持用 `装饰器` 的方法来导出,因此,我们可以这样定义 `funcA`: + +```python +@export.sub +def funcA(): + return "funcA" +``` + +或者: + +```python +@export +def funcA(): + return "funcA" +``` + +通过 `装饰器` 的方法导出函数时,命名固定为函数的命名,也就是说,上面的两个例子等同于: + +```python +export.sub.funcA = funcA + +export.funcA = funcA +``` + +这样,我们就成功导出 `varA` 和 `funcA` 对象了。 + +下面我们将介绍如何在 `pluginB` 中导入这些对象。 + +### 使用 require + +在 `pluginB` 中,我们调用 `require` 机制 `导入对象`。 + +:::warning 警告 + +在导入来自其他插件的对象时, 请确保导出该对象的插件在引用该对象的插件之前加载。如果该插件并未被加载,则会尝试加载,加载失败则会返回 `None`。 + +::: + +我们可以从 `nonebot.plugin` 中导入 `require()` 方法: + +```python +from nonebot.plugin import require +``` + +`require()` 方法的参数是插件名, 它会返回在指定插件中,用 `export()` 方法创建的字典。 + +```python +require_A = require('pluginA') +``` + +在之前,这个字典已经存入了 `'vA'` - `varA`, `'fA'` - `funcA` 或 `'funcA'` - `funcA` 这样的 `key` - `value` 对。因此在这里我们直接用 `属性` 的方法来获取导入对象: + +```python +varA = require_A.vA +funcA = require_A.fA or require_A.funcA +``` + +这样,我们就在 `pluginB` 中成功导入了 `varA` 和 `funcA` 对象了。 diff --git a/archive/2.0.0a9.post1/advanced/overloaded-handlers.md b/archive/2.0.0a10/advanced/overloaded-handlers.md similarity index 100% rename from archive/2.0.0a9.post1/advanced/overloaded-handlers.md rename to archive/2.0.0a10/advanced/overloaded-handlers.md diff --git a/archive/2.0.0a9.post1/advanced/permission.md b/archive/2.0.0a10/advanced/permission.md similarity index 100% rename from archive/2.0.0a9.post1/advanced/permission.md rename to archive/2.0.0a10/advanced/permission.md diff --git a/archive/2.0.0a9.post1/advanced/publish-plugin.md b/archive/2.0.0a10/advanced/publish-plugin.md similarity index 100% rename from archive/2.0.0a9.post1/advanced/publish-plugin.md rename to archive/2.0.0a10/advanced/publish-plugin.md diff --git a/archive/2.0.0a9.post1/advanced/runtime-hook.md b/archive/2.0.0a10/advanced/runtime-hook.md similarity index 100% rename from archive/2.0.0a9.post1/advanced/runtime-hook.md rename to archive/2.0.0a10/advanced/runtime-hook.md diff --git a/archive/2.0.0a9.post1/advanced/scheduler.md b/archive/2.0.0a10/advanced/scheduler.md similarity index 100% rename from archive/2.0.0a9.post1/advanced/scheduler.md rename to archive/2.0.0a10/advanced/scheduler.md diff --git a/archive/2.0.0a9.post1/api/README.md b/archive/2.0.0a10/api/README.md similarity index 93% rename from archive/2.0.0a9.post1/api/README.md rename to archive/2.0.0a10/api/README.md index 36e9803e..e12dd0ff 100644 --- a/archive/2.0.0a9.post1/api/README.md +++ b/archive/2.0.0a10/api/README.md @@ -43,6 +43,9 @@ * [nonebot.drivers.fastapi](drivers/fastapi.html) + * [nonebot.drivers.quart](drivers/quart.html) + + * [nonebot.adapters](adapters/) diff --git a/archive/2.0.0a9.post1/api/adapters/README.md b/archive/2.0.0a10/api/adapters/README.md similarity index 100% rename from archive/2.0.0a9.post1/api/adapters/README.md rename to archive/2.0.0a10/api/adapters/README.md diff --git a/archive/2.0.0a9.post1/api/adapters/cqhttp.md b/archive/2.0.0a10/api/adapters/cqhttp.md similarity index 100% rename from archive/2.0.0a9.post1/api/adapters/cqhttp.md rename to archive/2.0.0a10/api/adapters/cqhttp.md diff --git a/archive/2.0.0a9.post1/api/adapters/ding.md b/archive/2.0.0a10/api/adapters/ding.md similarity index 100% rename from archive/2.0.0a9.post1/api/adapters/ding.md rename to archive/2.0.0a10/api/adapters/ding.md diff --git a/archive/2.0.0a9.post1/api/adapters/mirai.md b/archive/2.0.0a10/api/adapters/mirai.md similarity index 98% rename from archive/2.0.0a9.post1/api/adapters/mirai.md rename to archive/2.0.0a10/api/adapters/mirai.md index b627f72d..89ee9c0f 100644 --- a/archive/2.0.0a9.post1/api/adapters/mirai.md +++ b/archive/2.0.0a10/api/adapters/mirai.md @@ -965,15 +965,40 @@ CQHTTP 协议 MessageSegment 适配。具体方法参考 [mirai-api-http 消息 基类:[`nonebot.adapters.Message`](README.md#nonebot.adapters.Message) -Mirai 协议 Messaqge 适配 +Mirai 协议 Message 适配 由于Mirai协议的Message实现较为特殊, 故使用MessageChain命名 +### `reduce()` + + +* **说明** + + 忽略为空的消息段, 合并相邻的纯文本消息段 + + + ### `export()` 导出为可以被正常json序列化的数组 + +### `extract_first(*type)` + + +* **说明** + + 弹出该消息链的第一个消息 + + + +* **参数** + + + * \*type: MessageType: 指定的消息类型, 当指定后如类型不匹配不弹出 + + # NoneBot.adapters.mirai.utils 模块 @@ -1070,20 +1095,6 @@ mirai-api-http 协议事件,字段与 mirai-api-http 一致。各事件字段 > * `MEMBER`: 普通群成员 -## _class_ `MessageChain` - -基类:[`nonebot.adapters.Message`](README.md#nonebot.adapters.Message) - -Mirai 协议 Messaqge 适配 - -由于Mirai协议的Message实现较为特殊, 故使用MessageChain命名 - - -### `export()` - -导出为可以被正常json序列化的数组 - - ## _class_ `MessageEvent` 基类:`nonebot.adapters.mirai.event.base.Event` diff --git a/archive/2.0.0a9.post1/api/config.md b/archive/2.0.0a10/api/config.md similarity index 100% rename from archive/2.0.0a9.post1/api/config.md rename to archive/2.0.0a10/api/config.md diff --git a/archive/2.0.0a9.post1/api/drivers/README.md b/archive/2.0.0a10/api/drivers/README.md similarity index 100% rename from archive/2.0.0a9.post1/api/drivers/README.md rename to archive/2.0.0a10/api/drivers/README.md diff --git a/archive/2.0.0a9.post1/api/drivers/fastapi.md b/archive/2.0.0a10/api/drivers/fastapi.md similarity index 100% rename from archive/2.0.0a9.post1/api/drivers/fastapi.md rename to archive/2.0.0a10/api/drivers/fastapi.md diff --git a/archive/2.0.0a10/api/drivers/quart.md b/archive/2.0.0a10/api/drivers/quart.md new file mode 100644 index 00000000..068769e0 --- /dev/null +++ b/archive/2.0.0a10/api/drivers/quart.md @@ -0,0 +1,62 @@ +--- +contentSidebar: true +sidebarDepth: 0 +--- + +# NoneBot.drivers.quart 模块 + +## Quart 驱动适配 + +后端使用方法请参考: [Quart 文档](https://pgjones.gitlab.io/quart/index.html) + + +## _class_ `Driver` + +基类:[`nonebot.drivers.Driver`](README.md#nonebot.drivers.Driver) + +Quart 驱动框架 + + +* **上报地址** + + + * `/{adapter name}/http`: HTTP POST 上报 + + + * `/{adapter name}/ws`: WebSocket 上报 + + + +### _property_ `type` + +驱动名称: `quart` + + +### _property_ `server_app` + +`Quart` 对象 + + +### _property_ `asgi` + +`Quart` 对象 + + +### _property_ `logger` + +fastapi 使用的 logger + + +### `on_startup(func)` + +参考文档: [Startup and Shutdown](https://pgjones.gitlab.io/quart/how_to_guides/startup_shutdown.html) + + +### `on_shutdown(func)` + +参考文档: [Startup and Shutdown](https://pgjones.gitlab.io/quart/how_to_guides/startup_shutdown.html) + + +### `run(host=None, port=None, *, app=None, **kwargs)` + +使用 `uvicorn` 启动 Quart diff --git a/archive/2.0.0a9.post1/api/exception.md b/archive/2.0.0a10/api/exception.md similarity index 100% rename from archive/2.0.0a9.post1/api/exception.md rename to archive/2.0.0a10/api/exception.md diff --git a/archive/2.0.0a9.post1/api/log.md b/archive/2.0.0a10/api/log.md similarity index 100% rename from archive/2.0.0a9.post1/api/log.md rename to archive/2.0.0a10/api/log.md diff --git a/archive/2.0.0a9.post1/api/matcher.md b/archive/2.0.0a10/api/matcher.md similarity index 100% rename from archive/2.0.0a9.post1/api/matcher.md rename to archive/2.0.0a10/api/matcher.md diff --git a/archive/2.0.0a9.post1/api/message.md b/archive/2.0.0a10/api/message.md similarity index 100% rename from archive/2.0.0a9.post1/api/message.md rename to archive/2.0.0a10/api/message.md diff --git a/archive/2.0.0a9.post1/api/nonebot.md b/archive/2.0.0a10/api/nonebot.md similarity index 100% rename from archive/2.0.0a9.post1/api/nonebot.md rename to archive/2.0.0a10/api/nonebot.md diff --git a/archive/2.0.0a9.post1/api/permission.md b/archive/2.0.0a10/api/permission.md similarity index 100% rename from archive/2.0.0a9.post1/api/permission.md rename to archive/2.0.0a10/api/permission.md diff --git a/archive/2.0.0a9.post1/api/plugin.md b/archive/2.0.0a10/api/plugin.md similarity index 100% rename from archive/2.0.0a9.post1/api/plugin.md rename to archive/2.0.0a10/api/plugin.md diff --git a/archive/2.0.0a9.post1/api/rule.md b/archive/2.0.0a10/api/rule.md similarity index 100% rename from archive/2.0.0a9.post1/api/rule.md rename to archive/2.0.0a10/api/rule.md diff --git a/archive/2.0.0a9.post1/api/typing.md b/archive/2.0.0a10/api/typing.md similarity index 100% rename from archive/2.0.0a9.post1/api/typing.md rename to archive/2.0.0a10/api/typing.md diff --git a/archive/2.0.0a9.post1/api/utils.md b/archive/2.0.0a10/api/utils.md similarity index 100% rename from archive/2.0.0a9.post1/api/utils.md rename to archive/2.0.0a10/api/utils.md diff --git a/archive/2.0.0a9.post1/guide/README.md b/archive/2.0.0a10/guide/README.md similarity index 100% rename from archive/2.0.0a9.post1/guide/README.md rename to archive/2.0.0a10/guide/README.md diff --git a/archive/2.0.0a9.post1/guide/basic-configuration.md b/archive/2.0.0a10/guide/basic-configuration.md similarity index 100% rename from archive/2.0.0a9.post1/guide/basic-configuration.md rename to archive/2.0.0a10/guide/basic-configuration.md diff --git a/archive/2.0.0a9.post1/guide/cqhttp-guide.md b/archive/2.0.0a10/guide/cqhttp-guide.md similarity index 100% rename from archive/2.0.0a9.post1/guide/cqhttp-guide.md rename to archive/2.0.0a10/guide/cqhttp-guide.md diff --git a/archive/2.0.0a9.post1/guide/creating-a-handler.md b/archive/2.0.0a10/guide/creating-a-handler.md similarity index 100% rename from archive/2.0.0a9.post1/guide/creating-a-handler.md rename to archive/2.0.0a10/guide/creating-a-handler.md diff --git a/archive/2.0.0a9.post1/guide/creating-a-matcher.md b/archive/2.0.0a10/guide/creating-a-matcher.md similarity index 99% rename from archive/2.0.0a9.post1/guide/creating-a-matcher.md rename to archive/2.0.0a10/guide/creating-a-matcher.md index ac74f6c1..83debe13 100644 --- a/archive/2.0.0a9.post1/guide/creating-a-matcher.md +++ b/archive/2.0.0a10/guide/creating-a-matcher.md @@ -123,7 +123,7 @@ async def async_checker(bot: Bot, event: Event, state: T_State) -> bool: def sync_checker(bot: Bot, event: Event, state: T_State) -> bool: return True -def check(arg1, args2): +def check(arg1, arg2): async def _checker(bot: Bot, event: Event, state: T_State) -> bool: return bool(arg1 + arg2) diff --git a/archive/2.0.0a9.post1/guide/creating-a-plugin.md b/archive/2.0.0a10/guide/creating-a-plugin.md similarity index 100% rename from archive/2.0.0a9.post1/guide/creating-a-plugin.md rename to archive/2.0.0a10/guide/creating-a-plugin.md diff --git a/archive/2.0.0a9.post1/guide/creating-a-project.md b/archive/2.0.0a10/guide/creating-a-project.md similarity index 100% rename from archive/2.0.0a9.post1/guide/creating-a-project.md rename to archive/2.0.0a10/guide/creating-a-project.md diff --git a/archive/2.0.0a9.post1/guide/ding-guide.md b/archive/2.0.0a10/guide/ding-guide.md similarity index 100% rename from archive/2.0.0a9.post1/guide/ding-guide.md rename to archive/2.0.0a10/guide/ding-guide.md diff --git a/archive/2.0.0a9.post1/guide/end-or-start.md b/archive/2.0.0a10/guide/end-or-start.md similarity index 100% rename from archive/2.0.0a9.post1/guide/end-or-start.md rename to archive/2.0.0a10/guide/end-or-start.md diff --git a/archive/2.0.0a9.post1/guide/getting-started.md b/archive/2.0.0a10/guide/getting-started.md similarity index 100% rename from archive/2.0.0a9.post1/guide/getting-started.md rename to archive/2.0.0a10/guide/getting-started.md diff --git a/archive/2.0.0a9.post1/guide/installation.md b/archive/2.0.0a10/guide/installation.md similarity index 100% rename from archive/2.0.0a9.post1/guide/installation.md rename to archive/2.0.0a10/guide/installation.md diff --git a/archive/2.0.0a9.post1/guide/loading-a-plugin.md b/archive/2.0.0a10/guide/loading-a-plugin.md similarity index 88% rename from archive/2.0.0a9.post1/guide/loading-a-plugin.md rename to archive/2.0.0a10/guide/loading-a-plugin.md index f026bbe0..e3c7af2f 100644 --- a/archive/2.0.0a9.post1/guide/loading-a-plugin.md +++ b/archive/2.0.0a10/guide/loading-a-plugin.md @@ -6,12 +6,15 @@ 在 `bot.py` 文件中添加以下行: -```python{5} +```python{8} import nonebot +from nonebot.adapters.cqhttp import Bot nonebot.init() -# 加载 nonebot 内置插件 -nonebot.load_builtin_plugins() + +driver = nonebot.get_driver() +driver.register_adapter("cqhttp", Bot) # 注册 CQHTTP 的 Adapter +nonebot.load_builtin_plugins() # 加载 nonebot 内置插件 app = nonebot.get_asgi() @@ -19,6 +22,12 @@ if __name__ == "__main__": nonebot.run() ``` +::: warning +目前, 内建插件仅支持 CQHTTP 的 Adapter + +如果您使用的是其他 Adapter, 请移步该 Adapter 相应的文档 +::: + 这将会加载 nonebot 内置的插件,它包含: - 命令 `say`:可由**superuser**使用,可以将消息内容由特殊纯文本转为富文本 diff --git a/archive/2.0.0a9.post1/guide/mirai-guide.md b/archive/2.0.0a10/guide/mirai-guide.md similarity index 87% rename from archive/2.0.0a9.post1/guide/mirai-guide.md rename to archive/2.0.0a10/guide/mirai-guide.md index 403e55e0..c22631e0 100644 --- a/archive/2.0.0a9.post1/guide/mirai-guide.md +++ b/archive/2.0.0a10/guide/mirai-guide.md @@ -193,3 +193,36 @@ Mirai-API-HTTP 的适配器以 [AGPLv3 许可](https://opensource.org/licenses/A ``` 恭喜你, 你的配置已经成功! + +现在, 我们可以写一个简单的插件来测试一下 + +```python +from nonebot.plugin import on_keyword, on_command +from nonebot.rule import to_me +from nonebot.adapters.mirai import Bot, MessageEvent + +message_test = on_keyword({'reply'}, rule=to_me()) + + +@message_test.handle() +async def _message(bot: Bot, event: MessageEvent): + text = event.get_plaintext() + await bot.send(event, text, at_sender=True) + + +command_test = on_command('miecho') + + +@command_test.handle() +async def _echo(bot: Bot, event: MessageEvent): + text = event.get_plaintext() + await bot.send(event, text, at_sender=True) +``` + +它具有两种行为 + +- 在指定机器人,即私聊、群聊内@机器人、群聊内称呼机器人昵称的情况下 (即 [Rule: to_me](../api/rule.md#to-me)), 如果消息内包含 `reply` 字段, 则该消息会被机器人重复一次 + +- 在执行指令`miecho xxx`时, 机器人会发送回参数`xxx` + +至此, 你已经初步掌握了如何使用 Mirai Adapter diff --git a/archive/2.0.0a9.post1/sidebar.config.json b/archive/2.0.0a10/sidebar.config.json similarity index 97% rename from archive/2.0.0a9.post1/sidebar.config.json rename to archive/2.0.0a10/sidebar.config.json index 5498d089..5c4ef15c 100644 --- a/archive/2.0.0a9.post1/sidebar.config.json +++ b/archive/2.0.0a10/sidebar.config.json @@ -151,6 +151,10 @@ "title": "nonebot.drivers.fastapi 模块", "path": "drivers/fastapi" }, + { + "title": "nonebot.drivers.quart 模块", + "path": "drivers/quart" + }, { "title": "nonebot.adapters 模块", "path": "adapters/" diff --git a/archive/2.0.0a9.post1/advanced/export-and-require.md b/archive/2.0.0a9.post1/advanced/export-and-require.md deleted file mode 100644 index 832b0e75..00000000 --- a/archive/2.0.0a9.post1/advanced/export-and-require.md +++ /dev/null @@ -1 +0,0 @@ -# 跨插件访问 diff --git a/docs/.vuepress/versions.json b/docs/.vuepress/versions.json index f4d9a4cb..6744a990 100644 --- a/docs/.vuepress/versions.json +++ b/docs/.vuepress/versions.json @@ -1,5 +1,5 @@ [ - "2.0.0a9.post1", + "2.0.0a10", "2.0.0a8.post2", "2.0.0a7" ] \ No newline at end of file