Skip to content

Commit 6489612

Browse files
committed
chore: initial commit
0 parents  commit 6489612

15 files changed

Lines changed: 901 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: ["main", "dev"]
6+
pull_request:
7+
8+
jobs:
9+
typecheck:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: pnpm/action-setup@v4
14+
- uses: actions/setup-node@v4
15+
with:
16+
node-version: 22
17+
cache: pnpm
18+
- run: pnpm i
19+
- run: pnpm type-check

.github/workflows/release.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*"
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
release:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v4
16+
- uses: pnpm/action-setup@v4
17+
- uses: actions/setup-node@v4
18+
with:
19+
node-version: 22
20+
cache: pnpm
21+
- run: pnpm i
22+
- run: pnpm pack:zip
23+
- run: pnpm pack:tgz
24+
- run: pnpm -s marketplace:snippet > marketplace-index-snippet.json
25+
- uses: softprops/action-gh-release@v2
26+
with:
27+
files: |
28+
release/*
29+
marketplace-index-snippet.json

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dist/
2+
release/
3+
.cache/
4+
node_modules/
5+

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2025
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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

napgram-plugin.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"schemaVersion": 1,
3+
"kind": "native",
4+
"id": "ping-pong",
5+
"name": "Ping Pong Plugin (Template)",
6+
"description": "A native NapGram plugin example that replies pong to ping.",
7+
"entry": "dist/index.mjs",
8+
"permissions": {
9+
"network": [],
10+
"fs": [],
11+
"instances": [0]
12+
}
13+
}

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "napgram-plugin-template",
3+
"private": true,
4+
"version": "0.1.0",
5+
"description": "NapGram native plugin template (runs directly in NapGram runtime).",
6+
"type": "module",
7+
"packageManager": "pnpm@10.26.0",
8+
"scripts": {
9+
"clean": "rm -rf dist release .cache",
10+
"build": "pnpm clean && tsc -p tsconfig.build.json",
11+
"type-check": "tsc -p tsconfig.json --noEmit",
12+
"pack:zip": "pnpm build && node scripts/pack.mjs zip",
13+
"pack:tgz": "pnpm build && node scripts/pack.mjs tgz",
14+
"marketplace:snippet": "node scripts/marketplace-snippet.mjs"
15+
},
16+
"dependencies": {},
17+
"devDependencies": {
18+
"@types/node": "^25.0.3",
19+
"typescript": "^5.9.3"
20+
}
21+
}

pnpm-lock.yaml

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/install-local.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
DATA_DIR="${1:-}"
5+
if [[ -z "${DATA_DIR}" ]]; then
6+
echo "usage: ./scripts/install-local.sh /path/to/napgram/data"
7+
echo "example: ./scripts/install-local.sh ../NapGram/main/data"
8+
exit 1
9+
fi
10+
11+
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
12+
PKG_JSON="${ROOT}/package.json"
13+
META_JSON="${ROOT}/napgram-plugin.json"
14+
15+
ID="$(node -p "JSON.parse(require('fs').readFileSync('${META_JSON}','utf8')).id")"
16+
VER="$(node -p "require('${PKG_JSON}').version")"
17+
18+
echo "[1/3] build..."
19+
(cd "${ROOT}" && pnpm build)
20+
21+
DEST_DIR="${DATA_DIR}/plugins/local"
22+
DEST_FILE="${DEST_DIR}/${ID}.mjs"
23+
echo "[2/3] copy to ${DEST_FILE} ..."
24+
mkdir -p "${DEST_DIR}"
25+
if [[ -f "${ROOT}/dist/index.mjs" ]]; then
26+
cp -f "${ROOT}/dist/index.mjs" "${DEST_FILE}"
27+
else
28+
cp -f "${ROOT}/dist/index.js" "${DEST_FILE}"
29+
fi
30+
31+
echo "[3/3] next steps:"
32+
echo " - Restart NapGram, or call: POST /api/admin/plugins/reload"
33+
echo " - Or reload only this plugin: POST /api/admin/plugins/${ID}/reload"

0 commit comments

Comments
 (0)