插件是 plugins/ 下的一个目录,含一份 basic.json 清单和一个 Python 入口文件。它在进程内运行,与内核共用同一个事件循环,因此可以影响内核行为——这正是为什么清单必须事先声明意图,并由宿主强制执行。当前 API 版本为 1。
清单 basic.json
{
"id": "guard",
"name": "消息白名单 / 黑名单",
"version": "1.0.0",
"api": 1,
"entry": "plugin.py:GuardPlugin",
"tier": "core",
"permissions": [
"events.read",
"events.drop",
"config.read",
"config.write",
"runtime.read"
],
"hooks": ["on_message"],
"log": {"register": true},
"priority": 10,
"enabled": true,
"description": "白名单外或黑名单内的消息在进入事件管线前丢弃。"
}
| 字段 | 必填 | 含义 |
|---|---|---|
id | ✅ | ^[a-z][a-z0-9_-]{1,31}$,全局唯一 |
name / version | ✅ | 用于显示与审计 |
entry | ✅ | file.py:ClassName;该类以无参方式构造 |
tier | — | sandbox < standard < core(默认 standard)。plugins.max_tier 会拒绝要求更高层级的插件 |
permissions | — | 下表权限的子集;出现其他值即加载错误 |
accounts | — | 允许收发的 QQ 号数组、["ALL"] 或省略;省略 = 账号无关 |
log / priority / enabled | — | 日志分类注册;数值越小越先运行(守卫用 10);false 时跳过该插件 |
权限模型
| 权限 | 授予的能力 | 未声明时的表现 |
|---|---|---|
events.read | 收到 on_message / on_event 调用 | 钩子不会被调用,加载时告警 |
events.drop | ctx.drop() | 抛 PermissionDenied |
messages.send | ctx.send_group_msg / ctx.send_private_msg | 抛 PermissionDenied |
actions.invoke | ctx.call() / ctx.invoke_for() | 抛 PermissionDenied |
actions.register | 注册或替换动作 | 抛 PermissionDenied |
config.read / config.write | ctx.read_config() / ctx.write_config() | 抛 PermissionDenied |
runtime.read | ctx.runtime_info() / ctx.accounts() | 抛 PermissionDenied |
core.hooks | 当前不授予任何能力 | — |
未声明权限就调用对应 API 会抛 PermissionDenied,宿主记录并隔离本次调用;ctx.drop() 是唯一会向外传播的例外——它是控制流信号,不是错误。events.read 是真的会拦:实现了 on_message 却没声明它的插件,加载时得到一条警告,然后确实收不到消息。
账号范围有三种状态,别混淆:省略或 [] 表示账号无关,on_message 照常收到所有账号的消息,但不能调账号类接口;写具体号码表示只看、也只发这些号;["ALL"] 明确表示全部账号。省略与"全部"不是一回事,"忘了写"不该和"故意要全部"长得一样。账号类接口只认已登录的账号;拒绝分 AccountScopeUndeclared / AccountOutOfScope / AccountNotOnline 三种,判断顺序是先范围、后在线。只有 tier: "core" 的插件能用 ctx.restrict_account(uin, direction) 做内核级限制(in / out / both),且只在内存里生效。
生命周期钩子
class MyPlugin:
def on_load(self, ctx):
... # 可以 async
def on_message(self, message, ctx):
... # 返回 False / ctx.drop() 丢弃
# message.account 是这条消息属于哪个号
def on_event(self, event, ctx):
... # 返回 dict 替换,ctx.drop() 消费
def on_unload(self, ctx):
...
on_message在 URL 解析与存储之前运行;on_event在映射之后运行,每个 OneBot 事件执行一次。- 返回
False等价于丢弃,但需要events.drop权限;没有该权限的False会被忽略并告警。 - 任何异常都会被捕获并记录;该插件在本次事件中被跳过,其它插件照常。
- 热重载会监视插件目录下所有
*.py与basic.json,文件一变就重载(≤ 2 s)。
配置界面
basic.json 同时决定插件如何被配置。默认模式由 QSICK 渲染表单:
{
"config_ui": "default",
"config_fields": [
{"key": "mode", "label": "模式", "type": "str", "default": "blacklist"},
{"key": "groups", "label": "群名单(JSON)", "type": "json", "default": {"whitelist": [], "blacklist": []}},
{"key": "log_drops", "label": "记录日志", "type": "bool", "default": true}
]
}
config_fields 的 type 支持 str / int / float / bool / json;字段可用 configEnable 指定控件与可见性(ifShow,控件 text / switch / select)。默认页面在 /plugins/<id>/config,保存会写入 data/plugins/<id>/config.json 并切换启用状态。config_ui: "custom" 时插件自带 HTML 页面,以 iframe 形式在 /plugins/<id>/config/page 提供(已做路径穿越防护),通过 GET / POST /api/plugins/<id>/config 读写配置。
插件市场
/plugins/market 列出部署在 plugins.market_url(默认 https://sickcc.online/QSICK/plugins-market)的服务,支持搜索、排序、点赞与安装/更新。安装时会下载归档包、校验 basic.json、解压到插件目录并热加载;服务端源码位于仓库的 market/。
plugins.allow_remote_install=false 直接禁用远程安装;只安装你读过或信任的插件。故障隔离
- 导入或
on_load失败 ⇒ 插件不被加载,记录错误。 - 钩子抛出异常 ⇒ 该钩子在本次事件中被跳过。
- 钩子内出现
PermissionDenied⇒ 记录后事件继续。 - 没有
events.read⇒ 钩子不会被调用(加载时已告警)。 - 阻塞事件循环的插件确实会让一切卡住——请写异步代码,或用
asyncio.to_thread把阻塞工作卸载出去。
参考插件:guard
plugins/guard/ 是白/黑名单参考实现。策略放在 data/plugins/guard/config.json(首次运行时创建),字段包括 mode(blacklist / whitelist)、scope(group / private / both)、groups 与 users 的双向名单、always_allow_users(绕过两种模式的号主账号)与 log_drops。它用 DEBUG 而非 INFO 记录丢弃——一个活跃的群可能丢弃上千条消息,那不算事故;清单里声明了 "log": {"register": true},所以日志页能单独筛出 Plugin.guard。它没有 accounts 字段:只是旁观消息做判断,属于账号无关。