|
| 1 | +# napgram-plugin-template |
| 2 | + |
| 3 | +NapGram 原生插件模板仓库(可作为 GitHub **Template repository** 使用)。 |
| 4 | + |
| 5 | +这是 **NapGram 原生插件** 模板:插件直接运行在 NapGram 进程内,通过原生 API 访问平台功能(不需要 Koishi,也不需要 WebSocket Gateway)。 |
| 6 | + |
| 7 | +## 特点 |
| 8 | + |
| 9 | +- ✅ **原生集成** - 直接运行在 NapGram 进程内,无需独立进程 |
| 10 | +- ✅ **类型安全** - 内置最小 TypeScript 类型,开箱即用 |
| 11 | +- ✅ **高性能** - 内存级事件通信,零延迟 |
| 12 | +- ✅ **简单易用** - 清晰的 API,开箱即用 |
| 13 | + |
| 14 | +## 快速开始 |
| 15 | + |
| 16 | +### 1) 安装依赖 |
| 17 | + |
| 18 | +```bash |
| 19 | +pnpm install |
| 20 | +``` |
| 21 | + |
| 22 | +### 2) 开发插件 |
| 23 | + |
| 24 | +编辑 `src/index.ts`,实现你的插件逻辑: |
| 25 | + |
| 26 | +```typescript |
| 27 | +import type { NapGramPlugin } from '@naplink/napgram-plugin-types'; |
| 28 | + |
| 29 | +const plugin: NapGramPlugin = { |
| 30 | + id: 'my-plugin', |
| 31 | + name: 'My Plugin', |
| 32 | + version: '1.0.0', |
| 33 | + |
| 34 | + async install(ctx) { |
| 35 | + ctx.on('message', async (event) => { |
| 36 | + if (event.message.text === 'ping') await event.reply('pong'); |
| 37 | + }); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +export default plugin; |
| 42 | +``` |
| 43 | + |
| 44 | +> 注:模板内包含 `src/types/@naplink/napgram-plugin-types/index.d.ts` 的最小类型声明,便于直接开发与通过 CI;构建产物不会包含该依赖。 |
| 45 | +
|
| 46 | +### 3) 构建 |
| 47 | + |
| 48 | +```bash |
| 49 | +pnpm build |
| 50 | +``` |
| 51 | + |
| 52 | +### 3.1) 本地安装(可选) |
| 53 | + |
| 54 | +将构建产物安装到 NapGram 的 data 目录(无需改 `plugins.yaml`): |
| 55 | + |
| 56 | +```bash |
| 57 | +./scripts/install-local.sh /path/to/napgram/data |
| 58 | +``` |
| 59 | + |
| 60 | +重载方式(二选一): |
| 61 | + |
| 62 | +- 重启 NapGram |
| 63 | +- `POST /api/admin/plugins/reload`(全量重载)或 `POST /api/admin/plugins/:id/reload`(单插件重载) |
| 64 | + |
| 65 | +### 4) 打包发布 |
| 66 | + |
| 67 | +```bash |
| 68 | +# 打包为 zip |
| 69 | +pnpm pack:zip |
| 70 | + |
| 71 | +# 打包为 tgz |
| 72 | +pnpm pack:tgz |
| 73 | + |
| 74 | +# 生成 marketplace 索引片段 |
| 75 | +pnpm marketplace:snippet |
| 76 | +``` |
| 77 | + |
| 78 | +产物位于 `release/` 目录。 |
| 79 | + |
| 80 | +## 插件 API |
| 81 | + |
| 82 | +### 核心接口 |
| 83 | + |
| 84 | +- **PluginContext** - 插件上下文,提供所有 API |
| 85 | +- **MessageEvent** - 消息事件 |
| 86 | +- **MessageAPI** - 发送/撤回消息 |
| 87 | +- **InstanceAPI** - 实例管理 |
| 88 | +- **UserAPI** - 用户信息 |
| 89 | +- **GroupAPI** - 群组管理 |
| 90 | +- **PluginStorage** - 数据存储 |
| 91 | +- **PluginLogger** - 日志记录 |
| 92 | + |
| 93 | +### 事件监听 |
| 94 | + |
| 95 | +```typescript |
| 96 | +ctx.on('message', async (event) => { |
| 97 | + // 处理消息 |
| 98 | +}); |
| 99 | + |
| 100 | +ctx.on('friend-request', async (event) => { |
| 101 | + // 处理好友请求 |
| 102 | +}); |
| 103 | +``` |
| 104 | + |
| 105 | +### 消息发送 |
| 106 | + |
| 107 | +```typescript |
| 108 | +// 回复消息 |
| 109 | +await event.reply('Hello!'); |
| 110 | + |
| 111 | +// 发送消息 |
| 112 | +await event.send([ |
| 113 | + { type: 'text', data: { text: 'Hello ' } }, |
| 114 | + { type: 'at', data: { userId: 'user123' } } |
| 115 | +]); |
| 116 | +``` |
| 117 | + |
| 118 | +### 数据存储 |
| 119 | + |
| 120 | +```typescript |
| 121 | +// 保存数据 |
| 122 | +await ctx.storage.set('key', { data: 'value' }); |
| 123 | + |
| 124 | +// 读取数据 |
| 125 | +const data = await ctx.storage.get('key'); |
| 126 | + |
| 127 | +// 删除数据 |
| 128 | +await ctx.storage.delete('key'); |
| 129 | +``` |
| 130 | + |
| 131 | +### 日志记录 |
| 132 | + |
| 133 | +```typescript |
| 134 | +ctx.logger.info('插件已启动'); |
| 135 | +ctx.logger.debug('调试信息'); |
| 136 | +ctx.logger.warn('警告信息'); |
| 137 | +ctx.logger.error('错误信息'); |
| 138 | +``` |
| 139 | + |
| 140 | +## 权限声明 |
| 141 | + |
| 142 | +在 `napgram-plugin.json` 中声明插件权限: |
| 143 | + |
| 144 | +```json |
| 145 | +{ |
| 146 | + "permissions": { |
| 147 | + "instances": [0], // 可访问的实例(按需填写) |
| 148 | + "network": [], // 网络访问 allowlist(默认空 = 不申请) |
| 149 | + "fs": [] // 文件系统访问(默认空 = 不申请) |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +## 目录结构 |
| 155 | + |
| 156 | +``` |
| 157 | +napgram-plugin-template/ |
| 158 | +├── src/ |
| 159 | +│ ├── index.ts # 插件主文件 |
| 160 | +│ └── types/ # 内置类型声明(仅用于开发) |
| 161 | +├── scripts/ |
| 162 | +│ ├── pack.mjs # 打包脚本 |
| 163 | +│ └── marketplace-snippet.mjs |
| 164 | +├── napgram-plugin.json # 插件元信息 |
| 165 | +├── package.json |
| 166 | +├── tsconfig.json |
| 167 | +└── README.md |
| 168 | +``` |
| 169 | + |
| 170 | +## 发布到 Marketplace |
| 171 | + |
| 172 | +1. 打包插件:`pnpm pack:zip` |
| 173 | +2. 上传到 GitHub Release 或 CDN |
| 174 | +3. 生成索引片段:`pnpm marketplace:snippet` |
| 175 | +4. 提交 PR 到 NapGram Marketplace 仓库 |
| 176 | + |
| 177 | +## 示例插件 |
| 178 | + |
| 179 | +查看 `src/index.ts` 中的 Ping Pong 插件示例。 |
| 180 | + |
| 181 | +## License |
| 182 | + |
| 183 | +MIT |
0 commit comments