nonebot-plugin-onebot-luckperms 0.1.0__tar.gz → 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nonebot_plugin_onebot_luckperms-0.1.2/PKG-INFO +510 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/README.md +7 -6
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/__init__.py +25 -26
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/adapter/identity.py +1 -3
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/adapter/permission.py +14 -14
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/commands/admin.py +1 -4
- nonebot_plugin_onebot_luckperms-0.1.2/nonebot_plugin_onebot_luckperms/config.py +14 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/core/cache.py +7 -3
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/core/context_provider.py +1 -3
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/core/registry.py +2 -3
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/message.py +2 -4
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/storage/__init__.py +1 -1
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/storage/redis.py +2 -3
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/storage/sqlite.py +3 -4
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/webeditor/bytebin.py +2 -3
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/webeditor/manager.py +8 -9
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/webeditor/session.py +11 -12
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/webeditor/websocket.py +10 -11
- nonebot_plugin_onebot_luckperms-0.1.2/nonebot_plugin_onebot_luckperms.egg-info/PKG-INFO +510 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms.egg-info/requires.txt +2 -1
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/pyproject.toml +4 -2
- nonebot_plugin_onebot_luckperms-0.1.0/PKG-INFO +0 -23
- nonebot_plugin_onebot_luckperms-0.1.0/nonebot_plugin_onebot_luckperms/config.py +0 -20
- nonebot_plugin_onebot_luckperms-0.1.0/nonebot_plugin_onebot_luckperms.egg-info/PKG-INFO +0 -23
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/LICENSE +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/adapter/__init__.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/adapter/context.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/commands/__init__.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/core/__init__.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/core/engine.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/core/exceptions.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/core/models.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/py.typed +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/storage/memory.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/storage/protocol.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms/webeditor/__init__.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms.egg-info/SOURCES.txt +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms.egg-info/dependency_links.txt +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/nonebot_plugin_onebot_luckperms.egg-info/top_level.txt +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/setup.cfg +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/tests/test_borderline.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/tests/test_context.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/tests/test_models.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/tests/test_storage.py +0 -0
- {nonebot_plugin_onebot_luckperms-0.1.0 → nonebot_plugin_onebot_luckperms-0.1.2}/tests/test_webeditor.py +0 -0
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nonebot-plugin-onebot-luckperms
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: LuckPerms-style permission system for NoneBot2 + OneBot
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://github.com/XChen446/nonebot-plugin-onebot-luckperms
|
|
7
|
+
Requires-Python: >=3.10
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: nonebot2>=2.5.0
|
|
11
|
+
Requires-Dist: nonebot-adapter-onebot>=2.4.6
|
|
12
|
+
Requires-Dist: nonebot-plugin-localstore>=0.6.0
|
|
13
|
+
Requires-Dist: pydantic>=1.10.0
|
|
14
|
+
Requires-Dist: aiohttp>=3.14.1
|
|
15
|
+
Requires-Dist: pyyaml>=6.0
|
|
16
|
+
Requires-Dist: aiosqlite>=0.22.1
|
|
17
|
+
Provides-Extra: redis
|
|
18
|
+
Requires-Dist: redis>=8.0; extra == "redis"
|
|
19
|
+
Provides-Extra: test
|
|
20
|
+
Requires-Dist: pytest>=9.0; extra == "test"
|
|
21
|
+
Requires-Dist: pytest-asyncio>=1.4; extra == "test"
|
|
22
|
+
Provides-Extra: all
|
|
23
|
+
Requires-Dist: nonebot-plugin-onebot-luckperms[redis]; extra == "all"
|
|
24
|
+
Requires-Dist: nonebot-plugin-onebot-luckperms[test]; extra == "all"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# nonebot-plugin-onebot-luckperms(OBLP)
|
|
28
|
+
|
|
29
|
+
LuckPerms 风格的权限节点管理系统,专为 NoneBot2 + OneBot 适配器设计。
|
|
30
|
+
|
|
31
|
+
> English: [README_EN.md](./README_EN.md)
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 目录
|
|
36
|
+
|
|
37
|
+
- [安装](#安装)
|
|
38
|
+
- [快速开始](#快速开始)
|
|
39
|
+
- [配置](#配置)
|
|
40
|
+
- [命令列表](#命令列表)
|
|
41
|
+
- [内建 Context 键](#内建-context-键)
|
|
42
|
+
- [权限节点注册(面向开发者)](#权限节点注册面向开发者)
|
|
43
|
+
- [ContextProvider(面向开发者)](#contextprovider面向开发者)
|
|
44
|
+
- [API 参考](#api-参考)
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## 安装
|
|
49
|
+
|
|
50
|
+
### 通过 NB-CLI(推荐)
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
nb plugin install nonebot-plugin-onebot-luckperms
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### 通过 pip
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install nonebot-plugin-onebot-luckperms
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
如需 Redis 后端:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install nonebot-plugin-onebot-luckperms[redis]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
全部安装:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
pip install nonebot-plugin-onebot-luckperms[all]
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## 快速开始
|
|
77
|
+
|
|
78
|
+
在 NoneBot2 的 `pyproject.toml` 或 `.env` 中添加插件:
|
|
79
|
+
|
|
80
|
+
```toml
|
|
81
|
+
[tool.nonebot]
|
|
82
|
+
plugins = ["nonebot_plugin_onebot_luckperms"]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
插件加载后,所有 `/lp` 命令默认仅 **SUPERUSER**(在 `env` 中配置 `SUPERUSER=123456`)可执行。
|
|
86
|
+
|
|
87
|
+
### 给用户一个权限
|
|
88
|
+
|
|
89
|
+
```text
|
|
90
|
+
/lp user 123456 permission set myplugin.ban true
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### 创建一个组并分配权限
|
|
94
|
+
|
|
95
|
+
```text
|
|
96
|
+
/lp creategroup admin 100
|
|
97
|
+
/lp group admin permission set myplugin.kick true
|
|
98
|
+
/lp user 123456 parent add admin
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 设置仅在特定群生效的权限
|
|
102
|
+
|
|
103
|
+
```text
|
|
104
|
+
/lp user 123456 permission set myplugin.mute true group_id=987654
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### 临时权限(30 分钟后自动过期)
|
|
108
|
+
|
|
109
|
+
```text
|
|
110
|
+
/lp user 123456 permission settemp myplugin.ban true 30m
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### 打开 Web Editor
|
|
114
|
+
|
|
115
|
+
```text
|
|
116
|
+
/lp editor
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
在群聊中打开时,editor 会自动附带该群所有成员 QQ 到用户列表。
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## 配置
|
|
124
|
+
|
|
125
|
+
在 `.env` 或 `pyproject.toml` 中(前缀 `OBLP_`):
|
|
126
|
+
|
|
127
|
+
| 环境变量 | 默认值 | 说明 |
|
|
128
|
+
|---------|--------|------|
|
|
129
|
+
| `OBLP_STORE_TYPE` | `sqlite` | 存储后端:`memory` / `sqlite` / `redis` |
|
|
130
|
+
| `OBLP_REDIS_URL` | `redis://localhost:6379/0` | Redis 连接 URL |
|
|
131
|
+
| `OBLP_DEFAULT_GROUP_OWNER` | `["luckperms.help"]` | 群主自动继承的权限节点 |
|
|
132
|
+
| `OBLP_DEFAULT_GROUP_ADMIN` | `[]` | 群管理自动继承的权限节点 |
|
|
133
|
+
| `OBLP_DEFAULT_GROUP_MEMBER` | `[]` | 普通成员自动继承的权限节点 |
|
|
134
|
+
| `OBLP_SUPERUSER_INHERIT` | `["luckperms.*"]` | SUPERUSER 自动继承的权限节点 |
|
|
135
|
+
| `OBLP_CACHE_TTL` | `300` | 权限缓存时间(秒),`0` 禁用缓存 |
|
|
136
|
+
|
|
137
|
+
### 配置示例
|
|
138
|
+
|
|
139
|
+
```ini
|
|
140
|
+
SUPERUSER=123456
|
|
141
|
+
OBLP_STORE_TYPE=sqlite
|
|
142
|
+
OBLP_DEFAULT_GROUP_ADMIN=["myplugin.mute","myplugin.kick"]
|
|
143
|
+
OBLP_DEFAULT_GROUP_MEMBER=[]
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### 数据存储
|
|
147
|
+
|
|
148
|
+
SQLite 数据库与自定义回复消息文件存放在 `nonebot-plugin-localstore` 管理的数据目录下(可用
|
|
149
|
+
`LOCALSTORE_*` 系列环境变量自定义,例如 `LOCALSTORE_PLUGIN_DATA_DIR`)。
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## 命令列表
|
|
154
|
+
|
|
155
|
+
所有命令均以 `/lp` 开头。每个命令都受对应的 `luckperms.*` 权限节点保护。
|
|
156
|
+
|
|
157
|
+
SUPERUSER 默认拥有 `luckperms.*`(全部权限)。群主默认拥有 `luckperms.help`(仅能看帮助)。
|
|
158
|
+
|
|
159
|
+
### 通用命令
|
|
160
|
+
|
|
161
|
+
| 命令 | 所需权限节点 | 说明 |
|
|
162
|
+
|------|-------------|------|
|
|
163
|
+
| `/lp help` | `luckperms.help` | 显示帮助 |
|
|
164
|
+
| `/lp sync` | `luckperms.sync` | 重新加载数据 |
|
|
165
|
+
| `/lp info` | `luckperms.info` | 显示统计信息 |
|
|
166
|
+
| `/lp editor` | `luckperms.editor` | 打开 LuckPerms Web Editor |
|
|
167
|
+
| `/lp applyedits <code>` | `luckperms.applyedits` | 应用 Web Editor 的 edits |
|
|
168
|
+
|
|
169
|
+
### 用户管理
|
|
170
|
+
|
|
171
|
+
| 命令 | 所需权限节点 | 说明 |
|
|
172
|
+
|------|-------------|------|
|
|
173
|
+
| `/lp user <id> info` | `luckperms.user.info` | 查看用户详情 |
|
|
174
|
+
| `/lp user <id> permission ...` | `luckperms.user.permission` | 管理用户权限节点 |
|
|
175
|
+
| `/lp user <id> parent ...` | `luckperms.user.parent` | 管理用户组 |
|
|
176
|
+
| `/lp user <id> promote <track>` | `luckperms.user.promote` | 晋升 |
|
|
177
|
+
| `/lp user <id> demote <track>` | `luckperms.user.demote` | 降级 |
|
|
178
|
+
| `/lp user <id> clear [ctx...]` | `luckperms.user.clear` | 清空所有权限节点 |
|
|
179
|
+
| `/lp user <id> clone <target>` | `luckperms.user.clone` | 克隆用户 |
|
|
180
|
+
| `/lp user <id> editor` | `luckperms.editor` | 在 editor 中编辑此用户 |
|
|
181
|
+
| `/lp user create <id>` | `luckperms.user.create` | 创建用户 |
|
|
182
|
+
| `/lp user delete <id>` | `luckperms.user.delete` | 删除用户 |
|
|
183
|
+
| `/lp user list` | `luckperms.user.list` | 列出所有用户 |
|
|
184
|
+
|
|
185
|
+
### 组管理
|
|
186
|
+
|
|
187
|
+
| 命令 | 所需权限节点 | 说明 |
|
|
188
|
+
|------|-------------|------|
|
|
189
|
+
| `/lp group <name> info` | `luckperms.group.info` | 查看组详情 |
|
|
190
|
+
| `/lp group <name> permission ...` | `luckperms.group.permission` | 管理组权限 |
|
|
191
|
+
| `/lp group <name> parent ...` | `luckperms.group.parent` | 继承管理 |
|
|
192
|
+
| `/lp group <name> setweight <weight>` | `luckperms.group.setweight` | 设置权重 |
|
|
193
|
+
| `/lp group <name> setdisplayname <name>` | `luckperms.group.setdisplayname` | 设置显示名 |
|
|
194
|
+
| `/lp group <name> clear [ctx...]` | `luckperms.group.clear` | 清空权限 |
|
|
195
|
+
| `/lp group <name> rename <new>` | `luckperms.group.rename` | 重命名组 |
|
|
196
|
+
| `/lp group <name> clone <new>` | `luckperms.group.clone` | 克隆组 |
|
|
197
|
+
| `/lp group <name> listmembers` | `luckperms.group.listmembers` | 列出组成员 |
|
|
198
|
+
| `/lp group <name> editor` | `luckperms.editor` | 在 editor 中编辑此组 |
|
|
199
|
+
| `/lp creategroup <name> [weight]` | `luckperms.group.create` | 创建组 |
|
|
200
|
+
| `/lp deletegroup <name>` | `luckperms.group.delete` | 删除组 |
|
|
201
|
+
| `/lp listgroups` | `luckperms.group.list` | 列出所有组 |
|
|
202
|
+
|
|
203
|
+
### 轨道管理(预留)
|
|
204
|
+
|
|
205
|
+
| 命令 | 所需权限节点 | 说明 |
|
|
206
|
+
|------|-------------|------|
|
|
207
|
+
| `/lp track <name> ...` | `luckperms.*` | 轨道操作 |
|
|
208
|
+
|
|
209
|
+
### 系统命令
|
|
210
|
+
|
|
211
|
+
| 命令 | 所需权限节点 | 说明 |
|
|
212
|
+
|------|-------------|------|
|
|
213
|
+
| `/lp check <user> <node> [ctx...]` | `luckperms.check` | 模拟权限检查 |
|
|
214
|
+
| `/lp tree [scope]` | `luckperms.tree` | 权限继承树可视化 |
|
|
215
|
+
|
|
216
|
+
### 命令执行反馈
|
|
217
|
+
|
|
218
|
+
无权限时(用户没有所需的 `luckperms.xxx` 节点),bot 回复 `deny` 消息。
|
|
219
|
+
|
|
220
|
+
### 自定义回复消息
|
|
221
|
+
|
|
222
|
+
所有用户可见的回复文本均存储在插件数据目录下的 `messages.yml` 文件中。
|
|
223
|
+
插件首次运行时会自动生成此文件。你可以编辑它来自定义所有回复,支持占位符:
|
|
224
|
+
|
|
225
|
+
```yaml
|
|
226
|
+
# messages.yml (位于插件数据目录)
|
|
227
|
+
deny: "你没有权限执行此命令。"
|
|
228
|
+
deny_hint: "请联系管理员开通对应权限节点。"
|
|
229
|
+
perm_set: "{entity}: {action} {node}"
|
|
230
|
+
perm_temp: "{entity}: 临时{action} {node} ({duration})"
|
|
231
|
+
editor_opened: "Web 编辑器已开启: {url}"
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
可用占位符:
|
|
235
|
+
| 占位符 | 说明 |
|
|
236
|
+
|--------|------|
|
|
237
|
+
| `{user_id}` | 用户 ID |
|
|
238
|
+
| `{node}` | 权限节点名 |
|
|
239
|
+
| `{entity}` | 用户或组 ID |
|
|
240
|
+
| `{action}` | grant 或 deny |
|
|
241
|
+
| `{duration}` | 持续时间 |
|
|
242
|
+
| `{context}` | 上下文键值对 |
|
|
243
|
+
| `{url}` | Editor URL |
|
|
244
|
+
| `{code}` | Edit code |
|
|
245
|
+
| `{error}` | 错误描述 |
|
|
246
|
+
| `{name}` | 组名 |
|
|
247
|
+
| `{weight}` | 组权重 |
|
|
248
|
+
|
|
249
|
+
有权限时的执行反馈示例:
|
|
250
|
+
|
|
251
|
+
```
|
|
252
|
+
/lp sync
|
|
253
|
+
→ Data reloaded
|
|
254
|
+
|
|
255
|
+
/lp user 123456 permission set myplugin.ban true
|
|
256
|
+
→ 123456: grant myplugin.ban
|
|
257
|
+
|
|
258
|
+
/lp user 123456 permission settemp myplugin.kick false 30m
|
|
259
|
+
→ 123456: temp deny myplugin.kick (30m)
|
|
260
|
+
|
|
261
|
+
/lp group admin permission set myplugin.mute true group_id=987654
|
|
262
|
+
→ admin: grant myplugin.mute (ctx: {'group_id': '987654'})
|
|
263
|
+
|
|
264
|
+
/lp check 123456 myplugin.ban
|
|
265
|
+
→ Check: user=123456, node=myplugin.ban
|
|
266
|
+
Context: (empty)
|
|
267
|
+
Result: ALLOW
|
|
268
|
+
Resolved nodes (2):
|
|
269
|
+
myplugin.ban=grant
|
|
270
|
+
luckperms.*=grant
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
### 默认角色权限
|
|
274
|
+
|
|
275
|
+
| 角色 | 默认拥有的节点 | 说明 |
|
|
276
|
+
|------|--------------|------|
|
|
277
|
+
| SUPERUSER | `luckperms.*` | 全部命令 |
|
|
278
|
+
| 群主 (owner) | `luckperms.help` | 仅可看帮助(可配置) |
|
|
279
|
+
| 管理 (admin) | *(空)* | 无内建命令权限(可配置) |
|
|
280
|
+
| 成员 (member) | *(空)* | 无内建命令权限(可配置) |
|
|
281
|
+
|
|
282
|
+
可通过 `OBLP_DEFAULT_GROUP_OWNER`、`OBLP_DEFAULT_GROUP_ADMIN`、`OBLP_DEFAULT_GROUP_MEMBER` 配置。
|
|
283
|
+
|
|
284
|
+
### 临时权限 duration 格式
|
|
285
|
+
|
|
286
|
+
| 写法 | 含义 |
|
|
287
|
+
|------|------|
|
|
288
|
+
| `30s` | 30 秒 |
|
|
289
|
+
| `5m` | 5 分钟 |
|
|
290
|
+
| `2h` | 2 小时 |
|
|
291
|
+
| `1d` | 1 天 |
|
|
292
|
+
|
|
293
|
+
---
|
|
294
|
+
|
|
295
|
+
## 内建 Context 键
|
|
296
|
+
|
|
297
|
+
权限节点可以通过 `key=value` 附加上下文约束,使节点仅在特定环境中生效。由系统自动注入:
|
|
298
|
+
|
|
299
|
+
| Context 键 | 类型 | 来源 | 示例值 |
|
|
300
|
+
|-----------|------|---------|--------|
|
|
301
|
+
| `platform` | `str` | 适配器类型 | `onebot_v11`, `onebot_v12` |
|
|
302
|
+
| `user_id` | `str` | 发送者 ID | `123456` |
|
|
303
|
+
| `group_id` | `str` | 群号(群聊时) | `987654` |
|
|
304
|
+
| `role` | `str` | 群内角色 | `owner`, `admin`, `member`, `superuser` |
|
|
305
|
+
|
|
306
|
+
第三方插件可通过 ContextProvider(见下文)注册自定义 Context 键。
|
|
307
|
+
|
|
308
|
+
### Context 使用示例
|
|
309
|
+
|
|
310
|
+
仅群 123456 中有效:
|
|
311
|
+
|
|
312
|
+
```text
|
|
313
|
+
/lp user 123456 permission set myplugin.ban true group_id=123456
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
仅群管理时有效:
|
|
317
|
+
|
|
318
|
+
```text
|
|
319
|
+
/lp user 123456 permission set myplugin.kick true role=admin
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
仅群 123456 且为群主时有效:
|
|
323
|
+
|
|
324
|
+
```text
|
|
325
|
+
/lp group admin permission set myplugin.ban true group_id=123456 role=owner
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
---
|
|
329
|
+
|
|
330
|
+
## 权限节点注册(面向开发者)
|
|
331
|
+
|
|
332
|
+
### 基本用法
|
|
333
|
+
|
|
334
|
+
```python
|
|
335
|
+
from nonebot import on_command
|
|
336
|
+
from nonebot_plugin_onebot_luckperms import register_node, require
|
|
337
|
+
|
|
338
|
+
# 在插件加载时注册节点
|
|
339
|
+
register_node("myplugin.ban", "禁言用户", default=False)
|
|
340
|
+
|
|
341
|
+
# 绑定到命令
|
|
342
|
+
ban = on_command("ban", permission=require("myplugin.ban"))
|
|
343
|
+
|
|
344
|
+
@ban.handle()
|
|
345
|
+
async def _():
|
|
346
|
+
await ban.send("已执行")
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
### 带默认上下文的注册
|
|
350
|
+
|
|
351
|
+
```python
|
|
352
|
+
from nonebot_plugin_onebot_luckperms import register_node, ContextSet
|
|
353
|
+
|
|
354
|
+
# 仅群主默认拥有此权限
|
|
355
|
+
register_node("myplugin.admincmd", "管理命令", default=True,
|
|
356
|
+
contexts=ContextSet({"role": "owner"}))
|
|
357
|
+
|
|
358
|
+
# 仅在特定群 + admin 时默认拥有
|
|
359
|
+
register_node("myplugin.vipcmd", "VIP 命令", default=True,
|
|
360
|
+
contexts=ContextSet({"group_id": "123456", "role": "admin"}))
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
### require / require_any / require_all
|
|
364
|
+
|
|
365
|
+
```python
|
|
366
|
+
from nonebot_plugin_onebot_luckperms import require, require_any, require_all
|
|
367
|
+
from nonebot.adapters.onebot.v11.permission import GROUP_ADMIN
|
|
368
|
+
|
|
369
|
+
# 单节点
|
|
370
|
+
cmd = on_command("cmd", permission=require("myplugin.cmd"))
|
|
371
|
+
|
|
372
|
+
# 多节点 OR
|
|
373
|
+
cmd = on_command("cmd", permission=require_any("node.a", "node.b"))
|
|
374
|
+
|
|
375
|
+
# 多节点 AND
|
|
376
|
+
cmd = on_command("cmd", permission=require_all("node.a", "node.b"))
|
|
377
|
+
|
|
378
|
+
# 与原生权限组合
|
|
379
|
+
cmd = on_command("cmd", permission=require("myplugin.cmd") | GROUP_ADMIN)
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### 获取权限上下文
|
|
383
|
+
|
|
384
|
+
```python
|
|
385
|
+
from nonebot_plugin_onebot_luckperms import get_context
|
|
386
|
+
|
|
387
|
+
@cmd.handle()
|
|
388
|
+
async def _():
|
|
389
|
+
ctx = get_context()
|
|
390
|
+
if not ctx:
|
|
391
|
+
return
|
|
392
|
+
print(ctx.user.user_id) # "123456"
|
|
393
|
+
print(ctx.identity.role) # "admin"
|
|
394
|
+
print(ctx.resolved_nodes) # {"myplugin.ban": True, ...}
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
---
|
|
398
|
+
|
|
399
|
+
## ContextProvider(面向开发者)
|
|
400
|
+
|
|
401
|
+
ContextProvider 允许第三方插件向全局权限判定环境注入自定义 Context 键值对。
|
|
402
|
+
|
|
403
|
+
### 定义并注册
|
|
404
|
+
|
|
405
|
+
```python
|
|
406
|
+
from nonebot_plugin_onebot_luckperms import register_context_provider
|
|
407
|
+
|
|
408
|
+
class BindProvider:
|
|
409
|
+
context_keys = {"bind"} # 声明此 provider 会注入的键
|
|
410
|
+
|
|
411
|
+
async def __call__(self, bot, event, current_ctx):
|
|
412
|
+
# 从数据库或 API 查询用户的绑定状态
|
|
413
|
+
return {"bind": "true"} # 键值对会合并到判定上下文中
|
|
414
|
+
|
|
415
|
+
register_context_provider(BindProvider())
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
注册后,其他所有插件都可以利用这个 Context 来控制权限生效范围:
|
|
419
|
+
|
|
420
|
+
```python
|
|
421
|
+
register_node("someplugin.premium", default=True,
|
|
422
|
+
contexts=ContextSet({"bind": "true"}))
|
|
423
|
+
```
|
|
424
|
+
|
|
425
|
+
### 冲突检测
|
|
426
|
+
|
|
427
|
+
如果两个不同的 Provider 声明了相同的 `context_keys`,后者注册时会抛出 `DuplicateProviderError`:
|
|
428
|
+
|
|
429
|
+
```python
|
|
430
|
+
ProviderA: context_keys = {"bind"} # 注册成功
|
|
431
|
+
ProviderB: context_keys = {"bind"} # → DuplicateProviderError!
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
---
|
|
435
|
+
|
|
436
|
+
## API 参考
|
|
437
|
+
|
|
438
|
+
### 用户端函数
|
|
439
|
+
|
|
440
|
+
| 函数 | 说明 |
|
|
441
|
+
|------|------|
|
|
442
|
+
| `require(node_key, contexts=None) -> Permission` | 单节点权限检查器 |
|
|
443
|
+
| `require_any(*node_keys, contexts=None) -> Permission` | OR 权限检查器 |
|
|
444
|
+
| `require_all(*node_keys, contexts=None) -> Permission` | AND 权限检查器 |
|
|
445
|
+
| `get_context() -> Optional[LPContext]` | 在 Handler 中获取权限上下文 |
|
|
446
|
+
| `get_store() -> Optional[PermissionStore]` | 获取当前存储后端 |
|
|
447
|
+
|
|
448
|
+
### 开发者端函数
|
|
449
|
+
|
|
450
|
+
| 函数 | 说明 |
|
|
451
|
+
|------|------|
|
|
452
|
+
| `register_node(key, description="", default=False, contexts=None)` | 注册权限节点 |
|
|
453
|
+
| `register_context_provider(provider)` | 注册全局 ContextProvider |
|
|
454
|
+
| `set_resolver(resolver)` | 覆盖 IdentityResolver |
|
|
455
|
+
| `ContextSet(data={})` | 上下文集合 |
|
|
456
|
+
| `PermissionNode(key, value=True, expiry=None, contexts=ContextSet())` | 权限节点 |
|
|
457
|
+
|
|
458
|
+
### 模型类
|
|
459
|
+
|
|
460
|
+
```python
|
|
461
|
+
@dataclass(frozen=True)
|
|
462
|
+
class ContextSet:
|
|
463
|
+
data: Dict[str, str]
|
|
464
|
+
def with_context(self, key, value) -> ContextSet
|
|
465
|
+
def matches(self, requirement) -> bool
|
|
466
|
+
def is_empty(self) -> bool
|
|
467
|
+
|
|
468
|
+
@dataclass
|
|
469
|
+
class PermissionNode:
|
|
470
|
+
key: str
|
|
471
|
+
value: bool
|
|
472
|
+
expiry: Optional[int]
|
|
473
|
+
contexts: ContextSet
|
|
474
|
+
def is_expired(self) -> bool
|
|
475
|
+
def applies_in(self, ctx) -> bool
|
|
476
|
+
|
|
477
|
+
@dataclass(frozen=True)
|
|
478
|
+
class LPContext:
|
|
479
|
+
user: User
|
|
480
|
+
identity: Identity
|
|
481
|
+
query_options: QueryOptions
|
|
482
|
+
resolved_nodes: Dict[str, bool]
|
|
483
|
+
matched_node: Optional[str]
|
|
484
|
+
timestamp: float
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
---
|
|
488
|
+
|
|
489
|
+
## 测试
|
|
490
|
+
|
|
491
|
+
```bash
|
|
492
|
+
pytest tests/ -v
|
|
493
|
+
```
|
|
494
|
+
|
|
495
|
+
当前 91 项测试全部通过。
|
|
496
|
+
|
|
497
|
+
---
|
|
498
|
+
|
|
499
|
+
## 架构
|
|
500
|
+
|
|
501
|
+
```
|
|
502
|
+
Layer 4: NoneBot Integration (adapter/) — require(), IdentityResolver, LPContext
|
|
503
|
+
Layer 3: Core Engine (core/) — 纯 Python,不依赖 NoneBot
|
|
504
|
+
Layer 2: Storage (storage/) — Memory / SQLite / Redis
|
|
505
|
+
Layer 1: Config & Bootstrap (config/) — Pydantic 配置
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
## License
|
|
509
|
+
|
|
510
|
+
MIT
|
|
@@ -101,26 +101,27 @@ plugins = ["nonebot_plugin_onebot_luckperms"]
|
|
|
101
101
|
| 环境变量 | 默认值 | 说明 |
|
|
102
102
|
|---------|--------|------|
|
|
103
103
|
| `OBLP_STORE_TYPE` | `sqlite` | 存储后端:`memory` / `sqlite` / `redis` |
|
|
104
|
-
| `OBLP_SQLITE_PATH` | `./data/oblp/permissions.db` | SQLite 文件路径 |
|
|
105
104
|
| `OBLP_REDIS_URL` | `redis://localhost:6379/0` | Redis 连接 URL |
|
|
106
105
|
| `OBLP_DEFAULT_GROUP_OWNER` | `["luckperms.help"]` | 群主自动继承的权限节点 |
|
|
107
106
|
| `OBLP_DEFAULT_GROUP_ADMIN` | `[]` | 群管理自动继承的权限节点 |
|
|
108
107
|
| `OBLP_DEFAULT_GROUP_MEMBER` | `[]` | 普通成员自动继承的权限节点 |
|
|
109
108
|
| `OBLP_SUPERUSER_INHERIT` | `["luckperms.*"]` | SUPERUSER 自动继承的权限节点 |
|
|
110
109
|
| `OBLP_CACHE_TTL` | `300` | 权限缓存时间(秒),`0` 禁用缓存 |
|
|
111
|
-
| `OBLP_MESSAGE_FILE` | `./data/oblp/messages.yml` | 自定义回复消息的 YAML 文件路径 |
|
|
112
|
-
| `OBLP_DEBUG_MODE` | `false` | 开启后打印每次权限判定日志 |
|
|
113
110
|
|
|
114
111
|
### 配置示例
|
|
115
112
|
|
|
116
113
|
```ini
|
|
117
114
|
SUPERUSER=123456
|
|
118
115
|
OBLP_STORE_TYPE=sqlite
|
|
119
|
-
OBLP_SQLITE_PATH=./data/oblp/permissions.db
|
|
120
116
|
OBLP_DEFAULT_GROUP_ADMIN=["myplugin.mute","myplugin.kick"]
|
|
121
117
|
OBLP_DEFAULT_GROUP_MEMBER=[]
|
|
122
118
|
```
|
|
123
119
|
|
|
120
|
+
### 数据存储
|
|
121
|
+
|
|
122
|
+
SQLite 数据库与自定义回复消息文件存放在 `nonebot-plugin-localstore` 管理的数据目录下(可用
|
|
123
|
+
`LOCALSTORE_*` 系列环境变量自定义,例如 `LOCALSTORE_PLUGIN_DATA_DIR`)。
|
|
124
|
+
|
|
124
125
|
---
|
|
125
126
|
|
|
126
127
|
## 命令列表
|
|
@@ -192,11 +193,11 @@ SUPERUSER 默认拥有 `luckperms.*`(全部权限)。群主默认拥有 `luc
|
|
|
192
193
|
|
|
193
194
|
### 自定义回复消息
|
|
194
195
|
|
|
195
|
-
|
|
196
|
+
所有用户可见的回复文本均存储在插件数据目录下的 `messages.yml` 文件中。
|
|
196
197
|
插件首次运行时会自动生成此文件。你可以编辑它来自定义所有回复,支持占位符:
|
|
197
198
|
|
|
198
199
|
```yaml
|
|
199
|
-
#
|
|
200
|
+
# messages.yml (位于插件数据目录)
|
|
200
201
|
deny: "你没有权限执行此命令。"
|
|
201
202
|
deny_hint: "请联系管理员开通对应权限节点。"
|
|
202
203
|
perm_set: "{entity}: {action} {node}"
|
|
@@ -1,19 +1,30 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
from nonebot import get_driver, get_plugin_config, require
|
|
2
|
+
from nonebot.log import logger
|
|
3
3
|
from nonebot.plugin import PluginMetadata
|
|
4
4
|
|
|
5
|
+
# 依赖插件需在加载时声明:localstore 自身在导入时读取配置,
|
|
6
|
+
# 此时 driver 已由 nonebot.init 初始化完毕,可以安全调用 get_plugin_config。
|
|
7
|
+
require("nonebot_plugin_localstore")
|
|
8
|
+
import nonebot_plugin_localstore as store
|
|
9
|
+
|
|
10
|
+
from .config import OBLPConfig
|
|
5
11
|
from .core.registry import register_node
|
|
6
12
|
from .core.models import ContextSet, PermissionNode
|
|
7
13
|
from .core.context_provider import register_context_provider, ContextProvider
|
|
8
14
|
from .storage import get_store
|
|
9
15
|
from .adapter import set_resolver, require, require_any, require_all, get_context
|
|
10
16
|
from .adapter.context import LPContext
|
|
11
|
-
from .config import OBLPConfig, oblp_config
|
|
12
|
-
|
|
13
|
-
logger = logging.getLogger("oblp")
|
|
14
17
|
|
|
15
18
|
__version__ = "0.1.0"
|
|
16
19
|
|
|
20
|
+
# 插件配置在加载时读取(而非等待 on_startup)
|
|
21
|
+
plugin_config = get_plugin_config(OBLPConfig)
|
|
22
|
+
|
|
23
|
+
# 数据文件路径在加载时由 localstore 确定
|
|
24
|
+
# (仅创建目录,不创建文件;实际写入在初始化阶段进行)
|
|
25
|
+
PLUGIN_DB_FILE = str(store.get_plugin_data_file("permissions.db"))
|
|
26
|
+
PLUGIN_MESSAGE_FILE = str(store.get_plugin_data_file("messages.yml"))
|
|
27
|
+
|
|
17
28
|
__plugin_meta__ = PluginMetadata(
|
|
18
29
|
name="nonebot-plugin-onebot-luckperms",
|
|
19
30
|
description="LuckPerms-style permission system for NoneBot2 + OneBot",
|
|
@@ -33,7 +44,6 @@ __all__ = [
|
|
|
33
44
|
"set_resolver",
|
|
34
45
|
"ContextSet",
|
|
35
46
|
"PermissionNode",
|
|
36
|
-
"oblp_config",
|
|
37
47
|
"get_store",
|
|
38
48
|
"LPContext",
|
|
39
49
|
"register_context_provider",
|
|
@@ -85,33 +95,26 @@ async def _init():
|
|
|
85
95
|
if _store_initialized:
|
|
86
96
|
return
|
|
87
97
|
|
|
88
|
-
from nonebot import get_plugin_config
|
|
89
|
-
from .config import OBLPConfig
|
|
90
98
|
from .storage import init_store
|
|
91
99
|
from .adapter.identity import set_resolver as _set_resolver, OneBotV11Resolver
|
|
92
100
|
from .commands import register_admin_commands
|
|
93
101
|
from .message import load_messages, _export_defaults
|
|
94
102
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
for field in raw_cfg.model_fields:
|
|
98
|
-
setattr(oblp_config, field, getattr(raw_cfg, field))
|
|
99
|
-
|
|
100
|
-
store_type = oblp_config.store_type
|
|
103
|
+
store_type = plugin_config.oblp_store_type
|
|
101
104
|
kwargs = {}
|
|
102
105
|
if store_type == "sqlite":
|
|
103
|
-
kwargs["db_path"] =
|
|
106
|
+
kwargs["db_path"] = PLUGIN_DB_FILE
|
|
104
107
|
elif store_type == "redis":
|
|
105
|
-
kwargs["redis_url"] =
|
|
108
|
+
kwargs["redis_url"] = plugin_config.oblp_redis_url
|
|
106
109
|
|
|
107
|
-
|
|
108
|
-
await
|
|
110
|
+
backend = init_store(store_type, **kwargs)
|
|
111
|
+
await backend.load_all()
|
|
109
112
|
_set_resolver(OneBotV11Resolver())
|
|
110
113
|
register_admin_commands()
|
|
111
114
|
|
|
112
|
-
await load_messages(
|
|
115
|
+
await load_messages(PLUGIN_MESSAGE_FILE)
|
|
113
116
|
try:
|
|
114
|
-
await _export_defaults(
|
|
117
|
+
await _export_defaults(PLUGIN_MESSAGE_FILE)
|
|
115
118
|
except Exception:
|
|
116
119
|
pass
|
|
117
120
|
|
|
@@ -119,9 +122,5 @@ async def _init():
|
|
|
119
122
|
logger.info(f"OBLP initialized (store: {store_type})")
|
|
120
123
|
|
|
121
124
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
driver = get_driver()
|
|
125
|
-
driver.on_startup(_init)
|
|
126
|
-
except (ValueError, RuntimeError, ImportError):
|
|
127
|
-
logger.debug("NoneBot not initialized, OBLP will init on first use")
|
|
125
|
+
driver = get_driver()
|
|
126
|
+
driver.on_startup(_init)
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
import logging
|
|
4
3
|
from dataclasses import dataclass, field
|
|
5
4
|
from typing import Any, Dict, Optional, Protocol
|
|
6
5
|
|
|
7
6
|
from nonebot.adapters import Bot, Event
|
|
8
|
-
|
|
9
|
-
logger = logging.getLogger("oblp")
|
|
7
|
+
from nonebot.log import logger
|
|
10
8
|
|
|
11
9
|
|
|
12
10
|
@dataclass
|