Compare commits

...

400 Commits

Author SHA1 Message Date
noneflow[bot]
94eecaf448 🔖 Release 2.1.2 2023-10-31 10:05:03 +00:00
Ju4tCode
fa91e0e79b 🔖 bump version 2.1.2 (#2437) 2023-10-31 17:55:31 +08:00
noneflow[bot]
891adc38fc 📝 Update changelog 2023-10-31 09:27:26 +00:00
Ju4tCode
af6cc63db2 ⬆️ upgrade pytest-asyncio and fix test (#2436) 2023-10-31 17:26:06 +08:00
noneflow[bot]
af73e14b64 📝 Update changelog 2023-10-27 15:11:11 +00:00
Ju4tCode
9305fe7875 🐛 修复依赖注入对 Literal 检查报错 (#2433) 2023-10-27 23:09:32 +08:00
noneflow[bot]
613fde4639 📝 Update changelog 2023-10-25 02:57:15 +00:00
T0nyX1ang
61db2c898b 🍻 publish plugin 定时广播插件 (#2431) 2023-10-25 02:55:43 +00:00
dependabot[bot]
acf313c420 ⬆️ Bump the actions group (#2430)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-10-24 13:47:28 +08:00
noneflow[bot]
15fca08641 📝 Update changelog 2023-10-22 06:29:50 +00:00
StarHeart
e2cbe3c1f8 📝 Docs: 修复 Alconna 文档 typo (#2429) 2023-10-22 14:28:29 +08:00
noneflow[bot]
d3883ea3ae 📝 Update changelog 2023-10-21 12:25:27 +00:00
SherkeyXD
8b2c4b3e60 🍻 publish plugin 选择困难症 (#2427) 2023-10-21 12:23:54 +00:00
noneflow[bot]
65d0d00591 📝 Update changelog 2023-10-18 07:56:54 +00:00
RainEggplant
97a57c2f6e Feature: 添加多消息段命令解析支持 (#2419)
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
2023-10-18 15:55:09 +08:00
noneflow[bot]
6559b2ff27 📝 Update changelog 2023-10-17 10:51:13 +00:00
StarHeartHunt
4c1deeb899 🍻 publish bot 芙芙 (#2425) 2023-10-17 10:49:41 +00:00
noneflow[bot]
a65ea6805d 📝 Update changelog 2023-10-17 02:13:27 +00:00
hanasa2023
effe65b034 🍻 publish plugin nonebot-plugin-getbapics (#2420) 2023-10-17 02:11:40 +00:00
noneflow[bot]
37296cf048 📝 Update changelog 2023-10-16 14:31:07 +00:00
StarHeart
1b597c1301 📝 add baidu statistic script (#2424) 2023-10-16 09:29:48 -05:00
noneflow[bot]
c2454d0689 📝 Update changelog 2023-10-15 07:33:10 +00:00
Yuri-YuzuChaN
9b60b44554 🍻 publish plugin nonebot-plugin-maimaidx (#2421) 2023-10-15 07:31:48 +00:00
noneflow[bot]
75516bdafb 📝 Update changelog 2023-10-14 12:33:47 +00:00
MerCuJerry
12f5a487c1 🍻 publish plugin BlueArchive Title Generator (#2417) 2023-10-14 12:32:20 +00:00
noneflow[bot]
8d128d5035 📝 Update changelog 2023-10-13 06:13:24 +00:00
Agnes4m
cfa7117e64 🍻 publish plugin VRChat查询 (#2410) 2023-10-13 06:12:10 +00:00
noneflow[bot]
7880bf0dc1 📝 Update changelog 2023-10-13 05:50:53 +00:00
influ3nza
0054041829 🍻 publish plugin nonebot_plugin_fgoavatarguess (#2415) 2023-10-13 05:49:26 +00:00
noneflow[bot]
99931f785a 📝 Update changelog 2023-10-10 12:35:55 +00:00
jiangyuxiaoxiao
5e121269f0 🍻 publish bot 妃爱 (#2412) 2023-10-10 12:34:36 +00:00
noneflow[bot]
38ced0243f 📝 Update changelog 2023-10-10 06:28:40 +00:00
EuDs63
869db878e1 🍻 publish plugin nonebot-plugin-yesman​ (#2408) 2023-10-10 06:27:23 +00:00
noneflow[bot]
e6c6e355e1 📝 Update changelog 2023-10-09 02:19:46 +00:00
ninthseason
6221b9a5fd 🍻 publish plugin morep-finder (#2406) 2023-10-09 02:18:19 +00:00
noneflow[bot]
5f2c9c935b 📝 Update changelog 2023-10-08 10:11:53 +00:00
Ju4tCode
76559b253c Update assets/adapters.json 2023-10-08 10:10:28 +00:00
pre-commit-ci[bot]
3c54655c39 🚨 auto fix by pre-commit hooks 2023-10-08 10:10:28 +00:00
Ju4tCode
7a851ac199 Apply suggestions from code review 2023-10-08 10:10:28 +00:00
pre-commit-ci[bot]
b2ba5dfcd1 🚨 auto fix by pre-commit hooks 2023-10-08 10:10:28 +00:00
Tarrailt
4a4fae8f8c Update README.md 2023-10-08 10:10:28 +00:00
RF-Tar-Railt
de894ce7b2 🍻 publish adapter Satori (#2404) 2023-10-08 10:10:28 +00:00
noneflow[bot]
09c4a955c9 📝 Update changelog 2023-10-07 07:31:56 +00:00
ninthseason
db1581a0a2 🍻 publish plugin op-finder (#2402) 2023-10-07 07:30:07 +00:00
noneflow[bot]
db9d7b3060 📝 Update changelog 2023-10-07 04:07:34 +00:00
Tarrailt
7e0c29472e 📝 Docs: 更新最佳实践 Alconna (#2401)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-10-07 12:06:07 +08:00
noneflow[bot]
d13492070d 📝 Update changelog 2023-10-06 10:04:18 +00:00
Ohdmire
695ede51ea 🍻 publish plugin nonebot-plugin-playercheck (#2399) 2023-10-06 10:02:52 +00:00
noneflow[bot]
168f382aa6 📝 Update changelog 2023-10-06 06:23:20 +00:00
nikissXI
5bd433318d ✏️ Plugin: 移除 nonebot-plugin-nya-music 插件 (#2398) 2023-10-06 14:21:51 +08:00
noneflow[bot]
d1cd2a793e 📝 Update changelog 2023-10-06 03:17:45 +00:00
nikissXI
5a4464f338 🍻 publish plugin talk with eop ai (#2396) 2023-10-06 03:16:15 +00:00
noneflow[bot]
561d25320b 📝 Update changelog 2023-10-04 10:21:51 +00:00
Komorebi
b225c2dd3b 📝 Docs: 修改商店发布的跳转链接 (#2387)
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
2023-10-04 18:20:06 +08:00
noneflow[bot]
2a2e357513 📝 Update changelog 2023-10-04 10:13:30 +00:00
zhuhiki
28bfe1ecb8 🍻 publish plugin 算法比赛查询和今日比赛自动提醒 (#2394) 2023-10-04 10:12:01 +00:00
pre-commit-ci[bot]
cc12f0af7e ⬆️ auto update by pre-commit hooks (#2393)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-10-03 21:49:16 +08:00
noneflow[bot]
da831a1b08 📝 Update changelog 2023-10-03 11:59:10 +00:00
MelodyKnit
eb97be17dd 🍻 publish plugin 屏蔽词插件 (#2386) 2023-10-03 11:57:53 +00:00
noneflow[bot]
2dd1c9b2ad 📝 Update changelog 2023-10-03 11:38:02 +00:00
MingxuanGame
41191db863 🐛 Docs: 修复文档主页 Features 不居中 (#2390) 2023-10-03 19:36:40 +08:00
noneflow[bot]
ee20204b22 📝 Update changelog 2023-10-03 10:53:54 +00:00
Dobiichi-Origami
f1032804bb 🍻 publish plugin Nonebot Agent (#2388) 2023-10-03 10:52:35 +00:00
noneflow[bot]
ba1540d75b 📝 Update changelog 2023-10-02 15:04:27 +00:00
uy/sun
f5c87f80e1 🧑‍💻 CI: 调整商店数据存放位置与内容 (#2385) 2023-10-02 23:03:05 +08:00
noneflow[bot]
d2d7603ff5 📝 Update changelog 2023-10-02 06:44:11 +00:00
KomoriDev
56013dca48 🍻 publish plugin 聚能环 (#2383) 2023-10-02 06:42:55 +00:00
noneflow[bot]
d33ed4a69f 📝 Update changelog 2023-10-02 06:38:57 +00:00
Ju4tCode
ed753b5564 ✏️ Adapter: 修改频道适配器为 QQ 适配器 (#2382)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-10-02 14:37:28 +08:00
Ju4tCode
7e65552d01 👷 fix doc package publish ci 2023-10-01 07:31:27 +00:00
Ju4tCode
f77dc523e6 👷 CI: 修复 Release 权限错误 (#2381) 2023-10-01 15:15:32 +08:00
noneflow[bot]
0d84bf3592 🔖 Release 2.1.1 2023-10-01 06:38:37 +00:00
Ju4tCode
94dff49e60 🔖 bump version 2.1.1 (#2380) 2023-10-01 14:27:31 +08:00
noneflow[bot]
5d4cf7e421 📝 Update changelog 2023-10-01 03:45:36 +00:00
Ju4tCode
0e3e16e809 🚨 make pyright happy (#2379) 2023-10-01 11:44:00 +08:00
noneflow[bot]
183fc8defb 📝 Update changelog 2023-09-29 11:15:25 +00:00
Komorebi
8712e89322 ✏️ 修复商店搜索信息的错字 (#2377) 2023-09-29 19:13:33 +08:00
noneflow[bot]
e2b49f9b65 📝 Update changelog 2023-09-27 10:27:33 +00:00
Ju4tCode
7e11f3a3d6 📝 Docs: 修复侧边栏 TOC 在 SSR 模式下的渲染问题 (#2376) 2023-09-27 18:26:13 +08:00
noneflow[bot]
71bebb6ec7 📝 Update changelog 2023-09-27 08:01:54 +00:00
Ju4tCode
842c6ff4c6 📝 Docs: 升级新版 NonePress 主题 (#2375) 2023-09-27 16:00:26 +08:00
noneflow[bot]
7754f6da1d 📝 Update changelog 2023-09-25 03:04:05 +00:00
Ailitonia
60e0752f1a 🐛 Fix: bot.call_api 在被 called api hook mock 后应该忽略 exception (#2374)
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
2023-09-25 11:02:50 +08:00
noneflow[bot]
ede1a20c53 📝 Update changelog 2023-09-23 09:21:01 +00:00
student_2333
04289fd50f ✏️ Plugin: 修改 Sekai Stickers 插件信息 (#2372) 2023-09-23 17:19:38 +08:00
noneflow[bot]
ba3efa9e7c 📝 Update changelog 2023-09-22 02:55:04 +00:00
StarHeart
c5a66a6ed0 📝 Docs: 增加赞助者显示 (#2371)
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
2023-09-22 10:53:33 +08:00
noneflow[bot]
8a23b1554a 📝 Update changelog 2023-09-22 02:39:17 +00:00
lgc2333
d73f226cbd 🍻 publish plugin 大电老师活字印刷 (#2369) 2023-09-22 02:38:08 +00:00
noneflow[bot]
fd9ba678ec 📝 Update changelog 2023-09-20 05:54:15 +00:00
Q1351998764
d29ba62ff9 🍻 publish plugin nonebot-plugin-video-api (#2366) 2023-09-20 05:52:59 +00:00
noneflow[bot]
00c97fd18f 📝 Update changelog 2023-09-13 16:22:19 +00:00
uy/sun
9531c3fa74 👷 CI: 使用更现代的功能 (#2362) 2023-09-14 00:21:06 +08:00
noneflow[bot]
94293122e8 📝 Update changelog 2023-09-13 16:16:15 +00:00
Bryan不可思议
7aaa66c8ba Feature: 优先使用 Annotated 的最后一个子依赖 (#2360)
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
2023-09-14 00:14:45 +08:00
noneflow[bot]
0030bf725e 📝 Update changelog 2023-09-13 05:57:07 +00:00
Ju4tCode
22b6062900 Docs: 添加 wwads (#2361) 2023-09-13 13:55:48 +08:00
noneflow[bot]
005968ab70 📝 Update changelog 2023-09-12 07:14:46 +00:00
Akirami
dc6c194701 Feature: 优化检查事件响应器的日志 (#2355)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-09-12 15:13:35 +08:00
noneflow[bot]
9b8772b590 📝 Update changelog 2023-09-12 06:32:51 +00:00
Akirami
ae8ba9f55d 📝 Docs: 更新 get_asgi 函数的文档字符串 (#2359) 2023-09-12 14:31:41 +08:00
noneflow[bot]
f4a7ce2c09 📝 Update changelog 2023-09-11 05:03:01 +00:00
TeenStudyFlow
c84723668f 🍻 publish plugin 青年大学习提交 (#2356) 2023-09-11 05:01:50 +00:00
dependabot[bot]
bd3ed4207a ⬆️ Bump tibdex/github-app-token from 1 to 2 (#2358)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-09-11 12:52:59 +08:00
noneflow[bot]
1e8c2cfc9f 🔖 Release 2.1.0 2023-09-10 03:45:49 +00:00
Ju4tCode
5ce0238ace 🔖 bump version 2.1.0 (#2354) 2023-09-10 11:34:15 +08:00
noneflow[bot]
4e6b52b85c 📝 Update changelog 2023-09-09 17:22:00 +00:00
幼稚园园长
05fe7bb715 ✏️ Plugin: 删除插件 nonebot-plugin-heisi (#2353) 2023-09-10 01:20:48 +08:00
noneflow[bot]
c555e2fac6 📝 Update changelog 2023-09-09 13:00:23 +00:00
Tarrailt
fd126ae154 Feature: 为 Matcher.HANDLER_PARAM_TYPES 补增类型 (#2352)
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
2023-09-09 20:58:50 +08:00
noneflow[bot]
6c7b6a9575 📝 Update changelog 2023-09-09 05:47:23 +00:00
Ju4tCode
c4716e3e17 Feature: 为事件响应器添加更多源码信息 (#2351) 2023-09-09 13:46:09 +08:00
noneflow[bot]
3601a33f20 📝 Update changelog 2023-09-09 03:54:56 +00:00
Tarrailt
451023518b 📝 Docs: 更新最佳实践部分的 Alconna 章节 (#2349)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-09-09 11:53:06 +08:00
noneflow[bot]
2bd377a221 📝 Update changelog 2023-09-07 04:02:19 +00:00
Noctulus
66384adad4 🍻 publish plugin 文心一言 (#2341) 2023-09-07 04:00:51 +00:00
pre-commit-ci[bot]
ec1f7ba5bc ⬆️ auto update by pre-commit hooks (#2347)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-09-05 18:33:56 +08:00
dependabot[bot]
e7fc5b7b7e ⬆️ Bump actions/checkout from 3 to 4 (#2345)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-09-05 12:00:44 +08:00
noneflow[bot]
11477ea9d7 📝 Update changelog 2023-09-05 02:50:44 +00:00
StarHeart
6adf40f45d ⬆️ bump node version to 18 (#2344) 2023-09-05 10:49:09 +08:00
noneflow[bot]
1bdf169980 📝 Update changelog 2023-09-04 16:19:22 +00:00
Akirami
81cb356503 📝 Feature: 补充依赖注入部分情况下类型错误时的日志提示 (#2343) 2023-09-05 00:17:55 +08:00
noneflow[bot]
805778794c 📝 Update changelog 2023-09-04 03:58:08 +00:00
Rikka-desu
28cd8dd08a 🍻 publish plugin nonebot_plugin_group_whitelist (#2319) 2023-09-04 03:56:46 +00:00
noneflow[bot]
139b39984e 📝 Update changelog 2023-09-03 10:59:42 +00:00
GuGuMur
f9b5fece80 🍻 publish plugin 森空岛明日方舟签到器 (#2339) 2023-09-03 10:58:10 +00:00
noneflow[bot]
8076c6bc0a 📝 Update changelog 2023-09-03 05:40:53 +00:00
Ju4tCode
44b89d13f8 🚨 fix ruff error (#2338) 2023-09-03 13:39:26 +08:00
noneflow[bot]
fbc4225110 📝 Update changelog 2023-09-03 02:48:40 +00:00
Lfhsheng
f07f35ccc1 🍻 publish plugin 女装 ! (#2335) 2023-09-03 02:47:25 +00:00
noneflow[bot]
111dfbf164 📝 Update changelog 2023-09-02 11:38:52 +00:00
fR0Z863xF
c713c7723b 🍻 publish plugin helper_plus (#2322) 2023-09-02 11:37:35 +00:00
noneflow[bot]
4fa2af41b0 📝 Update changelog 2023-09-02 03:47:51 +00:00
xiaoWangSec
39c09d22d1 🍻 publish plugin nonebot-plugin-souti (#2333) 2023-09-02 03:46:33 +00:00
noneflow[bot]
4819b21f52 📝 Update changelog 2023-09-02 02:45:09 +00:00
uy/sun
6ef6721527 👷 插件测试使用最新的稳定版 Python 版本 (#2336) 2023-09-02 10:43:48 +08:00
noneflow[bot]
14cb447874 📝 Update changelog 2023-08-31 09:34:31 +00:00
ZM25XC
1b2b89074d ✏️ Plugin: 删除不再维护的插件 (#2330)
Co-authored-by: ZM25XC <xingling25@qq.com>
2023-08-31 17:32:52 +08:00
noneflow[bot]
75c5678782 📝 Update changelog 2023-08-30 15:50:29 +00:00
RF-Tar-Railt
45ec5cdfb4 🍻 publish plugin Alconna 帮助工具 (#2325) 2023-08-30 15:49:08 +00:00
noneflow[bot]
f6dd98825b 📝 Update changelog 2023-08-29 10:46:36 +00:00
Ju4tCode
f59271bd47 Feature: 支持子依赖定义 Pydantic 类型校验 (#2310) 2023-08-29 18:45:12 +08:00
noneflow[bot]
79f833b946 📝 Update changelog 2023-08-29 07:04:44 +00:00
惜月
9ad562bbfd 📝 Docs: 添加 Discord 适配器描述,补充 Villa 适配器协议链接 (#2316)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-08-29 15:03:21 +08:00
noneflow[bot]
267b49247d 📝 Update changelog 2023-08-29 05:53:43 +00:00
Ju4tCode
dbda4150fb Update website/static/adapters.json 2023-08-29 05:52:08 +00:00
CMHopeSunshine
a4e17f0c49 🍻 publish adapter Discord (#2314) 2023-08-29 05:52:08 +00:00
noneflow[bot]
8d8d1169d1 📝 Update changelog 2023-08-29 02:26:38 +00:00
Cvandia
7bc9e61985 🍻 publish plugin 消息伪造 (#2311) 2023-08-29 02:24:53 +00:00
Tarrailt
35cc6011b5 📝 Docs: 添加 Red 适配器描述 (#2313)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-08-29 10:23:17 +08:00
noneflow[bot]
086af8fd22 📝 Update changelog 2023-08-28 03:49:31 +00:00
tomorinao-www
a60d1520e6 🍻 publish plugin 二维码 (#2301) 2023-08-28 03:48:12 +00:00
noneflow[bot]
30c22ba25a 📝 Update changelog 2023-08-28 02:30:21 +00:00
nikissXI
41fbaec42c ✏️ Plugin: 删除插件 poe ai (#2308) 2023-08-28 10:29:04 +08:00
noneflow[bot]
562ec79e3b 📝 Update changelog 2023-08-27 15:11:41 +00:00
XTxiaoting14332
f620bd8eb2 🍻 publish plugin httpcat-状态猫😺 (#2305) 2023-08-27 15:10:11 +00:00
noneflow[bot]
13e40458d7 📝 Update changelog 2023-08-26 13:51:15 +00:00
Tarrailt
dc4ac6d8d7 📝 Docs: 更新最佳实践部分的 Alconna 章节 (#2303)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-08-26 21:49:28 +08:00
noneflow[bot]
41498bdf21 📝 Update changelog 2023-08-26 03:14:28 +00:00
Agnes4m
b8eae2eb82 🍻 publish plugin 雪豹闭嘴 (#2299) 2023-08-26 03:12:53 +00:00
noneflow[bot]
039c2b5509 📝 Update changelog 2023-08-26 03:05:06 +00:00
Ju4tCode
2e635370bb Feature: 细化 driver 职责类型 (#2296) 2023-08-26 11:03:24 +08:00
noneflow[bot]
807a86371d 📝 Update changelog 2023-08-24 02:36:22 +00:00
Ailitonia
c66953779c 🍻 publish plugin Nonebot Requests (#2293) 2023-08-24 02:34:50 +00:00
noneflow[bot]
117ef18f1c 📝 Update changelog 2023-08-23 06:36:32 +00:00
Well404
520dd03d77 ✏️ Plugin: 移除不再维护的插件,修改插件信息 (#2292) 2023-08-23 14:35:13 +08:00
noneflow[bot]
63f3ca2f6f 📝 Update changelog 2023-08-23 06:15:39 +00:00
eya46
2e8230e9f4 🐛 Fix: 设置 file request 默认 filename (#2284)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
2023-08-23 14:14:07 +08:00
noneflow[bot]
4bfea99e54 📝 Update changelog 2023-08-23 02:55:04 +00:00
bingqiu456
f58eba7975 🍻 publish plugin 双向聊天插件 (#2264) 2023-08-23 02:53:48 +00:00
noneflow[bot]
53d1de4aec 📝 Update changelog 2023-08-19 12:46:43 +00:00
tomorinao-www
00f18c1bd8 🍻 publish plugin 识别动漫gal角色 (#2287) 2023-08-19 12:45:05 +00:00
noneflow[bot]
ba4fbb2ec3 📝 Update changelog 2023-08-19 05:43:52 +00:00
LuckySJTU
b3722bd637 🍻 publish plugin arxiv订阅 (#2281) 2023-08-19 05:42:43 +00:00
noneflow[bot]
012bd6d4fb 📝 Update changelog 2023-08-18 11:08:58 +00:00
Ju4tCode
9c4ca28d61 🚨 make linter happy (#2286) 2023-08-18 19:07:35 +08:00
noneflow[bot]
53bcae04ff 📝 Update changelog 2023-08-18 07:01:12 +00:00
This-is-XiaoDeng
754c54e268 🍻 publish plugin SUDO (#2276) 2023-08-18 06:59:44 +00:00
noneflow[bot]
f97fbc814e 📝 Update changelog 2023-08-18 05:50:08 +00:00
mobyw
b8856a0577 🍻 publish plugin 消息推送插件 (#2272) 2023-08-18 05:48:49 +00:00
noneflow[bot]
1c0e88907b 📝 Update changelog 2023-08-18 03:47:58 +00:00
Komorebi
31b6df5b39 📝 Docs: 修复 Alconna 中 CommandResult 描述错误 (#2282) 2023-08-18 11:46:39 +08:00
noneflow[bot]
bca9e4fd08 📝 Update changelog 2023-08-18 03:12:30 +00:00
Akirami
026ceb5028 📝 Docs: 修复子依赖部分代码行号错误 (#2279) 2023-08-18 11:11:23 +08:00
noneflow[bot]
47d5a647b7 📝 Update changelog 2023-08-18 03:02:49 +00:00
Akirami
37d7230949 📝 Docs: 补充 get_last_receive 示例 (#2278) 2023-08-18 11:01:23 +08:00
noneflow[bot]
be458b1d5e 📝 Update changelog 2023-08-17 05:34:53 +00:00
fu050409
f375a4a723 🍻 publish plugin 周易蓍草占卜 (#2267) 2023-08-17 05:33:24 +00:00
noneflow[bot]
3edce9a630 📝 Update changelog 2023-08-16 16:47:20 +00:00
Akirami
c525bda1e0 📝 Docs: 修复文档中错误的标点 (#2275) 2023-08-17 00:45:53 +08:00
noneflow[bot]
417f586e0d 📝 Update changelog 2023-08-16 02:19:02 +00:00
ACnoway
80d7e68835 🍻 publish bot OCNbot (#2260) 2023-08-16 02:17:44 +00:00
noneflow[bot]
a284e6df5c 📝 Update changelog 2023-08-14 13:11:21 +00:00
Akirami
7176a69f81 📝 Docs: 修复配置文档中 Nickname 属性的描述错误 (#2271) 2023-08-14 21:09:56 +08:00
noneflow[bot]
e3a1c02e8a 📝 Update changelog 2023-08-14 02:48:09 +00:00
fu050409
5e789ae4e0 🍻 publish plugin 欧若可骰娘 (#2265) 2023-08-14 02:46:51 +00:00
noneflow[bot]
bb684e20cb 📝 Update changelog 2023-08-13 03:20:03 +00:00
惜月
e11293e46b 📝 Docs: 适配器编写教程 (#2079)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-08-13 11:18:41 +08:00
noneflow[bot]
e0d74a1657 📝 Update changelog 2023-08-12 12:43:00 +00:00
A-kirami
fdd36565b1 🍻 publish bot 星见Kirami (#2262) 2023-08-12 12:41:43 +00:00
noneflow[bot]
28c53fe0d7 📝 Update changelog 2023-08-12 03:13:01 +00:00
Alpaca4610
26539bf2b1 🍻 publish plugin 科大讯飞星火大模型聊天 (#2257) 2023-08-12 03:11:53 +00:00
dependabot[bot]
347889c822 ⬆️ Bump actions/setup-node in /.github/actions/setup-node (#2259)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2023-08-11 20:32:11 +08:00
noneflow[bot]
91849b762c 📝 Update changelog 2023-08-11 12:23:01 +00:00
Ju4tCode
5d1319ddb9 🔧 add dependabot actions check (#2256) 2023-08-11 20:21:31 +08:00
noneflow[bot]
d98228926e 📝 Update changelog 2023-08-09 14:44:06 +00:00
Akirami
493997d998 📝 Docs: 更新贡献指南 (#2255) 2023-08-09 22:42:38 +08:00
noneflow[bot]
3098b7c153 📝 Update changelog 2023-08-09 07:59:41 +00:00
fuyang0811
2b0a050226 🍻 publish plugin 剑网三查询和推送 (#2253) 2023-08-09 07:58:33 +00:00
noneflow[bot]
1f3abc2bb9 📝 Update changelog 2023-08-08 13:24:50 +00:00
XTxiaoting14332
dd5541e658 🍻 publish plugin Muteme(我禁我自己) (#2251) 2023-08-08 13:23:28 +00:00
noneflow[bot]
a76bf27f60 📝 Update changelog 2023-08-08 02:21:17 +00:00
CN171-1
d70ce366cc 🍻 publish plugin MC版本更新检测 (#2246) 2023-08-08 02:19:18 +00:00
noneflow[bot]
f94b802c9b 📝 Update changelog 2023-08-07 12:47:46 +00:00
itsevin
17d7bd4e31 🍻 publish bot 不正经的妹妹 (#2248) 2023-08-07 12:46:37 +00:00
noneflow[bot]
76a40b60ff 📝 Update changelog 2023-08-07 12:40:29 +00:00
SuperGuGuGu
469efedab2 🍻 publish plugin KanonBot (#2243) 2023-08-07 12:39:09 +00:00
noneflow[bot]
383699a8b4 📝 Update changelog 2023-08-05 06:03:22 +00:00
eya46
1b9a07b923 🐛 Docs: 修复文档 Last updated author 错误 (#2241) 2023-08-05 14:01:53 +08:00
noneflow[bot]
15b76c266c 📝 Update changelog 2023-08-04 10:12:23 +00:00
zhaomaoniu
dfdecaddb1 🍻 publish adapter RedProtocol (#2238) 2023-08-04 10:11:11 +00:00
noneflow[bot]
5de9de903d 📝 Update changelog 2023-08-04 09:34:00 +00:00
Tarrailt
327f3fa441 📝 Docs: 更新最佳实践部分的 Alconna 章节 (#2237)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-08-04 17:32:43 +08:00
noneflow[bot]
08fde7580c 📝 Update changelog 2023-08-04 04:08:03 +00:00
Sydrr0
4ca91ecc7e 🍻 publish plugin CSGO饰品查询机器人 (#2224) 2023-08-04 04:06:38 +00:00
noneflow[bot]
885db90bc0 📝 Update changelog 2023-08-04 02:55:40 +00:00
nikissXI
c43d631eb5 🍻 publish plugin talk with poe ai (#2229) 2023-08-04 02:54:21 +00:00
noneflow[bot]
cfda433d14 📝 Update changelog 2023-08-03 02:34:48 +00:00
EmiyaGm
ea4a27bf89 🍻 publish plugin 命运方舟流浪商人卡牌刷新提示 (#2233) 2023-08-03 02:33:28 +00:00
noneflow[bot]
23944833f2 📝 Update changelog 2023-08-02 06:50:39 +00:00
Yan-Zero
4a40782be0 🍻 publish plugin Savepic (#2231) 2023-08-02 06:49:20 +00:00
noneflow[bot]
babafcaa87 📝 Update changelog 2023-08-01 13:45:03 +00:00
canxin121
9b164a6f5a 🍻 publish plugin 跨平台账户绑定 (#2226) 2023-08-01 13:43:42 +00:00
pre-commit-ci[bot]
4a07981972 ⬆️ auto update by pre-commit hooks (#2228)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-08-01 20:53:53 +08:00
noneflow[bot]
6bb2c46f8a 📝 Update changelog 2023-07-30 02:44:17 +00:00
qwqZYLqwq
2054655912 🍻 publish plugin Among US中的TOH模组职业介绍 (#2220) 2023-07-30 02:43:07 +00:00
noneflow[bot]
062af45367 📝 Update changelog 2023-07-27 11:25:39 +00:00
lgc2333
83c3ed5966 🍻 publish plugin NoneMeme (#2218) 2023-07-27 11:24:31 +00:00
noneflow[bot]
a2f2b818a7 📝 Update changelog 2023-07-25 06:53:29 +00:00
A-kirami
e7941efd9a 🍻 publish plugin The World (#2215) 2023-07-25 06:52:10 +00:00
noneflow[bot]
aa6faba9ae 📝 Update changelog 2023-07-25 02:34:07 +00:00
ZM25XC
8ca72f3c64 🍻 publish plugin Bot上下线邮件通知 (#2213) 2023-07-25 02:32:47 +00:00
noneflow[bot]
45e10e7139 📝 Update changelog 2023-07-24 15:05:00 +00:00
Cypas
73d1b19669 🍻 publish plugin bot断连通知 (#2211) 2023-07-24 15:03:43 +00:00
noneflow[bot]
ad4cf86a96 📝 Update changelog 2023-07-24 03:35:45 +00:00
Ju4tCode
48b3e3aaf3 add git attributes (#2210) 2023-07-24 11:34:34 +08:00
noneflow[bot]
f2b0b1752b 🔖 Release 2.0.1 2023-07-23 08:24:19 +00:00
Ju4tCode
81dcc65f99 🔖 bump version 2.0.1 (#2209) 2023-07-23 16:21:58 +08:00
noneflow[bot]
ac90df929e 📝 Update changelog 2023-07-21 14:38:47 +00:00
Tarrailt
555268239f 📝 Docs: 移动 Alconna 文档至最佳实践 (#2208)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
2023-07-21 22:37:34 +08:00
noneflow[bot]
7009c8e8c1 📝 Update changelog 2023-07-21 12:53:17 +00:00
Jigsaw
2f3cc84f82 📝 Docs: 移除商店中不符合现规范的 tag (#2205) 2023-07-21 20:51:48 +08:00
noneflow[bot]
9444e01f0f 📝 Update changelog 2023-07-21 08:57:14 +00:00
NCBM
23b7a94b9a 🍻 publish plugin 方寸狭间 (#2206) 2023-07-21 08:56:06 +00:00
noneflow[bot]
70ece41b66 📝 Update changelog 2023-07-19 05:23:59 +00:00
Rockytkg
a5bb6e4220 🍻 publish plugin DALL-E绘图 (#2200) 2023-07-19 05:22:34 +00:00
noneflow[bot]
4fc99771c5 📝 Update changelog 2023-07-19 02:49:00 +00:00
canxin121
6601def5f7 🍻 publish plugin 指定戳一戳 (#2201) 2023-07-19 02:47:51 +00:00
noneflow[bot]
b2edea141e 📝 Update changelog 2023-07-19 02:15:58 +00:00
Ju4tCode
38886b9651 📝 Docs: 添加 scoped 插件配置指南 (#2198) 2023-07-19 10:14:36 +08:00
noneflow[bot]
1b225cbbca 📝 Update changelog 2023-07-18 05:36:22 +00:00
canxin121
b4f004c500 🍻 publish plugin templates_render (#2196) 2023-07-18 05:34:54 +00:00
noneflow[bot]
7a345714aa 📝 Update changelog 2023-07-17 12:43:28 +00:00
Ju4tCode
cb9fcae64c 🧑‍💻 Develop: 添加 Pyright 检查 (#2194) 2023-07-17 20:42:15 +08:00
noneflow[bot]
6ebeefed79 📝 Update changelog 2023-07-17 07:57:36 +00:00
Ju4tCode
6dc87a9455 use typing.override instead (#2193) 2023-07-17 15:56:27 +08:00
noneflow[bot]
7dd7c927bf 📝 Update changelog 2023-07-17 07:02:34 +00:00
Ju4tCode
e167865686 🐛 fix quart context error (#2192) 2023-07-17 15:01:21 +08:00
noneflow[bot]
29364679c4 📝 Update changelog 2023-07-16 13:43:30 +00:00
LambdaYH
ebbe8beec0 🍻 publish bot 米缸 (#2190) 2023-07-16 13:42:16 +00:00
noneflow[bot]
a04580e79e 📝 Update changelog 2023-07-14 16:28:15 +00:00
Well2333
bfe9e7e253 🍻 publish plugin MongoDB (#2188) 2023-07-14 16:26:50 +00:00
noneflow[bot]
720398198f 📝 Update changelog 2023-07-12 14:19:44 +00:00
Agnes4m
5ebf349886 🍻 publish plugin pjsk表情 (#2186) 2023-07-12 14:18:32 +00:00
noneflow[bot]
f8f5750c3b 📝 Update changelog 2023-07-11 15:16:11 +00:00
Q1351998764
8d9be61406 🍻 publish plugin nonebot-plugin-wenan (#2183) 2023-07-11 15:15:01 +00:00
noneflow[bot]
42ea650509 📝 Update changelog 2023-07-11 12:15:30 +00:00
mute23-code
a941a0f292 🍻 publish bot 林汐 (#2181) 2023-07-11 12:14:17 +00:00
noneflow[bot]
89f8745425 📝 Update changelog 2023-07-11 11:19:00 +00:00
Q1351998764
cc476528d8 🍻 publish plugin nonebot-plugin-picture-api (#2179) 2023-07-11 11:17:50 +00:00
noneflow[bot]
64f6c2dd4c 📝 Update changelog 2023-07-11 05:23:32 +00:00
MerCuJerry
81d9531b42 🍻 publish plugin Blocker (#2177) 2023-07-11 05:22:13 +00:00
noneflow[bot]
3512b0ab98 📝 Update changelog 2023-07-10 15:48:44 +00:00
Lptr-byte
ab3e916770 🍻 publish plugin nonebot-plugin-nobahpicture (#2175) 2023-07-10 15:47:23 +00:00
noneflow[bot]
21376a5bfa 📝 Update changelog 2023-07-08 07:35:00 +00:00
Akirami
5046b2a86e 📝 Docs: 钩子函数代码片段补充 (#2173) 2023-07-08 15:33:45 +08:00
noneflow[bot]
910c768910 📝 Update changelog 2023-07-08 07:27:52 +00:00
Akirami
5a526ddb40 📝 Docs: 格式化钩子函数中的代码片段 (#2172) 2023-07-08 15:26:30 +08:00
noneflow[bot]
4c5c97dca6 📝 Update changelog 2023-07-08 07:04:46 +00:00
Akirami
b3e0fb4830 ✏️ Plugin: 黑白名单添加标签 (#2170) 2023-07-08 15:03:35 +08:00
noneflow[bot]
258aa7d2d7 📝 Update changelog 2023-07-08 05:43:41 +00:00
A-kirami
5c72fd5ba7 🍻 publish plugin 过期事件过滤器 (#2168) 2023-07-08 05:42:28 +00:00
noneflow[bot]
26e4f23a67 📝 Update changelog 2023-07-08 03:41:39 +00:00
HuParry
28fc6c35f0 🍻 publish plugin 猫猫虫咖波图片发送 (#2166) 2023-07-08 03:40:25 +00:00
noneflow[bot]
3ef1d7d5d7 📝 Update changelog 2023-07-08 02:23:24 +00:00
Cypas
8474d8987e 🍻 publish plugin nonebot-plugin-splatoon3 (#2163) 2023-07-08 02:22:08 +00:00
noneflow[bot]
13ddfa1bdd 📝 Update changelog 2023-07-07 17:33:46 +00:00
coyude
ec8be10f26 🍻 publish plugin nonebot-plugin-cfassistant (#2162) 2023-07-07 17:32:16 +00:00
noneflow[bot]
511c521a68 📝 Update changelog 2023-07-07 13:28:22 +00:00
HuParry
0ef5940d0f 🍻 publish plugin 算法竞赛比赛查询 (#2158) 2023-07-07 13:26:51 +00:00
noneflow[bot]
eecc881cd8 📝 Update changelog 2023-07-07 03:29:19 +00:00
eya46
770141cf0a 📝 Docs: 补充 Message.only 文档 (#2155) 2023-07-07 11:28:08 +08:00
noneflow[bot]
b2b20ffc4a 📝 Update changelog 2023-07-06 07:29:30 +00:00
eya46
94a6067a4b Feature: 补充响应器组属性 (#2154)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-07-06 15:18:08 +08:00
noneflow[bot]
77220d9d1f 📝 Update changelog 2023-07-05 15:57:33 +00:00
djkcyl
647ad9ff8f 🍻 publish plugin nonebot-plugin-update (#2152) 2023-07-05 15:56:15 +00:00
pre-commit-ci[bot]
04182eefba ⬆️ auto update by pre-commit hooks (#2149)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-07-04 16:39:12 +08:00
noneflow[bot]
7b4aa08c54 📝 Update changelog 2023-07-04 02:47:02 +00:00
eya46
0033d7c686 🐛 Fix: 修复 dotenv 配置项为 None 将会跳过赋值 (#2143)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
2023-07-04 10:45:55 +08:00
noneflow[bot]
c40b95f3e9 📝 Update changelog 2023-07-03 02:29:35 +00:00
Fireinsect
1fa44ca5c1 ✏️ Plugin: 修改 nonebot-plugin-ocgbot-v2 插件名称 (#2147) 2023-07-03 10:28:27 +08:00
noneflow[bot]
381f6633f6 📝 Update changelog 2023-07-03 02:18:44 +00:00
Agnes4m
d617508e32 🍻 publish plugin 远程同意好友 (#2145) 2023-07-03 02:17:36 +00:00
noneflow[bot]
8248e88686 📝 Update changelog 2023-07-02 14:28:26 +00:00
canxin
25649373a6 ✏️ Plugin: 更新 SparkGPT 插件描述 (#2144)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-07-02 22:27:13 +08:00
noneflow[bot]
3bee189598 📝 Update changelog 2023-07-01 07:41:39 +00:00
eya46
c1b1742b20 Feature: CommandGroup 支持命令别名添加前缀选项 (#2134)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-07-01 15:40:30 +08:00
noneflow[bot]
3e826cab72 📝 Update changelog 2023-07-01 05:55:19 +00:00
Fireinsect
4ef4bb0042 ✏️ Plugin: 修改 nonebot-plugin-ocgbot-v2 插件名称 (#2141) 2023-07-01 13:54:09 +08:00
Agnes4m
25ac653623 🍻 publish plugin 戳一戳事件 (#2138) 2023-06-30 12:45:13 +00:00
noneflow[bot]
b35bdfe6dc 📝 Update changelog 2023-06-30 12:44:37 +00:00
17TheWord
f06efca8cc 📝 Docs: 修复日志自定义文档 typo (#2140) 2023-06-30 20:43:26 +08:00
noneflow[bot]
a899523607 📝 Update changelog 2023-06-29 02:27:59 +00:00
lgc2333
2c162335cb 🍻 publish plugin EitherChoice (#2136) 2023-06-29 02:26:32 +00:00
noneflow[bot]
3a12984d4b 📝 Update changelog 2023-06-28 12:23:53 +00:00
MeetWq
7211f24a7d 🍻 publish plugin 用户信息 (#2132) 2023-06-28 12:22:35 +00:00
noneflow[bot]
649624ed80 📝 Update changelog 2023-06-27 08:26:35 +00:00
worldmozara
c03ff4e676 Feature: 添加用于动态继承支持适配器数据的方法 (#2127) 2023-06-27 16:25:27 +08:00
noneflow[bot]
0b5a18cb63 📝 Update changelog 2023-06-27 02:13:28 +00:00
wsdtl
518bf16082 🍻 publish bot web_bot (#2129) 2023-06-27 02:12:19 +00:00
noneflow[bot]
b625a5d19a 📝 Update changelog 2023-06-26 05:27:55 +00:00
kexue
acca22e179 ✏️ Plugin: 删除 nonebot-plugin-phlogo (#2128) 2023-06-26 13:26:26 +08:00
noneflow[bot]
a3009d45dc 📝 Update changelog 2023-06-25 15:02:27 +00:00
QBkira
fd3d1bb115 🍻 publish plugin Diablo4地狱狂潮boss提醒小助手 (#2121) 2023-06-25 15:01:22 +00:00
noneflow[bot]
7282da8b04 📝 Update changelog 2023-06-25 03:30:28 +00:00
eya46
7a3c7476fb 📝 Docs: 修复依赖注入文档 ArgStr 3.9+ 和 3.8+ 版本代码写反 (#2126)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-06-25 11:29:10 +08:00
noneflow[bot]
f1046cfb11 📝 Update changelog 2023-06-24 11:19:31 +00:00
eya46
8de25447b3 🐛 Fix: 修复 ArgParam 不支持 Annotated (#2124)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-06-24 19:18:24 +08:00
noneflow[bot]
3cdbf35dc6 📝 Update changelog 2023-06-24 09:06:04 +00:00
Agnes Digital
0228e255e1 ✏️ Plugin: 修改 nonebot-plugin-gw2 模块名 (#2123)
Co-authored-by: Agnes Digital <Z735803792@163.com>
2023-06-24 17:04:50 +08:00
noneflow[bot]
353d16ebfd 📝 Update changelog 2023-06-24 06:48:39 +00:00
Ju4tCode
3d5dd5969c 🚨 Develop: 添加 ruff linter (#2114) 2023-06-24 14:47:35 +08:00
noneflow[bot]
fe21cbfa1d 📝 Update changelog 2023-06-23 14:00:26 +00:00
fireinsect
c20f65636f 🍻 publish plugin nonbot-plugin-ocgbot-v2 (#2118) 2023-06-23 13:59:13 +00:00
noneflow[bot]
eade8face6 📝 Update changelog 2023-06-23 12:28:20 +00:00
worldmozara
ab75133e9d ✏️ Plugin: 更新 nonebot-plugin-msgbuf 插件的名称等信息 (#2119) 2023-06-23 20:27:08 +08:00
noneflow[bot]
89596fb708 📝 Update changelog 2023-06-22 02:42:23 +00:00
ssttkkl
eedcf0779d 🍻 publish plugin 错误告警 (#2116) 2023-06-22 02:41:06 +00:00
noneflow[bot]
05333260b7 📝 Update changelog 2023-06-21 05:28:42 +00:00
Agnes Digital
55fd447230 ✏️ Plugin: 修改插件信息和仓库地址 (#2115)
Co-authored-by: Agnes Digital <Z735803792@163.com>
2023-06-21 13:27:37 +08:00
noneflow[bot]
263e6b25e2 📝 Update changelog 2023-06-20 05:51:13 +00:00
Ju4tCode
e00890033e add plugin metadata to builtin plugins (#2113) 2023-06-20 13:50:05 +08:00
noneflow[bot]
20d3d62bd5 📝 Update changelog 2023-06-19 09:50:07 +00:00
Ju4tCode
080b876d93 👷 Test: 移除 httpbin 并整理测试 (#2110) 2023-06-19 17:48:59 +08:00
noneflow[bot]
27a3d1f0bb 📝 Update changelog 2023-06-19 08:48:59 +00:00
CMHopeSunshine
7a47985c2b 🍻 publish plugin follow_withdraw (#2111) 2023-06-19 08:47:51 +00:00
noneflow[bot]
8d97081948 📝 Update changelog 2023-06-18 13:17:46 +00:00
ThirdBlood
f4ffa07c8b 🍻 publish bot ReimeiBot-黎明机器人 (#2106) 2023-06-18 13:16:43 +00:00
noneflow[bot]
1b1ddc5c0f 📝 Update changelog 2023-06-14 09:07:00 +00:00
uy/sun
30dbd270a6 👷 CI: 缓存 NoneFlow 所需的 pre-commit hooks (#2104) 2023-06-14 17:05:52 +08:00
noneflow[bot]
7d3c7c4933 📝 Update changelog 2023-06-14 05:05:52 +00:00
0Neptune0
8c8436a94f 🍻 publish plugin 战雷查水表 (#2102) 2023-06-14 05:04:44 +00:00
noneflow[bot]
8601942ed3 📝 Update changelog 2023-06-13 13:46:01 +00:00
pre-commit-ci[bot]
4cc958ca17 🚨 auto fix by pre-commit hooks 2023-06-13 13:44:48 +00:00
SuperGuGuGu
472a2c7866 🍻 publish plugin bili_push (#2100) 2023-06-13 13:44:48 +00:00
noneflow[bot]
222609182e 📝 Update changelog 2023-06-12 13:19:50 +00:00
forchannot
dccf2f3ca8 🔥 Docs: 删除商店插件发布多余模块 (#2095)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-06-12 21:18:25 +08:00
noneflow[bot]
156807c365 📝 Update changelog 2023-06-12 12:40:57 +00:00
worldmozara
50941f5259 📝 Docs: 微调插件元数据的部分描述 (#2096) 2023-06-12 20:39:28 +08:00
noneflow[bot]
2de1524a89 📝 Update changelog 2023-06-11 15:49:49 +00:00
uy/sun
bdd17b62cc Feature: 插件商店适配最新的插件元数据 (#2094)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-06-11 23:48:37 +08:00
noneflow[bot]
3a9e800a58 📝 Update changelog 2023-06-11 15:42:26 +00:00
worldmozara
cb8d48c362 📝 Docs: 完成发布插件教程 (#2078)
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
Co-authored-by: uy/sun <hmy0119@hotmail.com>
2023-06-11 23:41:16 +08:00
noneflow[bot]
a5981c05d5 📝 Update changelog 2023-06-11 13:06:02 +00:00
worldmozara
4cb87e596d 📝 Docs: 更新插件元数据的相关描述 (#2087) 2023-06-11 21:04:58 +08:00
noneflow[bot]
2725a0a324 📝 Update changelog 2023-06-11 07:34:45 +00:00
Ju4tCode
f6b0809e5f Feature: 依赖注入支持 Generic TypeVar 和 Matcher 重载 (#2089) 2023-06-11 15:33:33 +08:00
noneflow[bot]
6181c1760f 📝 Update changelog 2023-06-11 07:00:08 +00:00
Jigsaw
324277091c 🐛 Fix: aiohttp 请求时 data 和 file 不能同时存在 (#2088) 2023-06-11 14:59:05 +08:00
noneflow[bot]
6eef863b70 📝 Update changelog 2023-06-11 06:50:18 +00:00
Alpaca4610
7d52f5af4d 🍻 publish plugin AI作曲 (#2092) 2023-06-11 06:48:53 +00:00
noneflow[bot]
0a70721ec0 📝 Update changelog 2023-06-11 04:47:10 +00:00
reine-ishyanami
f430f061ec 🍻 publish plugin pcrjjc (#2090) 2023-06-11 04:46:06 +00:00
noneflow[bot]
572be1eb47 📝 Update changelog 2023-06-07 09:33:36 +00:00
惜月
29cf7de1a6 📝 add Villa adapter to README (#2086) 2023-06-07 17:32:31 +08:00
noneflow[bot]
c61e3cab90 📝 Update changelog 2023-06-06 16:23:08 +00:00
CMHopeSunshine
77bdc5ecba 🍻 publish adapter 大别野 (#2084) 2023-06-06 16:22:06 +00:00
pre-commit-ci[bot]
16054d18c6 ⬆️ auto update by pre-commit hooks (#2083)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2023-06-06 17:22:45 +08:00
noneflow[bot]
f0361295c3 📝 Update changelog 2023-06-05 09:18:27 +00:00
nek0us
9bd1964ae2 🍻 publish plugin twitter订阅 (#2081) 2023-06-05 09:17:09 +00:00
noneflow[bot]
9141c88f77 📝 Update changelog 2023-06-03 14:46:48 +00:00
DiheChen
491855876b 🐛 Fix: 修复因 loguru 更新导致的启动和关闭日志 name 不正常 (#2080)
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
2023-06-03 22:45:46 +08:00
noneflow[bot]
6df28dd2a8 📝 Update changelog 2023-06-03 03:28:06 +00:00
ssttkkl
142d0f4d95 🍻 publish plugin 链接防夹 (#2073) 2023-06-03 03:27:04 +00:00
noneflow[bot]
0127d765ae 📝 Update changelog 2023-06-02 10:14:15 +00:00
Bill Chen
207c6b3c15 ✏️ Plugin: 移除过时未更新的插件&Bot (#2072) 2023-06-02 18:13:04 +08:00
noneflow[bot]
d2e699a13a 📝 Update changelog 2023-06-02 10:10:24 +00:00
Agnes4m
ce9ba7dd9b 🍻 publish plugin 碧蓝航线攻略 (#2075) 2023-06-02 10:09:13 +00:00
noneflow[bot]
2af23c9d89 📝 Update changelog 2023-06-01 15:55:58 +00:00
BalconyJH
8ee0f5efc4 ✏️ Plugin: 删除插件 nonebot_plugin_r6s (#2071) 2023-06-01 23:54:46 +08:00
497 changed files with 41525 additions and 12311 deletions

View File

@@ -10,9 +10,11 @@
"settings": { "settings": {
"python.analysis.diagnosticMode": "workspace", "python.analysis.diagnosticMode": "workspace",
"python.analysis.typeCheckingMode": "basic", "python.analysis.typeCheckingMode": "basic",
"ruff.organizeImports": false,
"[python]": { "[python]": {
"editor.defaultFormatter": "ms-python.black-formatter", "editor.defaultFormatter": "ms-python.black-formatter",
"editor.codeActionsOnSave": { "editor.codeActionsOnSave": {
"source.fixAll.ruff": true,
"source.organizeImports": true "source.organizeImports": true
} }
}, },
@@ -44,6 +46,7 @@
"ms-python.vscode-pylance", "ms-python.vscode-pylance",
"ms-python.isort", "ms-python.isort",
"ms-python.black-formatter", "ms-python.black-formatter",
"charliermarsh.ruff",
"EditorConfig.EditorConfig", "EditorConfig.EditorConfig",
"esbenp.prettier-vscode", "esbenp.prettier-vscode",
"bradlc.vscode-tailwindcss" "bradlc.vscode-tailwindcss"

6
.eslintignore Normal file
View File

@@ -0,0 +1,6 @@
dist
node_modules
.yarn
.history
build
lib

85
.eslintrc.js Normal file
View File

@@ -0,0 +1,85 @@
module.exports = {
root: true,
env: {
browser: true,
commonjs: true,
node: true,
},
parser: "@typescript-eslint/parser",
parserOptions: {
tsconfigRootDir: __dirname,
project: ["./tsconfig.json", "./website/tsconfig.json"],
},
globals: {
JSX: true,
},
extends: [
"eslint:recommended",
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/recommended",
"plugin:regexp/recommended",
"plugin:prettier/recommended",
],
settings: {
"import/resolver": {
node: {
extensions: [".js", ".jsx", ".ts", ".tsx"],
},
typescript: true,
},
react: {
version: "detect",
},
},
overrides: [
{
files: ["*.ts", "*.tsx"],
rules: {
"import/no-unresolved": "off",
},
},
{
files: ["*.js", "*.cjs"],
rules: {
"@typescript-eslint/no-var-requires": "off",
},
},
],
plugins: ["@typescript-eslint"],
rules: {
"linebreak-style": ["error", "unix"],
quotes: ["error", "double", { avoidEscape: true }],
semi: ["error", "always"],
"@typescript-eslint/no-non-null-assertion": "off",
"import/order": [
"error",
{
groups: [
"builtin",
"external",
"internal",
"parent",
"sibling",
"index",
],
pathGroups: [
{ pattern: "react", group: "builtin", position: "before" },
{ pattern: "fs-extra", group: "builtin" },
{ pattern: "lodash", group: "external", position: "before" },
{ pattern: "clsx", group: "external", position: "before" },
{ pattern: "@theme/**", group: "internal" },
{ pattern: "@site/**", group: "internal" },
{ pattern: "@theme-init/**", group: "internal" },
{ pattern: "@theme-original/**", group: "internal" },
],
pathGroupsExcludedImportTypes: [],
"newlines-between": "always",
alphabetize: {
order: "asc",
},
},
],
},
};

1
.gitattributes vendored Normal file
View File

@@ -0,0 +1 @@
website/versioned_*/** linguist-documentation

View File

@@ -3,22 +3,6 @@ title: "Plugin: {name}"
description: 发布插件到 NoneBot 官方商店 description: 发布插件到 NoneBot 官方商店
labels: ["Plugin"] labels: ["Plugin"]
body: body:
- type: input
id: name
attributes:
label: 插件名称
description: 插件名称
validations:
required: true
- type: input
id: description
attributes:
label: 插件描述
description: 插件描述
validations:
required: true
- type: input - type: input
id: pypi id: pypi
attributes: attributes:
@@ -37,15 +21,6 @@ body:
validations: validations:
required: true required: true
- type: input
id: homepage
attributes:
label: 插件项目仓库/主页链接
description: 插件项目仓库/主页链接
placeholder: e.g. https://github.com/xxx/xxx
validations:
required: true
- type: input - type: input
id: tags id: tags
attributes: attributes:
@@ -55,3 +30,14 @@ body:
value: "[]" value: "[]"
validations: validations:
required: true required: true
- type: textarea
id: config
attributes:
label: 插件配置项
description: 插件配置项
render: dotenv
placeholder: |
# e.g.
# KEY=VALUE
# KEY2=VALUE2

View File

@@ -4,18 +4,10 @@ description: Setup Node
runs: runs:
using: "composite" using: "composite"
steps: steps:
- uses: actions/setup-node@v2 - uses: actions/setup-node@v4
with: with:
node-version: "16" node-version: "18"
cache: "yarn"
- id: yarn-cache-dir-path - run: yarn install --frozen-lockfile
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
shell: bash
- uses: actions/cache@v3
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
- run: yarn install
shell: bash shell: bash

View File

@@ -4,3 +4,34 @@ updates:
directory: "/" directory: "/"
schedule: schedule:
interval: daily interval: daily
groups:
actions:
patterns:
- "*"
- package-ecosystem: github-actions
directory: "/.github/actions/build-api-doc"
schedule:
interval: daily
groups:
actions:
patterns:
- "*"
- package-ecosystem: github-actions
directory: "/.github/actions/setup-node"
schedule:
interval: daily
groups:
actions:
patterns:
- "*"
- package-ecosystem: github-actions
directory: "/.github/actions/setup-python"
schedule:
interval: daily
groups:
actions:
patterns:
- "*"

View File

@@ -27,7 +27,7 @@ jobs:
PYTHON_VERSION: ${{ matrix.python-version }} PYTHON_VERSION: ${{ matrix.python-version }}
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
- name: Setup Python environment - name: Setup Python environment
uses: ./.github/actions/setup-python uses: ./.github/actions/setup-python

View File

@@ -12,7 +12,7 @@ on:
concurrency: concurrency:
group: ${{ github.workflow }}-${{ github.event.issue.number || github.run_id }} group: ${{ github.workflow }}-${{ github.event.issue.number || github.run_id }}
cancel-in-progress: true cancel-in-progress: false
jobs: jobs:
plugin_test: plugin_test:
@@ -40,6 +40,7 @@ jobs:
outputs: outputs:
result: ${{ steps.plugin-test.outputs.RESULT }} result: ${{ steps.plugin-test.outputs.RESULT }}
output: ${{ steps.plugin-test.outputs.OUTPUT }} output: ${{ steps.plugin-test.outputs.OUTPUT }}
metadata: ${{ steps.plugin-test.outputs.METADATA }}
steps: steps:
- name: Install Poetry - name: Install Poetry
if: ${{ !startsWith(github.event_name, 'pull_request') }} if: ${{ !startsWith(github.event_name, 'pull_request') }}
@@ -48,7 +49,7 @@ jobs:
- name: Setup Python - name: Setup Python
uses: actions/setup-python@v4 uses: actions/setup-python@v4
with: with:
python-version: "3.10" python-version: "3.x"
- name: Test Plugin - name: Test Plugin
id: plugin-test id: plugin-test
@@ -61,28 +62,39 @@ jobs:
steps: steps:
- name: Generate token - name: Generate token
id: generate-token id: generate-token
uses: tibdex/github-app-token@v1 uses: tibdex/github-app-token@v2
with: with:
app_id: ${{ secrets.APP_ID }} app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.APP_KEY }} private_key: ${{ secrets.APP_KEY }}
- name: Checkout Code - name: Checkout Code
uses: actions/checkout@v3 uses: actions/checkout@v4
with: with:
token: ${{ steps.generate-token.outputs.token }} token: ${{ steps.generate-token.outputs.token }}
- name: Cache pre-commit hooks
uses: actions/cache@v3
with:
path: .cache/.pre-commit
key: noneflow-${{ runner.os }}-${{ hashFiles('.pre-commit-config.yaml') }}
- name: NoneFlow - name: NoneFlow
uses: docker://ghcr.io/nonebot/noneflow:latest uses: docker://ghcr.io/nonebot/noneflow:latest
with: with:
config: > config: >
{ {
"base": "master", "base": "master",
"plugin_path": "website/static/plugins.json", "plugin_path": "assets/plugins.json",
"bot_path": "website/static/bots.json", "bot_path": "assets/bots.json",
"adapter_path": "website/static/adapters.json" "adapter_path": "assets/adapters.json"
} }
env: env:
PLUGIN_TEST_RESULT: ${{ needs.plugin_test.outputs.result }} PLUGIN_TEST_RESULT: ${{ needs.plugin_test.outputs.result }}
PLUGIN_TEST_OUTPUT: ${{ needs.plugin_test.outputs.output }} PLUGIN_TEST_OUTPUT: ${{ needs.plugin_test.outputs.output }}
PLUGIN_TEST_METADATA: ${{ needs.plugin_test.outputs.metadata }}
APP_ID: ${{ secrets.APP_ID }} APP_ID: ${{ secrets.APP_ID }}
PRIVATE_KEY: ${{ secrets.APP_KEY }} PRIVATE_KEY: ${{ secrets.APP_KEY }}
PRE_COMMIT_HOME: /github/workspace/.cache/.pre-commit
- name: Fix permission
run: sudo chown -R $(whoami):$(id -ng) .cache/.pre-commit

26
.github/workflows/pyright.yml vendored Normal file
View File

@@ -0,0 +1,26 @@
name: Pyright Lint
on:
push:
branches:
- master
pull_request:
paths:
- "nonebot/**"
- "packages/**"
- "tests/**"
jobs:
pyright:
name: Pyright Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Python environment
uses: ./.github/actions/setup-python
- run: echo "$(poetry env info --path)/bin" >> $GITHUB_PATH
- name: Run Pyright
uses: jakebailey/pyright-action@v1

View File

@@ -20,12 +20,12 @@ jobs:
steps: steps:
- name: Generate token - name: Generate token
id: generate-token id: generate-token
uses: tibdex/github-app-token@v1 uses: tibdex/github-app-token@v2
with: with:
app_id: ${{ secrets.APP_ID }} app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.APP_KEY }} private_key: ${{ secrets.APP_KEY }}
- uses: actions/checkout@v3 - uses: actions/checkout@v4
with: with:
token: ${{ steps.generate-token.outputs.token }} token: ${{ steps.generate-token.outputs.token }}
@@ -35,7 +35,7 @@ jobs:
- uses: release-drafter/release-drafter@v5 - uses: release-drafter/release-drafter@v5
id: release-drafter id: release-drafter
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
- name: Update Changelog - name: Update Changelog
uses: docker://ghcr.io/nonebot/auto-changelog:master uses: docker://ghcr.io/nonebot/auto-changelog:master
@@ -59,8 +59,18 @@ jobs:
release: release:
if: startsWith(github.ref, 'refs/tags/') if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
steps: steps:
- uses: actions/checkout@v3 - name: Generate token
id: generate-token
uses: tibdex/github-app-token@v2
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.APP_KEY }}
- uses: actions/checkout@v4
- name: Setup Python Environment - name: Setup Python Environment
uses: ./.github/actions/setup-python uses: ./.github/actions/setup-python
@@ -71,33 +81,53 @@ jobs:
- name: Build API Doc - name: Build API Doc
uses: ./.github/actions/build-api-doc uses: ./.github/actions/build-api-doc
- run: | - name: Get Version
echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV id: version
run: |
echo "VERSION=$(poetry version -s)" >> $GITHUB_OUTPUT
echo "TAG_VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
echo "TAG_NAME=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT
- name: Check Version
if: steps.version.outputs.VERSION != steps.version.outputs.TAG_VERSION
run: exit 1
- uses: release-drafter/release-drafter@v5 - uses: release-drafter/release-drafter@v5
with: with:
name: Release ${{ env.TAG_NAME }} 🌈 name: Release ${{ steps.version.outputs.TAG_NAME }} 🌈
tag: ${{ env.TAG_NAME }} tag: ${{ steps.version.outputs.TAG_NAME }}
publish: true publish: true
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
- name: Build and Publish Package - name: Build Package
run: | run: |
poetry build poetry build
poetry publish -u ${{secrets.PYPI_USERNAME}} -p ${{secrets.PYPI_PASSWORD}}
gh release upload --clobber ${{ env.TAG_NAME }} dist/*.tar.gz dist/*.whl - name: Publish package to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
- name: Publish package to GitHub
run: |
gh release upload --clobber ${{ steps.version.outputs.TAG_NAME }} dist/*.tar.gz dist/*.whl
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
- name: Build and Publish Doc Package - name: Build and Publish Doc Package
run: | run: |
yarn build:plugin --out-dir ../packages/nonebot-plugin-docs/nonebot_plugin_docs/dist yarn build:plugin --out-dir ../packages/nonebot-plugin-docs/nonebot_plugin_docs/dist
export NONEBOT_VERSION=`poetry version -s`
cd packages/nonebot-plugin-docs/ cd packages/nonebot-plugin-docs/
poetry version $NONEBOT_VERSION poetry version ${{ steps.version.outputs.VERSION }}
poetry build poetry build
poetry publish -u ${{secrets.PYPI_USERNAME}} -p ${{secrets.PYPI_PASSWORD}}
gh release upload --clobber ${{ env.TAG_NAME }} dist/*.tar.gz dist/*.whl - name: Publish Doc Package to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: packages/nonebot-plugin-docs/dist/
- name: Publish Doc Package to GitHub
run: |
cd packages/nonebot-plugin-docs/
gh release upload --clobber ${{ steps.version.outputs.TAG_NAME }} dist/*.tar.gz dist/*.whl
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}

View File

@@ -9,12 +9,12 @@ jobs:
steps: steps:
- name: Generate token - name: Generate token
id: generate-token id: generate-token
uses: tibdex/github-app-token@v1 uses: tibdex/github-app-token@v2
with: with:
app_id: ${{ secrets.APP_ID }} app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.APP_KEY }} private_key: ${{ secrets.APP_KEY }}
- uses: actions/checkout@v3 - uses: actions/checkout@v4
with: with:
token: ${{ steps.generate-token.outputs.token }} token: ${{ steps.generate-token.outputs.token }}

21
.github/workflows/ruff.yml vendored Normal file
View File

@@ -0,0 +1,21 @@
name: Ruff Lint
on:
push:
branches:
- master
pull_request:
paths:
- "nonebot/**"
- "packages/**"
- "tests/**"
jobs:
ruff:
name: Ruff Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run Ruff Lint
uses: chartboost/ruff-action@v1

View File

@@ -13,7 +13,9 @@ jobs:
cancel-in-progress: true cancel-in-progress: true
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Python Environment - name: Setup Python Environment
uses: ./.github/actions/setup-python uses: ./.github/actions/setup-python

View File

@@ -11,9 +11,10 @@ jobs:
cancel-in-progress: true cancel-in-progress: true
steps: steps:
- uses: actions/checkout@v3 - uses: actions/checkout@v4
with: with:
ref: ${{ github.event.pull_request.head.sha }} ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: Setup Python Environment - name: Setup Python Environment
uses: ./.github/actions/setup-python uses: ./.github/actions/setup-python

2
.gitignore vendored
View File

@@ -139,7 +139,7 @@ fabric.properties
.LSOverride .LSOverride
# Icon must end with two \r # Icon must end with two \r
Icon # Icon
# Thumbnails # Thumbnails
._* ._*

View File

@@ -6,11 +6,12 @@ ci:
autoupdate_schedule: monthly autoupdate_schedule: monthly
autoupdate_commit_msg: ":arrow_up: auto update by pre-commit hooks" autoupdate_commit_msg: ":arrow_up: auto update by pre-commit hooks"
repos: repos:
- repo: https://github.com/hadialqattan/pycln - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v2.1.3 rev: v0.0.292
hooks: hooks:
- id: pycln - id: ruff
args: [--config, pyproject.toml] args: [--fix, --exit-non-zero-on-fix]
stages: [commit]
- repo: https://github.com/pycqa/isort - repo: https://github.com/pycqa/isort
rev: 5.12.0 rev: 5.12.0
@@ -19,13 +20,13 @@ repos:
stages: [commit] stages: [commit]
- repo: https://github.com/psf/black - repo: https://github.com/psf/black
rev: 23.3.0 rev: 23.9.1
hooks: hooks:
- id: black - id: black
stages: [commit] stages: [commit]
- repo: https://github.com/pre-commit/mirrors-prettier - repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.0-alpha.9-for-vscode rev: v3.0.3
hooks: hooks:
- id: prettier - id: prettier
types_or: [javascript, jsx, ts, tsx, markdown, yaml, json] types_or: [javascript, jsx, ts, tsx, markdown, yaml, json]

31
.stylelintrc.js Normal file
View File

@@ -0,0 +1,31 @@
module.exports = {
extends: ["stylelint-config-standard", "stylelint-prettier/recommended"],
overrides: [
{
files: ["*.css"],
rules: {
"function-no-unknown": [true, { ignoreFunctions: ["theme"] }],
"selector-class-pattern": [
"^([a-z][a-z0-9]*)(-[a-z0-9]+)*$",
{
resolveNestedSelectors: true,
message: (selector) =>
`Expected class selector "${selector}" to be kebab-case`,
},
],
},
},
{
files: ["*.module.css"],
rules: {
"selector-class-pattern": [
"^[a-z][a-zA-Z0-9]+$",
{
message: (selector) =>
`Expected class selector "${selector}" to be lowerCamelCase`,
},
],
},
},
],
};

View File

@@ -10,12 +10,10 @@
### 报告问题、故障与漏洞 ### 报告问题、故障与漏洞
NoneBot2 仍然是一个不够稳定的开发中项目,如果你在使用过程中发现问题并确信是由 NoneBot2 引起的,欢迎提交 Issue。 如果你在使用过程中发现问题并确信是由 NoneBot2 引起的,欢迎提交 Issue。
### 建议功能 ### 建议功能
NoneBot2 还未进入正式版,欢迎在 Issue 中提议要加入哪些新功能。
为了让开发者更好地理解你的意图,请认真描述你所需要的特性,可能的话可以提出你认为可行的解决方案。 为了让开发者更好地理解你的意图,请认真描述你所需要的特性,可能的话可以提出你认为可行的解决方案。
## Pull Request ## Pull Request

View File

@@ -19,9 +19,19 @@ _✨ 跨平台 Python 异步机器人框架 ✨_
<img src="https://img.shields.io/github/license/nonebot/nonebot2" alt="license"> <img src="https://img.shields.io/github/license/nonebot/nonebot2" alt="license">
</a> </a>
<a href="https://pypi.python.org/pypi/nonebot2"> <a href="https://pypi.python.org/pypi/nonebot2">
<img src="https://img.shields.io/pypi/v/nonebot2" alt="pypi"> <img src="https://img.shields.io/pypi/v/nonebot2?logo=python&logoColor=edb641" alt="pypi">
</a> </a>
<img src="https://img.shields.io/badge/python-3.8+-blue" alt="python"> <img src="https://img.shields.io/badge/python-3.8+-blue?logo=python&logoColor=edb641" alt="python">
<a href="https://github.com/psf/black">
<img src="https://img.shields.io/badge/code%20style-black-000000.svg?logo=python&logoColor=edb641" alt="black">
</a>
<a href="https://github.com/Microsoft/pyright">
<img src="https://img.shields.io/badge/types-pyright-797952.svg?logo=python&logoColor=edb641" alt="pyright">
</a>
<a href="https://github.com/astral-sh/ruff">
<img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json" alt="ruff">
</a>
<br />
<a href="https://codecov.io/gh/nonebot/nonebot2"> <a href="https://codecov.io/gh/nonebot/nonebot2">
<img src="https://codecov.io/gh/nonebot/nonebot2/branch/master/graph/badge.svg?token=2P0G0VS7N4" alt="codecov"/> <img src="https://codecov.io/gh/nonebot/nonebot2/branch/master/graph/badge.svg?token=2P0G0VS7N4" alt="codecov"/>
</a> </a>
@@ -31,6 +41,12 @@ _✨ 跨平台 Python 异步机器人框架 ✨_
<a href="https://results.pre-commit.ci/latest/github/nonebot/nonebot2/master"> <a href="https://results.pre-commit.ci/latest/github/nonebot/nonebot2/master">
<img src="https://results.pre-commit.ci/badge/github/nonebot/nonebot2/master.svg" alt="pre-commit" /> <img src="https://results.pre-commit.ci/badge/github/nonebot/nonebot2/master.svg" alt="pre-commit" />
</a> </a>
<a href="https://github.com/nonebot/nonebot2/actions/workflows/pyright.yml">
<img src="https://github.com/nonebot/nonebot2/actions/workflows/pyright.yml/badge.svg?branch=master&event=push" alt="pyright">
</a>
<a href="https://github.com/nonebot/nonebot2/actions/workflows/ruff.yml">
<img src="https://github.com/nonebot/nonebot2/actions/workflows/ruff.yml/badge.svg?branch=master&event=push" alt="ruff">
</a>
<br /> <br />
<a href="https://onebot.dev/"> <a href="https://onebot.dev/">
<img src="https://img.shields.io/badge/OneBot-v11-black?style=social&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABABAMAAABYR2ztAAAAIVBMVEUAAAAAAAADAwMHBwceHh4UFBQNDQ0ZGRkoKCgvLy8iIiLWSdWYAAAAAXRSTlMAQObYZgAAAQVJREFUSMftlM0RgjAQhV+0ATYK6i1Xb+iMd0qgBEqgBEuwBOxU2QDKsjvojQPvkJ/ZL5sXkgWrFirK4MibYUdE3OR2nEpuKz1/q8CdNxNQgthZCXYVLjyoDQftaKuniHHWRnPh2GCUetR2/9HsMAXyUT4/3UHwtQT2AggSCGKeSAsFnxBIOuAggdh3AKTL7pDuCyABcMb0aQP7aM4AnAbc/wHwA5D2wDHTTe56gIIOUA/4YYV2e1sg713PXdZJAuncdZMAGkAukU9OAn40O849+0ornPwT93rphWF0mgAbauUrEOthlX8Zu7P5A6kZyKCJy75hhw1Mgr9RAUvX7A3csGqZegEdniCx30c3agAAAABJRU5ErkJggg==" alt="onebot"> <img src="https://img.shields.io/badge/OneBot-v11-black?style=social&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABABAMAAABYR2ztAAAAIVBMVEUAAAAAAAADAwMHBwceHh4UFBQNDQ0ZGRkoKCgvLy8iIiLWSdWYAAAAAXRSTlMAQObYZgAAAQVJREFUSMftlM0RgjAQhV+0ATYK6i1Xb+iMd0qgBEqgBEuwBOxU2QDKsjvojQPvkJ/ZL5sXkgWrFirK4MibYUdE3OR2nEpuKz1/q8CdNxNQgthZCXYVLjyoDQftaKuniHHWRnPh2GCUetR2/9HsMAXyUT4/3UHwtQT2AggSCGKeSAsFnxBIOuAggdh3AKTL7pDuCyABcMb0aQP7aM4AnAbc/wHwA5D2wDHTTe56gIIOUA/4YYV2e1sg713PXdZJAuncdZMAGkAukU9OAn40O849+0ornPwT93rphWF0mgAbauUrEOthlX8Zu7P5A6kZyKCJy75hhw1Mgr9RAUvX7A3csGqZegEdniCx30c3agAAAABJRU5ErkJggg==" alt="onebot">
@@ -38,6 +54,9 @@ _✨ 跨平台 Python 异步机器人框架 ✨_
<a href="https://onebot.dev/"> <a href="https://onebot.dev/">
<img src="https://img.shields.io/badge/OneBot-v12-black?style=social&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABABAMAAABYR2ztAAAAIVBMVEUAAAAAAAADAwMHBwceHh4UFBQNDQ0ZGRkoKCgvLy8iIiLWSdWYAAAAAXRSTlMAQObYZgAAAQVJREFUSMftlM0RgjAQhV+0ATYK6i1Xb+iMd0qgBEqgBEuwBOxU2QDKsjvojQPvkJ/ZL5sXkgWrFirK4MibYUdE3OR2nEpuKz1/q8CdNxNQgthZCXYVLjyoDQftaKuniHHWRnPh2GCUetR2/9HsMAXyUT4/3UHwtQT2AggSCGKeSAsFnxBIOuAggdh3AKTL7pDuCyABcMb0aQP7aM4AnAbc/wHwA5D2wDHTTe56gIIOUA/4YYV2e1sg713PXdZJAuncdZMAGkAukU9OAn40O849+0ornPwT93rphWF0mgAbauUrEOthlX8Zu7P5A6kZyKCJy75hhw1Mgr9RAUvX7A3csGqZegEdniCx30c3agAAAABJRU5ErkJggg==" alt="onebot"> <img src="https://img.shields.io/badge/OneBot-v12-black?style=social&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABABAMAAABYR2ztAAAAIVBMVEUAAAAAAAADAwMHBwceHh4UFBQNDQ0ZGRkoKCgvLy8iIiLWSdWYAAAAAXRSTlMAQObYZgAAAQVJREFUSMftlM0RgjAQhV+0ATYK6i1Xb+iMd0qgBEqgBEuwBOxU2QDKsjvojQPvkJ/ZL5sXkgWrFirK4MibYUdE3OR2nEpuKz1/q8CdNxNQgthZCXYVLjyoDQftaKuniHHWRnPh2GCUetR2/9HsMAXyUT4/3UHwtQT2AggSCGKeSAsFnxBIOuAggdh3AKTL7pDuCyABcMb0aQP7aM4AnAbc/wHwA5D2wDHTTe56gIIOUA/4YYV2e1sg713PXdZJAuncdZMAGkAukU9OAn40O849+0ornPwT93rphWF0mgAbauUrEOthlX8Zu7P5A6kZyKCJy75hhw1Mgr9RAUvX7A3csGqZegEdniCx30c3agAAAABJRU5ErkJggg==" alt="onebot">
</a> </a>
<a href="https://bot.q.qq.com/wiki/">
<img src="https://img.shields.io/badge/QQ-Bot-lightgrey?style=social&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMTIuODIgMTMwLjg5Ij48ZyBkYXRhLW5hbWU9IuWbvuWxgiAyIj48ZyBkYXRhLW5hbWU9IuWbvuWxgiAxIj48cGF0aCBkPSJNNTUuNjMgMTMwLjhjLTcgMC0xMy45LjA4LTIwLjg2IDAtMTkuMTUtLjI1LTMxLjcxLTExLjQtMzQuMjItMzAuMy00LjA3LTMwLjY2IDE0LjkzLTU5LjIgNDQuODMtNjYuNjQgMi0uNTEgNS4yMS0uMzEgNS4yMS0xLjYzIDAtMi4xMy4xNC0yLjEzLjE0LTUuNTcgMC0uODktMS4zLTEuNDYtMi4yMi0yLjMxLTYuNzMtNi4yMy03LjY3LTEzLjQxLTEtMjAuMTggNS40LTUuNTIgMTEuODctNS40IDE3LjgtLjU5IDYuNDkgNS4yNiA2LjMxIDEzLjA4LS44NiAyMS0uNjguNzQtMS43OCAxLjYtMS43OCAyLjY3djQuMjFjMCAxLjM1IDIuMiAxLjYyIDQuNzkgMi4zNSAzMS4wOSA4LjY1IDQ4LjE3IDM0LjEzIDQ1IDY2LjM3LTEuNzYgMTguMTUtMTQuNTYgMzAuMjMtMzIuNyAzMC42My04LjAyLjE5LTE2LjA3LS4wMS0yNC4xMy0uMDF6IiBmaWxsPSIjMDI5OWZlIi8+PHBhdGggZD0iTTMxLjQ2IDExOC4zOGMtMTAuNS0uNjktMTYuOC02Ljg2LTE4LjM4LTE3LjI3LTMtMTkuNDIgMi43OC0zNS44NiAxOC40Ni00Ny44MyAxNC4xNi0xMC44IDI5Ljg3LTEyIDQ1LjM4LTMuMTkgMTcuMjUgOS44NCAyNC41OSAyNS44MSAyNCA0NS4yOS0uNDkgMTUuOS04LjQyIDIzLjE0LTI0LjM4IDIzLjUtNi41OS4xNC0xMy4xOSAwLTE5Ljc5IDAiIGZpbGw9IiNmZWZlZmUiLz48cGF0aCBkPSJNNDYuMDUgNzkuNThjLjA5IDUgLjIzIDkuODItNyA5Ljc3LTcuODItLjA2LTYuMS01LjY5LTYuMjQtMTAuMTktLjE1LTQuODItLjczLTEwIDYuNzMtOS44NHM2LjM3IDUuNTUgNi41MSAxMC4yNnoiIGZpbGw9IiMxMDlmZmUiLz48cGF0aCBkPSJNODAuMjcgNzkuMjdjLS41MyAzLjkxIDEuNzUgOS42NC01Ljg4IDEwLTcuNDcuMzctNi44MS00LjgyLTYuNjEtOS41LjItNC4zMi0xLjgzLTEwIDUuNzgtMTAuNDJzNi41OSA0Ljg5IDYuNzEgOS45MnoiIGZpbGw9IiMwODljZmUiLz48L2c+PC9nPjwvc3ZnPg==" alt="QQ">
</a>
<a href="https://core.telegram.org/bots/api"> <a href="https://core.telegram.org/bots/api">
<img src="https://img.shields.io/badge/telegram-Bot-lightgrey?style=social&logo=telegram" alt="telegram"> <img src="https://img.shields.io/badge/telegram-Bot-lightgrey?style=social&logo=telegram" alt="telegram">
</a> </a>
@@ -47,9 +66,6 @@ _✨ 跨平台 Python 异步机器人框架 ✨_
<a href="https://docs.github.com/en/developers/apps"> <a href="https://docs.github.com/en/developers/apps">
<img src="https://img.shields.io/badge/GitHub-Bot-181717?style=social&logo=github" alt="github"/> <img src="https://img.shields.io/badge/GitHub-Bot-181717?style=social&logo=github" alt="github"/>
</a> </a>
<a href="https://bot.q.qq.com/wiki/">
<img src="https://img.shields.io/badge/QQ%E9%A2%91%E9%81%93-Bot-lightgrey?style=social&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMTIuODIgMTMwLjg5Ij48ZyBkYXRhLW5hbWU9IuWbvuWxgiAyIj48ZyBkYXRhLW5hbWU9IuWbvuWxgiAxIj48cGF0aCBkPSJNNTUuNjMgMTMwLjhjLTcgMC0xMy45LjA4LTIwLjg2IDAtMTkuMTUtLjI1LTMxLjcxLTExLjQtMzQuMjItMzAuMy00LjA3LTMwLjY2IDE0LjkzLTU5LjIgNDQuODMtNjYuNjQgMi0uNTEgNS4yMS0uMzEgNS4yMS0xLjYzIDAtMi4xMy4xNC0yLjEzLjE0LTUuNTcgMC0uODktMS4zLTEuNDYtMi4yMi0yLjMxLTYuNzMtNi4yMy03LjY3LTEzLjQxLTEtMjAuMTggNS40LTUuNTIgMTEuODctNS40IDE3LjgtLjU5IDYuNDkgNS4yNiA2LjMxIDEzLjA4LS44NiAyMS0uNjguNzQtMS43OCAxLjYtMS43OCAyLjY3djQuMjFjMCAxLjM1IDIuMiAxLjYyIDQuNzkgMi4zNSAzMS4wOSA4LjY1IDQ4LjE3IDM0LjEzIDQ1IDY2LjM3LTEuNzYgMTguMTUtMTQuNTYgMzAuMjMtMzIuNyAzMC42My04LjAyLjE5LTE2LjA3LS4wMS0yNC4xMy0uMDF6IiBmaWxsPSIjMDI5OWZlIi8+PHBhdGggZD0iTTMxLjQ2IDExOC4zOGMtMTAuNS0uNjktMTYuOC02Ljg2LTE4LjM4LTE3LjI3LTMtMTkuNDIgMi43OC0zNS44NiAxOC40Ni00Ny44MyAxNC4xNi0xMC44IDI5Ljg3LTEyIDQ1LjM4LTMuMTkgMTcuMjUgOS44NCAyNC41OSAyNS44MSAyNCA0NS4yOS0uNDkgMTUuOS04LjQyIDIzLjE0LTI0LjM4IDIzLjUtNi41OS4xNC0xMy4xOSAwLTE5Ljc5IDAiIGZpbGw9IiNmZWZlZmUiLz48cGF0aCBkPSJNNDYuMDUgNzkuNThjLjA5IDUgLjIzIDkuODItNyA5Ljc3LTcuODItLjA2LTYuMS01LjY5LTYuMjQtMTAuMTktLjE1LTQuODItLjczLTEwIDYuNzMtOS44NHM2LjM3IDUuNTUgNi41MSAxMC4yNnoiIGZpbGw9IiMxMDlmZmUiLz48cGF0aCBkPSJNODAuMjcgNzkuMjdjLS41MyAzLjkxIDEuNzUgOS42NC01Ljg4IDEwLTcuNDcuMzctNi44MS00LjgyLTYuNjEtOS41LjItNC4zMi0xLjgzLTEwIDUuNzgtMTAuNDJzNi41OSA0Ljg5IDYuNzEgOS45MnoiIGZpbGw9IiMwODljZmUiLz48L2c+PC9nPjwvc3ZnPg==" alt="QQ频道">
</a>
<!-- <a href="https://ding-doc.dingtalk.com/document#/org-dev-guide/elzz1p"> <!-- <a href="https://ding-doc.dingtalk.com/document#/org-dev-guide/elzz1p">
<img src="https://img.shields.io/badge/%E9%92%89%E9%92%89-Bot-lightgrey?style=social&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAnFBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4jUzeAAAAM3RSTlMAQKSRaA+/f0YyFevh29R3cyklIfrlyrGsn41tVUs48c/HqJm9uZdhX1otGwkF9IN8V1CX0Q+IAAABY0lEQVRYw+3V2W7CMBAF0JuNQAhhX9OEfYdu9///rUVWpagE27Ef2gfO+0zGozsKnv6bMGzAhkNytIe5gDdzrwtTCwrbI8x4/NF668NAxgI3Q3UtFi3TyPwNQtPLUUmDd8YfqGLNe4v22XwEYb5zoOuF5baHq2UHtsKe5ivWfGAwrWu2mC34QM0PoCAuqZdOmiwV+5BLyMRtZ7dTSEcs48rzWfzwptMLyzpApka1SJ5FtR4kfCqNIBPEVDmqoqgwUYY5plQOlf6UEjNoOPnuKB6wzDyCrks///TDza8+PnR109WQdxLo8RKWq0PPnuXG0OXKQ6wWLFnCg75uYYbhmMIVVdQ709q33aHbGIj6Duz+2k1HQFX9VwqmY8xYsEJll2ahvhWgsjYLHFRXvIi2Qb0jzMQCzC3FAoydxCma88UCzE3JCWwkjCNYyMUCzHX4DiuTMawEwwhW6hnshPhjZzzJfAH0YacpbmRd7QAAAABJRU5ErkJggg==" alt="dingtalk"> --> <img src="https://img.shields.io/badge/%E9%92%89%E9%92%89-Bot-lightgrey?style=social&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAMAAACdt4HsAAAAnFBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD4jUzeAAAAM3RSTlMAQKSRaA+/f0YyFevh29R3cyklIfrlyrGsn41tVUs48c/HqJm9uZdhX1otGwkF9IN8V1CX0Q+IAAABY0lEQVRYw+3V2W7CMBAF0JuNQAhhX9OEfYdu9///rUVWpagE27Ef2gfO+0zGozsKnv6bMGzAhkNytIe5gDdzrwtTCwrbI8x4/NF668NAxgI3Q3UtFi3TyPwNQtPLUUmDd8YfqGLNe4v22XwEYb5zoOuF5baHq2UHtsKe5ivWfGAwrWu2mC34QM0PoCAuqZdOmiwV+5BLyMRtZ7dTSEcs48rzWfzwptMLyzpApka1SJ5FtR4kfCqNIBPEVDmqoqgwUYY5plQOlf6UEjNoOPnuKB6wzDyCrks///TDza8+PnR109WQdxLo8RKWq0PPnuXG0OXKQ6wWLFnCg75uYYbhmMIVVdQ709q33aHbGIj6Duz+2k1HQFX9VwqmY8xYsEJll2ahvhWgsjYLHFRXvIi2Qb0jzMQCzC3FAoydxCma88UCzE3JCWwkjCNYyMUCzHX4DiuTMawEwwhW6hnshPhjZzzJfAH0YacpbmRd7QAAAABJRU5ErkJggg==" alt="dingtalk"> -->
</a> </a>
@@ -94,21 +110,25 @@ NoneBot2 是一个现代、跨平台、可扩展的 Python 聊天机器人框架
- 社区丰富:社区用户众多,直接和间接用户超过十万人,每天都有大量的活跃用户 ([社区资源](#社区资源)) - 社区丰富:社区用户众多,直接和间接用户超过十万人,每天都有大量的活跃用户 ([社区资源](#社区资源))
- 海纳百川:一个框架,支持多个聊天软件平台,可自定义通信协议 - 海纳百川:一个框架,支持多个聊天软件平台,可自定义通信协议
| 协议名称 | 状态 | 注释 | | 协议名称 | 状态 | 注释 |
| :-------------------------------------------------------------------------------------------------------------------: | :--: | :-----------------------------------------------------------------------: | | :--------------------------------------------------------------------------------------------------------------------------: | :--: | :-----------------------------------------------------------------------: |
| OneBot[仓库](https://github.com/nonebot/adapter-onebot)[协议](https://onebot.dev/) | ✅ | 支持 QQ、TG、微信公众号、KOOK 等[平台](https://onebot.dev/ecosystem.html) | | OneBot[仓库](https://github.com/nonebot/adapter-onebot)[协议](https://onebot.dev/) | ✅ | 支持 QQ、TG、微信公众号、KOOK 等[平台](https://onebot.dev/ecosystem.html) |
| Telegram[仓库](https://github.com/nonebot/adapter-telegram)[协议](https://core.telegram.org/bots/api) | ✅ | | | Telegram[仓库](https://github.com/nonebot/adapter-telegram)[协议](https://core.telegram.org/bots/api) | ✅ | |
| 飞书([仓库](https://github.com/nonebot/adapter-feishu)[协议](https://open.feishu.cn/document/home/index) | ✅ | | | 飞书([仓库](https://github.com/nonebot/adapter-feishu)[协议](https://open.feishu.cn/document/home/index) | ✅ | |
| GitHub[仓库](https://github.com/nonebot/adapter-github)[协议](https://docs.github.com/en/apps) | ✅ | GitHub APP & OAuth APP | | GitHub[仓库](https://github.com/nonebot/adapter-github)[协议](https://docs.github.com/en/apps) | ✅ | GitHub APP & OAuth APP |
| QQ 频道[仓库](https://github.com/nonebot/adapter-qqguild)[协议](https://bot.q.qq.com/wiki/) | ✅ | 官方接口调整较多 | | QQ[仓库](https://github.com/nonebot/adapter-qq)[协议](https://bot.q.qq.com/wiki/) | ✅ | QQ 官方接口调整较多 |
| 钉钉([仓库](https://github.com/nonebot/adapter-ding)[协议](https://open.dingtalk.com/document/) | 🤗 | 寻找 Maintainer暂不可用 | | 钉钉([仓库](https://github.com/nonebot/adapter-ding)[协议](https://open.dingtalk.com/document/) | 🤗 | 寻找 Maintainer暂不可用 |
| Console[仓库](https://github.com/nonebot/adapter-console) | ✅ | 控制台交互 | | Console[仓库](https://github.com/nonebot/adapter-console) | ✅ | 控制台交互 |
| 开黑啦[仓库](https://github.com/Tian-que/nonebot-adapter-kaiheila)[协议](https://developer.kookapp.cn/) | ↗️ | 由社区贡献 | | Red [仓库](https://github.com/nonebot/adapter-red)[协议](https://chrononeko.github.io/QQNTRedProtocol/) | | QQ 协议 |
| Mirai[仓库](https://github.com/ieew/nonebot_adapter_mirai2)[协议](https://docs.mirai.mamoe.net/mirai-api-http/) | ↗️ | QQ 协议,由社区贡献 | | Satori[仓库](https://github.com/nonebot/adapter-satori)[协议](https://satori.js.org/zh-CN) | ✅ | 支持 Onebot、TG、飞书、微信公众号、Koishi 等 |
| Ntchat[仓库](https://github.com/JustUndertaker/adapter-ntchat) | ↗️ | 微信协议,由社区贡献 | | Discord [仓库](https://github.com/nonebot/adapter-discord)[协议](https://discord.com/developers/docs/intro) | | Discord Bot 协议 |
| MineCraft[仓库](https://github.com/17TheWord/nonebot-adapter-minecraft) | ↗️ | 由社区贡献 | | 开黑啦[仓库](https://github.com/Tian-que/nonebot-adapter-kaiheila)[协议](https://developer.kookapp.cn/) | ↗️ | 由社区贡献 |
| BiliBili Live[仓库](https://github.com/wwweww/adapter-bilibili) | ↗️ | 由社区贡献 | | Mirai[仓库](https://github.com/ieew/nonebot_adapter_mirai2)[协议](https://docs.mirai.mamoe.net/mirai-api-http/) | ↗️ | QQ 协议,由社区贡献 |
| Walle-Q[仓库](https://github.com/onebot-walle/nonebot_adapter_walleq) | ↗️ | QQ 协议,由社区贡献 | | Ntchat[仓库](https://github.com/JustUndertaker/adapter-ntchat) | ↗️ | 微信协议,由社区贡献 |
| MineCraft[仓库](https://github.com/17TheWord/nonebot-adapter-minecraft) | ↗️ | 由社区贡献 |
| BiliBili Live[仓库](https://github.com/wwweww/adapter-bilibili) | ↗️ | 由社区贡献 |
| Walle-Q[仓库](https://github.com/onebot-walle/nonebot_adapter_walleq) | ↗️ | QQ 协议,由社区贡献 |
| Villa[仓库](https://github.com/CMHopeSunshine/nonebot-adapter-villa)[协议](https://webstatic.mihoyo.com/vila/bot/doc/) | ↗️ | 米游社大别野 Bot 协议,由社区贡献 |
- 坚实后盾:支持多种 web 框架,可自定义替换、组合 - 坚实后盾:支持多种 web 框架,可自定义替换、组合
@@ -187,9 +207,8 @@ NoneBot2 不是 NoneBot1 的替代品。事实上,它们都在被积极的维
或者尝试以下镜像: 或者尝试以下镜像:
- [文档镜像(中国境内)](https://nb2.baka.icu) - [文档镜像(中国境内)](https://nb2.baka.icu)
- [文档镜像(Vercel)](https://nonebot2-vercel-mirror.vercel.app)
- 其他插件请查看 [商店](https://nonebot.dev/store) - 其他插件请查看 [商店](https://nonebot.dev/store/plugins)
## 许可证 ## 许可证
@@ -208,7 +227,17 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
请参考 [贡献指南](./CONTRIBUTING.md) 请参考 [贡献指南](./CONTRIBUTING.md)
### 鸣谢 ## 鸣谢
### 赞助者
感谢以下赞助者对 NoneBot 项目提供的资金支持:
<a href="https://assets.nonebot.dev/sponsors.svg">
<img src='https://assets.nonebot.dev/sponsors.svg'/>
</a>
### 开发者
感谢以下开发者对 NoneBot2 作出的贡献: 感谢以下开发者对 NoneBot2 作出的贡献:

View File

@@ -40,12 +40,12 @@
"is_official": true "is_official": true
}, },
{ {
"module_name": "nonebot.adapters.qqguild", "module_name": "nonebot.adapters.qq",
"project_link": "nonebot-adapter-qqguild", "project_link": "nonebot-adapter-qq",
"name": "QQ 频道", "name": "QQ",
"desc": "QQ 频道官方机器人", "desc": "QQ 官方机器人",
"author": "yanyongyu", "author": "yanyongyu",
"homepage": "https://github.com/nonebot/adapter-qqguild", "homepage": "https://github.com/nonebot/adapter-qq",
"tags": [], "tags": [],
"is_official": true "is_official": true
}, },
@@ -153,5 +153,55 @@
} }
], ],
"is_official": false "is_official": false
},
{
"module_name": "nonebot.adapters.villa",
"project_link": "nonebot-adapter-villa",
"name": "大别野",
"desc": "米游社大别野官方Bot适配",
"author": "CMHopeSunshine",
"homepage": "https://github.com/CMHopeSunshine/nonebot-adapter-villa",
"tags": [
{
"label": "米哈游",
"color": "#e10909"
}
],
"is_official": false
},
{
"module_name": "nonebot.adapters.red",
"project_link": "nonebot-adapter-red",
"name": "RedProtocol",
"desc": "QQNT RedProtocol 适配",
"author": "zhaomaoniu",
"homepage": "https://github.com/nonebot/adapter-red",
"tags": [],
"is_official": true
},
{
"module_name": "nonebot.adapters.discord",
"project_link": "nonebot-adapter-discord",
"name": "Discord",
"desc": "Discord 官方 Bot 协议适配",
"author": "CMHopeSunshine",
"homepage": "https://github.com/nonebot/adapter-discord",
"tags": [],
"is_official": true
},
{
"module_name": "nonebot.adapters.satori",
"project_link": "nonebot-adapter-satori",
"name": "Satori",
"desc": "Satori 协议适配器",
"author": "RF-Tar-Railt",
"homepage": "https://github.com/nonebot/adapter-satori",
"tags": [
{
"label": "跨平台",
"color": "#bf40bf"
}
],
"is_official": true
} }
] ]

View File

@@ -71,14 +71,6 @@
"tags": [], "tags": [],
"is_official": false "is_official": false
}, },
{
"name": "Takker",
"desc": "综合了各种娱乐功能的Bot",
"author": "FYWinds",
"homepage": "https://github.com/FYWinds/takker",
"tags": [],
"is_official": false
},
{ {
"name": "剑网三bot", "name": "剑网三bot",
"desc": "网络游戏《剑侠情缘三》的群聊机器人数据使用www.jx3api.com", "desc": "网络游戏《剑侠情缘三》的群聊机器人数据使用www.jx3api.com",
@@ -163,10 +155,6 @@
{ {
"label": "AI", "label": "AI",
"color": "#ea5252" "color": "#ea5252"
},
{
"label": "a:onebot",
"color": "#000000"
} }
], ],
"is_official": false "is_official": false
@@ -261,12 +249,7 @@
"desc": "🐱🤖 一个以娱乐功能为主的缝合怪划掉QQ机器人包含一定Furry要素但是不会卖萌就是逊啦", "desc": "🐱🤖 一个以娱乐功能为主的缝合怪划掉QQ机器人包含一定Furry要素但是不会卖萌就是逊啦",
"author": "su226", "author": "su226",
"homepage": "https://github.com/su226/IdhagnBot", "homepage": "https://github.com/su226/IdhagnBot",
"tags": [ "tags": [],
{
"label": "a:OneBot",
"color": "#ea5252"
}
],
"is_official": false "is_official": false
}, },
{ {
@@ -339,15 +322,15 @@
"homepage": "https://github.com/Rinfair-CSP-A016/SuzunoBot-AGLAS", "homepage": "https://github.com/Rinfair-CSP-A016/SuzunoBot-AGLAS",
"tags": [ "tags": [
{ {
"label": "t:maimaiDX", "label": "maimaiDX",
"color": "#189ede" "color": "#189ede"
}, },
{ {
"label": "t:Arcaea", "label": "Arcaea",
"color": "#d551ef" "color": "#d551ef"
}, },
{ {
"label": "t:coc", "label": "coc",
"color": "#7fe4d0" "color": "#7fe4d0"
} }
], ],
@@ -434,10 +417,6 @@
{ {
"label": "maimai", "label": "maimai",
"color": "#52eaa5" "color": "#52eaa5"
},
{
"label": "a:onebot",
"color": "#000000"
} }
], ],
"is_official": false "is_official": false
@@ -447,12 +426,7 @@
"desc": "一个会拆家的高性能缝合萝卜子", "desc": "一个会拆家的高性能缝合萝卜子",
"author": "tkgs0", "author": "tkgs0",
"homepage": "https://github.com/tkgs0/Momoko", "homepage": "https://github.com/tkgs0/Momoko",
"tags": [ "tags": [],
{
"label": "a:onebot",
"color": "#000000"
}
],
"is_official": false "is_official": false
}, },
{ {
@@ -468,12 +442,7 @@
"desc": "简单的QQ功能型机器人", "desc": "简单的QQ功能型机器人",
"author": "This-is-XiaoDeng", "author": "This-is-XiaoDeng",
"homepage": "https://github.com/ITCraftDevelopmentTeam/XDbot2", "homepage": "https://github.com/ITCraftDevelopmentTeam/XDbot2",
"tags": [ "tags": [],
{
"label": "a:onebot",
"color": "#ea5252"
}
],
"is_official": false "is_official": false
}, },
{ {
@@ -521,14 +490,105 @@
"homepage": "https://github.com/bingqiu456/shouyun", "homepage": "https://github.com/bingqiu456/shouyun",
"tags": [ "tags": [
{ {
"label": "a:onebot", "label": "shouyun",
"color": "#ea5252"
},
{
"label": "t:shouyun",
"color": "#52ea7a" "color": "#52ea7a"
} }
], ],
"is_official": false "is_official": false
},
{
"name": "ReimeiBot-黎明机器人",
"desc": "流星飞逝,黎明终将到来。",
"author": "ThirdBlood",
"homepage": "https://github.com/3rdBit/ReimeiBot",
"tags": [],
"is_official": false
},
{
"name": "web_bot",
"desc": "把机器人搬到网络上",
"author": "wsdtl",
"homepage": "https://github.com/wsdtl/web_bot",
"tags": [
{
"label": "xiaonan",
"color": "#775151"
}
],
"is_official": false
},
{
"name": "林汐",
"desc": "多平台功能型Bot",
"author": "mute23-code",
"homepage": "https://github.com/netsora/SoraBot",
"tags": [
{
"label": "QQ频道",
"color": "#f47070"
},
{
"label": "OneBot v11",
"color": "#212121"
}
],
"is_official": false
},
{
"name": "米缸",
"desc": "基于nonebot2的米缸Bot",
"author": "LambdaYH",
"homepage": "https://github.com/LambdaYH/MigangBot",
"tags": [],
"is_official": false
},
{
"name": "不正经的妹妹",
"desc": "一款功能丰富、简单易用、自定义性强、扩展性强的可爱的QQ娱乐机器人",
"author": "itsevin",
"homepage": "https://github.com/itsevin/sister_bot",
"tags": [],
"is_official": false
},
{
"name": "星见Kirami",
"desc": "🌟 读作 Kirami写作星见简明轻快的聊天机器人应用。",
"author": "A-kirami",
"homepage": "https://kiramibot.dev/",
"tags": [],
"is_official": false
},
{
"name": "OCNbot",
"desc": "OI Contest Notifier bot一个可以推送洛谷、cf、atcoder、牛客比赛通知的bot",
"author": "ACnoway",
"homepage": "https://github.com/ACnoway/OCNbot",
"tags": [
{
"label": "OI",
"color": "#2fccff"
},
{
"label": "ACM",
"color": "#ff0004"
}
],
"is_official": false
},
{
"name": "妃爱",
"desc": "超可爱的妃爱QQ群聊机器人",
"author": "jiangyuxiaoxiao",
"homepage": "https://github.com/jiangyuxiaoxiao/Hiyori",
"tags": [],
"is_official": false
},
{
"name": "芙芙",
"desc": "供 Mooncell Wiki 协作使用的跨平台机器人",
"author": "StarHeartHunt",
"homepage": "https://github.com/MooncellWiki/BotFooChan",
"tags": [],
"is_official": false
} }
] ]

File diff suppressed because it is too large Load Diff

View File

@@ -24,12 +24,17 @@
- `load_all_plugins` => {ref}``load_all_plugins` <nonebot.plugin.load.load_all_plugins>` - `load_all_plugins` => {ref}``load_all_plugins` <nonebot.plugin.load.load_all_plugins>`
- `load_from_json` => {ref}``load_from_json` <nonebot.plugin.load.load_from_json>` - `load_from_json` => {ref}``load_from_json` <nonebot.plugin.load.load_from_json>`
- `load_from_toml` => {ref}``load_from_toml` <nonebot.plugin.load.load_from_toml>` - `load_from_toml` => {ref}``load_from_toml` <nonebot.plugin.load.load_from_toml>`
- `load_builtin_plugin` => {ref}``load_builtin_plugin` <nonebot.plugin.load.load_builtin_plugin>` - `load_builtin_plugin` =>
- `load_builtin_plugins` => {ref}``load_builtin_plugins` <nonebot.plugin.load.load_builtin_plugins>` {ref}``load_builtin_plugin` <nonebot.plugin.load.load_builtin_plugin>`
- `load_builtin_plugins` =>
{ref}``load_builtin_plugins` <nonebot.plugin.load.load_builtin_plugins>`
- `get_plugin` => {ref}``get_plugin` <nonebot.plugin.get_plugin>` - `get_plugin` => {ref}``get_plugin` <nonebot.plugin.get_plugin>`
- `get_plugin_by_module_name` => {ref}``get_plugin_by_module_name` <nonebot.plugin.get_plugin_by_module_name>` - `get_plugin_by_module_name` =>
- `get_loaded_plugins` => {ref}``get_loaded_plugins` <nonebot.plugin.get_loaded_plugins>` {ref}``get_plugin_by_module_name` <nonebot.plugin.get_plugin_by_module_name>`
- `get_available_plugin_names` => {ref}``get_available_plugin_names` <nonebot.plugin.get_available_plugin_names>` - `get_loaded_plugins` =>
{ref}``get_loaded_plugins` <nonebot.plugin.get_loaded_plugins>`
- `get_available_plugin_names` =>
{ref}``get_available_plugin_names` <nonebot.plugin.get_available_plugin_names>`
- `require` => {ref}``require` <nonebot.plugin.load.require>` - `require` => {ref}``require` <nonebot.plugin.load.require>`
FrontMatter: FrontMatter:
@@ -48,7 +53,7 @@ from nonebot.config import Env, Config
from nonebot.log import logger as logger from nonebot.log import logger as logger
from nonebot.adapters import Bot, Adapter from nonebot.adapters import Bot, Adapter
from nonebot.utils import escape_tag, resolve_dot_notation from nonebot.utils import escape_tag, resolve_dot_notation
from nonebot.drivers import Driver, ReverseDriver, combine_driver from nonebot.drivers import Driver, ASGIMixin, combine_driver
try: try:
__version__ = version("nonebot2") __version__ = version("nonebot2")
@@ -69,7 +74,8 @@ def get_driver() -> Driver:
全局 {ref}`nonebot.drivers.Driver` 对象 全局 {ref}`nonebot.drivers.Driver` 对象
异常: 异常:
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化 ({ref}`nonebot.init <nonebot.init>` 尚未调用) ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
({ref}`nonebot.init <nonebot.init>` 尚未调用)
用法: 用法:
```python ```python
@@ -83,23 +89,33 @@ def get_driver() -> Driver:
@overload @overload
def get_adapter(name: str) -> Adapter: def get_adapter(name: str) -> Adapter:
... """
参数:
name: 适配器名称
返回:
指定名称的 {ref}`nonebot.adapters.Adapter` 对象
"""
@overload @overload
def get_adapter(name: Type[A]) -> A: def get_adapter(name: Type[A]) -> A:
... """
参数:
name: 适配器类型
返回:
指定类型的 {ref}`nonebot.adapters.Adapter` 对象
"""
def get_adapter(name: Union[str, Type[Adapter]]) -> Adapter: def get_adapter(name: Union[str, Type[Adapter]]) -> Adapter:
"""获取已注册的 {ref}`nonebot.adapters.Adapter` 实例。 """获取已注册的 {ref}`nonebot.adapters.Adapter` 实例。
返回:
指定名称或类型的 {ref}`nonebot.adapters.Adapter` 对象
异常: 异常:
ValueError: 指定的 {ref}`nonebot.adapters.Adapter` 未注册 ValueError: 指定的 {ref}`nonebot.adapters.Adapter` 未注册
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化 ({ref}`nonebot.init <nonebot.init>` 尚未调用) ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
({ref}`nonebot.init <nonebot.init>` 尚未调用)
用法: 用法:
```python ```python
@@ -121,7 +137,8 @@ def get_adapters() -> Dict[str, Adapter]:
所有 {ref}`nonebot.adapters.Adapter` 实例字典 所有 {ref}`nonebot.adapters.Adapter` 实例字典
异常: 异常:
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化 ({ref}`nonebot.init <nonebot.init>` 尚未调用) ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
({ref}`nonebot.init <nonebot.init>` 尚未调用)
用法: 用法:
```python ```python
@@ -132,14 +149,15 @@ def get_adapters() -> Dict[str, Adapter]:
def get_app() -> Any: def get_app() -> Any:
"""获取全局 {ref}`nonebot.drivers.ReverseDriver` 对应的 Server App 对象。 """获取全局 {ref}`nonebot.drivers.ASGIMixin` 对应的 Server App 对象。
返回: 返回:
Server App 对象 Server App 对象
异常: 异常:
AssertionError: 全局 Driver 对象不是 {ref}`nonebot.drivers.ReverseDriver` 类型 AssertionError: 全局 Driver 对象不是 {ref}`nonebot.drivers.ASGIMixin` 类型
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化 ({ref}`nonebot.init <nonebot.init>` 尚未调用) ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
({ref}`nonebot.init <nonebot.init>` 尚未调用)
用法: 用法:
```python ```python
@@ -147,21 +165,21 @@ def get_app() -> Any:
``` ```
""" """
driver = get_driver() driver = get_driver()
assert isinstance( assert isinstance(driver, ASGIMixin), "app object is only available for asgi driver"
driver, ReverseDriver
), "app object is only available for reverse driver"
return driver.server_app return driver.server_app
def get_asgi() -> Any: def get_asgi() -> Any:
"""获取全局 {ref}`nonebot.drivers.ReverseDriver` 对应 [ASGI](https://asgi.readthedocs.io/) 对象。 """获取全局 {ref}`nonebot.drivers.ASGIMixin` 对应的
[ASGI](https://asgi.readthedocs.io/) 对象。
返回: 返回:
ASGI 对象 ASGI 对象
异常: 异常:
AssertionError: 全局 Driver 对象不是 {ref}`nonebot.drivers.ReverseDriver` 类型 AssertionError: 全局 Driver 对象不是 {ref}`nonebot.drivers.ASGIMixin` 类型
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化 ({ref}`nonebot.init <nonebot.init>` 尚未调用) ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
({ref}`nonebot.init <nonebot.init>` 尚未调用)
用法: 用法:
```python ```python
@@ -170,8 +188,8 @@ def get_asgi() -> Any:
""" """
driver = get_driver() driver = get_driver()
assert isinstance( assert isinstance(
driver, ReverseDriver driver, ASGIMixin
), "asgi object is only available for reverse driver" ), "asgi object is only available for asgi driver"
return driver.asgi return driver.asgi
@@ -182,7 +200,8 @@ def get_bot(self_id: Optional[str] = None) -> Bot:
当不提供时,返回一个 {ref}`nonebot.adapters.Bot`。 当不提供时,返回一个 {ref}`nonebot.adapters.Bot`。
参数: 参数:
self_id: 用来识别 {ref}`nonebot.adapters.Bot` 的 {ref}`nonebot.adapters.Bot.self_id` 属性 self_id: 用来识别 {ref}`nonebot.adapters.Bot` 的
{ref}`nonebot.adapters.Bot.self_id` 属性
返回: 返回:
{ref}`nonebot.adapters.Bot` 对象 {ref}`nonebot.adapters.Bot` 对象
@@ -190,7 +209,8 @@ def get_bot(self_id: Optional[str] = None) -> Bot:
异常: 异常:
KeyError: 对应 self_id 的 Bot 不存在 KeyError: 对应 self_id 的 Bot 不存在
ValueError: 没有传入 self_id 且没有 Bot 可用 ValueError: 没有传入 self_id 且没有 Bot 可用
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化 ({ref}`nonebot.init <nonebot.init>` 尚未调用) ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
({ref}`nonebot.init <nonebot.init>` 尚未调用)
用法: 用法:
```python ```python
@@ -213,10 +233,12 @@ def get_bots() -> Dict[str, Bot]:
"""获取所有连接到 NoneBot 的 {ref}`nonebot.adapters.Bot` 对象。 """获取所有连接到 NoneBot 的 {ref}`nonebot.adapters.Bot` 对象。
返回: 返回:
一个以 {ref}`nonebot.adapters.Bot.self_id` 为键{ref}`nonebot.adapters.Bot` 对象为值的字典 一个以 {ref}`nonebot.adapters.Bot.self_id` 为键
{ref}`nonebot.adapters.Bot` 对象为值的字典
异常: 异常:
ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化 ({ref}`nonebot.init <nonebot.init>` 尚未调用) ValueError: 全局 {ref}`nonebot.drivers.Driver` 对象尚未初始化
({ref}`nonebot.init <nonebot.init>` 尚未调用)
用法: 用法:
```python ```python

View File

@@ -1,19 +1,23 @@
"""本模块定义了 NoneBot 本身运行所需的配置项。 """本模块定义了 NoneBot 本身运行所需的配置项。
NoneBot 使用 [`pydantic`](https://pydantic-docs.helpmanual.io/) 以及 [`python-dotenv`](https://saurabh-kumar.com/python-dotenv/) 来读取配置。 NoneBot 使用 [`pydantic`](https://pydantic-docs.helpmanual.io/) 以及
[`python-dotenv`](https://saurabh-kumar.com/python-dotenv/) 来读取配置。
配置项需符合特殊格式或 json 序列化格式。详情见 [`pydantic Field Type`](https://pydantic-docs.helpmanual.io/usage/types/) 文档。 配置项需符合特殊格式或 json 序列化格式
详情见 [`pydantic Field Type`](https://pydantic-docs.helpmanual.io/usage/types/) 文档。
FrontMatter: FrontMatter:
sidebar_position: 1 sidebar_position: 1
description: nonebot.config 模块 description: nonebot.config 模块
""" """
import os import os
from datetime import timedelta from datetime import timedelta
from ipaddress import IPv4Address from ipaddress import IPv4Address
from typing import TYPE_CHECKING, Any, Set, Dict, Tuple, Union, Mapping, Optional from typing import TYPE_CHECKING, Any, Set, Dict, Tuple, Union, Mapping, Optional
from pydantic.utils import deep_update from pydantic.utils import deep_update
from pydantic.fields import Undefined, UndefinedType
from pydantic import Extra, Field, BaseSettings, IPvAnyAddress from pydantic import Extra, Field, BaseSettings, IPvAnyAddress
from pydantic.env_settings import ( from pydantic.env_settings import (
DotenvType, DotenvType,
@@ -28,9 +32,8 @@ from nonebot.log import logger
class CustomEnvSettings(EnvSettingsSource): class CustomEnvSettings(EnvSettingsSource):
def __call__(self, settings: BaseSettings) -> Dict[str, Any]: def __call__(self, settings: BaseSettings) -> Dict[str, Any]:
""" """从环境变量和 dotenv 配置文件中读取配置项。"""
Build environment variables suitable for passing to the Model.
"""
d: Dict[str, Any] = {} d: Dict[str, Any] = {}
if settings.__config__.case_sensitive: if settings.__config__.case_sensitive:
@@ -42,21 +45,25 @@ class CustomEnvSettings(EnvSettingsSource):
env_vars = {**env_file_vars, **env_vars} env_vars = {**env_file_vars, **env_vars}
for field in settings.__fields__.values(): for field in settings.__fields__.values():
env_val: Optional[str] = None env_val: Union[str, None, UndefinedType] = Undefined
for env_name in field.field_info.extra["env_names"]: for env_name in field.field_info.extra["env_names"]:
env_val = env_vars.get(env_name) env_val = env_vars.get(env_name, Undefined)
if env_name in env_file_vars: if env_name in env_file_vars:
del env_file_vars[env_name] del env_file_vars[env_name]
if env_val is not None: if env_val is not Undefined:
break break
is_complex, allow_parse_failure = self.field_is_complex(field) is_complex, allow_parse_failure = self.field_is_complex(field)
if is_complex: if is_complex:
if env_val is None: if isinstance(env_val, UndefinedType):
# field is complex but no value found so far, try explode_env_vars
if env_val_built := self.explode_env_vars(field, env_vars): if env_val_built := self.explode_env_vars(field, env_vars):
d[field.alias] = env_val_built d[field.alias] = env_val_built
elif env_val is None:
d[field.alias] = env_val
else: else:
# field is complex and there's a value, decode that as JSON, then add explode_env_vars # field is complex and there's a value
# decode that as JSON, then add explode_env_vars
try: try:
env_val = settings.__config__.parse_env_var(field.name, env_val) env_val = settings.__config__.parse_env_var(field.name, env_val)
except ValueError as e: except ValueError as e:
@@ -71,8 +78,9 @@ class CustomEnvSettings(EnvSettingsSource):
) )
else: else:
d[field.alias] = env_val d[field.alias] = env_val
elif env_val is not None: elif not isinstance(env_val, UndefinedType):
# simplest case, field is not complex, we only need to add the value if it was found # simplest case, field is not complex
# we only need to add the value if it was found
d[field.alias] = env_val d[field.alias] = env_val
# remain user custom config # remain user custom config
@@ -82,7 +90,7 @@ class CustomEnvSettings(EnvSettingsSource):
# there's a value, decode that as JSON # there's a value, decode that as JSON
try: try:
env_val = settings.__config__.parse_env_var(env_name, val_striped) env_val = settings.__config__.parse_env_var(env_name, val_striped)
except ValueError as e: except ValueError:
logger.trace( logger.trace(
"Error while parsing JSON for " "Error while parsing JSON for "
f"{env_name!r}={val_striped!r}. " f"{env_name!r}={val_striped!r}. "
@@ -139,7 +147,7 @@ class BaseConfig(BaseSettings):
class Env(BaseConfig): class Env(BaseConfig):
"""运行环境配置。大小写不敏感。 """运行环境配置。大小写不敏感。
将会从 `环境变量` > `.env 环境配置文件` 的优先级读取环境信息。 将会从 **环境变量** > **dotenv 配置文件** 的优先级读取环境信息。
""" """
environment: str = "prod" environment: str = "prod"
@@ -170,15 +178,17 @@ class Config(BaseConfig):
配置格式为 `<module>[:<Driver>][+<module>[:<Mixin>]]*`。 配置格式为 `<module>[:<Driver>][+<module>[:<Mixin>]]*`。
`~` 为 `nonebot.drivers.` 的缩写。 `~` 为 `nonebot.drivers.` 的缩写。
配置方法参考: [配置驱动器](https://nonebot.dev/docs/advanced/driver#%E9%85%8D%E7%BD%AE%E9%A9%B1%E5%8A%A8%E5%99%A8)
""" """
host: IPvAnyAddress = IPv4Address("127.0.0.1") # type: ignore host: IPvAnyAddress = IPv4Address("127.0.0.1") # type: ignore
"""NoneBot {ref}`nonebot.drivers.ReverseDriver` 服务端监听的 IP/主机名。""" """NoneBot {ref}`nonebot.drivers.ReverseDriver` 服务端监听的 IP/主机名。"""
port: int = Field(default=8080, ge=1, le=65535) port: int = Field(default=8080, ge=1, le=65535)
"""NoneBot {ref}`nonebot.drivers.ReverseDriver` 服务端监听的端口。""" """NoneBot {ref}`nonebot.drivers.ReverseDriver` 服务端监听的端口。"""
log_level: Union[int, str] = "INFO" log_level: Union[int, str] = "INFO"
"""NoneBot 日志输出等级,可以为 `int` 类型等级或等级名称 """NoneBot 日志输出等级,可以为 `int` 类型等级或等级名称
参考 [`loguru 日志等级`](https://loguru.readthedocs.io/en/stable/api/logger.html#levels)。 参考 [记录日志](https://nonebot.dev/docs/appendices/log)[loguru 日志等级](https://loguru.readthedocs.io/en/stable/api/logger.html#levels)。
:::tip 提示 :::tip 提示
日志等级名称应为大写,如 `INFO`。 日志等级名称应为大写,如 `INFO`。
@@ -209,6 +219,8 @@ class Config(BaseConfig):
command_start: Set[str] = {"/"} command_start: Set[str] = {"/"}
"""命令的起始标记,用于判断一条消息是不是命令。 """命令的起始标记,用于判断一条消息是不是命令。
参考[命令响应规则](https://nonebot.dev/docs/advanced/matcher#command)。
用法: 用法:
```conf ```conf
COMMAND_START=["/", ""] COMMAND_START=["/", ""]
@@ -217,6 +229,8 @@ class Config(BaseConfig):
command_sep: Set[str] = {"."} command_sep: Set[str] = {"."}
"""命令的分隔标记,用于将文本形式的命令切分为元组(实际的命令名)。 """命令的分隔标记,用于将文本形式的命令切分为元组(实际的命令名)。
参考[命令响应规则](https://nonebot.dev/docs/advanced/matcher#command)。
用法: 用法:
```conf ```conf
COMMAND_SEP=["."] COMMAND_SEP=["."]

View File

@@ -4,6 +4,7 @@ FrontMatter:
sidebar_position: 9 sidebar_position: 9
description: nonebot.consts 模块 description: nonebot.consts 模块
""" """
import os import os
import sys import sys
from typing import Literal from typing import Literal

View File

@@ -45,6 +45,10 @@ class Param(abc.ABC, FieldInfo):
继承自 `pydantic.fields.FieldInfo`,用于描述参数信息(不包括参数名)。 继承自 `pydantic.fields.FieldInfo`,用于描述参数信息(不包括参数名)。
""" """
def __init__(self, *args, validate: bool = False, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.validate = validate
@classmethod @classmethod
def _check_param( def _check_param(
cls, param: inspect.Parameter, allow_types: Tuple[Type["Param"], ...] cls, param: inspect.Parameter, allow_types: Tuple[Type["Param"], ...]
@@ -82,8 +86,8 @@ class Dependent(Generic[R]):
""" """
call: _DependentCallable[R] call: _DependentCallable[R]
params: Tuple[ModelField] = field(default_factory=tuple) params: Tuple[ModelField, ...] = field(default_factory=tuple)
parameterless: Tuple[Param] = field(default_factory=tuple) parameterless: Tuple[Param, ...] = field(default_factory=tuple)
def __repr__(self) -> str: def __repr__(self) -> str:
if inspect.isfunction(self.call) or inspect.isclass(self.call): if inspect.isfunction(self.call) or inspect.isclass(self.call):
@@ -97,22 +101,26 @@ class Dependent(Generic[R]):
) )
async def __call__(self, **kwargs: Any) -> R: async def __call__(self, **kwargs: Any) -> R:
# do pre-check try:
await self.check(**kwargs) # do pre-check
await self.check(**kwargs)
# solve param values # solve param values
values = await self.solve(**kwargs) values = await self.solve(**kwargs)
# call function # call function
if is_coroutine_callable(self.call): if is_coroutine_callable(self.call):
return await cast(Callable[..., Awaitable[R]], self.call)(**values) return await cast(Callable[..., Awaitable[R]], self.call)(**values)
else: else:
return await run_sync(cast(Callable[..., R], self.call))(**values) return await run_sync(cast(Callable[..., R], self.call))(**values)
except SkippedException as e:
logger.trace(f"{self} skipped due to {e}")
raise
@staticmethod @staticmethod
def parse_params( def parse_params(
call: _DependentCallable[R], allow_types: Tuple[Type[Param], ...] call: _DependentCallable[R], allow_types: Tuple[Type[Param], ...]
) -> Tuple[ModelField]: ) -> Tuple[ModelField, ...]:
fields: List[ModelField] = [] fields: List[ModelField] = []
params = get_typed_signature(call).parameters.values() params = get_typed_signature(call).parameters.values()
@@ -129,7 +137,8 @@ class Dependent(Generic[R]):
break break
else: else:
raise ValueError( raise ValueError(
f"Unknown parameter {param.name} for function {call} with type {param.annotation}" f"Unknown parameter {param.name} "
f"for function {call} with type {param.annotation}"
) )
default_value = field_info.default default_value = field_info.default
@@ -182,7 +191,7 @@ class Dependent(Generic[R]):
params = cls.parse_params(call, allow_types) params = cls.parse_params(call, allow_types)
parameterless_params = ( parameterless_params = (
tuple() ()
if parameterless is None if parameterless is None
else cls.parse_parameterless(tuple(parameterless), allow_types) else cls.parse_parameterless(tuple(parameterless), allow_types)
) )
@@ -190,25 +199,18 @@ class Dependent(Generic[R]):
return cls(call, params, parameterless_params) return cls(call, params, parameterless_params)
async def check(self, **params: Any) -> None: async def check(self, **params: Any) -> None:
try: await asyncio.gather(*(param._check(**params) for param in self.parameterless))
await asyncio.gather( await asyncio.gather(
*(param._check(**params) for param in self.parameterless) *(cast(Param, param.field_info)._check(**params) for param in self.params)
) )
await asyncio.gather(
*(
cast(Param, param.field_info)._check(**params)
for param in self.params
)
)
except SkippedException as e:
logger.trace(f"{self} skipped due to {e}")
raise
async def _solve_field(self, field: ModelField, params: Dict[str, Any]) -> Any: async def _solve_field(self, field: ModelField, params: Dict[str, Any]) -> Any:
value = await cast(Param, field.field_info)._solve(**params) param = cast(Param, field.field_info)
value = await param._solve(**params)
if value is Undefined: if value is Undefined:
value = field.get_default() value = field.get_default()
return check_field_type(field, value) v = check_field_type(field, value)
return v if param.validate else value
async def solve(self, **params: Any) -> Dict[str, Any]: async def solve(self, **params: Any) -> Dict[str, Any]:
# solve parameterless # solve parameterless

View File

@@ -3,8 +3,9 @@ FrontMatter:
sidebar_position: 1 sidebar_position: 1
description: nonebot.dependencies.utils 模块 description: nonebot.dependencies.utils 模块
""" """
import inspect import inspect
from typing import Any, Dict, TypeVar, Callable, ForwardRef from typing import Any, Dict, Callable, ForwardRef
from loguru import logger from loguru import logger
from pydantic.fields import ModelField from pydantic.fields import ModelField
@@ -12,11 +13,10 @@ from pydantic.typing import evaluate_forwardref
from nonebot.exception import TypeMisMatch from nonebot.exception import TypeMisMatch
V = TypeVar("V")
def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature: def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:
"""获取可调用对象签名""" """获取可调用对象签名"""
signature = inspect.signature(call) signature = inspect.signature(call)
globalns = getattr(call, "__globals__", {}) globalns = getattr(call, "__globals__", {})
typed_params = [ typed_params = [
@@ -33,6 +33,7 @@ def get_typed_signature(call: Callable[..., Any]) -> inspect.Signature:
def get_typed_annotation(param: inspect.Parameter, globalns: Dict[str, Any]) -> Any: def get_typed_annotation(param: inspect.Parameter, globalns: Dict[str, Any]) -> Any:
"""获取参数的类型注解""" """获取参数的类型注解"""
annotation = param.annotation annotation = param.annotation
if isinstance(annotation, str): if isinstance(annotation, str):
annotation = ForwardRef(annotation) annotation = ForwardRef(annotation)
@@ -46,8 +47,10 @@ def get_typed_annotation(param: inspect.Parameter, globalns: Dict[str, Any]) ->
return annotation return annotation
def check_field_type(field: ModelField, value: V) -> V: def check_field_type(field: ModelField, value: Any) -> Any:
_, errs_ = field.validate(value, {}, loc=()) """检查字段类型是否匹配"""
v, errs_ = field.validate(value, {}, loc=())
if errs_: if errs_:
raise TypeMisMatch(field, value) raise TypeMisMatch(field, value)
return value return v

View File

@@ -8,30 +8,40 @@ FrontMatter:
""" """
from nonebot.internal.driver import URL as URL from nonebot.internal.driver import URL as URL
from nonebot.internal.driver import Mixin as Mixin
from nonebot.internal.driver import Driver as Driver from nonebot.internal.driver import Driver as Driver
from nonebot.internal.driver import Cookies as Cookies from nonebot.internal.driver import Cookies as Cookies
from nonebot.internal.driver import Request as Request from nonebot.internal.driver import Request as Request
from nonebot.internal.driver import Response as Response from nonebot.internal.driver import Response as Response
from nonebot.internal.driver import ASGIMixin as ASGIMixin
from nonebot.internal.driver import WebSocket as WebSocket from nonebot.internal.driver import WebSocket as WebSocket
from nonebot.internal.driver import HTTPVersion as HTTPVersion from nonebot.internal.driver import HTTPVersion as HTTPVersion
from nonebot.internal.driver import ForwardMixin as ForwardMixin from nonebot.internal.driver import ForwardMixin as ForwardMixin
from nonebot.internal.driver import ReverseMixin as ReverseMixin
from nonebot.internal.driver import ForwardDriver as ForwardDriver from nonebot.internal.driver import ForwardDriver as ForwardDriver
from nonebot.internal.driver import ReverseDriver as ReverseDriver from nonebot.internal.driver import ReverseDriver as ReverseDriver
from nonebot.internal.driver import combine_driver as combine_driver from nonebot.internal.driver import combine_driver as combine_driver
from nonebot.internal.driver import HTTPClientMixin as HTTPClientMixin
from nonebot.internal.driver import HTTPServerSetup as HTTPServerSetup from nonebot.internal.driver import HTTPServerSetup as HTTPServerSetup
from nonebot.internal.driver import WebSocketClientMixin as WebSocketClientMixin
from nonebot.internal.driver import WebSocketServerSetup as WebSocketServerSetup from nonebot.internal.driver import WebSocketServerSetup as WebSocketServerSetup
__autodoc__ = { __autodoc__ = {
"URL": True, "URL": True,
"Driver": True,
"Cookies": True, "Cookies": True,
"Request": True, "Request": True,
"Response": True, "Response": True,
"WebSocket": True, "WebSocket": True,
"HTTPVersion": True, "HTTPVersion": True,
"Driver": True,
"Mixin": True,
"ForwardMixin": True, "ForwardMixin": True,
"ForwardDriver": True, "ForwardDriver": True,
"HTTPClientMixin": True,
"WebSocketClientMixin": True,
"ReverseMixin": True,
"ReverseDriver": True, "ReverseDriver": True,
"ASGIMixin": True,
"combine_driver": True, "combine_driver": True,
"HTTPServerSetup": True, "HTTPServerSetup": True,
"WebSocketServerSetup": True, "WebSocketServerSetup": True,

View File

@@ -1,10 +1,11 @@
from typing_extensions import TypeAlias
from typing import Any, List, Union, Callable, Awaitable, cast from typing import Any, List, Union, Callable, Awaitable, cast
from nonebot.utils import run_sync, is_coroutine_callable from nonebot.utils import run_sync, is_coroutine_callable
SYNC_LIFESPAN_FUNC = Callable[[], Any] SYNC_LIFESPAN_FUNC: TypeAlias = Callable[[], Any]
ASYNC_LIFESPAN_FUNC = Callable[[], Awaitable[Any]] ASYNC_LIFESPAN_FUNC: TypeAlias = Callable[[], Awaitable[Any]]
LIFESPAN_FUNC = Union[SYNC_LIFESPAN_FUNC, ASYNC_LIFESPAN_FUNC] LIFESPAN_FUNC: TypeAlias = Union[SYNC_LIFESPAN_FUNC, ASYNC_LIFESPAN_FUNC]
class Lifespan: class Lifespan:

View File

@@ -15,33 +15,39 @@ FrontMatter:
description: nonebot.drivers.aiohttp 模块 description: nonebot.drivers.aiohttp 模块
""" """
from typing import Type, AsyncGenerator from typing_extensions import override
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from typing import TYPE_CHECKING, AsyncGenerator
from nonebot.typing import overrides
from nonebot.drivers import Request, Response from nonebot.drivers import Request, Response
from nonebot.exception import WebSocketClosed from nonebot.exception import WebSocketClosed
from nonebot.drivers.none import Driver as NoneDriver from nonebot.drivers.none import Driver as NoneDriver
from nonebot.drivers import WebSocket as BaseWebSocket from nonebot.drivers import WebSocket as BaseWebSocket
from nonebot.drivers import HTTPVersion, ForwardMixin, ForwardDriver, combine_driver from nonebot.drivers import (
HTTPVersion,
HTTPClientMixin,
WebSocketClientMixin,
combine_driver,
)
try: try:
import aiohttp import aiohttp
except ModuleNotFoundError as e: # pragma: no cover except ModuleNotFoundError as e: # pragma: no cover
raise ImportError( raise ImportError(
"Please install aiohttp first to use this driver. `pip install nonebot2[aiohttp]`" "Please install aiohttp first to use this driver. "
"Install with pip: `pip install nonebot2[aiohttp]`"
) from e ) from e
class Mixin(ForwardMixin): class Mixin(HTTPClientMixin, WebSocketClientMixin):
"""AIOHTTP Mixin""" """AIOHTTP Mixin"""
@property @property
@overrides(ForwardMixin) @override
def type(self) -> str: def type(self) -> str:
return "aiohttp" return "aiohttp"
@overrides(ForwardMixin) @override
async def request(self, setup: Request) -> Response: async def request(self, setup: Request) -> Response:
if setup.version == HTTPVersion.H10: if setup.version == HTTPVersion.H10:
version = aiohttp.HttpVersion10 version = aiohttp.HttpVersion10
@@ -51,11 +57,12 @@ class Mixin(ForwardMixin):
raise RuntimeError(f"Unsupported HTTP version: {setup.version}") raise RuntimeError(f"Unsupported HTTP version: {setup.version}")
timeout = aiohttp.ClientTimeout(setup.timeout) timeout = aiohttp.ClientTimeout(setup.timeout)
files = None
data = setup.data
if setup.files: if setup.files:
files = aiohttp.FormData() data = aiohttp.FormData(data or {})
for name, file in setup.files: for name, file in setup.files:
files.add_field(name, file[1], content_type=file[2], filename=file[0]) data.add_field(name, file[1], content_type=file[2], filename=file[0])
cookies = { cookies = {
cookie.name: cookie.value for cookie in setup.cookies if cookie.value cookie.name: cookie.value for cookie in setup.cookies if cookie.value
@@ -66,7 +73,7 @@ class Mixin(ForwardMixin):
async with session.request( async with session.request(
setup.method, setup.method,
setup.url, setup.url,
data=setup.content or setup.data or files, data=setup.content or data,
json=setup.json, json=setup.json,
headers=setup.headers, headers=setup.headers,
timeout=timeout, timeout=timeout,
@@ -79,7 +86,7 @@ class Mixin(ForwardMixin):
request=setup, request=setup,
) )
@overrides(ForwardMixin) @override
@asynccontextmanager @asynccontextmanager
async def websocket(self, setup: Request) -> AsyncGenerator["WebSocket", None]: async def websocket(self, setup: Request) -> AsyncGenerator["WebSocket", None]:
if setup.version == HTTPVersion.H10: if setup.version == HTTPVersion.H10:
@@ -115,15 +122,15 @@ class WebSocket(BaseWebSocket):
self.websocket = websocket self.websocket = websocket
@property @property
@overrides(BaseWebSocket) @override
def closed(self): def closed(self):
return self.websocket.closed return self.websocket.closed
@overrides(BaseWebSocket) @override
async def accept(self): async def accept(self):
raise NotImplementedError raise NotImplementedError
@overrides(BaseWebSocket) @override
async def close(self, code: int = 1000): async def close(self, code: int = 1000):
await self.websocket.close(code=code) await self.websocket.close(code=code)
await self.session.close() await self.session.close()
@@ -134,7 +141,7 @@ class WebSocket(BaseWebSocket):
raise WebSocketClosed(self.websocket.close_code or 1006) raise WebSocketClosed(self.websocket.close_code or 1006)
return msg return msg
@overrides(BaseWebSocket) @override
async def receive(self) -> str: async def receive(self) -> str:
msg = await self._receive() msg = await self._receive()
if msg.type not in (aiohttp.WSMsgType.TEXT, aiohttp.WSMsgType.BINARY): if msg.type not in (aiohttp.WSMsgType.TEXT, aiohttp.WSMsgType.BINARY):
@@ -143,7 +150,7 @@ class WebSocket(BaseWebSocket):
) )
return msg.data return msg.data
@overrides(BaseWebSocket) @override
async def receive_text(self) -> str: async def receive_text(self) -> str:
msg = await self._receive() msg = await self._receive()
if msg.type != aiohttp.WSMsgType.TEXT: if msg.type != aiohttp.WSMsgType.TEXT:
@@ -152,7 +159,7 @@ class WebSocket(BaseWebSocket):
) )
return msg.data return msg.data
@overrides(BaseWebSocket) @override
async def receive_bytes(self) -> bytes: async def receive_bytes(self) -> bytes:
msg = await self._receive() msg = await self._receive()
if msg.type != aiohttp.WSMsgType.BINARY: if msg.type != aiohttp.WSMsgType.BINARY:
@@ -161,14 +168,20 @@ class WebSocket(BaseWebSocket):
) )
return msg.data return msg.data
@overrides(BaseWebSocket) @override
async def send_text(self, data: str) -> None: async def send_text(self, data: str) -> None:
await self.websocket.send_str(data) await self.websocket.send_str(data)
@overrides(BaseWebSocket) @override
async def send_bytes(self, data: bytes) -> None: async def send_bytes(self, data: bytes) -> None:
await self.websocket.send_bytes(data) await self.websocket.send_bytes(data)
Driver: Type[ForwardDriver] = combine_driver(NoneDriver, Mixin) # type: ignore if TYPE_CHECKING:
"""AIOHTTP Driver"""
class Driver(Mixin, NoneDriver):
...
else:
Driver = combine_driver(NoneDriver, Mixin)
"""AIOHTTP Driver"""

View File

@@ -19,18 +19,20 @@ FrontMatter:
import logging import logging
import contextlib import contextlib
from functools import wraps from functools import wraps
from typing_extensions import override
from typing import Any, Dict, List, Tuple, Union, Optional from typing import Any, Dict, List, Tuple, Union, Optional
from pydantic import BaseSettings from pydantic import BaseSettings
from nonebot.config import Env from nonebot.config import Env
from nonebot.typing import overrides from nonebot.drivers import ASGIMixin
from nonebot.exception import WebSocketClosed from nonebot.exception import WebSocketClosed
from nonebot.internal.driver import FileTypes from nonebot.internal.driver import FileTypes
from nonebot.drivers import Driver as BaseDriver
from nonebot.config import Config as NoneBotConfig from nonebot.config import Config as NoneBotConfig
from nonebot.drivers import Request as BaseRequest from nonebot.drivers import Request as BaseRequest
from nonebot.drivers import WebSocket as BaseWebSocket from nonebot.drivers import WebSocket as BaseWebSocket
from nonebot.drivers import ReverseDriver, HTTPServerSetup, WebSocketServerSetup from nonebot.drivers import HTTPServerSetup, WebSocketServerSetup
from ._lifespan import LIFESPAN_FUNC, Lifespan from ._lifespan import LIFESPAN_FUNC, Lifespan
@@ -41,7 +43,8 @@ try:
from starlette.websockets import WebSocket, WebSocketState, WebSocketDisconnect from starlette.websockets import WebSocket, WebSocketState, WebSocketDisconnect
except ModuleNotFoundError as e: # pragma: no cover except ModuleNotFoundError as e: # pragma: no cover
raise ImportError( raise ImportError(
"Please install FastAPI by using `pip install nonebot2[fastapi]`" "Please install FastAPI first to use this driver. "
"Install with pip: `pip install nonebot2[fastapi]`"
) from e ) from e
@@ -86,11 +89,11 @@ class Config(BaseSettings):
extra = "ignore" extra = "ignore"
class Driver(ReverseDriver): class Driver(BaseDriver, ASGIMixin):
"""FastAPI 驱动框架。""" """FastAPI 驱动框架。"""
def __init__(self, env: Env, config: NoneBotConfig): def __init__(self, env: Env, config: NoneBotConfig):
super(Driver, self).__init__(env, config) super().__init__(env, config)
self.fastapi_config: Config = Config(**config.dict()) self.fastapi_config: Config = Config(**config.dict())
@@ -105,30 +108,30 @@ class Driver(ReverseDriver):
) )
@property @property
@overrides(ReverseDriver) @override
def type(self) -> str: def type(self) -> str:
"""驱动名称: `fastapi`""" """驱动名称: `fastapi`"""
return "fastapi" return "fastapi"
@property @property
@overrides(ReverseDriver) @override
def server_app(self) -> FastAPI: def server_app(self) -> FastAPI:
"""`FastAPI APP` 对象""" """`FastAPI APP` 对象"""
return self._server_app return self._server_app
@property @property
@overrides(ReverseDriver) @override
def asgi(self) -> FastAPI: def asgi(self) -> FastAPI:
"""`FastAPI APP` 对象""" """`FastAPI APP` 对象"""
return self._server_app return self._server_app
@property @property
@overrides(ReverseDriver) @override
def logger(self) -> logging.Logger: def logger(self) -> logging.Logger:
"""fastapi 使用的 logger""" """fastapi 使用的 logger"""
return logging.getLogger("fastapi") return logging.getLogger("fastapi")
@overrides(ReverseDriver) @override
def setup_http_server(self, setup: HTTPServerSetup): def setup_http_server(self, setup: HTTPServerSetup):
async def _handle(request: Request) -> Response: async def _handle(request: Request) -> Response:
return await self._handle_http(request, setup) return await self._handle_http(request, setup)
@@ -141,7 +144,7 @@ class Driver(ReverseDriver):
include_in_schema=self.fastapi_config.fastapi_include_adapter_schema, include_in_schema=self.fastapi_config.fastapi_include_adapter_schema,
) )
@overrides(ReverseDriver) @override
def setup_websocket_server(self, setup: WebSocketServerSetup) -> None: def setup_websocket_server(self, setup: WebSocketServerSetup) -> None:
async def _handle(websocket: WebSocket) -> None: async def _handle(websocket: WebSocket) -> None:
await self._handle_ws(websocket, setup) await self._handle_ws(websocket, setup)
@@ -152,11 +155,11 @@ class Driver(ReverseDriver):
name=setup.name, name=setup.name,
) )
@overrides(ReverseDriver) @override
def on_startup(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC: def on_startup(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:
return self._lifespan.on_startup(func) return self._lifespan.on_startup(func)
@overrides(ReverseDriver) @override
def on_shutdown(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC: def on_shutdown(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:
return self._lifespan.on_shutdown(func) return self._lifespan.on_shutdown(func)
@@ -168,7 +171,7 @@ class Driver(ReverseDriver):
finally: finally:
await self._lifespan.shutdown() await self._lifespan.shutdown()
@overrides(ReverseDriver) @override
def run( def run(
self, self,
host: Optional[str] = None, host: Optional[str] = None,
@@ -178,7 +181,7 @@ class Driver(ReverseDriver):
**kwargs, **kwargs,
): ):
"""使用 `uvicorn` 启动 FastAPI""" """使用 `uvicorn` 启动 FastAPI"""
super().run(host, port, app, **kwargs) super().run(host, port, app=app, **kwargs)
LOGGING_CONFIG = { LOGGING_CONFIG = {
"version": 1, "version": 1,
"disable_existing_loggers": False, "disable_existing_loggers": False,
@@ -267,30 +270,30 @@ class Driver(ReverseDriver):
class FastAPIWebSocket(BaseWebSocket): class FastAPIWebSocket(BaseWebSocket):
"""FastAPI WebSocket Wrapper""" """FastAPI WebSocket Wrapper"""
@overrides(BaseWebSocket) @override
def __init__(self, *, request: BaseRequest, websocket: WebSocket): def __init__(self, *, request: BaseRequest, websocket: WebSocket):
super().__init__(request=request) super().__init__(request=request)
self.websocket = websocket self.websocket = websocket
@property @property
@overrides(BaseWebSocket) @override
def closed(self) -> bool: def closed(self) -> bool:
return ( return (
self.websocket.client_state == WebSocketState.DISCONNECTED self.websocket.client_state == WebSocketState.DISCONNECTED
or self.websocket.application_state == WebSocketState.DISCONNECTED or self.websocket.application_state == WebSocketState.DISCONNECTED
) )
@overrides(BaseWebSocket) @override
async def accept(self) -> None: async def accept(self) -> None:
await self.websocket.accept() await self.websocket.accept()
@overrides(BaseWebSocket) @override
async def close( async def close(
self, code: int = status.WS_1000_NORMAL_CLOSURE, reason: str = "" self, code: int = status.WS_1000_NORMAL_CLOSURE, reason: str = ""
) -> None: ) -> None:
await self.websocket.close(code, reason) await self.websocket.close(code, reason)
@overrides(BaseWebSocket) @override
async def receive(self) -> Union[str, bytes]: async def receive(self) -> Union[str, bytes]:
# assert self.websocket.application_state == WebSocketState.CONNECTED # assert self.websocket.application_state == WebSocketState.CONNECTED
msg = await self.websocket.receive() msg = await self.websocket.receive()
@@ -298,21 +301,21 @@ class FastAPIWebSocket(BaseWebSocket):
raise WebSocketClosed(msg["code"]) raise WebSocketClosed(msg["code"])
return msg["text"] if "text" in msg else msg["bytes"] return msg["text"] if "text" in msg else msg["bytes"]
@overrides(BaseWebSocket) @override
@catch_closed @catch_closed
async def receive_text(self) -> str: async def receive_text(self) -> str:
return await self.websocket.receive_text() return await self.websocket.receive_text()
@overrides(BaseWebSocket) @override
@catch_closed @catch_closed
async def receive_bytes(self) -> bytes: async def receive_bytes(self) -> bytes:
return await self.websocket.receive_bytes() return await self.websocket.receive_bytes()
@overrides(BaseWebSocket) @override
async def send_text(self, data: str) -> None: async def send_text(self, data: str) -> None:
await self.websocket.send({"type": "websocket.send", "text": data}) await self.websocket.send({"type": "websocket.send", "text": data})
@overrides(BaseWebSocket) @override
async def send_bytes(self, data: bytes) -> None: async def send_bytes(self, data: bytes) -> None:
await self.websocket.send({"type": "websocket.send", "bytes": data}) await self.websocket.send({"type": "websocket.send", "bytes": data})

View File

@@ -14,18 +14,16 @@ FrontMatter:
sidebar_position: 3 sidebar_position: 3
description: nonebot.drivers.httpx 模块 description: nonebot.drivers.httpx 模块
""" """
from typing import Type, AsyncGenerator
from contextlib import asynccontextmanager
from nonebot.typing import overrides from typing import TYPE_CHECKING
from typing_extensions import override
from nonebot.drivers.none import Driver as NoneDriver from nonebot.drivers.none import Driver as NoneDriver
from nonebot.drivers import ( from nonebot.drivers import (
Request, Request,
Response, Response,
WebSocket,
HTTPVersion, HTTPVersion,
ForwardMixin, HTTPClientMixin,
ForwardDriver,
combine_driver, combine_driver,
) )
@@ -33,19 +31,20 @@ try:
import httpx import httpx
except ModuleNotFoundError as e: # pragma: no cover except ModuleNotFoundError as e: # pragma: no cover
raise ImportError( raise ImportError(
"Please install httpx by using `pip install nonebot2[httpx]`" "Please install httpx first to use this driver. "
"Install with pip: `pip install nonebot2[httpx]`"
) from e ) from e
class Mixin(ForwardMixin): class Mixin(HTTPClientMixin):
"""HTTPX Mixin""" """HTTPX Mixin"""
@property @property
@overrides(ForwardMixin) @override
def type(self) -> str: def type(self) -> str:
return "httpx" return "httpx"
@overrides(ForwardMixin) @override
async def request(self, setup: Request) -> Response: async def request(self, setup: Request) -> Response:
async with httpx.AsyncClient( async with httpx.AsyncClient(
cookies=setup.cookies.jar, cookies=setup.cookies.jar,
@@ -70,12 +69,12 @@ class Mixin(ForwardMixin):
request=setup, request=setup,
) )
@overrides(ForwardMixin)
@asynccontextmanager
async def websocket(self, setup: Request) -> AsyncGenerator[WebSocket, None]:
async with super(Mixin, self).websocket(setup) as ws:
yield ws
if TYPE_CHECKING:
Driver: Type[ForwardDriver] = combine_driver(NoneDriver, Mixin) # type: ignore class Driver(Mixin, NoneDriver):
"""HTTPX Driver""" ...
else:
Driver = combine_driver(NoneDriver, Mixin)
"""HTTPX Driver"""

View File

@@ -9,14 +9,13 @@ FrontMatter:
description: nonebot.drivers.none 模块 description: nonebot.drivers.none 模块
""" """
import signal import signal
import asyncio import asyncio
import threading import threading
from typing_extensions import override
from nonebot.log import logger from nonebot.log import logger
from nonebot.consts import WINDOWS from nonebot.consts import WINDOWS
from nonebot.typing import overrides
from nonebot.config import Env, Config from nonebot.config import Env, Config
from nonebot.drivers import Driver as BaseDriver from nonebot.drivers import Driver as BaseDriver
@@ -42,28 +41,28 @@ class Driver(BaseDriver):
self.force_exit: bool = False self.force_exit: bool = False
@property @property
@overrides(BaseDriver) @override
def type(self) -> str: def type(self) -> str:
"""驱动名称: `none`""" """驱动名称: `none`"""
return "none" return "none"
@property @property
@overrides(BaseDriver) @override
def logger(self): def logger(self):
"""none driver 使用的 logger""" """none driver 使用的 logger"""
return logger return logger
@overrides(BaseDriver) @override
def on_startup(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC: def on_startup(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:
"""注册一个启动时执行的函数""" """注册一个启动时执行的函数"""
return self._lifespan.on_startup(func) return self._lifespan.on_startup(func)
@overrides(BaseDriver) @override
def on_shutdown(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC: def on_shutdown(self, func: LIFESPAN_FUNC) -> LIFESPAN_FUNC:
"""注册一个停止时执行的函数""" """注册一个停止时执行的函数"""
return self._lifespan.on_shutdown(func) return self._lifespan.on_shutdown(func)
@overrides(BaseDriver) @override
def run(self, *args, **kwargs): def run(self, *args, **kwargs):
"""启动 none driver""" """启动 none driver"""
super().run(*args, **kwargs) super().run(*args, **kwargs)

View File

@@ -17,29 +17,44 @@ FrontMatter:
import asyncio import asyncio
from functools import wraps from functools import wraps
from typing import Any, Dict, List, Tuple, Union, TypeVar, Callable, Optional, Coroutine from typing_extensions import override
from typing import (
Any,
Dict,
List,
Tuple,
Union,
TypeVar,
Callable,
Optional,
Coroutine,
cast,
)
from pydantic import BaseSettings from pydantic import BaseSettings
from nonebot.config import Env from nonebot.config import Env
from nonebot.typing import overrides from nonebot.drivers import ASGIMixin
from nonebot.exception import WebSocketClosed from nonebot.exception import WebSocketClosed
from nonebot.internal.driver import FileTypes from nonebot.internal.driver import FileTypes
from nonebot.drivers import Driver as BaseDriver
from nonebot.config import Config as NoneBotConfig from nonebot.config import Config as NoneBotConfig
from nonebot.drivers import Request as BaseRequest from nonebot.drivers import Request as BaseRequest
from nonebot.drivers import WebSocket as BaseWebSocket from nonebot.drivers import WebSocket as BaseWebSocket
from nonebot.drivers import ReverseDriver, HTTPServerSetup, WebSocketServerSetup from nonebot.drivers import HTTPServerSetup, WebSocketServerSetup
try: try:
import uvicorn import uvicorn
from quart import request as _request from quart import request as _request
from quart import websocket as _websocket from quart.ctx import WebsocketContext
from quart.globals import websocket_ctx
from quart import Quart, Request, Response from quart import Quart, Request, Response
from quart.datastructures import FileStorage from quart.datastructures import FileStorage
from quart import Websocket as QuartWebSocket from quart import Websocket as QuartWebSocket
except ModuleNotFoundError as e: # pragma: no cover except ModuleNotFoundError as e: # pragma: no cover
raise ImportError( raise ImportError(
"Please install Quart by using `pip install nonebot2[quart]`" "Please install Quart first to use this driver. "
"Install with pip: `pip install nonebot2[quart]`"
) from e ) from e
_AsyncCallable = TypeVar("_AsyncCallable", bound=Callable[..., Coroutine]) _AsyncCallable = TypeVar("_AsyncCallable", bound=Callable[..., Coroutine])
@@ -76,7 +91,7 @@ class Config(BaseSettings):
extra = "ignore" extra = "ignore"
class Driver(ReverseDriver): class Driver(BaseDriver, ASGIMixin):
"""Quart 驱动框架""" """Quart 驱动框架"""
def __init__(self, env: Env, config: NoneBotConfig): def __init__(self, env: Env, config: NoneBotConfig):
@@ -89,30 +104,30 @@ class Driver(ReverseDriver):
) )
@property @property
@overrides(ReverseDriver) @override
def type(self) -> str: def type(self) -> str:
"""驱动名称: `quart`""" """驱动名称: `quart`"""
return "quart" return "quart"
@property @property
@overrides(ReverseDriver) @override
def server_app(self) -> Quart: def server_app(self) -> Quart:
"""`Quart` 对象""" """`Quart` 对象"""
return self._server_app return self._server_app
@property @property
@overrides(ReverseDriver) @override
def asgi(self): def asgi(self):
"""`Quart` 对象""" """`Quart` 对象"""
return self._server_app return self._server_app
@property @property
@overrides(ReverseDriver) @override
def logger(self): def logger(self):
"""Quart 使用的 logger""" """Quart 使用的 logger"""
return self._server_app.logger return self._server_app.logger
@overrides(ReverseDriver) @override
def setup_http_server(self, setup: HTTPServerSetup): def setup_http_server(self, setup: HTTPServerSetup):
async def _handle() -> Response: async def _handle() -> Response:
return await self._handle_http(setup) return await self._handle_http(setup)
@@ -124,7 +139,7 @@ class Driver(ReverseDriver):
view_func=_handle, view_func=_handle,
) )
@overrides(ReverseDriver) @override
def setup_websocket_server(self, setup: WebSocketServerSetup) -> None: def setup_websocket_server(self, setup: WebSocketServerSetup) -> None:
async def _handle() -> None: async def _handle() -> None:
return await self._handle_ws(setup) return await self._handle_ws(setup)
@@ -135,17 +150,17 @@ class Driver(ReverseDriver):
view_func=_handle, view_func=_handle,
) )
@overrides(ReverseDriver) @override
def on_startup(self, func: _AsyncCallable) -> _AsyncCallable: def on_startup(self, func: _AsyncCallable) -> _AsyncCallable:
"""参考文档: [`Startup and Shutdown`](https://pgjones.gitlab.io/quart/how_to_guides/startup_shutdown.html)""" """参考文档: [`Startup and Shutdown`](https://pgjones.gitlab.io/quart/how_to_guides/startup_shutdown.html)"""
return self.server_app.before_serving(func) # type: ignore return self.server_app.before_serving(func) # type: ignore
@overrides(ReverseDriver) @override
def on_shutdown(self, func: _AsyncCallable) -> _AsyncCallable: def on_shutdown(self, func: _AsyncCallable) -> _AsyncCallable:
"""参考文档: [`Startup and Shutdown`](https://pgjones.gitlab.io/quart/how_to_guides/startup_shutdown.html)""" """参考文档: [`Startup and Shutdown`](https://pgjones.gitlab.io/quart/how_to_guides/startup_shutdown.html)"""
return self.server_app.after_serving(func) # type: ignore return self.server_app.after_serving(func) # type: ignore
@overrides(ReverseDriver) @override
def run( def run(
self, self,
host: Optional[str] = None, host: Optional[str] = None,
@@ -188,9 +203,7 @@ class Driver(ReverseDriver):
async def _handle_http(self, setup: HTTPServerSetup) -> Response: async def _handle_http(self, setup: HTTPServerSetup) -> Response:
request: Request = _request request: Request = _request
json = None json = await request.get_json() if request.is_json else None
if request.is_json:
json = await request.get_json()
data = await request.form data = await request.form
files_dict = await request.files files_dict = await request.files
@@ -223,7 +236,8 @@ class Driver(ReverseDriver):
) )
async def _handle_ws(self, setup: WebSocketServerSetup) -> None: async def _handle_ws(self, setup: WebSocketServerSetup) -> None:
websocket: QuartWebSocket = _websocket ctx = cast(WebsocketContext, websocket_ctx.copy())
websocket = websocket_ctx.websocket
http_request = BaseRequest( http_request = BaseRequest(
websocket.method, websocket.method,
@@ -233,7 +247,7 @@ class Driver(ReverseDriver):
version=websocket.http_version, version=websocket.http_version,
) )
ws = WebSocket(request=http_request, websocket=websocket) ws = WebSocket(request=http_request, websocket_ctx=ctx)
await setup.handle_func(ws) await setup.handle_func(ws)
@@ -241,30 +255,34 @@ class Driver(ReverseDriver):
class WebSocket(BaseWebSocket): class WebSocket(BaseWebSocket):
"""Quart WebSocket Wrapper""" """Quart WebSocket Wrapper"""
def __init__(self, *, request: BaseRequest, websocket: QuartWebSocket): def __init__(self, *, request: BaseRequest, websocket_ctx: WebsocketContext):
super().__init__(request=request) super().__init__(request=request)
self.websocket = websocket self.websocket_ctx = websocket_ctx
@property @property
@overrides(BaseWebSocket) def websocket(self) -> QuartWebSocket:
return self.websocket_ctx.websocket
@property
@override
def closed(self): def closed(self):
# FIXME # FIXME
return True return True
@overrides(BaseWebSocket) @override
async def accept(self): async def accept(self):
await self.websocket.accept() await self.websocket.accept()
@overrides(BaseWebSocket) @override
async def close(self, code: int = 1000, reason: str = ""): async def close(self, code: int = 1000, reason: str = ""):
await self.websocket.close(code, reason) await self.websocket.close(code, reason)
@overrides(BaseWebSocket) @override
@catch_closed @catch_closed
async def receive(self) -> Union[str, bytes]: async def receive(self) -> Union[str, bytes]:
return await self.websocket.receive() return await self.websocket.receive()
@overrides(BaseWebSocket) @override
@catch_closed @catch_closed
async def receive_text(self) -> str: async def receive_text(self) -> str:
msg = await self.websocket.receive() msg = await self.websocket.receive()
@@ -272,7 +290,7 @@ class WebSocket(BaseWebSocket):
raise TypeError("WebSocket received unexpected frame type: bytes") raise TypeError("WebSocket received unexpected frame type: bytes")
return msg return msg
@overrides(BaseWebSocket) @override
@catch_closed @catch_closed
async def receive_bytes(self) -> bytes: async def receive_bytes(self) -> bytes:
msg = await self.websocket.receive() msg = await self.websocket.receive()
@@ -280,11 +298,11 @@ class WebSocket(BaseWebSocket):
raise TypeError("WebSocket received unexpected frame type: str") raise TypeError("WebSocket received unexpected frame type: str")
return msg return msg
@overrides(BaseWebSocket) @override
async def send_text(self, data: str): async def send_text(self, data: str):
await self.websocket.send(data) await self.websocket.send(data)
@overrides(BaseWebSocket) @override
async def send_bytes(self, data: bytes): async def send_bytes(self, data: bytes):
await self.websocket.send(data) await self.websocket.send(data)

View File

@@ -14,34 +14,39 @@ FrontMatter:
sidebar_position: 4 sidebar_position: 4
description: nonebot.drivers.websockets 模块 description: nonebot.drivers.websockets 模块
""" """
import logging import logging
from functools import wraps from functools import wraps
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from typing import Type, Union, AsyncGenerator from typing_extensions import ParamSpec, override
from typing import TYPE_CHECKING, Union, TypeVar, Callable, Awaitable, AsyncGenerator
from nonebot.typing import overrides from nonebot.drivers import Request
from nonebot.log import LoguruHandler from nonebot.log import LoguruHandler
from nonebot.drivers import Request, Response
from nonebot.exception import WebSocketClosed from nonebot.exception import WebSocketClosed
from nonebot.drivers.none import Driver as NoneDriver from nonebot.drivers.none import Driver as NoneDriver
from nonebot.drivers import WebSocket as BaseWebSocket from nonebot.drivers import WebSocket as BaseWebSocket
from nonebot.drivers import ForwardMixin, ForwardDriver, combine_driver from nonebot.drivers import WebSocketClientMixin, combine_driver
try: try:
from websockets.exceptions import ConnectionClosed from websockets.exceptions import ConnectionClosed
from websockets.legacy.client import Connect, WebSocketClientProtocol from websockets.legacy.client import Connect, WebSocketClientProtocol
except ModuleNotFoundError as e: # pragma: no cover except ModuleNotFoundError as e: # pragma: no cover
raise ImportError( raise ImportError(
"Please install websockets by using `pip install nonebot2[websockets]`" "Please install websockets first to use this driver. "
"Install with pip: `pip install nonebot2[websockets]`"
) from e ) from e
T = TypeVar("T")
P = ParamSpec("P")
logger = logging.Logger("websockets.client", "INFO") logger = logging.Logger("websockets.client", "INFO")
logger.addHandler(LoguruHandler()) logger.addHandler(LoguruHandler())
def catch_closed(func): def catch_closed(func: Callable[P, Awaitable[T]]) -> Callable[P, Awaitable[T]]:
@wraps(func) @wraps(func)
async def decorator(*args, **kwargs): async def decorator(*args: P.args, **kwargs: P.kwargs) -> T:
try: try:
return await func(*args, **kwargs) return await func(*args, **kwargs)
except ConnectionClosed as e: except ConnectionClosed as e:
@@ -53,19 +58,15 @@ def catch_closed(func):
return decorator return decorator
class Mixin(ForwardMixin): class Mixin(WebSocketClientMixin):
"""Websockets Mixin""" """Websockets Mixin"""
@property @property
@overrides(ForwardMixin) @override
def type(self) -> str: def type(self) -> str:
return "websockets" return "websockets"
@overrides(ForwardMixin) @override
async def request(self, setup: Request) -> Response:
return await super(Mixin, self).request(setup)
@overrides(ForwardMixin)
@asynccontextmanager @asynccontextmanager
async def websocket(self, setup: Request) -> AsyncGenerator["WebSocket", None]: async def websocket(self, setup: Request) -> AsyncGenerator["WebSocket", None]:
connection = Connect( connection = Connect(
@@ -80,30 +81,30 @@ class Mixin(ForwardMixin):
class WebSocket(BaseWebSocket): class WebSocket(BaseWebSocket):
"""Websockets WebSocket Wrapper""" """Websockets WebSocket Wrapper"""
@overrides(BaseWebSocket) @override
def __init__(self, *, request: Request, websocket: WebSocketClientProtocol): def __init__(self, *, request: Request, websocket: WebSocketClientProtocol):
super().__init__(request=request) super().__init__(request=request)
self.websocket = websocket self.websocket = websocket
@property @property
@overrides(BaseWebSocket) @override
def closed(self) -> bool: def closed(self) -> bool:
return self.websocket.closed return self.websocket.closed
@overrides(BaseWebSocket) @override
async def accept(self): async def accept(self):
raise NotImplementedError raise NotImplementedError
@overrides(BaseWebSocket) @override
async def close(self, code: int = 1000, reason: str = ""): async def close(self, code: int = 1000, reason: str = ""):
await self.websocket.close(code, reason) await self.websocket.close(code, reason)
@overrides(BaseWebSocket) @override
@catch_closed @catch_closed
async def receive(self) -> Union[str, bytes]: async def receive(self) -> Union[str, bytes]:
return await self.websocket.recv() return await self.websocket.recv()
@overrides(BaseWebSocket) @override
@catch_closed @catch_closed
async def receive_text(self) -> str: async def receive_text(self) -> str:
msg = await self.websocket.recv() msg = await self.websocket.recv()
@@ -111,7 +112,7 @@ class WebSocket(BaseWebSocket):
raise TypeError("WebSocket received unexpected frame type: bytes") raise TypeError("WebSocket received unexpected frame type: bytes")
return msg return msg
@overrides(BaseWebSocket) @override
@catch_closed @catch_closed
async def receive_bytes(self) -> bytes: async def receive_bytes(self) -> bytes:
msg = await self.websocket.recv() msg = await self.websocket.recv()
@@ -119,14 +120,20 @@ class WebSocket(BaseWebSocket):
raise TypeError("WebSocket received unexpected frame type: str") raise TypeError("WebSocket received unexpected frame type: str")
return msg return msg
@overrides(BaseWebSocket) @override
async def send_text(self, data: str) -> None: async def send_text(self, data: str) -> None:
await self.websocket.send(data) await self.websocket.send(data)
@overrides(BaseWebSocket) @override
async def send_bytes(self, data: bytes) -> None: async def send_bytes(self, data: bytes) -> None:
await self.websocket.send(data) await self.websocket.send(data)
Driver: Type[ForwardDriver] = combine_driver(NoneDriver, Mixin) # type: ignore if TYPE_CHECKING:
"""Websockets Driver"""
class Driver(Mixin, NoneDriver):
...
else:
Driver = combine_driver(NoneDriver, Mixin)
"""Websockets Driver"""

View File

@@ -43,9 +43,9 @@ class NoneBotException(Exception):
# Rule Exception # Rule Exception
class ParserExit(NoneBotException): class ParserExit(NoneBotException):
"""{ref}`nonebot.rule.shell_command` 处理消息失败时返回的异常""" """{ref}`nonebot.rule.shell_command` 处理消息失败时返回的异常"""
def __init__(self, status: int = 0, message: Optional[str] = None): def __init__(self, status: int = 0, message: Optional[str] = None) -> None:
self.status = status self.status = status
self.message = message self.message = message
@@ -69,7 +69,7 @@ class IgnoredException(ProcessException):
reason: 忽略事件的原因 reason: 忽略事件的原因
""" """
def __init__(self, reason: Any): def __init__(self, reason: Any) -> None:
self.reason: Any = reason self.reason: Any = reason
def __repr__(self) -> str: def __repr__(self) -> str:
@@ -96,7 +96,7 @@ class SkippedException(ProcessException):
class TypeMisMatch(SkippedException): class TypeMisMatch(SkippedException):
"""当前 `Handler` 的参数类型不匹配。""" """当前 `Handler` 的参数类型不匹配。"""
def __init__(self, param: ModelField, value: Any): def __init__(self, param: ModelField, value: Any) -> None:
self.param: ModelField = param self.param: ModelField = param
self.value: Any = value self.value: Any = value
@@ -108,7 +108,8 @@ class TypeMisMatch(SkippedException):
class MockApiException(ProcessException): class MockApiException(ProcessException):
"""指示 NoneBot 阻止本次 API 调用或修改本次调用返回值,并返回自定义内容。可由 api hook 抛出。 """指示 NoneBot 阻止本次 API 调用或修改本次调用返回值,并返回自定义内容。
可由 api hook 抛出。
参数: 参数:
result: 返回的内容 result: 返回的内容
@@ -144,7 +145,8 @@ class MatcherException(NoneBotException):
class PausedException(MatcherException): class PausedException(MatcherException):
"""指示 NoneBot 结束当前 `Handler` 并等待下一条消息后继续下一个 `Handler`。可用于用户输入新信息。 """指示 NoneBot 结束当前 `Handler` 并等待下一条消息后继续下一个 `Handler`。
可用于用户输入新信息。
可以在 `Handler` 中通过 {ref}`nonebot.matcher.Matcher.pause` 抛出。 可以在 `Handler` 中通过 {ref}`nonebot.matcher.Matcher.pause` 抛出。
@@ -158,7 +160,8 @@ class PausedException(MatcherException):
class RejectedException(MatcherException): class RejectedException(MatcherException):
"""指示 NoneBot 结束当前 `Handler` 并等待下一条消息后重新运行当前 `Handler`。可用于用户重新输入。 """指示 NoneBot 结束当前 `Handler` 并等待下一条消息后重新运行当前 `Handler`。
可用于用户重新输入。
可以在 `Handler` 中通过 {ref}`nonebot.matcher.Matcher.reject` 抛出。 可以在 `Handler` 中通过 {ref}`nonebot.matcher.Matcher.reject` 抛出。
@@ -187,7 +190,7 @@ class FinishedException(MatcherException):
# Adapter Exceptions # Adapter Exceptions
class AdapterException(NoneBotException): class AdapterException(NoneBotException):
"""代表 `Adapter` 抛出的异常,所有的 `Adapter` 都要在内部继承自这个 `Exception` """代表 `Adapter` 抛出的异常,所有的 `Adapter` 都要在内部继承自这个 `Exception`
参数: 参数:
adapter_name: 标识 adapter adapter_name: 标识 adapter
@@ -210,7 +213,9 @@ class ApiNotAvailable(AdapterException):
class NetworkError(AdapterException): class NetworkError(AdapterException):
"""在网络出现问题时抛出,如: API 请求地址不正确, API 请求无返回或返回状态非正常等。""" """在网络出现问题时抛出,
如: API 请求地址不正确, API 请求无返回或返回状态非正常等。
"""
class ActionFailed(AdapterException): class ActionFailed(AdapterException):
@@ -219,13 +224,13 @@ class ActionFailed(AdapterException):
# Driver Exceptions # Driver Exceptions
class DriverException(NoneBotException): class DriverException(NoneBotException):
"""`Driver` 抛出的异常基类""" """`Driver` 抛出的异常基类"""
class WebSocketClosed(DriverException): class WebSocketClosed(DriverException):
"""WebSocket 连接已关闭""" """WebSocket 连接已关闭"""
def __init__(self, code: int, reason: Optional[str] = None): def __init__(self, code: int, reason: Optional[str] = None) -> None:
self.code = code self.code = code
self.reason = reason self.reason = reason

View File

@@ -7,10 +7,11 @@ from nonebot.internal.driver import (
Driver, Driver,
Request, Request,
Response, Response,
ASGIMixin,
WebSocket, WebSocket,
ForwardDriver, HTTPClientMixin,
ReverseDriver,
HTTPServerSetup, HTTPServerSetup,
WebSocketClientMixin,
WebSocketServerSetup, WebSocketServerSetup,
) )
@@ -72,26 +73,26 @@ class Adapter(abc.ABC):
def setup_http_server(self, setup: HTTPServerSetup): def setup_http_server(self, setup: HTTPServerSetup):
"""设置一个 HTTP 服务器路由配置""" """设置一个 HTTP 服务器路由配置"""
if not isinstance(self.driver, ReverseDriver): if not isinstance(self.driver, ASGIMixin):
raise TypeError("Current driver does not support http server") raise TypeError("Current driver does not support http server")
self.driver.setup_http_server(setup) self.driver.setup_http_server(setup)
def setup_websocket_server(self, setup: WebSocketServerSetup): def setup_websocket_server(self, setup: WebSocketServerSetup):
"""设置一个 WebSocket 服务器路由配置""" """设置一个 WebSocket 服务器路由配置"""
if not isinstance(self.driver, ReverseDriver): if not isinstance(self.driver, ASGIMixin):
raise TypeError("Current driver does not support websocket server") raise TypeError("Current driver does not support websocket server")
self.driver.setup_websocket_server(setup) self.driver.setup_websocket_server(setup)
async def request(self, setup: Request) -> Response: async def request(self, setup: Request) -> Response:
"""进行一个 HTTP 客户端请求""" """进行一个 HTTP 客户端请求"""
if not isinstance(self.driver, ForwardDriver): if not isinstance(self.driver, HTTPClientMixin):
raise TypeError("Current driver does not support http client") raise TypeError("Current driver does not support http client")
return await self.driver.request(setup) return await self.driver.request(setup)
@asynccontextmanager @asynccontextmanager
async def websocket(self, setup: Request) -> AsyncGenerator[WebSocket, None]: async def websocket(self, setup: Request) -> AsyncGenerator[WebSocket, None]:
"""建立一个 WebSocket 客户端连接请求""" """建立一个 WebSocket 客户端连接请求"""
if not isinstance(self.driver, ForwardDriver): if not isinstance(self.driver, WebSocketClientMixin):
raise TypeError("Current driver does not support websocket client") raise TypeError("Current driver does not support websocket client")
async with self.driver.websocket(setup) as ws: async with self.driver.websocket(setup) as ws:
yield ws yield ws

View File

@@ -106,7 +106,10 @@ class Bot(abc.ABC):
logger.debug("Running CalledAPI hooks...") logger.debug("Running CalledAPI hooks...")
await asyncio.gather(*coros) await asyncio.gather(*coros)
except MockApiException as e: except MockApiException as e:
# mock api result
result = e.result result = e.result
# ignore exception
exception = None
logger.debug( logger.debug(
f"Calling API {api} result is mocked. Return {result} instead." f"Calling API {api} result is mocked. Return {result} instead."
) )

View File

@@ -44,10 +44,11 @@ class Event(abc.ABC, BaseModel):
def get_log_string(self) -> str: def get_log_string(self) -> str:
"""获取事件日志信息的方法。 """获取事件日志信息的方法。
通常你不需要修改这个方法,只有当希望 NoneBot 隐藏该事件日志时,可以抛出 `NoLogException` 异常。 通常你不需要修改这个方法,只有当希望 NoneBot 隐藏该事件日志时,
可以抛出 `NoLogException` 异常。
异常: 异常:
NoLogException: NoLogException: 希望 NoneBot 隐藏该事件日志
""" """
return f"[{self.get_event_name()}]: {self.get_event_description()}" return f"[{self.get_event_name()}]: {self.get_event_description()}"
@@ -58,7 +59,9 @@ class Event(abc.ABC, BaseModel):
@abc.abstractmethod @abc.abstractmethod
def get_session_id(self) -> str: def get_session_id(self) -> str:
"""获取会话 id 的方法,用于判断当前事件属于哪一个会话,通常是用户 id、群组 id 组合。""" """获取会话 id 的方法,用于判断当前事件属于哪一个会话,
通常是用户 id、群组 id 组合。
"""
raise NotImplementedError raise NotImplementedError
@abc.abstractmethod @abc.abstractmethod

View File

@@ -98,7 +98,7 @@ class MessageSegment(abc.ABC, Generic[TM]):
class Message(List[TMS], abc.ABC): class Message(List[TMS], abc.ABC):
"""消息数组 """消息序列
参数: 参数:
message: 消息内容 message: 消息内容
@@ -124,9 +124,9 @@ class Message(List[TMS], abc.ABC):
def template(cls, format_string: Union[str, TM]) -> MessageTemplate[Self]: def template(cls, format_string: Union[str, TM]) -> MessageTemplate[Self]:
"""创建消息模板。 """创建消息模板。
用法和 `str.format` 大致相同, 但是可以输出消息对象, 并且支持以 `Message` 对象作为消息模板 用法和 `str.format` 大致相同支持以 `Message` 对象作为消息模板并输出消息对象。
并且提供了拓展的格式化控制符,
并且提供了拓展的格式化控制符, 可以用适用于该消息类型的 `MessageSegment` 工厂方法创建消息 可以通过该消息类型的 `MessageSegment` 工厂方法创建消息
参数: 参数:
format_string: 格式化模板 format_string: 格式化模板

View File

@@ -1,5 +1,6 @@
import functools import functools
from string import Formatter from string import Formatter
from typing_extensions import TypeAlias
from typing import ( from typing import (
TYPE_CHECKING, TYPE_CHECKING,
Any, Any,
@@ -25,7 +26,7 @@ if TYPE_CHECKING:
TM = TypeVar("TM", bound="Message") TM = TypeVar("TM", bound="Message")
TF = TypeVar("TF", str, "Message") TF = TypeVar("TF", str, "Message")
FormatSpecFunc = Callable[[Any], str] FormatSpecFunc: TypeAlias = Callable[[Any], str]
FormatSpecFunc_T = TypeVar("FormatSpecFunc_T", bound=FormatSpecFunc) FormatSpecFunc_T = TypeVar("FormatSpecFunc_T", bound=FormatSpecFunc)

View File

@@ -1,8 +1,9 @@
from .model import URL as URL from .model import URL as URL
from .model import RawURL as RawURL from .model import RawURL as RawURL
from .driver import Driver as Driver from .abstract import Mixin as Mixin
from .model import Cookies as Cookies from .model import Cookies as Cookies
from .model import Request as Request from .model import Request as Request
from .abstract import Driver as Driver
from .model import FileType as FileType from .model import FileType as FileType
from .model import Response as Response from .model import Response as Response
from .model import DataTypes as DataTypes from .model import DataTypes as DataTypes
@@ -10,16 +11,20 @@ from .model import FileTypes as FileTypes
from .model import WebSocket as WebSocket from .model import WebSocket as WebSocket
from .model import FilesTypes as FilesTypes from .model import FilesTypes as FilesTypes
from .model import QueryTypes as QueryTypes from .model import QueryTypes as QueryTypes
from .abstract import ASGIMixin as ASGIMixin
from .model import CookieTypes as CookieTypes from .model import CookieTypes as CookieTypes
from .model import FileContent as FileContent from .model import FileContent as FileContent
from .model import HTTPVersion as HTTPVersion from .model import HTTPVersion as HTTPVersion
from .model import HeaderTypes as HeaderTypes from .model import HeaderTypes as HeaderTypes
from .model import SimpleQuery as SimpleQuery from .model import SimpleQuery as SimpleQuery
from .model import ContentTypes as ContentTypes from .model import ContentTypes as ContentTypes
from .driver import ForwardMixin as ForwardMixin
from .model import QueryVariable as QueryVariable from .model import QueryVariable as QueryVariable
from .driver import ForwardDriver as ForwardDriver from .abstract import ForwardMixin as ForwardMixin
from .driver import ReverseDriver as ReverseDriver from .abstract import ReverseMixin as ReverseMixin
from .driver import combine_driver as combine_driver from .abstract import ForwardDriver as ForwardDriver
from .abstract import ReverseDriver as ReverseDriver
from .combine import combine_driver as combine_driver
from .model import HTTPServerSetup as HTTPServerSetup from .model import HTTPServerSetup as HTTPServerSetup
from .abstract import HTTPClientMixin as HTTPClientMixin
from .model import WebSocketServerSetup as WebSocketServerSetup from .model import WebSocketServerSetup as WebSocketServerSetup
from .abstract import WebSocketClientMixin as WebSocketClientMixin

View File

@@ -1,5 +1,6 @@
import abc import abc
import asyncio import asyncio
from typing_extensions import TypeAlias
from contextlib import AsyncExitStack, asynccontextmanager from contextlib import AsyncExitStack, asynccontextmanager
from typing import TYPE_CHECKING, Any, Set, Dict, Type, Callable, AsyncGenerator from typing import TYPE_CHECKING, Any, Set, Dict, Type, Callable, AsyncGenerator
@@ -25,7 +26,9 @@ BOT_HOOK_PARAMS = [DependParam, BotParam, DefaultParam]
class Driver(abc.ABC): class Driver(abc.ABC):
"""Driver 基类。 """驱动器基类。
驱动器控制框架的启动和停止适配器的注册以及机器人生命周期管理
参数: 参数:
env: 包含环境信息的 Env 对象 env: 包含环境信息的 Env 对象
@@ -45,6 +48,7 @@ class Driver(abc.ABC):
self.config: Config = config self.config: Config = config
"""全局配置对象""" """全局配置对象"""
self._bots: Dict[str, "Bot"] = {} self._bots: Dict[str, "Bot"] = {}
self._bot_tasks: Set[asyncio.Task] = set()
def __repr__(self) -> str: def __repr__(self) -> str:
return ( return (
@@ -89,13 +93,13 @@ class Driver(abc.ABC):
@abc.abstractmethod @abc.abstractmethod
def run(self, *args, **kwargs): def run(self, *args, **kwargs):
""" """启动驱动框架"""
启动驱动框架
"""
logger.opt(colors=True).debug( logger.opt(colors=True).debug(
f"<g>Loaded adapters: {escape_tag(', '.join(self._adapters))}</g>" f"<g>Loaded adapters: {escape_tag(', '.join(self._adapters))}</g>"
) )
self.on_shutdown(self._cleanup)
@abc.abstractmethod @abc.abstractmethod
def on_startup(self, func: Callable) -> Callable: def on_startup(self, func: Callable) -> Callable:
"""注册一个在驱动器启动时执行的函数""" """注册一个在驱动器启动时执行的函数"""
@@ -152,11 +156,15 @@ class Driver(abc.ABC):
await asyncio.gather(*coros) await asyncio.gather(*coros)
except Exception as e: except Exception as e:
logger.opt(colors=True, exception=e).error( logger.opt(colors=True, exception=e).error(
"<r><bg #f8bbd0>Error when running WebSocketConnection hook. " "<r><bg #f8bbd0>"
"Running cancelled!</bg #f8bbd0></r>" "Error when running WebSocketConnection hook. "
"Running cancelled!"
"</bg #f8bbd0></r>"
) )
asyncio.create_task(_run_hook(bot)) task = asyncio.create_task(_run_hook(bot))
task.add_done_callback(self._bot_tasks.discard)
self._bot_tasks.add(task)
def _bot_disconnect(self, bot: "Bot") -> None: def _bot_disconnect(self, bot: "Bot") -> None:
"""在连接断开后,调用该函数来注销 bot 对象""" """在连接断开后,调用该函数来注销 bot 对象"""
@@ -177,27 +185,55 @@ class Driver(abc.ABC):
await asyncio.gather(*coros) await asyncio.gather(*coros)
except Exception as e: except Exception as e:
logger.opt(colors=True, exception=e).error( logger.opt(colors=True, exception=e).error(
"<r><bg #f8bbd0>Error when running WebSocketDisConnection hook. " "<r><bg #f8bbd0>"
"Running cancelled!</bg #f8bbd0></r>" "Error when running WebSocketDisConnection hook. "
"Running cancelled!"
"</bg #f8bbd0></r>"
) )
asyncio.create_task(_run_hook(bot)) task = asyncio.create_task(_run_hook(bot))
task.add_done_callback(self._bot_tasks.discard)
self._bot_tasks.add(task)
async def _cleanup(self) -> None:
"""清理驱动器资源"""
if self._bot_tasks:
logger.opt(colors=True).debug(
"<y>Waiting for running bot connection hooks...</y>"
)
await asyncio.gather(*self._bot_tasks, return_exceptions=True)
class ForwardMixin(abc.ABC): class Mixin(abc.ABC):
"""客户端混入基类。""" """可与其他驱动器共用的混入基类。"""
@property @property
@abc.abstractmethod @abc.abstractmethod
def type(self) -> str: def type(self) -> str:
"""客户端驱动类型名称""" """混入驱动类型名称"""
raise NotImplementedError raise NotImplementedError
class ForwardMixin(Mixin):
"""客户端混入基类。"""
class ReverseMixin(Mixin):
"""服务端混入基类。"""
class HTTPClientMixin(ForwardMixin):
"""HTTP 客户端混入基类。"""
@abc.abstractmethod @abc.abstractmethod
async def request(self, setup: Request) -> Response: async def request(self, setup: Request) -> Response:
"""发送一个 HTTP 请求""" """发送一个 HTTP 请求"""
raise NotImplementedError raise NotImplementedError
class WebSocketClientMixin(ForwardMixin):
"""WebSocket 客户端混入基类。"""
@abc.abstractmethod @abc.abstractmethod
@asynccontextmanager @asynccontextmanager
async def websocket(self, setup: Request) -> AsyncGenerator[WebSocket, None]: async def websocket(self, setup: Request) -> AsyncGenerator[WebSocket, None]:
@@ -206,12 +242,11 @@ class ForwardMixin(abc.ABC):
yield # used for static type checking's generator detection yield # used for static type checking's generator detection
class ForwardDriver(Driver, ForwardMixin): class ASGIMixin(ReverseMixin):
"""客户端基类。将客户端框架封装,以满足适配器使用。""" """ASGI 服务端基类。
将后端框架封装以满足适配器使用
class ReverseDriver(Driver): """
"""服务端基类。将后端框架封装,以满足适配器使用。"""
@property @property
@abc.abstractmethod @abc.abstractmethod
@@ -236,22 +271,14 @@ class ReverseDriver(Driver):
raise NotImplementedError raise NotImplementedError
def combine_driver(driver: Type[Driver], *mixins: Type[ForwardMixin]) -> Type[Driver]: ForwardDriver: TypeAlias = ForwardMixin
"""将一个驱动器和多个混入类合并。""" """支持客户端请求的驱动器。
# check first
assert issubclass(driver, Driver), "`driver` must be subclass of Driver"
assert all(
map(lambda m: issubclass(m, ForwardMixin), mixins)
), "`mixins` must be subclass of ForwardMixin"
if not mixins: **Deprecated**请使用 {ref}`nonebot.drivers.ForwardMixin` 或其子类代替
return driver """
def type_(self: ForwardDriver) -> str: ReverseDriver: TypeAlias = ReverseMixin
return ( """支持服务端请求的驱动器。
driver.type.__get__(self)
+ "+"
+ "+".join(map(lambda x: x.type.__get__(self), mixins))
)
return type("CombinedDriver", (*mixins, driver, ForwardDriver), {"type": property(type_)}) # type: ignore **Deprecated**请使用 {ref}`nonebot.drivers.ReverseMixin` 或其子类代替
"""

View File

@@ -0,0 +1,45 @@
from typing import TYPE_CHECKING, Type, Union, TypeVar, overload
from .abstract import Mixin, Driver
D = TypeVar("D", bound="Driver")
if TYPE_CHECKING:
class CombinedDriver(Driver, Mixin):
...
@overload
def combine_driver(driver: Type[D]) -> Type[D]:
...
@overload
def combine_driver(driver: Type[D], *mixins: Type[Mixin]) -> Type["CombinedDriver"]:
...
def combine_driver(
driver: Type[D], *mixins: Type[Mixin]
) -> Union[Type[D], Type["CombinedDriver"]]:
"""将一个驱动器和多个混入类合并。"""
# check first
if not issubclass(driver, Driver):
raise TypeError("`driver` must be subclass of Driver")
if not all(issubclass(m, Mixin) for m in mixins):
raise TypeError("`mixins` must be subclass of Mixin")
if not mixins:
return driver
def type_(self: "CombinedDriver") -> str:
return (
driver.type.__get__(self)
+ "+"
+ "+".join(x.type.__get__(self) for x in mixins)
)
return type(
"CombinedDriver", (*mixins, driver), {"type": property(type_)}
) # type: ignore

View File

@@ -2,6 +2,7 @@ import abc
import urllib.request import urllib.request
from enum import Enum from enum import Enum
from dataclasses import dataclass from dataclasses import dataclass
from typing_extensions import TypeAlias
from http.cookiejar import Cookie, CookieJar from http.cookiejar import Cookie, CookieJar
from typing import ( from typing import (
IO, IO,
@@ -21,28 +22,30 @@ from typing import (
from yarl import URL as URL from yarl import URL as URL
from multidict import CIMultiDict from multidict import CIMultiDict
RawURL = Tuple[bytes, bytes, Optional[int], bytes] RawURL: TypeAlias = Tuple[bytes, bytes, Optional[int], bytes]
SimpleQuery = Union[str, int, float] SimpleQuery: TypeAlias = Union[str, int, float]
QueryVariable = Union[SimpleQuery, List[SimpleQuery]] QueryVariable: TypeAlias = Union[SimpleQuery, List[SimpleQuery]]
QueryTypes = Union[ QueryTypes: TypeAlias = Union[
None, str, Mapping[str, QueryVariable], List[Tuple[str, QueryVariable]] None, str, Mapping[str, QueryVariable], List[Tuple[str, QueryVariable]]
] ]
HeaderTypes = Union[ HeaderTypes: TypeAlias = Union[
None, None,
CIMultiDict[str], CIMultiDict[str],
Dict[str, str], Dict[str, str],
List[Tuple[str, str]], List[Tuple[str, str]],
] ]
CookieTypes = Union[None, "Cookies", CookieJar, Dict[str, str], List[Tuple[str, str]]] CookieTypes: TypeAlias = Union[
None, "Cookies", CookieJar, Dict[str, str], List[Tuple[str, str]]
]
ContentTypes = Union[str, bytes, None] ContentTypes: TypeAlias = Union[str, bytes, None]
DataTypes = Union[dict, None] DataTypes: TypeAlias = Union[dict, None]
FileContent = Union[IO[bytes], bytes] FileContent: TypeAlias = Union[IO[bytes], bytes]
FileType = Tuple[Optional[str], FileContent, Optional[str]] FileType: TypeAlias = Tuple[Optional[str], FileContent, Optional[str]]
FileTypes = Union[ FileTypes: TypeAlias = Union[
# file (or bytes) # file (or bytes)
FileContent, FileContent,
# (filename, file (or bytes)) # (filename, file (or bytes))
@@ -50,7 +53,7 @@ FileTypes = Union[
# (filename, file (or bytes), content_type) # (filename, file (or bytes), content_type)
FileType, FileType,
] ]
FilesTypes = Union[Dict[str, FileTypes], List[Tuple[str, FileTypes]], None] FilesTypes: TypeAlias = Union[Dict[str, FileTypes], List[Tuple[str, FileTypes]], None]
class HTTPVersion(Enum): class HTTPVersion(Enum):
@@ -122,7 +125,7 @@ class Request:
files_ = files.items() if isinstance(files, dict) else files files_ = files.items() if isinstance(files, dict) else files
for name, file_info in files_: for name, file_info in files_:
if not isinstance(file_info, tuple): if not isinstance(file_info, tuple):
self.files.append((name, (None, file_info, None))) self.files.append((name, (name, file_info, None)))
elif len(file_info) == 2: elif len(file_info) == 2:
self.files.append((name, (file_info[0], file_info[1], None))) self.files.append((name, (file_info[0], file_info[1], None)))
else: else:
@@ -160,7 +163,6 @@ class Response:
class WebSocket(abc.ABC): class WebSocket(abc.ABC):
def __init__(self, *, request: Request): def __init__(self, *, request: Request):
# request
self.request: Request = request self.request: Request = request
def __repr__(self) -> str: def __repr__(self) -> str:
@@ -169,9 +171,7 @@ class WebSocket(abc.ABC):
@property @property
@abc.abstractmethod @abc.abstractmethod
def closed(self) -> bool: def closed(self) -> bool:
""" """连接是否已经关闭"""
连接是否已经关闭
"""
raise NotImplementedError raise NotImplementedError
@abc.abstractmethod @abc.abstractmethod

View File

@@ -6,6 +6,7 @@ matchers = MatcherManager()
from .matcher import Matcher as Matcher from .matcher import Matcher as Matcher
from .matcher import current_bot as current_bot from .matcher import current_bot as current_bot
from .matcher import MatcherSource as MatcherSource
from .matcher import current_event as current_event from .matcher import current_event as current_event
from .matcher import current_handler as current_handler from .matcher import current_handler as current_handler
from .matcher import current_matcher as current_matcher from .matcher import current_matcher as current_matcher

View File

@@ -1,6 +1,5 @@
from typing import ( from typing import (
TYPE_CHECKING, TYPE_CHECKING,
Any,
List, List,
Type, Type,
Tuple, Tuple,
@@ -53,7 +52,7 @@ class MatcherManager(MutableMapping[int, List[Type["Matcher"]]]):
def __delitem__(self, key: int) -> None: def __delitem__(self, key: int) -> None:
del self.provider[key] del self.provider[key]
def __eq__(self, other: Any) -> bool: def __eq__(self, other: object) -> bool:
return isinstance(other, MatcherManager) and self.provider == other.provider return isinstance(other, MatcherManager) and self.provider == other.provider
def keys(self) -> KeysView[int]: def keys(self) -> KeysView[int]:

View File

@@ -1,4 +1,9 @@
import sys
import inspect
import warnings
from pathlib import Path
from types import ModuleType from types import ModuleType
from dataclasses import dataclass
from contextvars import ContextVar from contextvars import ContextVar
from typing_extensions import Self from typing_extensions import Self
from datetime import datetime, timedelta from datetime import datetime, timedelta
@@ -8,6 +13,7 @@ from typing import (
Any, Any,
List, List,
Type, Type,
Tuple,
Union, Union,
TypeVar, TypeVar,
Callable, Callable,
@@ -20,7 +26,8 @@ from typing import (
from nonebot.log import logger from nonebot.log import logger
from nonebot.internal.rule import Rule from nonebot.internal.rule import Rule
from nonebot.dependencies import Dependent from nonebot.utils import classproperty
from nonebot.dependencies import Param, Dependent
from nonebot.internal.permission import User, Permission from nonebot.internal.permission import User, Permission
from nonebot.internal.adapter import ( from nonebot.internal.adapter import (
Bot, Bot,
@@ -29,6 +36,13 @@ from nonebot.internal.adapter import (
MessageSegment, MessageSegment,
MessageTemplate, MessageTemplate,
) )
from nonebot.typing import (
T_State,
T_Handler,
T_TypeUpdater,
T_DependencyCache,
T_PermissionUpdater,
)
from nonebot.consts import ( from nonebot.consts import (
ARG_KEY, ARG_KEY,
RECEIVE_KEY, RECEIVE_KEY,
@@ -36,14 +50,6 @@ from nonebot.consts import (
LAST_RECEIVE_KEY, LAST_RECEIVE_KEY,
REJECT_CACHE_TARGET, REJECT_CACHE_TARGET,
) )
from nonebot.typing import (
Any,
T_State,
T_Handler,
T_TypeUpdater,
T_DependencyCache,
T_PermissionUpdater,
)
from nonebot.exception import ( from nonebot.exception import (
PausedException, PausedException,
StopPropagation, StopPropagation,
@@ -75,15 +81,51 @@ current_matcher: ContextVar["Matcher"] = ContextVar("current_matcher")
current_handler: ContextVar[Dependent] = ContextVar("current_handler") current_handler: ContextVar[Dependent] = ContextVar("current_handler")
@dataclass
class MatcherSource:
"""Matcher 源代码上下文信息"""
plugin_name: Optional[str] = None
"""事件响应器所在插件名称"""
module_name: Optional[str] = None
"""事件响应器所在插件模块的路径名"""
lineno: Optional[int] = None
"""事件响应器所在行号"""
@property
def plugin(self) -> Optional["Plugin"]:
"""事件响应器所在插件"""
from nonebot.plugin import get_plugin
if self.plugin_name is not None:
return get_plugin(self.plugin_name)
@property
def module(self) -> Optional[ModuleType]:
if self.module_name is not None:
return sys.modules.get(self.module_name)
@property
def file(self) -> Optional[Path]:
if self.module is not None and (file := inspect.getsourcefile(self.module)):
return Path(file).absolute()
class MatcherMeta(type): class MatcherMeta(type):
if TYPE_CHECKING: if TYPE_CHECKING:
module_name: Optional[str]
type: str type: str
_source: Optional[MatcherSource]
module_name: Optional[str]
def __repr__(self) -> str: def __repr__(self) -> str:
return ( return (
f"{self.__name__}(type={self.type!r}" f"{self.__name__}(type={self.type!r}"
+ (f", module={self.module_name}" if self.module_name else "") + (f", module={self.module_name}" if self.module_name else "")
+ (
f", lineno={self._source.lineno}"
if self._source and self._source.lineno is not None
else ""
)
+ ")" + ")"
) )
@@ -91,14 +133,7 @@ class MatcherMeta(type):
class Matcher(metaclass=MatcherMeta): class Matcher(metaclass=MatcherMeta):
"""事件响应器类""" """事件响应器类"""
plugin: ClassVar[Optional["Plugin"]] = None _source: ClassVar[Optional[MatcherSource]] = None
"""事件响应器所在插件"""
module: ClassVar[Optional[ModuleType]] = None
"""事件响应器所在插件模块"""
plugin_name: ClassVar[Optional[str]] = None
"""事件响应器所在插件名"""
module_name: ClassVar[Optional[str]] = None
"""事件响应器所在点分割插件模块路径"""
type: ClassVar[str] = "" type: ClassVar[str] = ""
"""事件响应器类型""" """事件响应器类型"""
@@ -125,7 +160,7 @@ class Matcher(metaclass=MatcherMeta):
_default_permission_updater: ClassVar[Optional[Dependent[Permission]]] = None _default_permission_updater: ClassVar[Optional[Dependent[Permission]]] = None
"""事件响应器权限更新函数""" """事件响应器权限更新函数"""
HANDLER_PARAM_TYPES = ( HANDLER_PARAM_TYPES: ClassVar[Tuple[Type[Param], ...]] = (
DependParam, DependParam,
BotParam, BotParam,
EventParam, EventParam,
@@ -143,6 +178,11 @@ class Matcher(metaclass=MatcherMeta):
return ( return (
f"{self.__class__.__name__}(type={self.type!r}" f"{self.__class__.__name__}(type={self.type!r}"
+ (f", module={self.module_name}" if self.module_name else "") + (f", module={self.module_name}" if self.module_name else "")
+ (
f", lineno={self._source.lineno}"
if self._source and self._source.lineno is not None
else ""
)
+ ")" + ")"
) )
@@ -159,6 +199,7 @@ class Matcher(metaclass=MatcherMeta):
*, *,
plugin: Optional["Plugin"] = None, plugin: Optional["Plugin"] = None,
module: Optional[ModuleType] = None, module: Optional[ModuleType] = None,
source: Optional[MatcherSource] = None,
expire_time: Optional[Union[datetime, timedelta]] = None, expire_time: Optional[Union[datetime, timedelta]] = None,
default_state: Optional[T_State] = None, default_state: Optional[T_State] = None,
default_type_updater: Optional[Union[T_TypeUpdater, Dependent[str]]] = None, default_type_updater: Optional[Union[T_TypeUpdater, Dependent[str]]] = None,
@@ -177,22 +218,47 @@ class Matcher(metaclass=MatcherMeta):
temp: 是否为临时事件响应器,即触发一次后删除 temp: 是否为临时事件响应器,即触发一次后删除
priority: 响应优先级 priority: 响应优先级
block: 是否阻止事件向更低优先级的响应器传播 block: 是否阻止事件向更低优先级的响应器传播
plugin: 事件响应器所在插件 plugin: **Deprecated.** 事件响应器所在插件
module: 事件响应器所在模块 module: **Deprecated.** 事件响应器所在模块
default_state: 默认状态 `state` source: 事件响应器源代码上下文信息
expire_time: 事件响应器最终有效时间点,过时即被删除 expire_time: 事件响应器最终有效时间点,过时即被删除
default_state: 默认状态 `state`
default_type_updater: 默认事件类型更新函数
default_permission_updater: 默认会话权限更新函数
返回: 返回:
Type[Matcher]: 新的事件响应器类 Type[Matcher]: 新的事件响应器类
""" """
if plugin is not None:
warnings.warn(
(
"Pass `plugin` context info to create Matcher is deprecated. "
"Use `source` instead."
),
DeprecationWarning,
)
if module is not None:
warnings.warn(
(
"Pass `module` context info to create Matcher is deprecated. "
"Use `source` instead."
),
DeprecationWarning,
)
source = source or (
MatcherSource(
plugin_name=plugin and plugin.name,
module_name=module and module.__name__,
)
if plugin is not None or module is not None
else None
)
NewMatcher = type( NewMatcher = type(
cls.__name__, cls.__name__,
(cls,), (cls,),
{ {
"plugin": plugin, "_source": source,
"module": module,
"plugin_name": plugin and plugin.name,
"module_name": module and module.__name__,
"type": type_, "type": type_,
"rule": rule or Rule(), "rule": rule or Rule(),
"permission": permission or Permission(), "permission": permission or Permission(),
@@ -254,6 +320,26 @@ class Matcher(metaclass=MatcherMeta):
"""销毁当前的事件响应器""" """销毁当前的事件响应器"""
matchers[cls.priority].remove(cls) matchers[cls.priority].remove(cls)
@classproperty
def plugin(cls) -> Optional["Plugin"]:
"""事件响应器所在插件"""
return cls._source and cls._source.plugin
@classproperty
def module(cls) -> Optional[ModuleType]:
"""事件响应器所在插件模块"""
return cls._source and cls._source.module
@classproperty
def plugin_name(cls) -> Optional[str]:
"""事件响应器所在插件名"""
return cls._source and cls._source.plugin_name
@classproperty
def module_name(cls) -> Optional[str]:
"""事件响应器所在插件模块路径"""
return cls._source and cls._source.module_name
@classmethod @classmethod
async def check_perm( async def check_perm(
cls, cls,
@@ -376,7 +462,7 @@ class Matcher(metaclass=MatcherMeta):
return return
await matcher.reject() await matcher.reject()
_parameterless = (Depends(_receive), *(parameterless or tuple())) _parameterless = (Depends(_receive), *(parameterless or ()))
def _decorator(func: T_Handler) -> T_Handler: def _decorator(func: T_Handler) -> T_Handler:
if cls.handlers and cls.handlers[-1].call is func: if cls.handlers and cls.handlers[-1].call is func:
@@ -406,7 +492,8 @@ class Matcher(metaclass=MatcherMeta):
) -> Callable[[T_Handler], T_Handler]: ) -> Callable[[T_Handler], T_Handler]:
"""装饰一个函数来指示 NoneBot 获取一个参数 `key` """装饰一个函数来指示 NoneBot 获取一个参数 `key`
当要获取的 `key` 不存在时接收用户新的一条消息再运行该函数,如果 `key` 已存在则直接继续运行 当要获取的 `key` 不存在时接收用户新的一条消息再运行该函数,
如果 `key` 已存在则直接继续运行
参数: 参数:
key: 参数名 key: 参数名
@@ -423,7 +510,7 @@ class Matcher(metaclass=MatcherMeta):
return return
await matcher.reject(prompt) await matcher.reject(prompt)
_parameterless = (Depends(_key_getter), *(parameterless or tuple())) _parameterless = (Depends(_key_getter), *(parameterless or ()))
def _decorator(func: T_Handler) -> T_Handler: def _decorator(func: T_Handler) -> T_Handler:
if cls.handlers and cls.handlers[-1].call is func: if cls.handlers and cls.handlers[-1].call is func:
@@ -454,7 +541,8 @@ class Matcher(metaclass=MatcherMeta):
参数: 参数:
message: 消息内容 message: 消息内容
kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,请参考对应 adapter 的 bot 对象 api kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,
请参考对应 adapter 的 bot 对象 api
""" """
bot = current_bot.get() bot = current_bot.get()
event = current_event.get() event = current_event.get()
@@ -475,7 +563,8 @@ class Matcher(metaclass=MatcherMeta):
参数: 参数:
message: 消息内容 message: 消息内容
kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,请参考对应 adapter 的 bot 对象 api kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,
请参考对应 adapter 的 bot 对象 api
""" """
if message is not None: if message is not None:
await cls.send(message, **kwargs) await cls.send(message, **kwargs)
@@ -491,7 +580,8 @@ class Matcher(metaclass=MatcherMeta):
参数: 参数:
prompt: 消息内容 prompt: 消息内容
kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,请参考对应 adapter 的 bot 对象 api kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,
请参考对应 adapter 的 bot 对象 api
""" """
if prompt is not None: if prompt is not None:
await cls.send(prompt, **kwargs) await cls.send(prompt, **kwargs)
@@ -508,7 +598,8 @@ class Matcher(metaclass=MatcherMeta):
参数: 参数:
prompt: 消息内容 prompt: 消息内容
kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,请参考对应 adapter 的 bot 对象 api kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,
请参考对应 adapter 的 bot 对象 api
""" """
if prompt is not None: if prompt is not None:
await cls.send(prompt, **kwargs) await cls.send(prompt, **kwargs)
@@ -527,7 +618,8 @@ class Matcher(metaclass=MatcherMeta):
参数: 参数:
key: 参数名 key: 参数名
prompt: 消息内容 prompt: 消息内容
kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,请参考对应 adapter 的 bot 对象 api kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,
请参考对应 adapter 的 bot 对象 api
""" """
matcher = current_matcher.get() matcher = current_matcher.get()
matcher.set_target(ARG_KEY.format(key=key)) matcher.set_target(ARG_KEY.format(key=key))
@@ -548,7 +640,8 @@ class Matcher(metaclass=MatcherMeta):
参数: 参数:
id: 消息 id id: 消息 id
prompt: 消息内容 prompt: 消息内容
kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,请参考对应 adapter 的 bot 对象 api kwargs: {ref}`nonebot.adapters.Bot.send` 的参数,
请参考对应 adapter 的 bot 对象 api
""" """
matcher = current_matcher.get() matcher = current_matcher.get()
matcher.set_target(RECEIVE_KEY.format(id=id)) matcher.set_target(RECEIVE_KEY.format(id=id))
@@ -767,8 +860,7 @@ class Matcher(metaclass=MatcherMeta):
temp=True, temp=True,
priority=0, priority=0,
block=True, block=True,
plugin=self.plugin, source=self.__class__._source,
module=self.module,
expire_time=bot.config.session_expire_timeout, expire_time=bot.config.session_expire_timeout,
default_state=self.state, default_state=self.state,
default_type_updater=self.__class__._default_type_updater, default_type_updater=self.__class__._default_type_updater,
@@ -788,8 +880,7 @@ class Matcher(metaclass=MatcherMeta):
temp=True, temp=True,
priority=0, priority=0,
block=True, block=True,
plugin=self.plugin, source=self.__class__._source,
module=self.module,
expire_time=bot.config.session_expire_timeout, expire_time=bot.config.session_expire_timeout,
default_state=self.state, default_state=self.state,
default_type_updater=self.__class__._default_type_updater, default_type_updater=self.__class__._default_type_updater,

View File

@@ -1,11 +1,21 @@
import asyncio import asyncio
import inspect import inspect
from typing_extensions import Annotated from typing_extensions import Self, Annotated, override
from contextlib import AsyncExitStack, contextmanager, asynccontextmanager from contextlib import AsyncExitStack, contextmanager, asynccontextmanager
from typing import TYPE_CHECKING, Any, Type, Tuple, Literal, Callable, Optional, cast from typing import (
TYPE_CHECKING,
Any,
Type,
Tuple,
Union,
Literal,
Callable,
Optional,
cast,
)
from pydantic.typing import get_args, get_origin from pydantic.typing import get_args, get_origin
from pydantic.fields import Required, Undefined, ModelField from pydantic.fields import Required, FieldInfo, Undefined, ModelField
from nonebot.dependencies.utils import check_field_type from nonebot.dependencies.utils import check_field_type
from nonebot.dependencies import Param, Dependent, CustomConfig from nonebot.dependencies import Param, Dependent, CustomConfig
@@ -24,6 +34,23 @@ if TYPE_CHECKING:
from nonebot.matcher import Matcher from nonebot.matcher import Matcher
from nonebot.adapters import Bot, Event from nonebot.adapters import Bot, Event
EXTRA_FIELD_INFO = (
"gt",
"lt",
"ge",
"le",
"multiple_of",
"allow_inf_nan",
"max_digits",
"decimal_places",
"min_items",
"max_items",
"unique_items",
"min_length",
"max_length",
"regex",
)
class DependsInner: class DependsInner:
def __init__( def __init__(
@@ -31,26 +58,31 @@ class DependsInner:
dependency: Optional[T_Handler] = None, dependency: Optional[T_Handler] = None,
*, *,
use_cache: bool = True, use_cache: bool = True,
validate: Union[bool, FieldInfo] = False,
) -> None: ) -> None:
self.dependency = dependency self.dependency = dependency
self.use_cache = use_cache self.use_cache = use_cache
self.validate = validate
def __repr__(self) -> str: def __repr__(self) -> str:
dep = get_name(self.dependency) dep = get_name(self.dependency)
cache = "" if self.use_cache else ", use_cache=False" cache = "" if self.use_cache else ", use_cache=False"
return f"DependsInner({dep}{cache})" validate = f", validate={self.validate}" if self.validate else ""
return f"DependsInner({dep}{cache}{validate})"
def Depends( def Depends(
dependency: Optional[T_Handler] = None, dependency: Optional[T_Handler] = None,
*, *,
use_cache: bool = True, use_cache: bool = True,
validate: Union[bool, FieldInfo] = False,
) -> Any: ) -> Any:
"""子依赖装饰器 """子依赖装饰器
参数: 参数:
dependency: 依赖函数。默认为参数的类型注释。 dependency: 依赖函数。默认为参数的类型注释。
use_cache: 是否使用缓存。默认为 `True`。 use_cache: 是否使用缓存。默认为 `True`。
validate: 是否使用 Pydantic 类型校验。默认为 `False`。
用法: 用法:
```python ```python
@@ -63,11 +95,14 @@ def Depends(
finally: finally:
... ...
async def handler(param_name: Any = Depends(depend_func), gen: Any = Depends(depend_gen_func)): async def handler(
param_name: Any = Depends(depend_func),
gen: Any = Depends(depend_gen_func),
):
... ...
``` ```
""" """
return DependsInner(dependency, use_cache=use_cache) return DependsInner(dependency, use_cache=use_cache, validate=validate)
class DependParam(Param): class DependParam(Param):
@@ -82,23 +117,44 @@ class DependParam(Param):
return f"Depends({self.extra['dependent']})" return f"Depends({self.extra['dependent']})"
@classmethod @classmethod
def _from_field(
cls, sub_dependent: Dependent, use_cache: bool, validate: Union[bool, FieldInfo]
) -> Self:
kwargs = {}
if isinstance(validate, FieldInfo):
kwargs.update((k, getattr(validate, k)) for k in EXTRA_FIELD_INFO)
return cls(
Required,
validate=bool(validate),
**kwargs,
dependent=sub_dependent,
use_cache=use_cache,
)
@classmethod
@override
def _check_param( def _check_param(
cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...] cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...]
) -> Optional["DependParam"]: ) -> Optional[Self]:
type_annotation, depends_inner = param.annotation, None type_annotation, depends_inner = param.annotation, None
# extract type annotation and dependency from Annotated
if get_origin(param.annotation) is Annotated: if get_origin(param.annotation) is Annotated:
type_annotation, *extra_args = get_args(param.annotation) type_annotation, *extra_args = get_args(param.annotation)
depends_inner = next( depends_inner = next(
(x for x in extra_args if isinstance(x, DependsInner)), None (x for x in reversed(extra_args) if isinstance(x, DependsInner)), None
) )
# param default value takes higher priority
depends_inner = ( depends_inner = (
param.default if isinstance(param.default, DependsInner) else depends_inner param.default if isinstance(param.default, DependsInner) else depends_inner
) )
# not a dependent
if depends_inner is None: if depends_inner is None:
return return
dependency: T_Handler dependency: T_Handler
# sub dependency is not specified, use type annotation
if depends_inner.dependency is None: if depends_inner.dependency is None:
assert ( assert (
type_annotation is not inspect.Signature.empty type_annotation is not inspect.Signature.empty
@@ -106,13 +162,18 @@ class DependParam(Param):
dependency = type_annotation dependency = type_annotation
else: else:
dependency = depends_inner.dependency dependency = depends_inner.dependency
# parse sub dependency
sub_dependent = Dependent[Any].parse( sub_dependent = Dependent[Any].parse(
call=dependency, call=dependency,
allow_types=allow_types, allow_types=allow_types,
) )
return cls(Required, use_cache=depends_inner.use_cache, dependent=sub_dependent)
return cls._from_field(
sub_dependent, depends_inner.use_cache, depends_inner.validate
)
@classmethod @classmethod
@override
def _check_parameterless( def _check_parameterless(
cls, value: Any, allow_types: Tuple[Type[Param], ...] cls, value: Any, allow_types: Tuple[Type[Param], ...]
) -> Optional["Param"]: ) -> Optional["Param"]:
@@ -121,8 +182,9 @@ class DependParam(Param):
dependent = Dependent[Any].parse( dependent = Dependent[Any].parse(
call=value.dependency, allow_types=allow_types call=value.dependency, allow_types=allow_types
) )
return cls(Required, use_cache=value.use_cache, dependent=dependent) return cls._from_field(dependent, value.use_cache, value.validate)
@override
async def _solve( async def _solve(
self, self,
stack: Optional[AsyncExitStack] = None, stack: Optional[AsyncExitStack] = None,
@@ -166,6 +228,7 @@ class DependParam(Param):
dependency_cache[call] = task dependency_cache[call] = task
return await task return await task
@override
async def _check(self, **kwargs: Any) -> None: async def _check(self, **kwargs: Any) -> None:
# run sub dependent pre-checkers # run sub dependent pre-checkers
sub_dependent: Dependent = self.extra["dependent"] sub_dependent: Dependent = self.extra["dependent"]
@@ -192,9 +255,10 @@ class BotParam(Param):
) )
@classmethod @classmethod
@override
def _check_param( def _check_param(
cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...] cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...]
) -> Optional["BotParam"]: ) -> Optional[Self]:
from nonebot.adapters import Bot from nonebot.adapters import Bot
# param type is Bot(s) or subclass(es) of Bot or None # param type is Bot(s) or subclass(es) of Bot or None
@@ -214,9 +278,11 @@ class BotParam(Param):
elif param.annotation == param.empty and param.name == "bot": elif param.annotation == param.empty and param.name == "bot":
return cls(Required) return cls(Required)
@override
async def _solve(self, bot: "Bot", **kwargs: Any) -> Any: async def _solve(self, bot: "Bot", **kwargs: Any) -> Any:
return bot return bot
@override
async def _check(self, bot: "Bot", **kwargs: Any) -> None: async def _check(self, bot: "Bot", **kwargs: Any) -> None:
if checker := self.extra.get("checker"): if checker := self.extra.get("checker"):
check_field_type(checker, bot) check_field_type(checker, bot)
@@ -242,9 +308,10 @@ class EventParam(Param):
) )
@classmethod @classmethod
@override
def _check_param( def _check_param(
cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...] cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...]
) -> Optional["EventParam"]: ) -> Optional[Self]:
from nonebot.adapters import Event from nonebot.adapters import Event
# param type is Event(s) or subclass(es) of Event or None # param type is Event(s) or subclass(es) of Event or None
@@ -264,9 +331,11 @@ class EventParam(Param):
elif param.annotation == param.empty and param.name == "event": elif param.annotation == param.empty and param.name == "event":
return cls(Required) return cls(Required)
@override
async def _solve(self, event: "Event", **kwargs: Any) -> Any: async def _solve(self, event: "Event", **kwargs: Any) -> Any:
return event return event
@override
async def _check(self, event: "Event", **kwargs: Any) -> Any: async def _check(self, event: "Event", **kwargs: Any) -> Any:
if checker := self.extra.get("checker", None): if checker := self.extra.get("checker", None):
check_field_type(checker, event) check_field_type(checker, event)
@@ -284,9 +353,10 @@ class StateParam(Param):
return "StateParam()" return "StateParam()"
@classmethod @classmethod
@override
def _check_param( def _check_param(
cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...] cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...]
) -> Optional["StateParam"]: ) -> Optional[Self]:
# param type is T_State # param type is T_State
if param.annotation is T_State: if param.annotation is T_State:
return cls(Required) return cls(Required)
@@ -294,6 +364,7 @@ class StateParam(Param):
elif param.annotation == param.empty and param.name == "state": elif param.annotation == param.empty and param.name == "state":
return cls(Required) return cls(Required)
@override
async def _solve(self, state: T_State, **kwargs: Any) -> Any: async def _solve(self, state: T_State, **kwargs: Any) -> Any:
return state return state
@@ -310,21 +381,38 @@ class MatcherParam(Param):
return "MatcherParam()" return "MatcherParam()"
@classmethod @classmethod
@override
def _check_param( def _check_param(
cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...] cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...]
) -> Optional["MatcherParam"]: ) -> Optional[Self]:
from nonebot.matcher import Matcher from nonebot.matcher import Matcher
# param type is Matcher(s) or subclass(es) of Matcher or None # param type is Matcher(s) or subclass(es) of Matcher or None
if generic_check_issubclass(param.annotation, Matcher): if generic_check_issubclass(param.annotation, Matcher):
return cls(Required) checker: Optional[ModelField] = None
if param.annotation is not Matcher:
checker = ModelField(
name=param.name,
type_=param.annotation,
class_validators=None,
model_config=CustomConfig,
default=None,
required=True,
)
return cls(Required, checker=checker)
# legacy: param is named "matcher" and has no type annotation # legacy: param is named "matcher" and has no type annotation
elif param.annotation == param.empty and param.name == "matcher": elif param.annotation == param.empty and param.name == "matcher":
return cls(Required) return cls(Required)
@override
async def _solve(self, matcher: "Matcher", **kwargs: Any) -> Any: async def _solve(self, matcher: "Matcher", **kwargs: Any) -> Any:
return matcher return matcher
@override
async def _check(self, matcher: "Matcher", **kwargs: Any) -> Any:
if checker := self.extra.get("checker", None):
check_field_type(checker, matcher)
class ArgInner: class ArgInner:
def __init__( def __init__(
@@ -365,13 +453,18 @@ class ArgParam(Param):
return f"ArgParam(key={self.extra['key']!r}, type={self.extra['type']!r})" return f"ArgParam(key={self.extra['key']!r}, type={self.extra['type']!r})"
@classmethod @classmethod
@override
def _check_param( def _check_param(
cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...] cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...]
) -> Optional["ArgParam"]: ) -> Optional[Self]:
if isinstance(param.default, ArgInner): if isinstance(param.default, ArgInner):
return cls( return cls(
Required, key=param.default.key or param.name, type=param.default.type Required, key=param.default.key or param.name, type=param.default.type
) )
elif get_origin(param.annotation) is Annotated:
for arg in get_args(param.annotation)[:0:-1]:
if isinstance(arg, ArgInner):
return cls(Required, key=arg.key or param.name, type=arg.type)
async def _solve(self, matcher: "Matcher", **kwargs: Any) -> Any: async def _solve(self, matcher: "Matcher", **kwargs: Any) -> Any:
key: str = self.extra["key"] key: str = self.extra["key"]
@@ -398,9 +491,10 @@ class ExceptionParam(Param):
return "ExceptionParam()" return "ExceptionParam()"
@classmethod @classmethod
@override
def _check_param( def _check_param(
cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...] cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...]
) -> Optional["ExceptionParam"]: ) -> Optional[Self]:
# param type is Exception(s) or subclass(es) of Exception or None # param type is Exception(s) or subclass(es) of Exception or None
if generic_check_issubclass(param.annotation, Exception): if generic_check_issubclass(param.annotation, Exception):
return cls(Required) return cls(Required)
@@ -408,6 +502,7 @@ class ExceptionParam(Param):
elif param.annotation == param.empty and param.name == "exception": elif param.annotation == param.empty and param.name == "exception":
return cls(Required) return cls(Required)
@override
async def _solve(self, exception: Optional[Exception] = None, **kwargs: Any) -> Any: async def _solve(self, exception: Optional[Exception] = None, **kwargs: Any) -> Any:
return exception return exception
@@ -424,12 +519,14 @@ class DefaultParam(Param):
return f"DefaultParam(default={self.default!r})" return f"DefaultParam(default={self.default!r})"
@classmethod @classmethod
@override
def _check_param( def _check_param(
cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...] cls, param: inspect.Parameter, allow_types: Tuple[Type[Param], ...]
) -> Optional["DefaultParam"]: ) -> Optional[Self]:
if param.default != param.empty: if param.default != param.empty:
return cls(param.default) return cls(param.default)
@override
async def _solve(self, **kwargs: Any) -> Any: async def _solve(self, **kwargs: Any) -> Any:
return Undefined return Undefined

View File

@@ -54,7 +54,7 @@ class LoguruHandler(logging.Handler): # pragma: no cover
except ValueError: except ValueError:
level = record.levelno level = record.levelno
frame, depth = logging.currentframe(), 2 frame, depth = sys._getframe(6), 6
while frame and frame.f_code.co_filename == logging.__file__: while frame and frame.f_code.co_filename == logging.__file__:
frame = frame.f_back frame = frame.f_back
depth += 1 depth += 1
@@ -88,5 +88,6 @@ logger_id = logger.add(
filter=default_filter, filter=default_filter,
format=default_format, format=default_format,
) )
"""默认日志处理器 id"""
__autodoc__ = {"logger_id": False} __autodoc__ = {"logger_id": False}

View File

@@ -8,6 +8,7 @@ FrontMatter:
from nonebot.internal.matcher import Matcher as Matcher from nonebot.internal.matcher import Matcher as Matcher
from nonebot.internal.matcher import matchers as matchers from nonebot.internal.matcher import matchers as matchers
from nonebot.internal.matcher import current_bot as current_bot from nonebot.internal.matcher import current_bot as current_bot
from nonebot.internal.matcher import MatcherSource as MatcherSource
from nonebot.internal.matcher import current_event as current_event from nonebot.internal.matcher import current_event as current_event
from nonebot.internal.matcher import MatcherManager as MatcherManager from nonebot.internal.matcher import MatcherManager as MatcherManager
from nonebot.internal.matcher import MatcherProvider as MatcherProvider from nonebot.internal.matcher import MatcherProvider as MatcherProvider

View File

@@ -166,7 +166,7 @@ async def _apply_event_preprocessors(
for proc in _event_preprocessors for proc in _event_preprocessors
) )
) )
except IgnoredException as e: except IgnoredException:
logger.opt(colors=True).info( logger.opt(colors=True).info(
f"Event {escape_tag(event.get_event_name())} is <b>ignored</b>" f"Event {escape_tag(event.get_event_name())} is <b>ignored</b>"
) )
@@ -293,7 +293,7 @@ async def _apply_run_postprocessors(
) -> None: ) -> None:
"""运行事件响应器运行后处理。 """运行事件响应器运行后处理。
Args: 参数:
bot: Bot 对象 bot: Bot 对象
event: Event 对象 event: Event 对象
matcher: 事件响应器 matcher: 事件响应器
@@ -358,9 +358,18 @@ async def _check_matcher(
return False return False
try: try:
if not await Matcher.check_perm( if not await Matcher.check_perm(bot, event, stack, dependency_cache):
bot, event, stack, dependency_cache logger.trace(f"Permission conditions not met for {Matcher}")
) or not await Matcher.check_rule(bot, event, state, stack, dependency_cache): return False
except Exception as e:
logger.opt(colors=True, exception=e).error(
f"<r><bg #f8bbd0>Permission check failed for {Matcher}.</bg #f8bbd0></r>"
)
return False
try:
if not await Matcher.check_rule(bot, event, state, stack, dependency_cache):
logger.trace(f"Rule conditions not met for {Matcher}")
return False return False
except Exception as e: except Exception as e:
logger.opt(colors=True, exception=e).error( logger.opt(colors=True, exception=e).error(

View File

@@ -1,7 +1,8 @@
"""本模块是 {ref}`nonebot.matcher.Matcher.permission` 的类型定义。 """本模块是 {ref}`nonebot.matcher.Matcher.permission` 的类型定义。
每个 {ref}`nonebot.matcher.Matcher` 拥有一个 {ref}`nonebot.permission.Permission` 每个{ref}`事件响应器 <nonebot.matcher.Matcher>`
其中是 `PermissionChecker` 的集合,只要有一个 `PermissionChecker` 检查结果为 `True` 时就会继续运行 拥有一个 {ref}`nonebot.permission.Permission`,其中是 `PermissionChecker` 的集合
只要有一个 `PermissionChecker` 检查结果为 `True` 时就会继续运行。
FrontMatter: FrontMatter:
sidebar_position: 6 sidebar_position: 6

View File

@@ -24,10 +24,12 @@
- `load_all_plugins` => {ref}``load_all_plugins` <nonebot.plugin.load.load_all_plugins>` - `load_all_plugins` => {ref}``load_all_plugins` <nonebot.plugin.load.load_all_plugins>`
- `load_from_json` => {ref}``load_from_json` <nonebot.plugin.load.load_from_json>` - `load_from_json` => {ref}``load_from_json` <nonebot.plugin.load.load_from_json>`
- `load_from_toml` => {ref}``load_from_toml` <nonebot.plugin.load.load_from_toml>` - `load_from_toml` => {ref}``load_from_toml` <nonebot.plugin.load.load_from_toml>`
- `load_builtin_plugin` => {ref}``load_builtin_plugin` <nonebot.plugin.load.load_builtin_plugin>` - `load_builtin_plugin` =>
- `load_builtin_plugins` => {ref}``load_builtin_plugins` <nonebot.plugin.load.load_builtin_plugins>` {ref}``load_builtin_plugin` <nonebot.plugin.load.load_builtin_plugin>`
- `load_builtin_plugins` =>
{ref}``load_builtin_plugins` <nonebot.plugin.load.load_builtin_plugins>`
- `require` => {ref}``require` <nonebot.plugin.load.require>` - `require` => {ref}``require` <nonebot.plugin.load.require>`
- `PluginMetadata` => {ref}``PluginMetadata` <nonebot.plugin.plugin.PluginMetadata>` - `PluginMetadata` => {ref}``PluginMetadata` <nonebot.plugin.model.PluginMetadata>`
FrontMatter: FrontMatter:
sidebar_position: 0 sidebar_position: 0
@@ -42,7 +44,7 @@ from typing import Set, Dict, List, Tuple, Optional
_plugins: Dict[str, "Plugin"] = {} _plugins: Dict[str, "Plugin"] = {}
_managers: List["PluginManager"] = [] _managers: List["PluginManager"] = []
_current_plugin_chain: ContextVar[Tuple["Plugin", ...]] = ContextVar( _current_plugin_chain: ContextVar[Tuple["Plugin", ...]] = ContextVar(
"_current_plugin_chain", default=tuple() "_current_plugin_chain", default=()
) )
@@ -75,7 +77,7 @@ def get_plugin(name: str) -> Optional["Plugin"]:
如果为 `load_plugins` 文件夹导入的插件,则为文件(夹)名。 如果为 `load_plugins` 文件夹导入的插件,则为文件(夹)名。
参数: 参数:
name: 插件名,即 {ref}`nonebot.plugin.plugin.Plugin.name`。 name: 插件名,即 {ref}`nonebot.plugin.model.Plugin.name`。
""" """
return _plugins.get(name) return _plugins.get(name)
@@ -86,7 +88,7 @@ def get_plugin_by_module_name(module_name: str) -> Optional["Plugin"]:
如果提供的模块名为某个插件的子模块,同样会返回该插件。 如果提供的模块名为某个插件的子模块,同样会返回该插件。
参数: 参数:
module_name: 模块名,即 {ref}`nonebot.plugin.plugin.Plugin.module_name`。 module_name: 模块名,即 {ref}`nonebot.plugin.model.Plugin.module_name`。
""" """
loaded = {plugin.module_name: plugin for plugin in _plugins.values()} loaded = {plugin.module_name: plugin for plugin in _plugins.values()}
has_parent = True has_parent = True
@@ -109,9 +111,9 @@ def get_available_plugin_names() -> Set[str]:
from .on import on as on from .on import on as on
from .manager import PluginManager from .manager import PluginManager
from .on import on_type as on_type from .on import on_type as on_type
from .model import Plugin as Plugin
from .load import require as require from .load import require as require
from .on import on_regex as on_regex from .on import on_regex as on_regex
from .plugin import Plugin as Plugin
from .on import on_notice as on_notice from .on import on_notice as on_notice
from .on import on_command as on_command from .on import on_command as on_command
from .on import on_keyword as on_keyword from .on import on_keyword as on_keyword
@@ -127,8 +129,9 @@ from .load import load_plugins as load_plugins
from .on import on_startswith as on_startswith from .on import on_startswith as on_startswith
from .load import load_from_json as load_from_json from .load import load_from_json as load_from_json
from .load import load_from_toml as load_from_toml from .load import load_from_toml as load_from_toml
from .model import PluginMetadata as PluginMetadata
from .on import on_shell_command as on_shell_command from .on import on_shell_command as on_shell_command
from .plugin import PluginMetadata as PluginMetadata
from .load import load_all_plugins as load_all_plugins from .load import load_all_plugins as load_all_plugins
from .load import load_builtin_plugin as load_builtin_plugin from .load import load_builtin_plugin as load_builtin_plugin
from .load import load_builtin_plugins as load_builtin_plugins from .load import load_builtin_plugins as load_builtin_plugins
from .load import inherit_supported_adapters as inherit_supported_adapters

View File

@@ -4,6 +4,7 @@ FrontMatter:
sidebar_position: 1 sidebar_position: 1
description: nonebot.plugin.load 模块 description: nonebot.plugin.load 模块
""" """
import json import json
from pathlib import Path from pathlib import Path
from types import ModuleType from types import ModuleType
@@ -11,7 +12,7 @@ from typing import Set, Union, Iterable, Optional
from nonebot.utils import path_to_module_name from nonebot.utils import path_to_module_name
from .plugin import Plugin from .model import Plugin
from .manager import PluginManager from .manager import PluginManager
from . import _managers, get_plugin, _current_plugin_chain, _module_name_to_plugin_name from . import _managers, get_plugin, _current_plugin_chain, _module_name_to_plugin_name
@@ -25,7 +26,8 @@ def load_plugin(module_path: Union[str, Path]) -> Optional[Plugin]:
"""加载单个插件,可以是本地插件或是通过 `pip` 安装的插件。 """加载单个插件,可以是本地插件或是通过 `pip` 安装的插件。
参数: 参数:
module_path: 插件名称 `path.to.your.plugin` 或插件路径 `pathlib.Path(path/to/your/plugin)` module_path: 插件名称 `path.to.your.plugin`
或插件路径 `pathlib.Path(path/to/your/plugin)`
""" """
module_path = ( module_path = (
path_to_module_name(module_path) path_to_module_name(module_path)
@@ -63,7 +65,8 @@ def load_all_plugins(
def load_from_json(file_path: str, encoding: str = "utf-8") -> Set[Plugin]: def load_from_json(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
"""导入指定 json 文件中的 `plugins` 以及 `plugin_dirs` 下多个插件,以 `_` 开头的插件不会被导入! """导入指定 json 文件中的 `plugins` 以及 `plugin_dirs` 下多个插件
以 `_` 开头的插件不会被导入!
参数: 参数:
file_path: 指定 json 文件路径 file_path: 指定 json 文件路径
@@ -81,7 +84,7 @@ def load_from_json(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
nonebot.load_from_json("plugins.json") nonebot.load_from_json("plugins.json")
``` ```
""" """
with open(file_path, "r", encoding=encoding) as f: with open(file_path, encoding=encoding) as f:
data = json.load(f) data = json.load(f)
if not isinstance(data, dict): if not isinstance(data, dict):
raise TypeError("json file must contains a dict!") raise TypeError("json file must contains a dict!")
@@ -93,7 +96,9 @@ def load_from_json(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
def load_from_toml(file_path: str, encoding: str = "utf-8") -> Set[Plugin]: def load_from_toml(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
"""导入指定 toml 文件 `[tool.nonebot]` 中的 `plugins` 以及 `plugin_dirs` 下多个插件,以 `_` 开头的插件不会被导入! """导入指定 toml 文件 `[tool.nonebot]` 中的
`plugins` 以及 `plugin_dirs` 下多个插件。
以 `_` 开头的插件不会被导入!
参数: 参数:
file_path: 指定 toml 文件路径 file_path: 指定 toml 文件路径
@@ -110,7 +115,7 @@ def load_from_toml(file_path: str, encoding: str = "utf-8") -> Set[Plugin]:
nonebot.load_from_toml("pyproject.toml") nonebot.load_from_toml("pyproject.toml")
``` ```
""" """
with open(file_path, "r", encoding=encoding) as f: with open(file_path, encoding=encoding) as f:
data = tomllib.loads(f.read()) data = tomllib.loads(f.read())
nonebot_data = data.get("tool", {}).get("nonebot") nonebot_data = data.get("tool", {}).get("nonebot")
@@ -155,7 +160,7 @@ def require(name: str) -> ModuleType:
如果为 `load_plugins` 文件夹导入的插件,则为文件(夹)名。 如果为 `load_plugins` 文件夹导入的插件,则为文件(夹)名。
参数: 参数:
name: 插件名,即 {ref}`nonebot.plugin.plugin.Plugin.name`。 name: 插件名,即 {ref}`nonebot.plugin.model.Plugin.name`。
异常: 异常:
RuntimeError: 插件无法加载 RuntimeError: 插件无法加载
@@ -177,3 +182,39 @@ def require(name: str) -> ModuleType:
if not plugin: if not plugin:
raise RuntimeError(f'Cannot load plugin "{name}"!') raise RuntimeError(f'Cannot load plugin "{name}"!')
return plugin.module return plugin.module
def inherit_supported_adapters(*names: str) -> Optional[Set[str]]:
"""获取已加载插件的适配器支持状态集合。
如果传入了多个插件名称,返回值会自动取交集。
参数:
names: 插件名称列表。
异常:
RuntimeError: 插件未加载
ValueError: 插件缺少元数据
"""
final_supported: Optional[Set[str]] = None
for name in names:
plugin = get_plugin(_module_name_to_plugin_name(name))
if plugin is None:
raise RuntimeError(f'Plugin "{name}" is not loaded!')
meta = plugin.metadata
if meta is None:
raise ValueError(f'Plugin "{name}" has no metadata!')
support = meta.supported_adapters
if support is None:
continue
final_supported = (
support if final_supported is None else (final_supported & support)
)
return final_supported and {
f"nonebot.adapters.{adapter_name[1:]}"
if adapter_name.startswith("~")
else adapter_name
for adapter_name in final_supported
}

View File

@@ -6,6 +6,7 @@ FrontMatter:
sidebar_position: 5 sidebar_position: 5
description: nonebot.plugin.manager 模块 description: nonebot.plugin.manager 模块
""" """
import sys import sys
import pkgutil import pkgutil
import importlib import importlib
@@ -19,7 +20,7 @@ from typing import Set, Dict, List, Iterable, Optional, Sequence
from nonebot.log import logger from nonebot.log import logger
from nonebot.utils import escape_tag, path_to_module_name from nonebot.utils import escape_tag, path_to_module_name
from .plugin import Plugin, PluginMetadata from .model import Plugin, PluginMetadata
from . import ( from . import (
_managers, _managers,
_new_plugin, _new_plugin,

View File

@@ -2,7 +2,7 @@
FrontMatter: FrontMatter:
sidebar_position: 3 sidebar_position: 3
description: nonebot.plugin.plugin 模块 description: nonebot.plugin.model 模块
""" """
import contextlib import contextlib

View File

@@ -4,16 +4,18 @@ FrontMatter:
sidebar_position: 2 sidebar_position: 2
description: nonebot.plugin.on 模块 description: nonebot.plugin.on 模块
""" """
import re import re
import inspect import inspect
import warnings
from types import ModuleType from types import ModuleType
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Any, Set, Dict, List, Type, Tuple, Union, Optional from typing import Any, Set, Dict, List, Type, Tuple, Union, Optional
from nonebot.adapters import Event from nonebot.adapters import Event
from nonebot.matcher import Matcher
from nonebot.permission import Permission from nonebot.permission import Permission
from nonebot.dependencies import Dependent from nonebot.dependencies import Dependent
from nonebot.matcher import Matcher, MatcherSource
from nonebot.typing import T_State, T_Handler, T_RuleChecker, T_PermissionChecker from nonebot.typing import T_State, T_Handler, T_RuleChecker, T_PermissionChecker
from nonebot.rule import ( from nonebot.rule import (
Rule, Rule,
@@ -28,7 +30,7 @@ from nonebot.rule import (
shell_command, shell_command,
) )
from .plugin import Plugin from .model import Plugin
from . import get_plugin_by_module_name from . import get_plugin_by_module_name
from .manager import _current_plugin_chain from .manager import _current_plugin_chain
@@ -44,25 +46,39 @@ def store_matcher(matcher: Type[Matcher]) -> None:
plugin_chain[-1].matcher.add(matcher) plugin_chain[-1].matcher.add(matcher)
def get_matcher_plugin(depth: int = 1) -> Optional[Plugin]: def get_matcher_plugin(depth: int = 1) -> Optional[Plugin]: # pragma: no cover
"""获取事件响应器定义所在插件。 """获取事件响应器定义所在插件。
**Deprecated**, 请使用 {ref}`nonebot.plugin.on.get_matcher_source` 获取信息。
参数: 参数:
depth: 调用栈深度 depth: 调用栈深度
""" """
# matcher defined when plugin loading warnings.warn(
if plugin_chain := _current_plugin_chain.get(): "`get_matcher_plugin` is deprecated, please use `get_matcher_source` instead",
return plugin_chain[-1] DeprecationWarning,
)
# matcher defined when plugin running return (source := get_matcher_source(depth + 1)) and source.plugin
if module := get_matcher_module(depth + 1):
if plugin := get_plugin_by_module_name(module.__name__):
return plugin
def get_matcher_module(depth: int = 1) -> Optional[ModuleType]: def get_matcher_module(depth: int = 1) -> Optional[ModuleType]: # pragma: no cover
"""获取事件响应器定义所在模块。 """获取事件响应器定义所在模块。
**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
def get_matcher_source(depth: int = 1) -> Optional[MatcherSource]:
"""获取事件响应器定义所在源码信息。
参数: 参数:
depth: 调用栈深度 depth: 调用栈深度
""" """
@@ -70,7 +86,22 @@ def get_matcher_module(depth: int = 1) -> Optional[ModuleType]:
if current_frame is None: if current_frame is None:
return None return None
frame = inspect.getouterframes(current_frame)[depth + 1].frame frame = inspect.getouterframes(current_frame)[depth + 1].frame
return inspect.getmodule(frame)
module_name = (module := inspect.getmodule(frame)) and module.__name__
plugin: Optional["Plugin"] = None
# matcher defined when plugin loading
if plugin_chain := _current_plugin_chain.get():
plugin = plugin_chain[-1]
# matcher defined when plugin running
elif module_name:
plugin = get_plugin_by_module_name(module_name)
return MatcherSource(
plugin_name=plugin and plugin.name,
module_name=module_name,
lineno=frame.f_lineno,
)
def on( def on(
@@ -108,8 +139,7 @@ def on(
priority=priority, priority=priority,
block=block, block=block,
handlers=handlers, handlers=handlers,
plugin=get_matcher_plugin(_depth + 1), source=get_matcher_source(_depth + 1),
module=get_matcher_module(_depth + 1),
default_state=state, default_state=state,
) )
store_matcher(matcher) store_matcher(matcher)
@@ -322,7 +352,8 @@ def on_shell_command(
与普通的 `on_command` 不同的是,在添加 `parser` 参数时, 响应器会自动处理消息。 与普通的 `on_command` 不同的是,在添加 `parser` 参数时, 响应器会自动处理消息。
并将用户输入的原始参数列表保存在 `state["argv"]`, `parser` 处理的参数保存在 `state["args"]` 中 可以通过 {ref}`nonebot.params.ShellCommandArgv` 获取原始参数列表,
通过 {ref}`nonebot.params.ShellCommandArgs` 获取解析后的参数字典。
参数: 参数:
cmd: 指定命令内容 cmd: 指定命令内容
@@ -427,6 +458,7 @@ class CommandGroup(_Group):
参数: 参数:
cmd: 指定命令内容 cmd: 指定命令内容
prefix_aliases: 是否影响命令别名,给命令别名加前缀
rule: 事件响应规则 rule: 事件响应规则
permission: 事件响应权限 permission: 事件响应权限
handlers: 事件处理函数列表 handlers: 事件处理函数列表
@@ -437,11 +469,14 @@ class CommandGroup(_Group):
state: 默认 state state: 默认 state
""" """
def __init__(self, cmd: Union[str, Tuple[str, ...]], **kwargs): def __init__(
self, cmd: Union[str, Tuple[str, ...]], prefix_aliases: bool = False, **kwargs
):
"""命令前缀""" """命令前缀"""
super().__init__(**kwargs) super().__init__(**kwargs)
self.basecmd: Tuple[str, ...] = (cmd,) if isinstance(cmd, str) else cmd self.basecmd: Tuple[str, ...] = (cmd,) if isinstance(cmd, str) else cmd
self.base_kwargs.pop("aliases", None) self.base_kwargs.pop("aliases", None)
self.prefix_aliases = prefix_aliases
def __repr__(self) -> str: def __repr__(self) -> str:
return f"CommandGroup(cmd={self.basecmd}, matchers={len(self.matchers)})" return f"CommandGroup(cmd={self.basecmd}, matchers={len(self.matchers)})"
@@ -464,6 +499,11 @@ class CommandGroup(_Group):
""" """
sub_cmd = (cmd,) if isinstance(cmd, str) else cmd sub_cmd = (cmd,) if isinstance(cmd, str) else cmd
cmd = self.basecmd + sub_cmd cmd = self.basecmd + sub_cmd
if self.prefix_aliases and (aliases := kwargs.get("aliases")):
kwargs["aliases"] = {
self.basecmd + ((alias,) if isinstance(alias, str) else alias)
for alias in aliases
}
matcher = on_command(cmd, **self._get_final_kwargs(kwargs)) matcher = on_command(cmd, **self._get_final_kwargs(kwargs))
self.matchers.append(matcher) self.matchers.append(matcher)
return matcher return matcher
@@ -488,6 +528,11 @@ class CommandGroup(_Group):
""" """
sub_cmd = (cmd,) if isinstance(cmd, str) else cmd sub_cmd = (cmd,) if isinstance(cmd, str) else cmd
cmd = self.basecmd + sub_cmd cmd = self.basecmd + sub_cmd
if self.prefix_aliases and (aliases := kwargs.get("aliases")):
kwargs["aliases"] = {
self.basecmd + ((alias,) if isinstance(alias, str) else alias)
for alias in aliases
}
matcher = on_shell_command(cmd, **self._get_final_kwargs(kwargs)) matcher = on_shell_command(cmd, **self._get_final_kwargs(kwargs))
self.matchers.append(matcher) self.matchers.append(matcher)
return matcher return matcher
@@ -712,7 +757,8 @@ class MatcherGroup(_Group):
与普通的 `on_command` 不同的是,在添加 `parser` 参数时, 响应器会自动处理消息。 与普通的 `on_command` 不同的是,在添加 `parser` 参数时, 响应器会自动处理消息。
并将用户输入的原始参数列表保存在 `state["argv"]`, `parser` 处理的参数保存在 `state["args"]` 中 可以通过 {ref}`nonebot.params.ShellCommandArgv` 获取原始参数列表,
通过 {ref}`nonebot.params.ShellCommandArgs` 获取解析后的参数字典。
参数: 参数:
cmd: 指定命令内容 cmd: 指定命令内容

View File

@@ -1,410 +1,421 @@
import re import re
from typing import Any
from types import ModuleType from types import ModuleType
from datetime import datetime, timedelta from datetime import datetime, timedelta
from typing import Set, List, Type, Tuple, Union, Optional
from nonebot.adapters import Event from nonebot.adapters import Event
from nonebot.matcher import Matcher
from nonebot.permission import Permission from nonebot.permission import Permission
from nonebot.dependencies import Dependent from nonebot.dependencies import Dependent
from nonebot.rule import Rule, ArgumentParser from nonebot.rule import Rule, ArgumentParser
from nonebot.matcher import Matcher, MatcherSource
from nonebot.typing import T_State, T_Handler, T_RuleChecker, T_PermissionChecker from nonebot.typing import T_State, T_Handler, T_RuleChecker, T_PermissionChecker
from .plugin import Plugin from .model import Plugin
def store_matcher(matcher: Type[Matcher]) -> None: ... def store_matcher(matcher: type[Matcher]) -> None: ...
def get_matcher_plugin(depth: int = ...) -> Optional[Plugin]: ... def get_matcher_plugin(depth: int = ...) -> Plugin | None: ...
def get_matcher_module(depth: int = ...) -> Optional[ModuleType]: ... def get_matcher_module(depth: int = ...) -> ModuleType | None: ...
def get_matcher_source(depth: int = ...) -> MatcherSource | None: ...
def on( def on(
type: str = "", type: str = "",
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
*, *,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_metaevent( def on_metaevent(
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
*, *,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_message( def on_message(
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
*, *,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_notice( def on_notice(
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
*, *,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_request( def on_request(
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
*, *,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_startswith( def on_startswith(
msg: Union[str, Tuple[str, ...]], msg: str | tuple[str, ...],
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
ignorecase: bool = ..., ignorecase: bool = ...,
*, *,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_endswith( def on_endswith(
msg: Union[str, Tuple[str, ...]], msg: str | tuple[str, ...],
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
ignorecase: bool = ..., ignorecase: bool = ...,
*, *,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_fullmatch( def on_fullmatch(
msg: Union[str, Tuple[str, ...]], msg: str | tuple[str, ...],
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
ignorecase: bool = ..., ignorecase: bool = ...,
*, *,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_keyword( def on_keyword(
keywords: Set[str], keywords: set[str],
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
*, *,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_command( def on_command(
cmd: Union[str, Tuple[str, ...]], cmd: str | tuple[str, ...],
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ..., aliases: set[str | tuple[str, ...]] | None = ...,
force_whitespace: Optional[Union[str, bool]] = ..., force_whitespace: str | bool | None = ...,
*, *,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_shell_command( def on_shell_command(
cmd: Union[str, Tuple[str, ...]], cmd: str | tuple[str, ...],
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ..., aliases: set[str | tuple[str, ...]] | None = ...,
parser: Optional[ArgumentParser] = ..., parser: ArgumentParser | None = ...,
*, *,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_regex( def on_regex(
pattern: str, pattern: str,
flags: Union[int, re.RegexFlag] = ..., flags: int | re.RegexFlag = ...,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
*, *,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_type( def on_type(
types: Union[Type[Event], Tuple[Type[Event], ...]], types: type[Event] | tuple[type[Event], ...],
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
*, *,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
class CommandGroup: class _Group:
matchers: list[type[Matcher]] = ...
base_kwargs: dict[str, Any] = ...
def _get_final_kwargs(
self, update: dict[str, Any], *, exclude: set[str] | None = None
) -> dict[str, Any]: ...
class CommandGroup(_Group):
basecmd: tuple[str, ...] = ...
prefix_aliases: bool = ...
def __init__( def __init__(
self, self,
cmd: Union[str, Tuple[str, ...]], cmd: str | tuple[str, ...],
prefix_aliases: bool = ...,
*, *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
): ... ): ...
def command( def command(
self, self,
cmd: Union[str, Tuple[str, ...]], cmd: str | tuple[str, ...],
*, *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ..., aliases: set[str | tuple[str, ...]] | None = ...,
force_whitespace: Optional[Union[str, bool]] = ..., force_whitespace: str | bool | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def shell_command( def shell_command(
self, self,
cmd: Union[str, Tuple[str, ...]], cmd: str | tuple[str, ...],
*, *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ..., aliases: set[str | tuple[str, ...]] | None = ...,
parser: Optional[ArgumentParser] = ..., parser: ArgumentParser | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
class MatcherGroup: class MatcherGroup(_Group):
def __init__( def __init__(
self, self,
*, *,
type: str = ..., type: str = ...,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
): ... ): ...
def on( def on(
self, self,
*, *,
type: str = ..., type: str = ...,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_metaevent( def on_metaevent(
self, self,
*, *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_message( def on_message(
self, self,
*, *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_notice( def on_notice(
self, self,
*, *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_request( def on_request(
self, self,
*, *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_startswith( def on_startswith(
self, self,
msg: Union[str, Tuple[str, ...]], msg: str | tuple[str, ...],
*, *,
ignorecase: bool = ..., ignorecase: bool = ...,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_endswith( def on_endswith(
self, self,
msg: Union[str, Tuple[str, ...]], msg: str | tuple[str, ...],
*, *,
ignorecase: bool = ..., ignorecase: bool = ...,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_fullmatch( def on_fullmatch(
self, self,
msg: Union[str, Tuple[str, ...]], msg: str | tuple[str, ...],
*, *,
ignorecase: bool = ..., ignorecase: bool = ...,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_keyword( def on_keyword(
self, self,
keywords: Set[str], keywords: set[str],
*, *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_command( def on_command(
self, self,
cmd: Union[str, Tuple[str, ...]], cmd: str | tuple[str, ...],
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ..., aliases: set[str | tuple[str, ...]] | None = ...,
force_whitespace: Optional[Union[str, bool]] = ..., force_whitespace: str | bool | None = ...,
*, *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_shell_command( def on_shell_command(
self, self,
cmd: Union[str, Tuple[str, ...]], cmd: str | tuple[str, ...],
aliases: Optional[Set[Union[str, Tuple[str, ...]]]] = ..., aliases: set[str | tuple[str, ...]] | None = ...,
parser: Optional[ArgumentParser] = ..., parser: ArgumentParser | None = ...,
*, *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_regex( def on_regex(
self, self,
pattern: str, pattern: str,
flags: Union[int, re.RegexFlag] = ..., flags: int | re.RegexFlag = ...,
*, *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...
def on_type( def on_type(
self, self,
types: Union[Type[Event], Tuple[Type[Event]]], types: type[Event] | tuple[type[Event]],
*, *,
rule: Optional[Union[Rule, T_RuleChecker]] = ..., rule: Rule | T_RuleChecker | None = ...,
permission: Optional[Union[Permission, T_PermissionChecker]] = ..., permission: Permission | T_PermissionChecker | None = ...,
handlers: Optional[List[Union[T_Handler, Dependent]]] = ..., handlers: list[T_Handler | Dependent] | None = ...,
temp: bool = ..., temp: bool = ...,
expire_time: Optional[Union[datetime, timedelta]] = ..., expire_time: datetime | timedelta | None = ...,
priority: int = ..., priority: int = ...,
block: bool = ..., block: bool = ...,
state: Optional[T_State] = ..., state: T_State | None = ...,
) -> Type[Matcher]: ... ) -> type[Matcher]: ...

View File

@@ -1,7 +1,18 @@
from nonebot import on_command
from nonebot.rule import to_me from nonebot.rule import to_me
from nonebot.adapters import Message from nonebot.adapters import Message
from nonebot.params import CommandArg from nonebot.params import CommandArg
from nonebot.plugin import on_command from nonebot.plugin import PluginMetadata
__plugin_meta__ = PluginMetadata(
name="echo",
description="重复你说的话",
usage="/echo [text]",
type="application",
homepage="https://github.com/nonebot/nonebot2/blob/master/nonebot/plugins/echo.py",
config=None,
supported_adapters=None,
)
echo = on_command("echo", to_me()) echo = on_command("echo", to_me())

View File

@@ -2,8 +2,19 @@ from typing import Dict, AsyncGenerator
from nonebot.adapters import Event from nonebot.adapters import Event
from nonebot.params import Depends from nonebot.params import Depends
from nonebot.plugin import PluginMetadata
from nonebot.message import IgnoredException, event_preprocessor from nonebot.message import IgnoredException, event_preprocessor
__plugin_meta__ = PluginMetadata(
name="唯一会话",
description="限制同一会话内同时只能运行一个响应器",
usage="加载插件后自动生效",
type="application",
homepage="https://github.com/nonebot/nonebot2/blob/master/nonebot/plugins/single_session.py",
config=None,
supported_adapters=None,
)
_running_matcher: Dict[str, int] = {} _running_matcher: Dict[str, int] = {}
@@ -15,7 +26,7 @@ async def matcher_mutex(event: Event) -> AsyncGenerator[bool, None]:
yield result yield result
else: else:
current_event_id = id(event) current_event_id = id(event)
if event_id := _running_matcher.get(session_id, None): if event_id := _running_matcher.get(session_id):
result = event_id != current_event_id result = event_id != current_event_id
else: else:
_running_matcher[session_id] = current_event_id _running_matcher[session_id] = current_event_id

View File

@@ -1,7 +1,8 @@
"""本模块是 {ref}`nonebot.matcher.Matcher.rule` 的类型定义。 """本模块是 {ref}`nonebot.matcher.Matcher.rule` 的类型定义。
每个事件响应器 {ref}`nonebot.matcher.Matcher` 拥有一个匹配规则 {ref}`nonebot.rule.Rule` 每个{ref}`事件响应器 <nonebot.matcher.Matcher>`拥有一个
其中是 `RuleChecker` 的集合,只有当所有 `RuleChecker` 检查结果为 `True` 时继续运行 {ref}`nonebot.rule.Rule`,其中是 `RuleChecker` 的集合
只有当所有 `RuleChecker` 检查结果为 `True` 时继续运行。
FrontMatter: FrontMatter:
sidebar_position: 5 sidebar_position: 5
@@ -60,20 +61,19 @@ from nonebot.consts import (
T = TypeVar("T") T = TypeVar("T")
CMD_RESULT = TypedDict(
"CMD_RESULT",
{
"command": Optional[Tuple[str, ...]],
"raw_command": Optional[str],
"command_arg": Optional[Message],
"command_start": Optional[str],
"command_whitespace": Optional[str],
},
)
TRIE_VALUE = NamedTuple( class CMD_RESULT(TypedDict):
"TRIE_VALUE", [("command_start", str), ("command", Tuple[str, ...])] command: Optional[Tuple[str, ...]]
) raw_command: Optional[str]
command_arg: Optional[Message]
command_start: Optional[str]
command_whitespace: Optional[str]
class TRIE_VALUE(NamedTuple):
command_start: str
command: Tuple[str, ...]
parser_message: ContextVar[str] = ContextVar("parser_message") parser_message: ContextVar[str] = ContextVar("parser_message")
@@ -117,6 +117,11 @@ class TrieRule:
# check whitespace # check whitespace
arg_str = segment_text[len(pf.key) :] arg_str = segment_text[len(pf.key) :]
arg_str_stripped = arg_str.lstrip() arg_str_stripped = arg_str.lstrip()
# check next segment until arg detected or no text remain
while not arg_str_stripped and msg and msg[0].is_text():
arg_str += str(msg.pop(0))
arg_str_stripped = arg_str.lstrip()
has_arg = arg_str_stripped or msg has_arg = arg_str_stripped or msg
if ( if (
has_arg has_arg
@@ -406,7 +411,7 @@ def command(
force_whitespace: 是否强制命令后必须有指定空白符 force_whitespace: 是否强制命令后必须有指定空白符
用法: 用法:
使用默认 `command_start`, `command_sep` 配置 使用默认 `command_start`, `command_sep` 配置情况下:
命令 `("test",)` 可以匹配: `/test` 开头的消息 命令 `("test",)` 可以匹配: `/test` 开头的消息
命令 `("test", "sub")` 可以匹配: `/test.sub` 开头的消息 命令 `("test", "sub")` 可以匹配: `/test.sub` 开头的消息
@@ -441,6 +446,8 @@ def command(
class ArgumentParser(ArgParser): class ArgumentParser(ArgParser):
"""`shell_like` 命令参数解析器,解析出错时不会退出程序。 """`shell_like` 命令参数解析器,解析出错时不会退出程序。
支持 {ref}`nonebot.adapters.Message` 富文本解析。
用法: 用法:
用法与 `argparse.ArgumentParser` 相同, 用法与 `argparse.ArgumentParser` 相同,
参考文档: [argparse](https://docs.python.org/3/library/argparse.html) 参考文档: [argparse](https://docs.python.org/3/library/argparse.html)
@@ -588,12 +595,16 @@ def shell_command(
根据配置里提供的 {ref}``command_start` <nonebot.config.Config.command_start>`, 根据配置里提供的 {ref}``command_start` <nonebot.config.Config.command_start>`,
{ref}``command_sep` <nonebot.config.Config.command_sep>` 判断消息是否为命令。 {ref}``command_sep` <nonebot.config.Config.command_sep>` 判断消息是否为命令。
可以通过 {ref}`nonebot.params.Command` 获取匹配成功的命令(例: `("test",)` 可以通过 {ref}`nonebot.params.Command` 获取匹配成功的命令
通过 {ref}`nonebot.params.RawCommand` 获取匹配成功的原始命令文本(例: `"/test"` (例: `("test",)`
通过 {ref}`nonebot.params.ShellCommandArgv` 获取解析前的参数列表(例: `["arg", "-h"]` 通过 {ref}`nonebot.params.RawCommand` 获取匹配成功的原始命令文本
通过 {ref}`nonebot.params.ShellCommandArgs` 获取解析后的参数字典(例: `{"arg": "arg", "h": True}` (例: `"/test"`
通过 {ref}`nonebot.params.ShellCommandArgv` 获取解析前的参数列表
(例: `["arg", "-h"]`
通过 {ref}`nonebot.params.ShellCommandArgs` 获取解析后的参数字典
(例: `{"arg": "arg", "h": True}`)。
:::warning 警告 :::caution 警告
如果参数解析失败,则通过 {ref}`nonebot.params.ShellCommandArgs` 如果参数解析失败,则通过 {ref}`nonebot.params.ShellCommandArgs`
获取的将是 {ref}`nonebot.exception.ParserExit` 异常。 获取的将是 {ref}`nonebot.exception.ParserExit` 异常。
::: :::
@@ -603,7 +614,8 @@ def shell_command(
parser: {ref}`nonebot.rule.ArgumentParser` 对象 parser: {ref}`nonebot.rule.ArgumentParser` 对象
用法: 用法:
使用默认 `command_start`, `command_sep` 配置,更多示例参考 `argparse` 标准库文档。 使用默认 `command_start`, `command_sep` 配置,更多示例参考
[argparse](https://docs.python.org/3/library/argparse.html) 标准库文档。
```python ```python
from nonebot.rule import ArgumentParser from nonebot.rule import ArgumentParser
@@ -698,7 +710,8 @@ def regex(regex: str, flags: Union[int, re.RegexFlag] = 0) -> Rule:
::: :::
:::tip 提示 :::tip 提示
正则表达式匹配使用 `EventMessage` 的 `str` 字符串,而非 `EventMessage` 的 `PlainText` 纯文本字符串 正则表达式匹配使用 `EventMessage` 的 `str` 字符串,
而非 `EventMessage` 的 `PlainText` 纯文本字符串
::: :::
""" """

View File

@@ -1,17 +1,17 @@
"""本模块定义了 NoneBot 模块中共享的一些类型。 """本模块定义了 NoneBot 模块中共享的一些类型。
下面的文档中,「类型」部分使用 Python 的 Type Hint 语法, 使用 Python 的 Type Hint 语法,
参考 [`PEP 484`](https://www.python.org/dev/peps/pep-0484/), 参考 [`PEP 484`](https://www.python.org/dev/peps/pep-0484/),
[`PEP 526`](https://www.python.org/dev/peps/pep-0526/) 和 [`PEP 526`](https://www.python.org/dev/peps/pep-0526/) 和
[`typing`](https://docs.python.org/3/library/typing.html)。 [`typing`](https://docs.python.org/3/library/typing.html)。
除了 Python 内置的类型,下面还出现了如下 NoneBot 自定类型,实际上它们是 Python 内置类型的别名。
FrontMatter: FrontMatter:
sidebar_position: 11 sidebar_position: 11
description: nonebot.typing 模块 description: nonebot.typing 模块
""" """
import warnings
from typing_extensions import ParamSpec, TypeAlias, override
from typing import ( from typing import (
TYPE_CHECKING, TYPE_CHECKING,
Any, Any,
@@ -30,28 +30,31 @@ if TYPE_CHECKING:
from nonebot.permission import Permission from nonebot.permission import Permission
T = TypeVar("T") T = TypeVar("T")
P = ParamSpec("P")
T_Wrapped = TypeVar("T_Wrapped", bound=Callable) T_Wrapped: TypeAlias = Callable[P, T]
def overrides(InterfaceClass: object) -> Callable[[T_Wrapped], T_Wrapped]: def overrides(InterfaceClass: object):
"""标记一个方法为父类 interface 的 implement""" """标记一个方法为父类 interface 的 implement"""
def overrider(func: T_Wrapped) -> T_Wrapped: warnings.warn(
assert func.__name__ in dir(InterfaceClass), f"Error method: {func.__name__}" "overrides is deprecated and will be removed in a future version, "
return func "use @typing_extensions.override instead. "
"See [PEP 698](https://peps.python.org/pep-0698/) for more details.",
return overrider DeprecationWarning,
)
return override
# state # state
T_State = Dict[Any, Any] T_State: TypeAlias = Dict[Any, Any]
"""事件处理状态 State 类型""" """事件处理状态 State 类型"""
_DependentCallable = Union[Callable[..., T], Callable[..., Awaitable[T]]] _DependentCallable: TypeAlias = Union[Callable[..., T], Callable[..., Awaitable[T]]]
# driver hooks # driver hooks
T_BotConnectionHook = _DependentCallable[Any] T_BotConnectionHook: TypeAlias = _DependentCallable[Any]
"""Bot 连接建立时钩子函数 """Bot 连接建立时钩子函数
依赖参数: 依赖参数:
@@ -60,7 +63,7 @@ T_BotConnectionHook = _DependentCallable[Any]
- BotParam: Bot 对象 - BotParam: Bot 对象
- DefaultParam: 带有默认值的参数 - DefaultParam: 带有默认值的参数
""" """
T_BotDisconnectionHook = _DependentCallable[Any] T_BotDisconnectionHook: TypeAlias = _DependentCallable[Any]
"""Bot 连接断开时钩子函数 """Bot 连接断开时钩子函数
依赖参数: 依赖参数:
@@ -71,15 +74,15 @@ T_BotDisconnectionHook = _DependentCallable[Any]
""" """
# api hooks # api hooks
T_CallingAPIHook = Callable[["Bot", str, Dict[str, Any]], Awaitable[Any]] T_CallingAPIHook: TypeAlias = Callable[["Bot", str, Dict[str, Any]], Awaitable[Any]]
"""`bot.call_api` 钩子函数""" """`bot.call_api` 钩子函数"""
T_CalledAPIHook = Callable[ T_CalledAPIHook: TypeAlias = Callable[
["Bot", Optional[Exception], str, Dict[str, Any], Any], Awaitable[Any] ["Bot", Optional[Exception], str, Dict[str, Any], Any], Awaitable[Any]
] ]
"""`bot.call_api` 后执行的函数,参数分别为 bot, exception, api, data, result""" """`bot.call_api` 后执行的函数,参数分别为 bot, exception, api, data, result"""
# event hooks # event hooks
T_EventPreProcessor = _DependentCallable[Any] T_EventPreProcessor: TypeAlias = _DependentCallable[Any]
"""事件预处理函数 EventPreProcessor 类型 """事件预处理函数 EventPreProcessor 类型
依赖参数: 依赖参数:
@@ -90,7 +93,7 @@ T_EventPreProcessor = _DependentCallable[Any]
- StateParam: State 对象 - StateParam: State 对象
- DefaultParam: 带有默认值的参数 - DefaultParam: 带有默认值的参数
""" """
T_EventPostProcessor = _DependentCallable[Any] T_EventPostProcessor: TypeAlias = _DependentCallable[Any]
"""事件预处理函数 EventPostProcessor 类型 """事件预处理函数 EventPostProcessor 类型
依赖参数: 依赖参数:
@@ -103,7 +106,7 @@ T_EventPostProcessor = _DependentCallable[Any]
""" """
# matcher run hooks # matcher run hooks
T_RunPreProcessor = _DependentCallable[Any] T_RunPreProcessor: TypeAlias = _DependentCallable[Any]
"""事件响应器运行前预处理函数 RunPreProcessor 类型 """事件响应器运行前预处理函数 RunPreProcessor 类型
依赖参数: 依赖参数:
@@ -115,7 +118,7 @@ T_RunPreProcessor = _DependentCallable[Any]
- MatcherParam: Matcher 对象 - MatcherParam: Matcher 对象
- DefaultParam: 带有默认值的参数 - DefaultParam: 带有默认值的参数
""" """
T_RunPostProcessor = _DependentCallable[Any] T_RunPostProcessor: TypeAlias = _DependentCallable[Any]
"""事件响应器运行后后处理函数 RunPostProcessor 类型 """事件响应器运行后后处理函数 RunPostProcessor 类型
依赖参数: 依赖参数:
@@ -130,7 +133,7 @@ T_RunPostProcessor = _DependentCallable[Any]
""" """
# rule, permission # rule, permission
T_RuleChecker = _DependentCallable[bool] T_RuleChecker: TypeAlias = _DependentCallable[bool]
"""RuleChecker 即判断是否响应事件的处理函数。 """RuleChecker 即判断是否响应事件的处理函数。
依赖参数: 依赖参数:
@@ -141,7 +144,7 @@ T_RuleChecker = _DependentCallable[bool]
- StateParam: State 对象 - StateParam: State 对象
- DefaultParam: 带有默认值的参数 - DefaultParam: 带有默认值的参数
""" """
T_PermissionChecker = _DependentCallable[bool] T_PermissionChecker: TypeAlias = _DependentCallable[bool]
"""PermissionChecker 即判断事件是否满足权限的处理函数。 """PermissionChecker 即判断事件是否满足权限的处理函数。
依赖参数: 依赖参数:
@@ -152,10 +155,11 @@ T_PermissionChecker = _DependentCallable[bool]
- DefaultParam: 带有默认值的参数 - DefaultParam: 带有默认值的参数
""" """
T_Handler = _DependentCallable[Any] T_Handler: TypeAlias = _DependentCallable[Any]
"""Handler 处理函数。""" """Handler 处理函数。"""
T_TypeUpdater = _DependentCallable[str] T_TypeUpdater: TypeAlias = _DependentCallable[str]
"""TypeUpdater 在 Matcher.pause, Matcher.reject 时被运行,用于更新响应的事件类型。默认会更新为 `message`。 """TypeUpdater 在 Matcher.pause, Matcher.reject 时被运行,用于更新响应的事件类型。
默认会更新为 `message`。
依赖参数: 依赖参数:
@@ -166,8 +170,9 @@ T_TypeUpdater = _DependentCallable[str]
- MatcherParam: Matcher 对象 - MatcherParam: Matcher 对象
- DefaultParam: 带有默认值的参数 - DefaultParam: 带有默认值的参数
""" """
T_PermissionUpdater = _DependentCallable["Permission"] T_PermissionUpdater: TypeAlias = _DependentCallable["Permission"]
"""PermissionUpdater 在 Matcher.pause, Matcher.reject 时被运行,用于更新会话对象权限。默认会更新为当前事件的触发对象。 """PermissionUpdater 在 Matcher.pause, Matcher.reject 时被运行,用于更新会话对象权限。
默认会更新为当前事件的触发对象。
依赖参数: 依赖参数:
@@ -178,5 +183,5 @@ T_PermissionUpdater = _DependentCallable["Permission"]
- MatcherParam: Matcher 对象 - MatcherParam: Matcher 对象
- DefaultParam: 带有默认值的参数 - DefaultParam: 带有默认值的参数
""" """
T_DependencyCache = Dict[_DependentCallable[Any], "Task[Any]"] T_DependencyCache: TypeAlias = Dict[_DependentCallable[Any], "Task[Any]"]
"""依赖缓存, 用于存储依赖函数的返回值""" """依赖缓存, 用于存储依赖函数的返回值"""

View File

@@ -15,12 +15,13 @@ from pathlib import Path
from contextvars import copy_context from contextvars import copy_context
from functools import wraps, partial from functools import wraps, partial
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from typing_extensions import ParamSpec, get_args, get_origin from typing_extensions import ParamSpec, get_args, override, get_origin
from typing import ( from typing import (
Any, Any,
Type, Type,
Tuple, Tuple,
Union, Union,
Generic,
TypeVar, TypeVar,
Callable, Callable,
Optional, Optional,
@@ -30,10 +31,9 @@ from typing import (
overload, overload,
) )
from pydantic.typing import is_union, is_none_type from pydantic.typing import is_union, is_none_type, is_literal_type, all_literal_values
from nonebot.log import logger from nonebot.log import logger
from nonebot.typing import overrides
P = ParamSpec("P") P = ParamSpec("P")
R = TypeVar("R") R = TypeVar("R")
@@ -58,8 +58,13 @@ def generic_check_issubclass(
) -> bool: ) -> bool:
"""检查 cls 是否是 class_or_tuple 中的一个类型子类。 """检查 cls 是否是 class_or_tuple 中的一个类型子类。
特别的,如果 cls 是 `typing.Union` 或 `types.UnionType` 类型, 特别的
则会检查其中的所有类型是否是 class_or_tuple 中一个类型的子类或 None。
- 如果 cls 是 `typing.Union` 或 `types.UnionType` 类型,
则会检查其中的所有类型是否是 class_or_tuple 中一个类型的子类或 None。
- 如果 cls 是 `typing.TypeVar` 类型,
则会检查其 `__bound__` 或 `__constraints__`
是否是 class_or_tuple 中一个类型的子类或 None。
""" """
try: try:
return issubclass(cls, class_or_tuple) return issubclass(cls, class_or_tuple)
@@ -70,8 +75,27 @@ def generic_check_issubclass(
is_none_type(type_) or generic_check_issubclass(type_, class_or_tuple) is_none_type(type_) or generic_check_issubclass(type_, class_or_tuple)
for type_ in get_args(cls) for type_ in get_args(cls)
) )
elif is_literal_type(cls):
return all(
is_none_type(value) or isinstance(value, class_or_tuple)
for value in all_literal_values(cls)
)
# ensure generic List, Dict can be checked
elif origin: elif origin:
return issubclass(origin, class_or_tuple) # avoid class check error (typing.Final, typing.ClassVar, etc...)
try:
return issubclass(origin, class_or_tuple)
except TypeError:
return False
elif isinstance(cls, TypeVar):
if cls.__constraints__:
return all(
is_none_type(type_)
or generic_check_issubclass(type_, class_or_tuple)
for type_ in cls.__constraints__
)
elif cls.__bound__:
return generic_check_issubclass(cls.__bound__, class_or_tuple)
return False return False
@@ -157,6 +181,17 @@ async def run_coro_with_catch(
exc: Tuple[Type[Exception], ...], exc: Tuple[Type[Exception], ...],
return_on_err: Optional[R] = None, return_on_err: Optional[R] = None,
) -> Optional[Union[T, R]]: ) -> Optional[Union[T, R]]:
"""运行协程并当遇到指定异常时返回指定值。
参数:
coro: 要运行的协程
exc: 要捕获的异常
return_on_err: 当发生异常时返回的值
返回:
协程的返回值或发生异常时的指定值
"""
try: try:
return await coro return await coro
except exc: except exc:
@@ -195,10 +230,20 @@ def resolve_dot_notation(
return instance return instance
class DataclassEncoder(json.JSONEncoder): class classproperty(Generic[T]):
"""在JSON序列化 {ref}`nonebot.adapters.Message` (List[Dataclass]) 时使用的 `JSONEncoder`""" """类属性装饰器"""
@overrides(json.JSONEncoder) def __init__(self, func: Callable[[Any], T]) -> None:
self.func = func
def __get__(self, instance: Any, owner: Optional[Type[Any]] = None) -> T:
return self.func(type(instance) if owner is None else owner)
class DataclassEncoder(json.JSONEncoder):
"""可以序列化 {ref}`nonebot.adapters.Message`(List[Dataclass]) 的 `JSONEncoder`"""
@override
def default(self, o): def default(self, o):
if dataclasses.is_dataclass(o): if dataclasses.is_dataclass(o):
return {f.name: getattr(o, f.name) for f in dataclasses.fields(o)} return {f.name: getattr(o, f.name) for f in dataclasses.fields(o)}
@@ -214,9 +259,11 @@ def logger_wrapper(logger_name: str):
返回: 返回:
日志记录函数 日志记录函数
- level: 日志等级 日志记录函数的参数:
- message: 日志信息
- exception: 异常信息 - level: 日志等级
- message: 日志信息
- exception: 异常信息
""" """
def log(level: str, message: str, exception: Optional[Exception] = None): def log(level: str, message: str, exception: Optional[Exception] = None):

View File

@@ -11,10 +11,31 @@
"start": "yarn workspace nonebot start", "start": "yarn workspace nonebot start",
"serve": "yarn workspace nonebot serve", "serve": "yarn workspace nonebot serve",
"clear": "yarn workspace nonebot clear", "clear": "yarn workspace nonebot clear",
"prettier": "prettier --config ./.prettierrc --write \"./website/\"" "prettier": "prettier --config ./.prettierrc --write \"./website/\"",
"lint": "yarn lint:js && yarn lint:style",
"lint:js": "eslint --cache --report-unused-disable-directives \"**/*.{js,jsx,ts,tsx,mjs}\"",
"lint:js:fix": "eslint --cache --report-unused-disable-directives --fix \"**/*.{js,jsx,ts,tsx,mjs}\"",
"lint:style": "stylelint \"**/*.css\"",
"lint:style:fix": "stylelint --fix \"**/*.css\"",
"pyright": "pyright"
}, },
"devDependencies": { "devDependencies": {
"@typescript-eslint/eslint-plugin": "^6.6.0",
"@typescript-eslint/parser": "^6.6.0",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"prettier": "^2.5.0" "eslint": "^8.48.0",
"eslint-config-prettier": "^9.0.0",
"eslint-import-resolver-typescript": "^3.6.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jsx-a11y": "^6.7.1",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-regexp": "^1.15.0",
"prettier": "^3.0.3",
"pyright": "^1.1.317",
"stylelint": "^15.10.3",
"stylelint-config-standard": "^34.0.0",
"stylelint-prettier": "^4.0.2"
} }
} }

View File

@@ -22,6 +22,6 @@ _✨ NoneBot 本地文档插件 ✨_
## 使用方式 ## 使用方式
加载插件并启动 Bot ,在浏览器内打开 `http://host:port/docs/` 加载插件并启动 Bot ,在浏览器内打开 `http://host:port/website/`
具体网址会在控制台内输出。 具体网址会在控制台内输出。

View File

@@ -2,6 +2,17 @@ import importlib
import nonebot import nonebot
from nonebot.log import logger from nonebot.log import logger
from nonebot.plugin import PluginMetadata
__plugin_meta__ = PluginMetadata(
name="NoneBot 离线文档",
description="在本地查看 NoneBot 文档",
usage="启动机器人后访问 http://localhost:port/website/ 查看文档",
type="application",
homepage="https://github.com/nonebot/nonebot2/blob/master/packages/nonebot-plugin-docs",
config=None,
supported_adapters=None,
)
def init(): def init():
@@ -17,7 +28,7 @@ def init():
register_route(driver) register_route(driver)
host = str(driver.config.host) host = str(driver.config.host)
port = driver.config.port port = driver.config.port
if host in ["0.0.0.0", "127.0.0.1"]: if host in {"0.0.0.0", "127.0.0.1"}:
host = "localhost" host = "localhost"
logger.opt(colors=True).info( logger.opt(colors=True).info(
f"Nonebot docs will be running at: " f"Nonebot docs will be running at: "

View File

@@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "nonebot-plugin-docs" name = "nonebot-plugin-docs"
version = "2.0.0-beta.1" version = "2.0.0"
description = "View NoneBot2 Docs Locally" description = "View NoneBot2 Docs Locally"
authors = ["yanyongyu <yyy@nonebot.dev>"] authors = ["yanyongyu <yyy@nonebot.dev>"]
license = "MIT" license = "MIT"
@@ -13,7 +13,7 @@ include = ["nonebot_plugin_docs/dist/**/*"]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.8" python = "^3.8"
nonebot2 = "^2.0.0-beta.1" nonebot2 = "^2.0.0"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]

2223
poetry.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "nonebot2" name = "nonebot2"
version = "2.0.0" version = "2.1.2"
description = "An asynchronous python bot framework." description = "An asynchronous python bot framework."
authors = ["yanyongyu <yyy@nonebot.dev>"] authors = ["yanyongyu <yyy@nonebot.dev>"]
license = "MIT" license = "MIT"
@@ -14,11 +14,9 @@ classifiers = [
"Framework :: Robot Framework", "Framework :: Robot Framework",
"Framework :: Robot Framework :: Library", "Framework :: Robot Framework :: Library",
"Operating System :: OS Independent", "Operating System :: OS Independent",
"Programming Language :: Python :: 3" "Programming Language :: Python :: 3",
]
packages = [
{ include = "nonebot" },
] ]
packages = [{ include = "nonebot" }]
include = ["nonebot/py.typed"] include = ["nonebot/py.typed"]
[tool.poetry.urls] [tool.poetry.urls]
@@ -31,7 +29,7 @@ python = "^3.8"
yarl = "^1.7.2" yarl = "^1.7.2"
pygtrie = "^2.4.1" pygtrie = "^2.4.1"
loguru = ">=0.6.0,<1.0.0" loguru = ">=0.6.0,<1.0.0"
typing-extensions = ">=4.0.0,<5.0.0" typing-extensions = ">=4.4.0,<5.0.0"
tomli = { version = "^2.0.1", python = "<3.11" } tomli = { version = "^2.0.1", python = "<3.11" }
pydantic = { version = "^1.10.0", extras = ["dotenv"] } pydantic = { version = "^1.10.0", extras = ["dotenv"] }
@@ -40,21 +38,24 @@ Quart = { version = ">=0.18.0,<1.0.0", optional = true }
fastapi = { version = ">=0.93.0,<1.0.0", optional = true } fastapi = { version = ">=0.93.0,<1.0.0", optional = true }
aiohttp = { version = "^3.7.4", extras = ["speedups"], optional = true } aiohttp = { version = "^3.7.4", extras = ["speedups"], optional = true }
httpx = { version = ">=0.20.0,<1.0.0", extras = ["http2"], optional = true } httpx = { version = ">=0.20.0,<1.0.0", extras = ["http2"], optional = true }
uvicorn = { version = ">=0.20.0,<1.0.0", extras = ["standard"], optional = true } uvicorn = { version = ">=0.20.0,<1.0.0", extras = [
"standard",
], optional = true }
[tool.poetry.group.dev.dependencies] [tool.poetry.group.dev.dependencies]
pycln = "^2.1.2"
isort = "^5.10.1" isort = "^5.10.1"
black = "^23.1.0" black = "^23.1.0"
nonemoji = "^0.1.2" nonemoji = "^0.1.2"
pre-commit = "^3.0.0" pre-commit = "^3.0.0"
ruff = ">=0.0.272,<1.0.0"
[tool.poetry.group.test.dependencies] [tool.poetry.group.test.dependencies]
nonebug = "^0.3.0" nonebug = "^0.3.0"
pytest-cov = "^4.0.0" pytest-cov = "^4.0.0"
pytest-xdist = "^3.0.2" pytest-xdist = "^3.0.2"
pytest-asyncio = "^0.21.0" pytest-asyncio = "^0.22.0"
coverage-conditional-plugin = "^0.8.0" werkzeug = ">=2.3.6,<4.0.0"
coverage-conditional-plugin = "^0.9.0"
[tool.poetry.group.docs.dependencies] [tool.poetry.group.docs.dependencies]
nb-autodoc = "^1.0.0a5" nb-autodoc = "^1.0.0a5"
@@ -68,12 +69,9 @@ fastapi = ["fastapi", "uvicorn"]
all = ["fastapi", "quart", "aiohttp", "httpx", "websockets", "uvicorn"] all = ["fastapi", "quart", "aiohttp", "httpx", "websockets", "uvicorn"]
[tool.pytest.ini_options] [tool.pytest.ini_options]
asyncio_mode = "auto" asyncio_mode = "strict"
addopts = "--cov=nonebot --cov-append --cov-report=term-missing" addopts = "--cov=nonebot --cov-append --cov-report=term-missing"
filterwarnings = [ filterwarnings = ["error", "ignore::DeprecationWarning"]
"error",
"ignore::DeprecationWarning",
]
[tool.black] [tool.black]
line-length = 88 line-length = 88
@@ -91,19 +89,30 @@ force_sort_within_sections = true
src_paths = ["nonebot", "tests"] src_paths = ["nonebot", "tests"]
extra_standard_library = ["typing_extensions"] extra_standard_library = ["typing_extensions"]
[tool.pycln] [tool.ruff]
path = "." select = ["E", "W", "F", "UP", "C", "T", "PYI", "PT", "Q"]
all = false ignore = ["E402", "C901", "UP037"]
line-length = 88
target-version = "py38"
[tool.ruff.flake8-pytest-style]
fixture-parentheses = false
mark-parentheses = false
[tool.pyright] [tool.pyright]
reportShadowedImports = false
pythonVersion = "3.8" pythonVersion = "3.8"
pythonPlatform = "All" pythonPlatform = "All"
executionEnvironments = [ executionEnvironments = [
{ root = "./tests", extraPaths = ["./"] }, { root = "./tests", extraPaths = [
"./",
] },
{ root = "./" }, { root = "./" },
] ]
typeCheckingMode = "basic"
reportShadowedImports = false
disableBytesTypePromotions = true
[build-system] [build-system]
requires = ["poetry_core>=1.0.0"] requires = ["poetry_core>=1.0.0"]

View File

@@ -11,6 +11,7 @@ exclude_lines =
if (typing\.)?TYPE_CHECKING( is True)?: if (typing\.)?TYPE_CHECKING( is True)?:
@(abc\.)?abstractmethod @(abc\.)?abstractmethod
raise NotImplementedError raise NotImplementedError
warnings\.warn
\.\.\. \.\.\.
pass pass
if __name__ == .__main__.: if __name__ == .__main__.:

View File

@@ -1,6 +1,8 @@
LOG_LEVEL=TRACE LOG_LEVEL=TRACE
NICKNAME=["test"] NICKNAME=["test"]
SUPERUSERS=["test", "fake:faketest"] SUPERUSERS=["test", "fake:faketest"]
API_TIMEOUT
SIMPLE_NONE
COMMON_OVERRIDE=new COMMON_OVERRIDE=new
CONFIG_FROM_ENV= CONFIG_FROM_ENV=
CONFIG_OVERRIDE=old CONFIG_OVERRIDE=old

View File

@@ -1,11 +1,17 @@
import os import os
import threading
from pathlib import Path from pathlib import Path
from typing import TYPE_CHECKING, Set from typing import TYPE_CHECKING, Set, Generator
import pytest import pytest
from nonebug import NONEBOT_INIT_KWARGS from nonebug import NONEBOT_INIT_KWARGS
from werkzeug.serving import BaseWSGIServer, make_server
import nonebot import nonebot
from nonebot.config import Env
from fake_server import request_handler
from nonebot.drivers import URL, Driver
from nonebot import _resolve_combine_expr
os.environ["CONFIG_FROM_ENV"] = '{"test": "test"}' os.environ["CONFIG_FROM_ENV"] = '{"test": "test"}'
os.environ["CONFIG_OVERRIDE"] = "new" os.environ["CONFIG_OVERRIDE"] = "new"
@@ -13,11 +19,24 @@ os.environ["CONFIG_OVERRIDE"] = "new"
if TYPE_CHECKING: if TYPE_CHECKING:
from nonebot.plugin import Plugin from nonebot.plugin import Plugin
collect_ignore = ["plugins/", "dynamic/", "bad_plugins/"]
def pytest_configure(config: pytest.Config) -> None: def pytest_configure(config: pytest.Config) -> None:
config.stash[NONEBOT_INIT_KWARGS] = {"config_from_init": "init"} config.stash[NONEBOT_INIT_KWARGS] = {"config_from_init": "init"}
@pytest.fixture(name="driver")
def load_driver(request: pytest.FixtureRequest) -> Driver:
driver_name = getattr(request, "param", None)
global_driver = nonebot.get_driver()
if driver_name is None:
return global_driver
DriverClass = _resolve_combine_expr(driver_name)
return DriverClass(Env(environment=global_driver.env), global_driver.config)
@pytest.fixture(scope="session", autouse=True) @pytest.fixture(scope="session", autouse=True)
def load_plugin(nonebug_init: None) -> Set["Plugin"]: def load_plugin(nonebug_init: None) -> Set["Plugin"]:
# preload global plugins # preload global plugins
@@ -28,3 +47,20 @@ def load_plugin(nonebug_init: None) -> Set["Plugin"]:
def load_builtin_plugin(nonebug_init: None) -> Set["Plugin"]: def load_builtin_plugin(nonebug_init: None) -> Set["Plugin"]:
# preload builtin plugins # preload builtin plugins
return nonebot.load_builtin_plugins("echo", "single_session") return nonebot.load_builtin_plugins("echo", "single_session")
@pytest.fixture(scope="session", autouse=True)
def server() -> Generator[BaseWSGIServer, None, None]:
server = make_server("127.0.0.1", 0, app=request_handler)
thread = threading.Thread(target=server.serve_forever)
thread.start()
try:
yield server
finally:
server.shutdown()
thread.join()
@pytest.fixture(scope="session")
def server_url(server: BaseWSGIServer) -> URL:
return URL(f"http://{server.host}:{server.port}")

69
tests/fake_server.py Normal file
View File

@@ -0,0 +1,69 @@
import json
import base64
from typing import Dict, List, Union, TypeVar
from werkzeug import Request, Response
from werkzeug.datastructures import MultiDict
K = TypeVar("K")
V = TypeVar("V")
def json_safe(string, content_type="application/octet-stream") -> str:
try:
string = string.decode("utf-8")
json.dumps(string)
return string
except (ValueError, TypeError):
return b"".join(
[
b"data:",
content_type.encode("utf-8"),
b";base64,",
base64.b64encode(string),
]
).decode("utf-8")
def flattern(d: "MultiDict[K, V]") -> Dict[K, Union[V, List[V]]]:
return {k: v[0] if len(v) == 1 else v for k, v in d.to_dict(flat=False).items()}
@Request.application
def request_handler(request: Request) -> Response:
try:
_json = json.loads(request.data.decode("utf-8"))
except (ValueError, TypeError):
_json = None
return Response(
json.dumps(
{
"url": request.url,
"method": request.method,
"origin": request.headers.get("X-Forwarded-For", request.remote_addr),
"headers": flattern(
MultiDict((k, v) for k, v in request.headers.items())
),
"args": flattern(request.args),
"form": flattern(request.form),
"data": json_safe(request.data),
"json": _json,
"files": flattern(
MultiDict(
(
k,
json_safe(
v.read(),
request.files[k].content_type
or "application/octet-stream",
),
)
for k, v in request.files.items()
)
),
}
),
status=200,
content_type="application/json",
)

View File

@@ -1 +1,3 @@
assert False import pytest
pytest.fail("should not be imported")

View File

@@ -0,0 +1,3 @@
from nonebot import on
matcher = on("message", temp=False, expire_time=None, priority=1, block=True)

View File

@@ -2,6 +2,7 @@ from nonebot.matcher import Matcher
from nonebot.permission import USER, Permission from nonebot.permission import USER, Permission
default_permission = Permission() default_permission = Permission()
new_permission = Permission()
test_permission_updater = Matcher.new(permission=default_permission) test_permission_updater = Matcher.new(permission=default_permission)
@@ -14,4 +15,4 @@ test_custom_updater = Matcher.new(permission=default_permission)
@test_custom_updater.permission_updater @test_custom_updater.permission_updater
async def _() -> Permission: async def _() -> Permission:
return default_permission return new_permission

View File

@@ -0,0 +1,11 @@
from nonebot.plugin import PluginMetadata
__plugin_meta__ = PluginMetadata(
name="测试插件2",
description="测试继承适配器",
usage="无法使用",
type="application",
homepage="https://nonebot.dev",
supported_adapters={"~onebot.v11", "~onebot.v12"},
extra={"author": "NoneBot"},
)

View File

@@ -1,6 +1,5 @@
from pathlib import Path from pathlib import Path
import nonebot
from nonebot.plugin import PluginManager, _managers from nonebot.plugin import PluginManager, _managers
manager = PluginManager( manager = PluginManager(

View File

@@ -1 +1 @@
from .nested_subplugin2 import a # nopycln: import from .nested_subplugin2 import a # noqa: F401

View File

@@ -1,4 +1,6 @@
from nonebot.adapters import Event, Message from typing_extensions import Annotated
from nonebot.adapters import Message
from nonebot.params import Arg, ArgStr, ArgPlainText from nonebot.params import Arg, ArgStr, ArgPlainText
@@ -12,3 +14,26 @@ async def arg_str(key: str = ArgStr()) -> str:
async def arg_plain_text(key: str = ArgPlainText()) -> str: async def arg_plain_text(key: str = ArgPlainText()) -> str:
return key return key
async def annotated_arg(key: Annotated[Message, Arg()]) -> Message:
return key
async def annotated_arg_str(key: Annotated[str, ArgStr()]) -> str:
return key
async def annotated_arg_plain_text(key: Annotated[str, ArgPlainText()]) -> str:
return key
# test dependency priority
async def annotated_prior_arg(key: Annotated[str, ArgStr("foo")] = ArgPlainText()):
return key
async def annotated_multi_arg(
key: Annotated[Annotated[str, ArgStr("foo")], ArgPlainText()]
):
return key

View File

@@ -1,4 +1,4 @@
from typing import Union from typing import Union, TypeVar
from nonebot.adapters import Bot from nonebot.adapters import Bot
@@ -31,5 +31,19 @@ async def union_bot(b: Union[FooBot, BarBot]) -> Union[FooBot, BarBot]:
return b return b
B = TypeVar("B", bound=Bot)
async def generic_bot(b: B) -> B:
return b
CB = TypeVar("CB", Bot, None)
async def generic_bot_none(b: CB) -> CB:
return b
async def not_bot(b: Union[int, Bot]): async def not_bot(b: Union[int, Bot]):
... ...

View File

@@ -1,7 +1,10 @@
from dataclasses import dataclass from dataclasses import dataclass
from typing_extensions import Annotated from typing_extensions import Annotated
from pydantic import Field
from nonebot import on_message from nonebot import on_message
from nonebot.adapters import Bot
from nonebot.params import Depends from nonebot.params import Depends
test_depends = on_message() test_depends = on_message()
@@ -33,6 +36,14 @@ class ClassDependency:
y: int = Depends(gen_async) y: int = Depends(gen_async)
class FooBot(Bot):
...
async def sub_bot(b: FooBot) -> FooBot:
return b
# test parameterless # test parameterless
@test_depends.handle(parameterless=[Depends(parameterless)]) @test_depends.handle(parameterless=[Depends(parameterless)])
async def depends(x: int = Depends(dependency)): async def depends(x: int = Depends(dependency)):
@@ -46,19 +57,52 @@ async def depends_cache(y: int = Depends(dependency, use_cache=True)):
return y return y
# test class dependency
async def class_depend(c: ClassDependency = Depends()): async def class_depend(c: ClassDependency = Depends()):
return c return c
# test annotated dependency
async def annotated_depend(x: Annotated[int, Depends(dependency)]): async def annotated_depend(x: Annotated[int, Depends(dependency)]):
return x return x
# test annotated class dependency
async def annotated_class_depend(c: Annotated[ClassDependency, Depends()]): async def annotated_class_depend(c: Annotated[ClassDependency, Depends()]):
return c return c
# test dependency priority
async def annotated_prior_depend( async def annotated_prior_depend(
x: Annotated[int, Depends(lambda: 2)] = Depends(dependency) x: Annotated[int, Depends(lambda: 2)] = Depends(dependency)
): ):
return x return x
async def annotated_multi_depend(
x: Annotated[Annotated[int, Depends(lambda: 2)], Depends(dependency)]
):
return x
# test sub dependency type mismatch
async def sub_type_mismatch(b: FooBot = Depends(sub_bot)):
return b
# test type validate
async def validate(x: int = Depends(lambda: "1", validate=True)):
return x
async def validate_fail(x: int = Depends(lambda: "not_number", validate=True)):
return x
# test FieldInfo validate
async def validate_field(x: int = Depends(lambda: "1", validate=Field(gt=0))):
return x
async def validate_field_fail(x: int = Depends(lambda: "0", validate=Field(gt=0))):
return x

View File

@@ -1,4 +1,4 @@
from typing import Union from typing import Union, TypeVar
from nonebot.adapters import Event, Message from nonebot.adapters import Event, Message
from nonebot.params import EventToMe, EventType, EventMessage, EventPlainText from nonebot.params import EventToMe, EventType, EventMessage, EventPlainText
@@ -32,6 +32,20 @@ async def union_event(e: Union[FooEvent, BarEvent]) -> Union[FooEvent, BarEvent]
return e return e
E = TypeVar("E", bound=Event)
async def generic_event(e: E) -> E:
return e
CE = TypeVar("CE", Event, None)
async def generic_event_none(e: CE) -> CE:
return e
async def not_event(e: Union[int, Event]): async def not_event(e: Union[int, Event]):
... ...

View File

@@ -1,3 +1,5 @@
from typing import Union, TypeVar
from nonebot.adapters import Event from nonebot.adapters import Event
from nonebot.matcher import Matcher from nonebot.matcher import Matcher
from nonebot.params import Received, LastReceived from nonebot.params import Received, LastReceived
@@ -7,6 +9,50 @@ async def matcher(m: Matcher) -> Matcher:
return m return m
async def legacy_matcher(matcher):
return matcher
async def not_legacy_matcher(matcher: int):
...
class FooMatcher(Matcher):
...
async def sub_matcher(m: FooMatcher) -> FooMatcher:
return m
class BarMatcher(Matcher):
...
async def union_matcher(
m: Union[FooMatcher, BarMatcher]
) -> Union[FooMatcher, BarMatcher]:
return m
M = TypeVar("M", bound=Matcher)
async def generic_matcher(m: M) -> M:
return m
CM = TypeVar("CM", Matcher, None)
async def generic_matcher_none(m: CM) -> CM:
return m
async def not_matcher(m: Union[int, Matcher]):
...
async def receive(e: Event = Received("test")) -> Event: async def receive(e: Event = Received("test")) -> Event:
return e return e

View File

@@ -1 +1 @@
from . import matchers from . import matchers as matchers

View File

@@ -220,7 +220,7 @@ matcher_on_type = on_type(
cmd_group = CommandGroup( cmd_group = CommandGroup(
"test", "prefix",
rule=rule, rule=rule,
permission=permission, permission=permission,
handlers=[handler], handlers=[handler],
@@ -230,8 +230,30 @@ cmd_group = CommandGroup(
block=True, block=True,
state=state, state=state,
) )
matcher_sub_cmd = cmd_group.command("sub") matcher_prefix_cmd = cmd_group.command("sub", aliases={"help", ("help", "foo")})
matcher_sub_shell_cmd = cmd_group.shell_command("sub") matcher_prefix_shell_cmd = cmd_group.shell_command(
"sub", aliases={"help", ("help", "foo")}
)
cmd_group_prefix_aliases = CommandGroup(
"prefix",
prefix_aliases=True,
rule=rule,
permission=permission,
handlers=[handler],
temp=True,
expire_time=expire_time,
priority=priority,
block=True,
state=state,
)
matcher_prefix_aliases_cmd = cmd_group_prefix_aliases.command(
"sub", aliases={"help", ("help", "foo")}
)
matcher_prefix_aliases_shell_cmd = cmd_group_prefix_aliases.shell_command(
"sub", aliases={"help", ("help", "foo")}
)
matcher_group = MatcherGroup( matcher_group = MatcherGroup(

View File

@@ -4,4 +4,5 @@ test_require = require("export").test
from plugins.export import test from plugins.export import test
assert test is test_require and test() == "export", "Export Require Error" assert test is test_require, "Export Require Error"
assert test() == "export", "Export Require Error"

View File

@@ -0,0 +1,211 @@
from typing import Optional
from contextlib import asynccontextmanager
import pytest
from nonebug import App
from utils import FakeAdapter
from nonebot.adapters import Bot
from nonebot.drivers import (
URL,
Driver,
Request,
Response,
WebSocket,
HTTPServerSetup,
WebSocketServerSetup,
)
@pytest.mark.asyncio
async def test_adapter_connect(app: App, driver: Driver):
last_connect_bot: Optional[Bot] = None
last_disconnect_bot: Optional[Bot] = None
def _fake_bot_connect(bot: Bot):
nonlocal last_connect_bot
last_connect_bot = bot
def _fake_bot_disconnect(bot: Bot):
nonlocal last_disconnect_bot
last_disconnect_bot = bot
with pytest.MonkeyPatch.context() as m:
m.setattr(driver, "_bot_connect", _fake_bot_connect)
m.setattr(driver, "_bot_disconnect", _fake_bot_disconnect)
adapter = FakeAdapter(driver)
async with app.test_api() as ctx:
bot = ctx.create_bot(adapter=adapter)
assert last_connect_bot is bot
assert adapter.bots[bot.self_id] is bot
assert last_disconnect_bot is bot
assert bot.self_id not in adapter.bots
@pytest.mark.asyncio
@pytest.mark.parametrize(
"driver",
[
pytest.param("nonebot.drivers.fastapi:Driver", id="fastapi"),
pytest.param("nonebot.drivers.quart:Driver", id="quart"),
pytest.param(
"nonebot.drivers.httpx:Driver",
id="httpx",
marks=pytest.mark.xfail(
reason="not a server", raises=TypeError, strict=True
),
),
pytest.param(
"nonebot.drivers.websockets:Driver",
id="websockets",
marks=pytest.mark.xfail(
reason="not a server", raises=TypeError, strict=True
),
),
pytest.param(
"nonebot.drivers.aiohttp:Driver",
id="aiohttp",
marks=pytest.mark.xfail(
reason="not a server", raises=TypeError, strict=True
),
),
],
indirect=True,
)
async def test_adapter_server(driver: Driver):
last_http_setup: Optional[HTTPServerSetup] = None
last_ws_setup: Optional[WebSocketServerSetup] = None
def _fake_setup_http_server(setup: HTTPServerSetup):
nonlocal last_http_setup
last_http_setup = setup
def _fake_setup_websocket_server(setup: WebSocketServerSetup):
nonlocal last_ws_setup
last_ws_setup = setup
with pytest.MonkeyPatch.context() as m:
m.setattr(driver, "setup_http_server", _fake_setup_http_server, raising=False)
m.setattr(
driver,
"setup_websocket_server",
_fake_setup_websocket_server,
raising=False,
)
async def handle_http(request: Request):
return Response(200, content="test")
async def handle_ws(ws: WebSocket):
...
adapter = FakeAdapter(driver)
setup = HTTPServerSetup(URL("/test"), "GET", "test", handle_http)
adapter.setup_http_server(setup)
assert last_http_setup is setup
setup = WebSocketServerSetup(URL("/test"), "test", handle_ws)
adapter.setup_websocket_server(setup)
assert last_ws_setup is setup
@pytest.mark.asyncio
@pytest.mark.parametrize(
"driver",
[
pytest.param(
"nonebot.drivers.fastapi:Driver",
id="fastapi",
marks=pytest.mark.xfail(
reason="not a http client", raises=TypeError, strict=True
),
),
pytest.param(
"nonebot.drivers.quart:Driver",
id="quart",
marks=pytest.mark.xfail(
reason="not a http client", raises=TypeError, strict=True
),
),
pytest.param("nonebot.drivers.httpx:Driver", id="httpx"),
pytest.param(
"nonebot.drivers.websockets:Driver",
id="websockets",
marks=pytest.mark.xfail(
reason="not a http client", raises=TypeError, strict=True
),
),
pytest.param("nonebot.drivers.aiohttp:Driver", id="aiohttp"),
],
indirect=True,
)
async def test_adapter_http_client(driver: Driver):
last_request: Optional[Request] = None
async def _fake_request(request: Request):
nonlocal last_request
last_request = request
with pytest.MonkeyPatch.context() as m:
m.setattr(driver, "request", _fake_request, raising=False)
adapter = FakeAdapter(driver)
request = Request("GET", URL("/test"))
await adapter.request(request)
assert last_request is request
@pytest.mark.asyncio
@pytest.mark.parametrize(
"driver",
[
pytest.param(
"nonebot.drivers.fastapi:Driver",
id="fastapi",
marks=pytest.mark.xfail(
reason="not a websocket client", raises=TypeError, strict=True
),
),
pytest.param(
"nonebot.drivers.quart:Driver",
id="quart",
marks=pytest.mark.xfail(
reason="not a websocket client", raises=TypeError, strict=True
),
),
pytest.param(
"nonebot.drivers.httpx:Driver",
id="httpx",
marks=pytest.mark.xfail(
reason="not a websocket client", raises=TypeError, strict=True
),
),
pytest.param("nonebot.drivers.websockets:Driver", id="websockets"),
pytest.param("nonebot.drivers.aiohttp:Driver", id="aiohttp"),
],
indirect=True,
)
async def test_adapter_websocket_client(driver: Driver):
_fake_ws = object()
_last_request: Optional[Request] = None
@asynccontextmanager
async def _fake_websocket(setup: Request):
nonlocal _last_request
_last_request = setup
yield _fake_ws
with pytest.MonkeyPatch.context() as m:
m.setattr(driver, "websocket", _fake_websocket, raising=False)
adapter = FakeAdapter(driver)
request = Request("GET", URL("/test"))
async with adapter.websocket(request) as ws:
assert _last_request is request
assert ws is _fake_ws

View File

@@ -0,0 +1,152 @@
from typing import Any, Dict, Optional
import pytest
from nonebug import App
from nonebot.adapters import Bot
from nonebot.exception import MockApiException
@pytest.mark.asyncio
async def test_bot_call_api(app: App):
async with app.test_api() as ctx:
bot = ctx.create_bot()
ctx.should_call_api("test", {}, True)
result = await bot.call_api("test")
assert result is True
async with app.test_api() as ctx:
bot = ctx.create_bot()
ctx.should_call_api("test", {}, exception=RuntimeError("test"))
with pytest.raises(RuntimeError, match="test"):
await bot.call_api("test")
@pytest.mark.asyncio
async def test_bot_calling_api_hook_simple(app: App):
runned: bool = False
async def calling_api_hook(bot: Bot, api: str, data: Dict[str, Any]):
nonlocal runned
runned = True
hooks = set()
with pytest.MonkeyPatch.context() as m:
m.setattr(Bot, "_calling_api_hook", hooks)
Bot.on_calling_api(calling_api_hook)
assert hooks == {calling_api_hook}
async with app.test_api() as ctx:
bot = ctx.create_bot()
ctx.should_call_api("test", {}, True)
result = await bot.call_api("test")
assert runned is True
assert result is True
@pytest.mark.asyncio
async def test_bot_calling_api_hook_mock(app: App):
runned: bool = False
async def calling_api_hook(bot: Bot, api: str, data: Dict[str, Any]):
nonlocal runned
runned = True
raise MockApiException(False)
hooks = set()
with pytest.MonkeyPatch.context() as m:
m.setattr(Bot, "_calling_api_hook", hooks)
Bot.on_calling_api(calling_api_hook)
assert hooks == {calling_api_hook}
async with app.test_api() as ctx:
bot = ctx.create_bot()
result = await bot.call_api("test")
assert runned is True
assert result is False
@pytest.mark.asyncio
async def test_bot_called_api_hook_simple(app: App):
runned: bool = False
async def called_api_hook(
bot: Bot,
exception: Optional[Exception],
api: str,
data: Dict[str, Any],
result: Any,
):
nonlocal runned
runned = True
hooks = set()
with pytest.MonkeyPatch.context() as m:
m.setattr(Bot, "_called_api_hook", hooks)
Bot.on_called_api(called_api_hook)
assert hooks == {called_api_hook}
async with app.test_api() as ctx:
bot = ctx.create_bot()
ctx.should_call_api("test", {}, True)
result = await bot.call_api("test")
assert runned is True
assert result is True
@pytest.mark.asyncio
async def test_bot_called_api_hook_mock(app: App):
runned: bool = False
async def called_api_hook(
bot: Bot,
exception: Optional[Exception],
api: str,
data: Dict[str, Any],
result: Any,
):
nonlocal runned
runned = True
raise MockApiException(False)
hooks = set()
with pytest.MonkeyPatch.context() as m:
m.setattr(Bot, "_called_api_hook", hooks)
Bot.on_called_api(called_api_hook)
assert hooks == {called_api_hook}
async with app.test_api() as ctx:
bot = ctx.create_bot()
ctx.should_call_api("test", {}, True)
result = await bot.call_api("test")
assert runned is True
assert result is False
runned = False
async with app.test_api() as ctx:
bot = ctx.create_bot()
ctx.should_call_api("test", {}, exception=RuntimeError("test"))
result = await bot.call_api("test")
assert runned is True
assert result is False

View File

@@ -1,135 +1,136 @@
import pytest import pytest
from pydantic import ValidationError, parse_obj_as from pydantic import ValidationError, parse_obj_as
from utils import make_fake_message from nonebot.adapters import Message
from utils import FakeMessage, FakeMessageSegment
def test_segment_add(): def test_segment_data():
Message = make_fake_message() assert len(FakeMessageSegment.text("text")) == 4
MessageSegment = Message.get_segment_class() assert FakeMessageSegment.text("text").get("data") == {"text": "text"}
assert list(FakeMessageSegment.text("text").keys()) == ["type", "data"]
assert MessageSegment.text("text") + MessageSegment.text("text") == Message( assert list(FakeMessageSegment.text("text").values()) == ["text", {"text": "text"}]
[MessageSegment.text("text"), MessageSegment.text("text")] assert list(FakeMessageSegment.text("text").items()) == [
)
assert MessageSegment.text("text") + "text" == Message(
[MessageSegment.text("text"), MessageSegment.text("text")]
)
assert (
MessageSegment.text("text") + Message([MessageSegment.text("text")])
) == Message([MessageSegment.text("text"), MessageSegment.text("text")])
assert "text" + MessageSegment.text("text") == Message(
[MessageSegment.text("text"), MessageSegment.text("text")]
)
def test_segment_validate():
Message = make_fake_message()
MessageSegment = Message.get_segment_class()
assert parse_obj_as(
MessageSegment,
{"type": "text", "data": {"text": "text"}, "extra": "should be ignored"},
) == MessageSegment.text("text")
with pytest.raises(ValidationError):
parse_obj_as(MessageSegment, "some str")
with pytest.raises(ValidationError):
parse_obj_as(MessageSegment, {"data": {}})
def test_segment_join():
Message = make_fake_message()
MessageSegment = Message.get_segment_class()
seg = MessageSegment.text("test")
iterable = [
MessageSegment.text("first"),
Message([MessageSegment.text("second"), MessageSegment.text("third")]),
]
assert seg.join(iterable) == Message(
[
MessageSegment.text("first"),
MessageSegment.text("test"),
MessageSegment.text("second"),
MessageSegment.text("third"),
]
)
def test_segment():
Message = make_fake_message()
MessageSegment = Message.get_segment_class()
assert len(MessageSegment.text("text")) == 4
assert MessageSegment.text("text") != MessageSegment.text("other")
assert MessageSegment.text("text").get("data") == {"text": "text"}
assert list(MessageSegment.text("text").keys()) == ["type", "data"]
assert list(MessageSegment.text("text").values()) == ["text", {"text": "text"}]
assert list(MessageSegment.text("text").items()) == [
("type", "text"), ("type", "text"),
("data", {"text": "text"}), ("data", {"text": "text"}),
] ]
origin = MessageSegment.text("text")
def test_segment_equal():
assert FakeMessageSegment("text", {"text": "text"}) == FakeMessageSegment(
"text", {"text": "text"}
)
assert FakeMessageSegment("text", {"text": "text"}) != FakeMessageSegment(
"text", {"text": "other"}
)
assert FakeMessageSegment("text", {"text": "text"}) != FakeMessageSegment(
"other", {"text": "text"}
)
def test_segment_add():
assert FakeMessageSegment.text("text") + FakeMessageSegment.text(
"text"
) == FakeMessage([FakeMessageSegment.text("text"), FakeMessageSegment.text("text")])
assert FakeMessageSegment.text("text") + "text" == FakeMessage(
[FakeMessageSegment.text("text"), FakeMessageSegment.text("text")]
)
assert (
FakeMessageSegment.text("text") + FakeMessage([FakeMessageSegment.text("text")])
) == FakeMessage([FakeMessageSegment.text("text"), FakeMessageSegment.text("text")])
assert "text" + FakeMessageSegment.text("text") == FakeMessage(
[FakeMessageSegment.text("text"), FakeMessageSegment.text("text")]
)
def test_segment_validate():
assert parse_obj_as(
FakeMessageSegment,
{"type": "text", "data": {"text": "text"}, "extra": "should be ignored"},
) == FakeMessageSegment.text("text")
with pytest.raises(ValidationError):
parse_obj_as(FakeMessageSegment, "some str")
with pytest.raises(ValidationError):
parse_obj_as(FakeMessageSegment, {"data": {}})
def test_segment_join():
seg = FakeMessageSegment.text("test")
iterable = [
FakeMessageSegment.text("first"),
FakeMessage(
[FakeMessageSegment.text("second"), FakeMessageSegment.text("third")]
),
]
assert seg.join(iterable) == FakeMessage(
[
FakeMessageSegment.text("first"),
FakeMessageSegment.text("test"),
FakeMessageSegment.text("second"),
FakeMessageSegment.text("third"),
]
)
def test_segment_copy():
origin = FakeMessageSegment.text("text")
copy = origin.copy() copy = origin.copy()
assert origin is not copy assert origin is not copy
assert origin == copy assert origin == copy
def test_message_add(): def test_message_add():
Message = make_fake_message()
MessageSegment = Message.get_segment_class()
assert ( assert (
Message([MessageSegment.text("text")]) + MessageSegment.text("text") FakeMessage([FakeMessageSegment.text("text")]) + FakeMessageSegment.text("text")
) == Message([MessageSegment.text("text"), MessageSegment.text("text")]) ) == FakeMessage([FakeMessageSegment.text("text"), FakeMessageSegment.text("text")])
assert Message([MessageSegment.text("text")]) + "text" == Message( assert FakeMessage([FakeMessageSegment.text("text")]) + "text" == FakeMessage(
[MessageSegment.text("text"), MessageSegment.text("text")] [FakeMessageSegment.text("text"), FakeMessageSegment.text("text")]
) )
assert ( assert (
Message([MessageSegment.text("text")]) + Message([MessageSegment.text("text")]) FakeMessage([FakeMessageSegment.text("text")])
) == Message([MessageSegment.text("text"), MessageSegment.text("text")]) + FakeMessage([FakeMessageSegment.text("text")])
) == FakeMessage([FakeMessageSegment.text("text"), FakeMessageSegment.text("text")])
assert "text" + Message([MessageSegment.text("text")]) == Message( assert "text" + FakeMessage([FakeMessageSegment.text("text")]) == FakeMessage(
[MessageSegment.text("text"), MessageSegment.text("text")] [FakeMessageSegment.text("text"), FakeMessageSegment.text("text")]
) )
msg = Message([MessageSegment.text("text")]) msg = FakeMessage([FakeMessageSegment.text("text")])
msg += MessageSegment.text("text") msg += FakeMessageSegment.text("text")
assert msg == Message([MessageSegment.text("text"), MessageSegment.text("text")]) assert msg == FakeMessage(
[FakeMessageSegment.text("text"), FakeMessageSegment.text("text")]
)
def test_message_getitem(): def test_message_getitem():
Message = make_fake_message() message = FakeMessage(
MessageSegment = Message.get_segment_class()
message = Message(
[ [
MessageSegment.text("test"), FakeMessageSegment.text("test"),
MessageSegment.image("test2"), FakeMessageSegment.image("test2"),
MessageSegment.image("test3"), FakeMessageSegment.image("test3"),
MessageSegment.text("test4"), FakeMessageSegment.text("test4"),
] ]
) )
assert message[0] == MessageSegment.text("test") assert message[0] == FakeMessageSegment.text("test")
assert message[:2] == Message( assert message[:2] == FakeMessage(
[MessageSegment.text("test"), MessageSegment.image("test2")] [FakeMessageSegment.text("test"), FakeMessageSegment.image("test2")]
) )
assert message["image"] == Message( assert message["image"] == FakeMessage(
[MessageSegment.image("test2"), MessageSegment.image("test3")] [FakeMessageSegment.image("test2"), FakeMessageSegment.image("test3")]
) )
assert message["image", 0] == MessageSegment.image("test2") assert message["image", 0] == FakeMessageSegment.image("test2")
assert message["image", 0:2] == message["image"] assert message["image", 0:2] == message["image"]
assert message.index(message[0]) == 0 assert message.index(message[0]) == 0
@@ -137,153 +138,137 @@ def test_message_getitem():
assert message.get("image") == message["image"] assert message.get("image") == message["image"]
assert message.get("image", 114514) == message["image"] assert message.get("image", 114514) == message["image"]
assert message.get("image", 1) == Message([message["image", 0]]) assert message.get("image", 1) == FakeMessage([message["image", 0]])
assert message.count("image") == 2 assert message.count("image") == 2
def test_message_validate(): def test_message_validate():
Message = make_fake_message() assert parse_obj_as(FakeMessage, FakeMessage([])) == FakeMessage([])
MessageSegment = Message.get_segment_class()
Message_ = make_fake_message()
assert parse_obj_as(Message, Message([])) == Message([])
with pytest.raises(ValidationError): with pytest.raises(ValidationError):
parse_obj_as(Message, Message_([])) parse_obj_as(type("FakeMessage2", (Message,), {}), FakeMessage([]))
assert parse_obj_as(Message, "text") == Message([MessageSegment.text("text")]) assert parse_obj_as(FakeMessage, "text") == FakeMessage(
[FakeMessageSegment.text("text")]
assert parse_obj_as(Message, {"type": "text", "data": {"text": "text"}}) == Message(
[MessageSegment.text("text")]
) )
assert parse_obj_as( assert parse_obj_as(
Message, FakeMessage, {"type": "text", "data": {"text": "text"}}
[MessageSegment.text("text"), {"type": "text", "data": {"text": "text"}}], ) == FakeMessage([FakeMessageSegment.text("text")])
) == Message([MessageSegment.text("text"), MessageSegment.text("text")])
assert parse_obj_as(
FakeMessage,
[FakeMessageSegment.text("text"), {"type": "text", "data": {"text": "text"}}],
) == FakeMessage([FakeMessageSegment.text("text"), FakeMessageSegment.text("text")])
with pytest.raises(ValidationError): with pytest.raises(ValidationError):
parse_obj_as(Message, object()) parse_obj_as(FakeMessage, object())
def test_message_contains(): def test_message_contains():
Message = make_fake_message() message = FakeMessage(
MessageSegment = Message.get_segment_class()
message = Message(
[ [
MessageSegment.text("test"), FakeMessageSegment.text("test"),
MessageSegment.image("test2"), FakeMessageSegment.image("test2"),
MessageSegment.image("test3"), FakeMessageSegment.image("test3"),
MessageSegment.text("test4"), FakeMessageSegment.text("test4"),
] ]
) )
assert message.has(MessageSegment.text("test")) is True assert message.has(FakeMessageSegment.text("test")) is True
assert MessageSegment.text("test") in message assert FakeMessageSegment.text("test") in message
assert message.has("image") is True assert message.has("image") is True
assert "image" in message assert "image" in message
assert message.has(MessageSegment.text("foo")) is False assert message.has(FakeMessageSegment.text("foo")) is False
assert MessageSegment.text("foo") not in message assert FakeMessageSegment.text("foo") not in message
assert message.has("foo") is False assert message.has("foo") is False
assert "foo" not in message assert "foo" not in message
def test_message_only(): def test_message_only():
Message = make_fake_message() message = FakeMessage(
MessageSegment = Message.get_segment_class()
message = Message(
[ [
MessageSegment.text("test"), FakeMessageSegment.text("test"),
MessageSegment.text("test2"), FakeMessageSegment.text("test2"),
] ]
) )
assert message.only("text") is True assert message.only("text") is True
assert message.only(MessageSegment.text("test")) is False assert message.only(FakeMessageSegment.text("test")) is False
message = Message( message = FakeMessage(
[ [
MessageSegment.text("test"), FakeMessageSegment.text("test"),
MessageSegment.image("test2"), FakeMessageSegment.image("test2"),
MessageSegment.image("test3"), FakeMessageSegment.image("test3"),
MessageSegment.text("test4"), FakeMessageSegment.text("test4"),
] ]
) )
assert message.only("text") is False assert message.only("text") is False
message = Message( message = FakeMessage(
[ [
MessageSegment.text("test"), FakeMessageSegment.text("test"),
MessageSegment.text("test"), FakeMessageSegment.text("test"),
] ]
) )
assert message.only(MessageSegment.text("test")) is True assert message.only(FakeMessageSegment.text("test")) is True
def test_message_join(): def test_message_join():
Message = make_fake_message() msg = FakeMessage([FakeMessageSegment.text("test")])
MessageSegment = Message.get_segment_class()
msg = Message([MessageSegment.text("test")])
iterable = [ iterable = [
MessageSegment.text("first"), FakeMessageSegment.text("first"),
Message([MessageSegment.text("second"), MessageSegment.text("third")]), FakeMessage(
[FakeMessageSegment.text("second"), FakeMessageSegment.text("third")]
),
] ]
assert msg.join(iterable) == Message( assert msg.join(iterable) == FakeMessage(
[ [
MessageSegment.text("first"), FakeMessageSegment.text("first"),
MessageSegment.text("test"), FakeMessageSegment.text("test"),
MessageSegment.text("second"), FakeMessageSegment.text("second"),
MessageSegment.text("third"), FakeMessageSegment.text("third"),
] ]
) )
def test_message_include(): def test_message_include():
Message = make_fake_message() message = FakeMessage(
MessageSegment = Message.get_segment_class()
message = Message(
[ [
MessageSegment.text("test"), FakeMessageSegment.text("test"),
MessageSegment.image("test2"), FakeMessageSegment.image("test2"),
MessageSegment.image("test3"), FakeMessageSegment.image("test3"),
MessageSegment.text("test4"), FakeMessageSegment.text("test4"),
] ]
) )
assert message.include("text") == Message( assert message.include("text") == FakeMessage(
[ [
MessageSegment.text("test"), FakeMessageSegment.text("test"),
MessageSegment.text("test4"), FakeMessageSegment.text("test4"),
] ]
) )
def test_message_exclude(): def test_message_exclude():
Message = make_fake_message() message = FakeMessage(
MessageSegment = Message.get_segment_class()
message = Message(
[ [
MessageSegment.text("test"), FakeMessageSegment.text("test"),
MessageSegment.image("test2"), FakeMessageSegment.image("test2"),
MessageSegment.image("test3"), FakeMessageSegment.image("test3"),
MessageSegment.text("test4"), FakeMessageSegment.text("test4"),
] ]
) )
assert message.exclude("image") == Message( assert message.exclude("image") == FakeMessage(
[ [
MessageSegment.text("test"), FakeMessageSegment.text("test"),
MessageSegment.text("test4"), FakeMessageSegment.text("test4"),
] ]
) )

View File

@@ -1,5 +1,5 @@
from nonebot.adapters import MessageTemplate from nonebot.adapters import MessageTemplate
from utils import escape_text, make_fake_message from utils import FakeMessage, FakeMessageSegment, escape_text
def test_template_basis(): def test_template_basis():
@@ -9,8 +9,7 @@ def test_template_basis():
def test_template_message(): def test_template_message():
Message = make_fake_message() template = FakeMessage.template("{a:custom}{b:text}{c:image}/{d}")
template = Message.template("{a:custom}{b:text}{c:image}/{d}")
@template.add_format_spec @template.add_format_spec
def custom(input: str) -> str: def custom(input: str) -> str:
@@ -37,29 +36,24 @@ def test_template_message():
def test_rich_template_message(): def test_rich_template_message():
Message = make_fake_message()
MS = Message.get_segment_class()
pic1, pic2, pic3 = ( pic1, pic2, pic3 = (
MS.image("file:///pic1.jpg"), FakeMessageSegment.image("file:///pic1.jpg"),
MS.image("file:///pic2.jpg"), FakeMessageSegment.image("file:///pic2.jpg"),
MS.image("file:///pic3.jpg"), FakeMessageSegment.image("file:///pic3.jpg"),
) )
template = Message.template("{}{}" + pic2 + "{}") template = FakeMessage.template("{}{}" + pic2 + "{}")
result = template.format(pic1, "[fake:image]", pic3) result = template.format(pic1, "[fake:image]", pic3)
assert result["image"] == Message([pic1, pic2, pic3]) assert result["image"] == FakeMessage([pic1, pic2, pic3])
assert str(result) == ( assert str(result) == (
"[fake:image]" + escape_text("[fake:image]") + "[fake:image]" + "[fake:image]" "[fake:image]" + escape_text("[fake:image]") + "[fake:image]" + "[fake:image]"
) )
def test_message_injection(): def test_message_injection():
Message = make_fake_message() template = FakeMessage.template("{name}Is Bad")
template = Message.template("{name}Is Bad")
message = template.format(name="[fake:image]") message = template.format(name="[fake:image]")
assert message.extract_plain_text() == escape_text("[fake:image]Is Bad") assert message.extract_plain_text() == escape_text("[fake:image]Is Bad")

View File

@@ -234,7 +234,7 @@ async def test_run_preprocessor(app: App, monkeypatch: pytest.MonkeyPatch):
bot = ctx.create_bot() bot = ctx.create_bot()
event = make_fake_event()() event = make_fake_event()()
ctx.receive_event(bot, event) ctx.receive_event(bot, event)
ctx.should_call_send(event, "test", True, bot) ctx.should_call_send(event, "test", True, bot=bot)
assert runned, "run_preprocessor should runned" assert runned, "run_preprocessor should runned"
@@ -346,7 +346,7 @@ async def test_run_postprocessor(app: App, monkeypatch: pytest.MonkeyPatch):
bot = ctx.create_bot() bot = ctx.create_bot()
event = make_fake_event()() event = make_fake_event()()
ctx.receive_event(bot, event) ctx.receive_event(bot, event)
ctx.should_call_send(event, "test", True, bot) ctx.should_call_send(event, "test", True, bot=bot)
assert runned, "run_postprocessor should runned" assert runned, "run_postprocessor should runned"

View File

@@ -1,15 +1,12 @@
import json import json
import asyncio import asyncio
from typing import Any, Set, cast from typing import Any, Set, Optional
import pytest import pytest
from nonebug import App from nonebug import App
import nonebot
from nonebot.config import Env
from nonebot.adapters import Bot from nonebot.adapters import Bot
from nonebot.params import Depends from nonebot.params import Depends
from nonebot import _resolve_combine_expr
from nonebot.dependencies import Dependent from nonebot.dependencies import Dependent
from nonebot.exception import WebSocketClosed from nonebot.exception import WebSocketClosed
from nonebot.drivers._lifespan import Lifespan from nonebot.drivers._lifespan import Lifespan
@@ -18,25 +15,15 @@ from nonebot.drivers import (
Driver, Driver,
Request, Request,
Response, Response,
ASGIMixin,
WebSocket, WebSocket,
ForwardDriver, HTTPClientMixin,
ReverseDriver,
HTTPServerSetup, HTTPServerSetup,
WebSocketClientMixin,
WebSocketServerSetup, WebSocketServerSetup,
) )
@pytest.fixture(name="driver")
def load_driver(request: pytest.FixtureRequest) -> Driver:
driver_name = getattr(request, "param", None)
global_driver = nonebot.get_driver()
if driver_name is None:
return global_driver
DriverClass = _resolve_combine_expr(driver_name)
return DriverClass(Env(environment=global_driver.env), global_driver.config)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_lifespan(): async def test_lifespan():
lifespan = Lifespan() lifespan = Lifespan()
@@ -79,13 +66,37 @@ async def test_lifespan():
], ],
indirect=True, indirect=True,
) )
async def test_reverse_driver(app: App, driver: Driver): async def test_http_server(app: App, driver: Driver):
driver = cast(ReverseDriver, driver) assert isinstance(driver, ASGIMixin)
async def _handle_http(request: Request) -> Response: async def _handle_http(request: Request) -> Response:
assert request.content in (b"test", "test") assert request.content in (b"test", "test")
return Response(200, content="test") return Response(200, content="test")
http_setup = HTTPServerSetup(URL("/http_test"), "POST", "http_test", _handle_http)
driver.setup_http_server(http_setup)
async with app.test_server(driver.asgi) as ctx:
client = ctx.get_client()
response = await client.post("/http_test", data="test")
assert response.status_code == 200
assert response.text == "test"
await asyncio.sleep(1)
@pytest.mark.asyncio
@pytest.mark.parametrize(
"driver",
[
pytest.param("nonebot.drivers.fastapi:Driver", id="fastapi"),
pytest.param("nonebot.drivers.quart:Driver", id="quart"),
],
indirect=True,
)
async def test_websocket_server(app: App, driver: Driver):
assert isinstance(driver, ASGIMixin)
async def _handle_ws(ws: WebSocket) -> None: async def _handle_ws(ws: WebSocket) -> None:
await ws.accept() await ws.accept()
data = await ws.receive() data = await ws.receive()
@@ -107,17 +118,11 @@ async def test_reverse_driver(app: App, driver: Driver):
with pytest.raises(WebSocketClosed): with pytest.raises(WebSocketClosed):
await ws.receive() await ws.receive()
http_setup = HTTPServerSetup(URL("/http_test"), "POST", "http_test", _handle_http)
driver.setup_http_server(http_setup)
ws_setup = WebSocketServerSetup(URL("/ws_test"), "ws_test", _handle_ws) ws_setup = WebSocketServerSetup(URL("/ws_test"), "ws_test", _handle_ws)
driver.setup_websocket_server(ws_setup) driver.setup_websocket_server(ws_setup)
async with app.test_server(driver.asgi) as ctx: async with app.test_server(driver.asgi) as ctx:
client = ctx.get_client() client = ctx.get_client()
response = await client.post("/http_test", data="test")
assert response.status_code == 200
assert response.text == "test"
async with client.websocket_connect("/ws_test") as ws: async with client.websocket_connect("/ws_test") as ws:
await ws.send_text("ping") await ws.send_text("ping")
@@ -136,6 +141,63 @@ async def test_reverse_driver(app: App, driver: Driver):
await asyncio.sleep(1) await asyncio.sleep(1)
@pytest.mark.asyncio
@pytest.mark.parametrize(
"driver",
[
pytest.param("nonebot.drivers.fastapi:Driver", id="fastapi"),
pytest.param("nonebot.drivers.quart:Driver", id="quart"),
],
indirect=True,
)
async def test_cross_context(app: App, driver: Driver):
assert isinstance(driver, ASGIMixin)
ws: Optional[WebSocket] = None
ws_ready = asyncio.Event()
ws_should_close = asyncio.Event()
async def background_task():
try:
await ws_ready.wait()
assert ws is not None
await ws.send("ping")
data = await ws.receive()
assert data == "pong"
finally:
ws_should_close.set()
task = asyncio.create_task(background_task())
async def _handle_ws(websocket: WebSocket) -> None:
nonlocal ws
await websocket.accept()
ws = websocket
ws_ready.set()
await ws_should_close.wait()
await websocket.close()
ws_setup = WebSocketServerSetup(URL("/ws_test"), "ws_test", _handle_ws)
driver.setup_websocket_server(ws_setup)
async with app.test_server(driver.asgi) as ctx:
client = ctx.get_client()
async with client.websocket_connect("/ws_test") as websocket:
try:
data = await websocket.receive_text()
assert data == "ping"
await websocket.send_text("pong")
except Exception as e:
if not e.args or "websocket.close" not in str(e.args[0]):
raise
await task
await asyncio.sleep(1)
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.parametrize( @pytest.mark.parametrize(
"driver", "driver",
@@ -145,51 +207,105 @@ async def test_reverse_driver(app: App, driver: Driver):
], ],
indirect=True, indirect=True,
) )
async def test_http_driver(driver: Driver): async def test_http_client(driver: Driver, server_url: URL):
driver = cast(ForwardDriver, driver) assert isinstance(driver, HTTPClientMixin)
# simple post with query, headers, cookies and content
request = Request( request = Request(
"POST", "POST",
"https://httpbin.org/post", server_url,
params={"param": "test"}, params={"param": "test"},
headers={"X-Test": "test"}, headers={"X-Test": "test"},
cookies={"session": "test"}, cookies={"session": "test"},
content="test", content="test",
) )
response = await driver.request(request) response = await driver.request(request)
assert response.status_code == 200 and response.content assert server_url.host is not None
request_raw_url = Request(
"POST",
(
server_url.scheme.encode("ascii"),
server_url.host.encode("ascii"),
server_url.port,
server_url.path.encode("ascii"),
),
params={"param": "test"},
headers={"X-Test": "test"},
cookies={"session": "test"},
content="test",
)
assert (
request.url == request_raw_url.url
), "request.url should be equal to request_raw_url.url"
assert response.status_code == 200
assert response.content
data = json.loads(response.content) data = json.loads(response.content)
assert data["method"] == "POST"
assert data["args"] == {"param": "test"} assert data["args"] == {"param": "test"}
assert data["headers"].get("X-Test") == "test" assert data["headers"].get("X-Test") == "test"
assert data["headers"].get("Cookie") == "session=test" assert data["headers"].get("Cookie") == "session=test"
assert data["data"] == "test" assert data["data"] == "test"
request = Request("POST", "https://httpbin.org/post", data={"form": "test"}) # post with data body
request = Request("POST", server_url, data={"form": "test"})
response = await driver.request(request) response = await driver.request(request)
assert response.status_code == 200 and response.content assert response.status_code == 200
assert response.content
data = json.loads(response.content) data = json.loads(response.content)
assert data["method"] == "POST"
assert data["form"] == {"form": "test"} assert data["form"] == {"form": "test"}
request = Request("POST", "https://httpbin.org/post", json={"json": "test"}) # post with json body
request = Request("POST", server_url, json={"json": "test"})
response = await driver.request(request) response = await driver.request(request)
assert response.status_code == 200 and response.content assert response.status_code == 200
assert response.content
data = json.loads(response.content) data = json.loads(response.content)
assert data["method"] == "POST"
assert data["json"] == {"json": "test"} assert data["json"] == {"json": "test"}
# post with files and form data
request = Request( request = Request(
"POST", "https://httpbin.org/post", files={"test": ("test.txt", b"test")} "POST",
server_url,
data={"form": "test"},
files=[
("test1", b"test"),
("test2", ("test.txt", b"test")),
("test3", ("test.txt", b"test", "text/plain")),
],
) )
response = await driver.request(request) response = await driver.request(request)
assert response.status_code == 200 and response.content assert response.status_code == 200
assert response.content
data = json.loads(response.content) data = json.loads(response.content)
assert data["files"] == {"test": "test"} assert data["method"] == "POST"
assert data["form"] == {"form": "test"}
assert data["files"] == {
"test1": "test",
"test2": "test",
"test3": "test",
}, "file parsing error"
await asyncio.sleep(1) await asyncio.sleep(1)
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.parametrize( @pytest.mark.parametrize(
"driver, driver_type", "driver",
[
pytest.param("nonebot.drivers.websockets:Driver", id="websockets"),
pytest.param("nonebot.drivers.aiohttp:Driver", id="aiohttp"),
],
indirect=True,
)
async def test_websocket_client(driver: Driver):
assert isinstance(driver, WebSocketClientMixin)
@pytest.mark.asyncio
@pytest.mark.parametrize(
("driver", "driver_type"),
[ [
pytest.param( pytest.param(
"nonebot.drivers.fastapi:Driver+nonebot.drivers.aiohttp:Mixin", "nonebot.drivers.fastapi:Driver+nonebot.drivers.aiohttp:Mixin",
@@ -232,7 +348,6 @@ async def test_bot_connect_hook(app: App, driver: Driver):
@driver.on_bot_connect @driver.on_bot_connect
async def conn_hook(foo: Bot, dep: int = Depends(dependency), default: int = 1): async def conn_hook(foo: Bot, dep: int = Depends(dependency), default: int = 1):
nonlocal conn_should_be_called nonlocal conn_should_be_called
conn_should_be_called = True
if foo is not bot: if foo is not bot:
pytest.fail("on_bot_connect hook called with wrong bot") pytest.fail("on_bot_connect hook called with wrong bot")
@@ -241,12 +356,13 @@ async def test_bot_connect_hook(app: App, driver: Driver):
if default != 1: if default != 1:
pytest.fail("on_bot_connect hook called with wrong default value") pytest.fail("on_bot_connect hook called with wrong default value")
conn_should_be_called = True
@driver.on_bot_disconnect @driver.on_bot_disconnect
async def disconn_hook( async def disconn_hook(
foo: Bot, dep: int = Depends(dependency), default: int = 1 foo: Bot, dep: int = Depends(dependency), default: int = 1
): ):
nonlocal disconn_should_be_called nonlocal disconn_should_be_called
disconn_should_be_called = True
if foo is not bot: if foo is not bot:
pytest.fail("on_bot_disconnect hook called with wrong bot") pytest.fail("on_bot_disconnect hook called with wrong bot")
@@ -255,6 +371,8 @@ async def test_bot_connect_hook(app: App, driver: Driver):
if default != 1: if default != 1:
pytest.fail("on_bot_connect hook called with wrong default value") pytest.fail("on_bot_connect hook called with wrong default value")
disconn_should_be_called = True
if conn_hook not in {hook.call for hook in conn_hooks}: if conn_hook not in {hook.call for hook in conn_hooks}:
pytest.fail("on_bot_connect hook not registered") pytest.fail("on_bot_connect hook not registered")
if disconn_hook not in {hook.call for hook in disconn_hooks}: if disconn_hook not in {hook.call for hook in disconn_hooks}:
@@ -264,6 +382,7 @@ async def test_bot_connect_hook(app: App, driver: Driver):
bot = ctx.create_bot() bot = ctx.create_bot()
await asyncio.sleep(1) await asyncio.sleep(1)
if not conn_should_be_called: if not conn_should_be_called:
pytest.fail("on_bot_connect hook not called") pytest.fail("on_bot_connect hook not called")
if not disconn_should_be_called: if not disconn_should_be_called:

View File

@@ -2,7 +2,7 @@ import pytest
from nonebug import App from nonebug import App
import nonebot import nonebot
from nonebot.drivers import Driver, ReverseDriver from nonebot.drivers import Driver, ASGIMixin, ReverseDriver
from nonebot import ( from nonebot import (
get_app, get_app,
get_bot, get_bot,
@@ -20,6 +20,11 @@ async def test_init():
assert env == "test" assert env == "test"
config = nonebot.get_driver().config config = nonebot.get_driver().config
assert config.nickname == {"test"}
assert config.superusers == {"test", "fake:faketest"}
assert config.api_timeout is None
assert config.simple_none is None
assert config.config_from_env == {"test": "test"} assert config.config_from_env == {"test": "test"}
assert config.config_override == "new" assert config.config_override == "new"
assert config.config_from_init == "init" assert config.config_from_init == "init"
@@ -31,17 +36,31 @@ async def test_init():
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get(app: App, monkeypatch: pytest.MonkeyPatch): async def test_get_driver(app: App, monkeypatch: pytest.MonkeyPatch):
with monkeypatch.context() as m: with monkeypatch.context() as m:
m.setattr(nonebot, "_driver", None) m.setattr(nonebot, "_driver", None)
with pytest.raises(ValueError): with pytest.raises(ValueError, match="initialized"):
get_driver() get_driver()
@pytest.mark.asyncio
async def test_get_asgi(app: App, monkeypatch: pytest.MonkeyPatch):
driver = get_driver() driver = get_driver()
assert isinstance(driver, ReverseDriver) assert isinstance(driver, ReverseDriver)
assert isinstance(driver, ASGIMixin)
assert get_asgi() == driver.asgi assert get_asgi() == driver.asgi
@pytest.mark.asyncio
async def test_get_app(app: App, monkeypatch: pytest.MonkeyPatch):
driver = get_driver()
assert isinstance(driver, ReverseDriver)
assert isinstance(driver, ASGIMixin)
assert get_app() == driver.server_app assert get_app() == driver.server_app
@pytest.mark.asyncio
async def test_get_adapter(app: App, monkeypatch: pytest.MonkeyPatch):
async with app.test_api() as ctx: async with app.test_api() as ctx:
adapter = ctx.create_adapter() adapter = ctx.create_adapter()
adapter_name = adapter.get_name() adapter_name = adapter.get_name()
@@ -51,24 +70,38 @@ async def test_get(app: App, monkeypatch: pytest.MonkeyPatch):
assert get_adapters() == {adapter_name: adapter} assert get_adapters() == {adapter_name: adapter}
assert get_adapter(adapter_name) is adapter assert get_adapter(adapter_name) is adapter
assert get_adapter(adapter.__class__) is adapter assert get_adapter(adapter.__class__) is adapter
with pytest.raises(ValueError): with pytest.raises(ValueError, match="registered"):
get_adapter("not exist") get_adapter("not exist")
@pytest.mark.asyncio
async def test_run(app: App, monkeypatch: pytest.MonkeyPatch):
runned = False runned = False
def mock_run(*args, **kwargs): def mock_run(*args, **kwargs):
nonlocal runned nonlocal runned
runned = True runned = True
assert args == ("arg",) and kwargs == {"kwarg": "kwarg"} assert args == ("arg",)
assert kwargs == {"kwarg": "kwarg"}
driver = get_driver()
with monkeypatch.context() as m:
m.setattr(driver, "run", mock_run)
nonebot.run("arg", kwarg="kwarg")
monkeypatch.setattr(driver, "run", mock_run)
nonebot.run("arg", kwarg="kwarg")
assert runned assert runned
with pytest.raises(ValueError):
@pytest.mark.asyncio
async def test_get_bot(app: App, monkeypatch: pytest.MonkeyPatch):
driver = get_driver()
with pytest.raises(ValueError, match="no bots"):
get_bot() get_bot()
monkeypatch.setattr(driver, "_bots", {"test": "test"}) with monkeypatch.context() as m:
assert get_bot() == "test" m.setattr(driver, "_bots", {"test": "test"})
assert get_bot("test") == "test" assert get_bot() == "test"
assert get_bots() == {"test": "test"} assert get_bot("test") == "test"
assert get_bots() == {"test": "test"}

View File

@@ -1,27 +1,96 @@
import sys
from pathlib import Path
import pytest import pytest
from nonebug import App from nonebug import App
from nonebot.permission import User from nonebot.rule import Rule
from nonebot import get_plugin
from nonebot.matcher import Matcher, matchers from nonebot.matcher import Matcher, matchers
from nonebot.message import check_and_run_matcher from utils import FakeMessage, make_fake_event
from utils import make_fake_event, make_fake_message from nonebot.permission import User, Permission
from nonebot.message import _check_matcher, check_and_run_matcher
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_matcher(app: App): async def test_matcher_info(app: App):
from plugins.matcher.matcher_process import ( from plugins.matcher.matcher_info import matcher
test_got,
test_handle, assert issubclass(matcher, Matcher)
test_preset, assert matcher.type == "message"
test_combine, assert matcher.priority == 1
test_receive, assert matcher.temp is False
test_overload, assert matcher.expire_time is None
assert matcher.block is True
assert matcher._source
assert matcher._source.module_name == "plugins.matcher.matcher_info"
assert matcher.module is sys.modules["plugins.matcher.matcher_info"]
assert matcher.module_name == "plugins.matcher.matcher_info"
assert matcher._source.plugin_name == "matcher_info"
assert matcher.plugin is get_plugin("matcher_info")
assert matcher.plugin_name == "matcher_info"
assert (
matcher._source.file
== (Path(__file__).parent.parent / "plugins/matcher/matcher_info.py").absolute()
) )
message = make_fake_message()("text") assert matcher._source.lineno == 3
@pytest.mark.asyncio
async def test_matcher_check(app: App):
async def falsy():
return False
async def truthy():
return True
async def error():
raise RuntimeError
event = make_fake_event(_type="test")()
with app.provider.context({}):
test_perm_falsy = Matcher.new(permission=Permission(falsy))
async with app.test_api() as ctx:
bot = ctx.create_bot()
assert await _check_matcher(test_perm_falsy, bot, event, {}) is False
test_perm_truthy = Matcher.new(permission=Permission(truthy))
async with app.test_api() as ctx:
bot = ctx.create_bot()
assert await _check_matcher(test_perm_truthy, bot, event, {}) is True
test_perm_error = Matcher.new(permission=Permission(error))
async with app.test_api() as ctx:
bot = ctx.create_bot()
assert await _check_matcher(test_perm_error, bot, event, {}) is False
test_rule_falsy = Matcher.new(rule=Rule(falsy))
async with app.test_api() as ctx:
bot = ctx.create_bot()
assert await _check_matcher(test_rule_falsy, bot, event, {}) is False
test_rule_truthy = Matcher.new(rule=Rule(truthy))
async with app.test_api() as ctx:
bot = ctx.create_bot()
assert await _check_matcher(test_rule_truthy, bot, event, {}) is True
test_rule_error = Matcher.new(rule=Rule(error))
async with app.test_api() as ctx:
bot = ctx.create_bot()
assert await _check_matcher(test_rule_error, bot, event, {}) is False
@pytest.mark.asyncio
async def test_matcher_handle(app: App):
from plugins.matcher.matcher_process import test_handle
message = FakeMessage("text")
event = make_fake_event(_message=message)() event = make_fake_event(_message=message)()
message_next = make_fake_message()("text_next")
event_next = make_fake_event(_message=message_next)()
assert len(test_handle.handlers) == 1 assert len(test_handle.handlers) == 1
async with app.test_matcher(test_handle) as ctx: async with app.test_matcher(test_handle) as ctx:
@@ -30,6 +99,16 @@ async def test_matcher(app: App):
ctx.should_call_send(event, "send", "result", at_sender=True) ctx.should_call_send(event, "send", "result", at_sender=True)
ctx.should_finished() ctx.should_finished()
@pytest.mark.asyncio
async def test_matcher_got(app: App):
from plugins.matcher.matcher_process import test_got
message = FakeMessage("text")
event = make_fake_event(_message=message)()
message_next = FakeMessage("text_next")
event_next = make_fake_event(_message=message_next)()
assert len(test_got.handlers) == 1 assert len(test_got.handlers) == 1
async with app.test_matcher(test_got) as ctx: async with app.test_matcher(test_got) as ctx:
bot = ctx.create_bot() bot = ctx.create_bot()
@@ -42,6 +121,14 @@ async def test_matcher(app: App):
ctx.should_rejected() ctx.should_rejected()
ctx.receive_event(bot, event_next) ctx.receive_event(bot, event_next)
@pytest.mark.asyncio
async def test_matcher_receive(app: App):
from plugins.matcher.matcher_process import test_receive
message = FakeMessage("text")
event = make_fake_event(_message=message)()
assert len(test_receive.handlers) == 1 assert len(test_receive.handlers) == 1
async with app.test_matcher(test_receive) as ctx: async with app.test_matcher(test_receive) as ctx:
bot = ctx.create_bot() bot = ctx.create_bot()
@@ -51,7 +138,17 @@ async def test_matcher(app: App):
ctx.should_call_send(event, "pause", "result", at_sender=True) ctx.should_call_send(event, "pause", "result", at_sender=True)
ctx.should_paused() ctx.should_paused()
assert len(test_receive.handlers) == 1
@pytest.mark.asyncio
async def test_matcher_combine(app: App):
from plugins.matcher.matcher_process import test_combine
message = FakeMessage("text")
event = make_fake_event(_message=message)()
message_next = FakeMessage("text_next")
event_next = make_fake_event(_message=message_next)()
assert len(test_combine.handlers) == 1
async with app.test_matcher(test_combine) as ctx: async with app.test_matcher(test_combine) as ctx:
bot = ctx.create_bot() bot = ctx.create_bot()
ctx.receive_event(bot, event) ctx.receive_event(bot, event)
@@ -64,6 +161,16 @@ async def test_matcher(app: App):
ctx.should_rejected() ctx.should_rejected()
ctx.receive_event(bot, event_next) ctx.receive_event(bot, event_next)
@pytest.mark.asyncio
async def test_matcher_preset(app: App):
from plugins.matcher.matcher_process import test_preset
message = FakeMessage("text")
event = make_fake_event(_message=message)()
message_next = FakeMessage("text_next")
event_next = make_fake_event(_message=message_next)()
assert len(test_preset.handlers) == 2 assert len(test_preset.handlers) == 2
async with app.test_matcher(test_preset) as ctx: async with app.test_matcher(test_preset) as ctx:
bot = ctx.create_bot() bot = ctx.create_bot()
@@ -72,6 +179,14 @@ async def test_matcher(app: App):
ctx.should_rejected() ctx.should_rejected()
ctx.receive_event(bot, event_next) ctx.receive_event(bot, event_next)
@pytest.mark.asyncio
async def test_matcher_overload(app: App):
from plugins.matcher.matcher_process import test_overload
message = FakeMessage("text")
event = make_fake_event(_message=message)()
assert len(test_overload.handlers) == 2 assert len(test_overload.handlers) == 2
async with app.test_matcher(test_overload) as ctx: async with app.test_matcher(test_overload) as ctx:
bot = ctx.create_bot() bot = ctx.create_bot()
@@ -83,7 +198,7 @@ async def test_matcher(app: App):
async def test_matcher_destroy(app: App): async def test_matcher_destroy(app: App):
from plugins.matcher.matcher_process import test_destroy from plugins.matcher.matcher_process import test_destroy
async with app.test_matcher(test_destroy) as ctx: async with app.test_matcher(test_destroy):
assert len(matchers) == 1 assert len(matchers) == 1
assert len(matchers[test_destroy.priority]) == 1 assert len(matchers[test_destroy.priority]) == 1
assert matchers[test_destroy.priority][0] is test_destroy assert matchers[test_destroy.priority][0] is test_destroy
@@ -115,12 +230,10 @@ async def test_type_updater(app: App):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_permission_updater(app: App): async def test_default_permission_updater(app: App):
from plugins.matcher.matcher_permission import ( from plugins.matcher.matcher_permission import (
default_permission, default_permission,
test_custom_updater,
test_permission_updater, test_permission_updater,
test_user_permission_updater,
) )
event = make_fake_event(_session_id="test")() event = make_fake_event(_session_id="test")()
@@ -136,6 +249,15 @@ async def test_permission_updater(app: App):
assert checker.users == ("test",) assert checker.users == ("test",)
assert checker.perm is default_permission assert checker.perm is default_permission
@pytest.mark.asyncio
async def test_user_permission_updater(app: App):
from plugins.matcher.matcher_permission import (
default_permission,
test_user_permission_updater,
)
event = make_fake_event(_session_id="test")()
user_permission = list(test_user_permission_updater.permission.checkers)[0].call user_permission = list(test_user_permission_updater.permission.checkers)[0].call
assert isinstance(user_permission, User) assert isinstance(user_permission, User)
assert user_permission.perm is default_permission assert user_permission.perm is default_permission
@@ -149,12 +271,22 @@ async def test_permission_updater(app: App):
assert checker.users == ("test",) assert checker.users == ("test",)
assert checker.perm is default_permission assert checker.perm is default_permission
@pytest.mark.asyncio
async def test_custom_permission_updater(app: App):
from plugins.matcher.matcher_permission import (
new_permission,
default_permission,
test_custom_updater,
)
event = make_fake_event(_session_id="test")()
assert test_custom_updater.permission is default_permission assert test_custom_updater.permission is default_permission
async with app.test_api() as ctx: async with app.test_api() as ctx:
bot = ctx.create_bot() bot = ctx.create_bot()
matcher = test_custom_updater() matcher = test_custom_updater()
new_perm = await matcher.update_permission(bot, event) new_perm = await matcher.update_permission(bot, event)
assert new_perm is default_permission assert new_perm is new_permission
@pytest.mark.asyncio @pytest.mark.asyncio
@@ -189,12 +321,8 @@ async def test_run(app: App):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_expire(app: App): async def test_temp(app: App):
from plugins.matcher.matcher_expire import ( from plugins.matcher.matcher_expire import test_temp_matcher
test_temp_matcher,
test_datetime_matcher,
test_timedelta_matcher,
)
event = make_fake_event(_type="test")() event = make_fake_event(_type="test")()
async with app.test_api() as ctx: async with app.test_api() as ctx:
@@ -203,6 +331,11 @@ async def test_expire(app: App):
await check_and_run_matcher(test_temp_matcher, bot, event, {}) await check_and_run_matcher(test_temp_matcher, bot, event, {})
assert test_temp_matcher not in matchers[test_temp_matcher.priority] assert test_temp_matcher not in matchers[test_temp_matcher.priority]
@pytest.mark.asyncio
async def test_datetime_expire(app: App):
from plugins.matcher.matcher_expire import test_datetime_matcher
event = make_fake_event()() event = make_fake_event()()
async with app.test_api() as ctx: async with app.test_api() as ctx:
bot = ctx.create_bot() bot = ctx.create_bot()
@@ -210,6 +343,11 @@ async def test_expire(app: App):
await check_and_run_matcher(test_datetime_matcher, bot, event, {}) await check_and_run_matcher(test_datetime_matcher, bot, event, {})
assert test_datetime_matcher not in matchers[test_datetime_matcher.priority] assert test_datetime_matcher not in matchers[test_datetime_matcher.priority]
@pytest.mark.asyncio
async def test_timedelta_expire(app: App):
from plugins.matcher.matcher_expire import test_timedelta_matcher
event = make_fake_event()() event = make_fake_event()()
async with app.test_api() as ctx: async with app.test_api() as ctx:
bot = ctx.create_bot() bot = ctx.create_bot()

View File

@@ -6,7 +6,7 @@ from nonebug import App
from nonebot.matcher import Matcher from nonebot.matcher import Matcher
from nonebot.dependencies import Dependent from nonebot.dependencies import Dependent
from nonebot.exception import TypeMisMatch from nonebot.exception import TypeMisMatch
from utils import make_fake_event, make_fake_message from utils import FakeMessage, make_fake_event
from nonebot.params import ( from nonebot.params import (
ArgParam, ArgParam,
BotParam, BotParam,
@@ -33,6 +33,8 @@ from nonebot.consts import (
CMD_WHITESPACE_KEY, CMD_WHITESPACE_KEY,
) )
UNKNOWN_PARAM = "Unknown parameter"
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_depend(app: App): async def test_depend(app: App):
@@ -40,17 +42,24 @@ async def test_depend(app: App):
ClassDependency, ClassDependency,
runned, runned,
depends, depends,
validate,
class_depend, class_depend,
test_depends, test_depends,
validate_fail,
validate_field,
annotated_depend, annotated_depend,
sub_type_mismatch,
validate_field_fail,
annotated_class_depend, annotated_class_depend,
annotated_multi_depend,
annotated_prior_depend, annotated_prior_depend,
) )
async with app.test_dependent(depends, allow_types=[DependParam]) as ctx: async with app.test_dependent(depends, allow_types=[DependParam]) as ctx:
ctx.should_return(1) ctx.should_return(1)
assert len(runned) == 1 and runned[0] == 1 assert len(runned) == 1
assert runned[0] == 1
runned.clear() runned.clear()
@@ -59,7 +68,7 @@ async def test_depend(app: App):
event_next = make_fake_event()() event_next = make_fake_event()()
ctx.receive_event(bot, event_next) ctx.receive_event(bot, event_next)
assert len(runned) == 2 and runned[0] == runned[1] == 1 assert runned == [1, 1]
runned.clear() runned.clear()
@@ -73,13 +82,42 @@ async def test_depend(app: App):
annotated_prior_depend, allow_types=[DependParam] annotated_prior_depend, allow_types=[DependParam]
) as ctx: ) as ctx:
ctx.should_return(1) ctx.should_return(1)
assert runned == [1, 1]
async with app.test_dependent(
annotated_multi_depend, allow_types=[DependParam]
) as ctx:
ctx.should_return(1)
assert runned == [1, 1, 1]
async with app.test_dependent( async with app.test_dependent(
annotated_class_depend, allow_types=[DependParam] annotated_class_depend, allow_types=[DependParam]
) as ctx: ) as ctx:
ctx.should_return(ClassDependency(x=1, y=2)) ctx.should_return(ClassDependency(x=1, y=2))
with pytest.raises(TypeMisMatch): # noqa: PT012
async with app.test_dependent(
sub_type_mismatch, allow_types=[DependParam, BotParam]
) as ctx:
bot = ctx.create_bot()
ctx.pass_params(bot=bot)
async with app.test_dependent(validate, allow_types=[DependParam]) as ctx:
ctx.should_return(1)
with pytest.raises(TypeMisMatch):
async with app.test_dependent(validate_fail, allow_types=[DependParam]) as ctx:
...
async with app.test_dependent(validate_field, allow_types=[DependParam]) as ctx:
ctx.should_return(1)
with pytest.raises(TypeMisMatch):
async with app.test_dependent(
validate_field_fail, allow_types=[DependParam]
) as ctx:
...
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_bot(app: App): async def test_bot(app: App):
@@ -90,7 +128,9 @@ async def test_bot(app: App):
sub_bot, sub_bot,
union_bot, union_bot,
legacy_bot, legacy_bot,
generic_bot,
not_legacy_bot, not_legacy_bot,
generic_bot_none,
) )
async with app.test_dependent(get_bot, allow_types=[BotParam]) as ctx: async with app.test_dependent(get_bot, allow_types=[BotParam]) as ctx:
@@ -103,16 +143,15 @@ async def test_bot(app: App):
ctx.pass_params(bot=bot) ctx.pass_params(bot=bot)
ctx.should_return(bot) ctx.should_return(bot)
with pytest.raises(ValueError): with pytest.raises(ValueError, match=UNKNOWN_PARAM):
async with app.test_dependent(not_legacy_bot, allow_types=[BotParam]) as ctx: app.test_dependent(not_legacy_bot, allow_types=[BotParam])
...
async with app.test_dependent(sub_bot, allow_types=[BotParam]) as ctx: async with app.test_dependent(sub_bot, allow_types=[BotParam]) as ctx:
bot = ctx.create_bot(base=FooBot) bot = ctx.create_bot(base=FooBot)
ctx.pass_params(bot=bot) ctx.pass_params(bot=bot)
ctx.should_return(bot) ctx.should_return(bot)
with pytest.raises(TypeMisMatch): with pytest.raises(TypeMisMatch): # noqa: PT012
async with app.test_dependent(sub_bot, allow_types=[BotParam]) as ctx: async with app.test_dependent(sub_bot, allow_types=[BotParam]) as ctx:
bot = ctx.create_bot() bot = ctx.create_bot()
ctx.pass_params(bot=bot) ctx.pass_params(bot=bot)
@@ -122,9 +161,18 @@ async def test_bot(app: App):
ctx.pass_params(bot=bot) ctx.pass_params(bot=bot)
ctx.should_return(bot) ctx.should_return(bot)
with pytest.raises(ValueError): async with app.test_dependent(generic_bot, allow_types=[BotParam]) as ctx:
async with app.test_dependent(not_bot, allow_types=[BotParam]) as ctx: bot = ctx.create_bot()
... ctx.pass_params(bot=bot)
ctx.should_return(bot)
async with app.test_dependent(generic_bot_none, allow_types=[BotParam]) as ctx:
bot = ctx.create_bot()
ctx.pass_params(bot=bot)
ctx.should_return(bot)
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
app.test_dependent(not_bot, allow_types=[BotParam])
@pytest.mark.asyncio @pytest.mark.asyncio
@@ -139,11 +187,13 @@ async def test_event(app: App):
union_event, union_event,
legacy_event, legacy_event,
event_message, event_message,
generic_event,
event_plain_text, event_plain_text,
not_legacy_event, not_legacy_event,
generic_event_none,
) )
fake_message = make_fake_message()("text") fake_message = FakeMessage("text")
fake_event = make_fake_event(_message=fake_message)() fake_event = make_fake_event(_message=fake_message)()
fake_fooevent = make_fake_event(_base=FooEvent)() fake_fooevent = make_fake_event(_base=FooEvent)()
@@ -155,17 +205,14 @@ async def test_event(app: App):
ctx.pass_params(event=fake_event) ctx.pass_params(event=fake_event)
ctx.should_return(fake_event) ctx.should_return(fake_event)
with pytest.raises(ValueError): with pytest.raises(ValueError, match=UNKNOWN_PARAM):
async with app.test_dependent( app.test_dependent(not_legacy_event, allow_types=[EventParam])
not_legacy_event, allow_types=[EventParam]
) as ctx:
...
async with app.test_dependent(sub_event, allow_types=[EventParam]) as ctx: async with app.test_dependent(sub_event, allow_types=[EventParam]) as ctx:
ctx.pass_params(event=fake_fooevent) ctx.pass_params(event=fake_fooevent)
ctx.should_return(fake_fooevent) ctx.should_return(fake_fooevent)
with pytest.raises(TypeMisMatch): with pytest.raises(TypeMisMatch): # noqa: PT012
async with app.test_dependent(sub_event, allow_types=[EventParam]) as ctx: async with app.test_dependent(sub_event, allow_types=[EventParam]) as ctx:
ctx.pass_params(event=fake_event) ctx.pass_params(event=fake_event)
@@ -173,9 +220,16 @@ async def test_event(app: App):
ctx.pass_params(event=fake_fooevent) ctx.pass_params(event=fake_fooevent)
ctx.should_return(fake_event) ctx.should_return(fake_event)
with pytest.raises(ValueError): async with app.test_dependent(generic_event, allow_types=[EventParam]) as ctx:
async with app.test_dependent(not_event, allow_types=[EventParam]) as ctx: ctx.pass_params(event=fake_event)
... ctx.should_return(fake_event)
async with app.test_dependent(generic_event_none, allow_types=[EventParam]) as ctx:
ctx.pass_params(event=fake_event)
ctx.should_return(fake_event)
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
app.test_dependent(not_event, allow_types=[EventParam])
async with app.test_dependent( async with app.test_dependent(
event_type, allow_types=[EventParam, DependParam] event_type, allow_types=[EventParam, DependParam]
@@ -225,7 +279,7 @@ async def test_state(app: App):
shell_command_argv, shell_command_argv,
) )
fake_message = make_fake_message()("text") fake_message = FakeMessage("text")
fake_matched = re.match(r"\[cq:(?P<type>.*?),(?P<arg>.*?)\]", "[cq:test,arg=value]") fake_matched = re.match(r"\[cq:(?P<type>.*?),(?P<arg>.*?)\]", "[cq:test,arg=value]")
fake_state = { fake_state = {
PREFIX_KEY: { PREFIX_KEY: {
@@ -252,11 +306,8 @@ async def test_state(app: App):
ctx.pass_params(state=fake_state) ctx.pass_params(state=fake_state)
ctx.should_return(fake_state) ctx.should_return(fake_state)
with pytest.raises(ValueError): with pytest.raises(ValueError, match=UNKNOWN_PARAM):
async with app.test_dependent( app.test_dependent(not_legacy_state, allow_types=[StateParam])
not_legacy_state, allow_types=[StateParam]
) as ctx:
...
async with app.test_dependent( async with app.test_dependent(
command, allow_types=[StateParam, DependParam] command, allow_types=[StateParam, DependParam]
@@ -351,14 +402,59 @@ async def test_state(app: App):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_matcher(app: App): async def test_matcher(app: App):
from plugins.param.param_matcher import matcher, receive, last_receive from plugins.param.param_matcher import (
FooMatcher,
matcher,
receive,
not_matcher,
sub_matcher,
last_receive,
union_matcher,
legacy_matcher,
generic_matcher,
not_legacy_matcher,
generic_matcher_none,
)
fake_matcher = Matcher() fake_matcher = Matcher()
foo_matcher = FooMatcher()
async with app.test_dependent(matcher, allow_types=[MatcherParam]) as ctx: async with app.test_dependent(matcher, allow_types=[MatcherParam]) as ctx:
ctx.pass_params(matcher=fake_matcher) ctx.pass_params(matcher=fake_matcher)
ctx.should_return(fake_matcher) ctx.should_return(fake_matcher)
async with app.test_dependent(legacy_matcher, allow_types=[MatcherParam]) as ctx:
ctx.pass_params(matcher=fake_matcher)
ctx.should_return(fake_matcher)
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
app.test_dependent(not_legacy_matcher, allow_types=[MatcherParam])
async with app.test_dependent(sub_matcher, allow_types=[MatcherParam]) as ctx:
ctx.pass_params(matcher=foo_matcher)
ctx.should_return(foo_matcher)
with pytest.raises(TypeMisMatch): # noqa: PT012
async with app.test_dependent(sub_matcher, allow_types=[MatcherParam]) as ctx:
ctx.pass_params(matcher=fake_matcher)
async with app.test_dependent(union_matcher, allow_types=[MatcherParam]) as ctx:
ctx.pass_params(matcher=foo_matcher)
ctx.should_return(foo_matcher)
async with app.test_dependent(generic_matcher, allow_types=[MatcherParam]) as ctx:
ctx.pass_params(matcher=fake_matcher)
ctx.should_return(fake_matcher)
async with app.test_dependent(
generic_matcher_none, allow_types=[MatcherParam]
) as ctx:
ctx.pass_params(matcher=fake_matcher)
ctx.should_return(fake_matcher)
with pytest.raises(ValueError, match=UNKNOWN_PARAM):
app.test_dependent(not_matcher, allow_types=[MatcherParam])
event = make_fake_event()() event = make_fake_event()()
fake_matcher.set_receive("test", event) fake_matcher.set_receive("test", event)
event_next = make_fake_event()() event_next = make_fake_event()()
@@ -379,10 +475,19 @@ async def test_matcher(app: App):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_arg(app: App): async def test_arg(app: App):
from plugins.param.param_arg import arg, arg_str, arg_plain_text from plugins.param.param_arg import (
arg,
arg_str,
annotated_arg,
arg_plain_text,
annotated_arg_str,
annotated_multi_arg,
annotated_prior_arg,
annotated_arg_plain_text,
)
matcher = Matcher() matcher = Matcher()
message = make_fake_message()("text") message = FakeMessage("text")
matcher.set_arg("key", message) matcher.set_arg("key", message)
async with app.test_dependent(arg, allow_types=[ArgParam]) as ctx: async with app.test_dependent(arg, allow_types=[ArgParam]) as ctx:
@@ -397,6 +502,28 @@ async def test_arg(app: App):
ctx.pass_params(matcher=matcher) ctx.pass_params(matcher=matcher)
ctx.should_return(message.extract_plain_text()) ctx.should_return(message.extract_plain_text())
async with app.test_dependent(annotated_arg, allow_types=[ArgParam]) as ctx:
ctx.pass_params(matcher=matcher)
ctx.should_return(message)
async with app.test_dependent(annotated_arg_str, allow_types=[ArgParam]) as ctx:
ctx.pass_params(matcher=matcher)
ctx.should_return(str(message))
async with app.test_dependent(
annotated_arg_plain_text, allow_types=[ArgParam]
) as ctx:
ctx.pass_params(matcher=matcher)
ctx.should_return(message.extract_plain_text())
async with app.test_dependent(annotated_multi_arg, allow_types=[ArgParam]) as ctx:
ctx.pass_params(matcher=matcher)
ctx.should_return(message.extract_plain_text())
async with app.test_dependent(annotated_prior_arg, allow_types=[ArgParam]) as ctx:
ctx.pass_params(matcher=matcher)
ctx.should_return(message.extract_plain_text())
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_exception(app: App): async def test_exception(app: App):

View File

@@ -47,15 +47,15 @@ async def test_permission(app: App):
async with app.test_api() as ctx: async with app.test_api() as ctx:
bot = ctx.create_bot() bot = ctx.create_bot()
assert await Permission(falsy)(bot, event) == False assert await Permission(falsy)(bot, event) is False
assert await Permission(truthy)(bot, event) == True assert await Permission(truthy)(bot, event) is True
assert await Permission(skipped)(bot, event) == False assert await Permission(skipped)(bot, event) is False
assert await Permission(truthy, falsy)(bot, event) == True assert await Permission(truthy, falsy)(bot, event) is True
assert await Permission(truthy, skipped)(bot, event) == True assert await Permission(truthy, skipped)(bot, event) is True
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.parametrize("type, expected", [("message", True), ("notice", False)]) @pytest.mark.parametrize(("type", "expected"), [("message", True), ("notice", False)])
async def test_message(type: str, expected: bool): async def test_message(type: str, expected: bool):
dependent = list(MESSAGE.checkers)[0] dependent = list(MESSAGE.checkers)[0]
checker = dependent.call checker = dependent.call
@@ -67,7 +67,7 @@ async def test_message(type: str, expected: bool):
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.parametrize("type, expected", [("message", False), ("notice", True)]) @pytest.mark.parametrize(("type", "expected"), [("message", False), ("notice", True)])
async def test_notice(type: str, expected: bool): async def test_notice(type: str, expected: bool):
dependent = list(NOTICE.checkers)[0] dependent = list(NOTICE.checkers)[0]
checker = dependent.call checker = dependent.call
@@ -79,7 +79,7 @@ async def test_notice(type: str, expected: bool):
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.parametrize("type, expected", [("message", False), ("request", True)]) @pytest.mark.parametrize(("type", "expected"), [("message", False), ("request", True)])
async def test_request(type: str, expected: bool): async def test_request(type: str, expected: bool):
dependent = list(REQUEST.checkers)[0] dependent = list(REQUEST.checkers)[0]
checker = dependent.call checker = dependent.call
@@ -91,7 +91,9 @@ async def test_request(type: str, expected: bool):
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.parametrize("type, expected", [("message", False), ("meta_event", True)]) @pytest.mark.parametrize(
("type", "expected"), [("message", False), ("meta_event", True)]
)
async def test_metaevent(type: str, expected: bool): async def test_metaevent(type: str, expected: bool):
dependent = list(METAEVENT.checkers)[0] dependent = list(METAEVENT.checkers)[0]
checker = dependent.call checker = dependent.call
@@ -104,7 +106,7 @@ async def test_metaevent(type: str, expected: bool):
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.parametrize( @pytest.mark.parametrize(
"type, user_id, expected", ("type", "user_id", "expected"),
[ [
("message", "test", True), ("message", "test", True),
("message", "foo", False), ("message", "foo", False),
@@ -128,7 +130,7 @@ async def test_superuser(app: App, type: str, user_id: str, expected: bool):
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.parametrize( @pytest.mark.parametrize(
"session_ids, session_id, expected", ("session_ids", "session_id", "expected"),
[ [
(("user", "foo"), "user", True), (("user", "foo"), "user", True),
(("user", "foo"), "bar", False), (("user", "foo"), "bar", False),

Some files were not shown because too many files have changed in this diff Show More