@zhin.js/plugin-group-suite 3.0.2 → 3.0.3
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.
- package/README.md +42 -58
- package/agent/tools/group_announce.ts +2 -2
- package/commands/checkin.ts +12 -0
- package/commands/forget.ts +12 -0
- package/commands/keyword-add/[keyword:string].ts +15 -0
- package/commands/keyword-list.ts +14 -0
- package/commands/keyword-remove/[keyword:string].ts +14 -0
- package/commands/mypoints.ts +12 -0
- package/commands/mystats.ts +12 -0
- package/commands/rank.ts +12 -0
- package/commands/stats-rank.ts +20 -0
- package/commands/stats-week.ts +20 -0
- package/commands/stats.ts +21 -0
- package/commands/teach-list/[page:number=1].ts +13 -0
- package/commands/teach-regex.ts +12 -0
- package/commands/teach.ts +12 -0
- package/lib/analysis-card.d.ts +4 -0
- package/lib/analysis-card.d.ts.map +1 -0
- package/lib/analysis-card.js.map +1 -0
- package/lib/analysis.d.ts +95 -0
- package/lib/analysis.d.ts.map +1 -0
- package/lib/analysis.js.map +1 -0
- package/lib/card-layout.d.ts +27 -0
- package/lib/card-layout.d.ts.map +1 -0
- package/lib/card-layout.js.map +1 -0
- package/lib/checkin-lib.d.ts +18 -0
- package/lib/checkin-lib.d.ts.map +1 -0
- package/lib/checkin-lib.js +118 -0
- package/lib/checkin-lib.js.map +1 -0
- package/lib/config.d.ts +15 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +19 -0
- package/lib/config.js.map +1 -0
- package/lib/db-store.d.ts +12 -0
- package/lib/db-store.d.ts.map +1 -0
- package/lib/db-store.js +38 -0
- package/lib/db-store.js.map +1 -0
- package/lib/index.d.ts +10 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +8 -0
- package/lib/index.js.map +1 -0
- package/lib/keyword-store.d.ts +6 -0
- package/lib/keyword-store.d.ts.map +1 -0
- package/lib/keyword-store.js +21 -0
- package/lib/keyword-store.js.map +1 -0
- package/lib/memory-store.d.ts +29 -0
- package/lib/memory-store.d.ts.map +1 -0
- package/lib/memory-store.js +59 -0
- package/lib/memory-store.js.map +1 -0
- package/lib/runtime-state.d.ts +24 -0
- package/lib/runtime-state.d.ts.map +1 -0
- package/lib/runtime-state.js +21 -0
- package/lib/runtime-state.js.map +1 -0
- package/lib/shared-runtime.d.ts +20 -0
- package/lib/shared-runtime.d.ts.map +1 -0
- package/lib/shared-runtime.js +34 -0
- package/lib/shared-runtime.js.map +1 -0
- package/lib/stats-card.d.ts +5 -0
- package/lib/stats-card.d.ts.map +1 -0
- package/lib/stats-card.js.map +1 -0
- package/lib/stats-data.d.ts +32 -0
- package/lib/stats-data.d.ts.map +1 -0
- package/lib/stats-data.js.map +1 -0
- package/lib/stats-lib.d.ts +29 -0
- package/lib/stats-lib.d.ts.map +1 -0
- package/lib/stats-lib.js +173 -0
- package/lib/stats-lib.js.map +1 -0
- package/lib/tables.d.ts +4 -0
- package/lib/tables.d.ts.map +1 -0
- package/lib/tables.js +40 -0
- package/lib/tables.js.map +1 -0
- package/lib/teach-lib.d.ts +22 -0
- package/lib/teach-lib.d.ts.map +1 -0
- package/lib/teach-lib.js +233 -0
- package/lib/teach-lib.js.map +1 -0
- package/middlewares/keyword-reply.ts +37 -0
- package/middlewares/stats-count.ts +22 -0
- package/middlewares/teach-reply.ts +22 -0
- package/package.json +37 -15
- package/plugin.ts +39 -0
- package/schema.json +17 -0
- package/src/checkin-lib.ts +148 -0
- package/src/config.ts +23 -57
- package/src/db-store.ts +51 -0
- package/src/index.ts +47 -33
- package/src/keyword-store.ts +24 -0
- package/src/memory-store.ts +88 -0
- package/src/runtime-state.ts +44 -0
- package/src/shared-runtime.ts +44 -0
- package/src/stats-lib.ts +202 -0
- package/src/tables.ts +46 -0
- package/src/teach-lib.ts +301 -0
- package/tools/checkin-query/handler.ts +2 -2
- package/tools/checkin-rank/handler.ts +1 -1
- package/tools/stats-query/handler.ts +2 -2
- package/tools/stats-user/handler.ts +3 -2
- package/lib/agent/tools/group_announce.js +0 -13
- package/lib/agent/tools/group_announce.js.map +0 -1
- package/lib/src/admin.js +0 -114
- package/lib/src/admin.js.map +0 -1
- package/lib/src/analysis-card.js.map +0 -1
- package/lib/src/analysis.js.map +0 -1
- package/lib/src/card-layout.js.map +0 -1
- package/lib/src/checkin.js +0 -168
- package/lib/src/checkin.js.map +0 -1
- package/lib/src/config.js +0 -41
- package/lib/src/config.js.map +0 -1
- package/lib/src/daily-analysis.js +0 -333
- package/lib/src/daily-analysis.js.map +0 -1
- package/lib/src/index.js +0 -30
- package/lib/src/index.js.map +0 -1
- package/lib/src/shared.js +0 -40
- package/lib/src/shared.js.map +0 -1
- package/lib/src/stats-card.js.map +0 -1
- package/lib/src/stats-data.js.map +0 -1
- package/lib/src/stats.js +0 -258
- package/lib/src/stats.js.map +0 -1
- package/lib/src/teach.js +0 -294
- package/lib/src/teach.js.map +0 -1
- package/plugin.yml +0 -3
- package/src/admin.ts +0 -136
- package/src/checkin.ts +0 -196
- package/src/daily-analysis.ts +0 -458
- package/src/shared.ts +0 -51
- package/src/stats.ts +0 -298
- package/src/teach.ts +0 -307
- /package/lib/{src/analysis-card.js → analysis-card.js} +0 -0
- /package/lib/{src/analysis.js → analysis.js} +0 -0
- /package/lib/{src/card-layout.js → card-layout.js} +0 -0
- /package/lib/{src/stats-card.js → stats-card.js} +0 -0
- /package/lib/{src/stats-data.js → stats-data.js} +0 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zhin.js/plugin-group-suite",
|
|
3
|
-
"version": "3.0.
|
|
4
|
-
"description": "Zhin.js 群运营套件 —
|
|
3
|
+
"version": "3.0.3",
|
|
4
|
+
"description": "Zhin.js 群运营套件 — slice-2 checkin/teach/stats + keywords (Plugin Runtime, in-memory)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/index.js",
|
|
7
7
|
"types": "./lib/index.d.ts",
|
|
@@ -14,11 +14,14 @@
|
|
|
14
14
|
"./package.json": "./package.json"
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
|
+
"plugin.ts",
|
|
18
|
+
"schema.json",
|
|
19
|
+
"commands",
|
|
20
|
+
"middlewares",
|
|
17
21
|
"src",
|
|
18
22
|
"lib",
|
|
19
23
|
"agent",
|
|
20
24
|
"tools",
|
|
21
|
-
"plugin.yml",
|
|
22
25
|
"README.md"
|
|
23
26
|
],
|
|
24
27
|
"keywords": [
|
|
@@ -29,8 +32,7 @@
|
|
|
29
32
|
"group",
|
|
30
33
|
"checkin",
|
|
31
34
|
"stats",
|
|
32
|
-
"teach"
|
|
33
|
-
"welcome"
|
|
35
|
+
"teach"
|
|
34
36
|
],
|
|
35
37
|
"author": {
|
|
36
38
|
"name": "lc-cn",
|
|
@@ -39,25 +41,24 @@
|
|
|
39
41
|
},
|
|
40
42
|
"license": "MIT",
|
|
41
43
|
"dependencies": {
|
|
44
|
+
"@zhin.js/command": "1.0.1",
|
|
45
|
+
"@zhin.js/core": "1.3.5",
|
|
46
|
+
"@zhin.js/middleware": "1.0.1",
|
|
47
|
+
"@zhin.js/plugin-runtime": "1.0.1",
|
|
42
48
|
"@zhin.js/satori": "1.0.16"
|
|
43
49
|
},
|
|
44
50
|
"devDependencies": {
|
|
45
51
|
"typescript": "^6.0.3",
|
|
52
|
+
"vitest": "^4.1.10",
|
|
46
53
|
"zod": "^4.4.3",
|
|
47
|
-
"zhin.js": "
|
|
48
|
-
"
|
|
49
|
-
"@zhin.js/agent": "1.0.3"
|
|
54
|
+
"@zhin.js/agent": "1.0.4",
|
|
55
|
+
"zhin.js": "4.1.3"
|
|
50
56
|
},
|
|
51
57
|
"peerDependencies": {
|
|
52
58
|
"zod": "^4.0.0",
|
|
53
|
-
"zhin.js": "
|
|
54
|
-
"@zhin.js/agent": "1.0.3",
|
|
55
|
-
"@zhin.js/html-renderer": "1.0.3"
|
|
59
|
+
"@zhin.js/agent": "1.0.4"
|
|
56
60
|
},
|
|
57
61
|
"peerDependenciesMeta": {
|
|
58
|
-
"@zhin.js/html-renderer": {
|
|
59
|
-
"optional": true
|
|
60
|
-
},
|
|
61
62
|
"@zhin.js/agent": {
|
|
62
63
|
"optional": true
|
|
63
64
|
},
|
|
@@ -74,9 +75,30 @@
|
|
|
74
75
|
"access": "public",
|
|
75
76
|
"registry": "https://registry.npmjs.org"
|
|
76
77
|
},
|
|
78
|
+
"engines": {
|
|
79
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
80
|
+
},
|
|
81
|
+
"zhin": {
|
|
82
|
+
"protocol": 1,
|
|
83
|
+
"type": "plugin",
|
|
84
|
+
"entry": "./plugin.ts",
|
|
85
|
+
"engine": "^1.0.0",
|
|
86
|
+
"runtime": "trusted",
|
|
87
|
+
"features": [
|
|
88
|
+
{
|
|
89
|
+
"package": "@zhin.js/command",
|
|
90
|
+
"api": "^1.0.0"
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
"package": "@zhin.js/middleware",
|
|
94
|
+
"api": "^1.0.0"
|
|
95
|
+
}
|
|
96
|
+
],
|
|
97
|
+
"plugins": []
|
|
98
|
+
},
|
|
77
99
|
"scripts": {
|
|
78
100
|
"build": "tsc",
|
|
79
101
|
"clean": "rimraf lib",
|
|
80
|
-
"test": "
|
|
102
|
+
"test": "NODE_OPTIONS=--experimental-strip-types vitest run --root ../../.. plugins/utils/group-suite/tests"
|
|
81
103
|
}
|
|
82
104
|
}
|
package/plugin.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { definePlugin, databaseHostToken } from '@zhin.js/plugin-runtime';
|
|
2
|
+
import type { GroupSuiteConfig } from './src/config.js';
|
|
3
|
+
import { registerGroupSuiteDb } from './src/db-store.js';
|
|
4
|
+
import { createInMemoryGroupSuiteDb } from './src/memory-store.js';
|
|
5
|
+
import { createGroupSuiteRuntime, groupSuiteRuntimeToken } from './src/runtime-state.js';
|
|
6
|
+
import { flushStatsBuffer } from './src/stats-lib.js';
|
|
7
|
+
import { defineGroupSuiteTables } from './src/tables.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Plugin Runtime:
|
|
11
|
+
* - checkin / teach / stats commands + keyword middleware
|
|
12
|
+
* - DB: prefer `databaseHostToken`; else in-memory fallback
|
|
13
|
+
* - Stats buffer: periodic flush every 10s + final flush on dispose
|
|
14
|
+
*/
|
|
15
|
+
export default definePlugin<GroupSuiteConfig>({
|
|
16
|
+
name: 'group-suite',
|
|
17
|
+
metadata: {
|
|
18
|
+
displayName: 'Group Suite',
|
|
19
|
+
},
|
|
20
|
+
setup(context) {
|
|
21
|
+
const db = context.resources.has(databaseHostToken)
|
|
22
|
+
? context.resources.use(databaseHostToken)
|
|
23
|
+
: createInMemoryGroupSuiteDb();
|
|
24
|
+
if (context.resources.has(databaseHostToken)) {
|
|
25
|
+
defineGroupSuiteTables(db);
|
|
26
|
+
}
|
|
27
|
+
const runtime = createGroupSuiteRuntime(db);
|
|
28
|
+
context.resources.provide(groupSuiteRuntimeToken, runtime);
|
|
29
|
+
context.lifecycle.add(registerGroupSuiteDb(db));
|
|
30
|
+
|
|
31
|
+
const flushTimer = setInterval(() => {
|
|
32
|
+
void flushStatsBuffer(runtime);
|
|
33
|
+
}, 10_000);
|
|
34
|
+
context.lifecycle.add(async () => {
|
|
35
|
+
clearInterval(flushTimer);
|
|
36
|
+
await flushStatsBuffer(runtime);
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
});
|
package/schema.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"type": "object",
|
|
4
|
+
"additionalProperties": false,
|
|
5
|
+
"properties": {
|
|
6
|
+
"keywordReply": { "type": "boolean", "default": false },
|
|
7
|
+
"basePointsMin": { "type": "number", "default": 10 },
|
|
8
|
+
"basePointsMax": { "type": "number", "default": 30 },
|
|
9
|
+
"streakBonus": { "type": "number", "default": 5 },
|
|
10
|
+
"streakCap": { "type": "number", "default": 50 },
|
|
11
|
+
"rankSize": { "type": "number", "default": 10 },
|
|
12
|
+
"teachMaxPerGroup": { "type": "number", "default": 200 },
|
|
13
|
+
"teachCooldownMs": { "type": "number", "default": 3000 },
|
|
14
|
+
"teachAllowRegex": { "type": "boolean", "default": true },
|
|
15
|
+
"teachPageSize": { "type": "number", "default": 10 }
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { GroupSuiteConfig } from './config.js';
|
|
2
|
+
import { getCheckinModel } from './db-store.js';
|
|
3
|
+
import {
|
|
4
|
+
randomInt,
|
|
5
|
+
resolveContextKey,
|
|
6
|
+
resolveSender,
|
|
7
|
+
todayStr,
|
|
8
|
+
ts,
|
|
9
|
+
yesterdayStr,
|
|
10
|
+
} from './shared-runtime.js';
|
|
11
|
+
import type { GroupSuiteRuntime } from './runtime-state.js';
|
|
12
|
+
|
|
13
|
+
async function getOrCreateUser(
|
|
14
|
+
model: any,
|
|
15
|
+
userId: string,
|
|
16
|
+
userName: string,
|
|
17
|
+
ctxType: string,
|
|
18
|
+
ctxId: string,
|
|
19
|
+
): Promise<Record<string, unknown> | null> {
|
|
20
|
+
const rows = (await model.select().where({
|
|
21
|
+
user_id: userId,
|
|
22
|
+
context_type: ctxType,
|
|
23
|
+
context_id: ctxId,
|
|
24
|
+
})) as Record<string, unknown>[];
|
|
25
|
+
if (rows.length > 0) return rows[0];
|
|
26
|
+
|
|
27
|
+
await model.insert({
|
|
28
|
+
user_id: userId,
|
|
29
|
+
user_name: userName,
|
|
30
|
+
points: 0,
|
|
31
|
+
total_checkins: 0,
|
|
32
|
+
streak: 0,
|
|
33
|
+
max_streak: 0,
|
|
34
|
+
last_checkin: '',
|
|
35
|
+
context_type: ctxType,
|
|
36
|
+
context_id: ctxId,
|
|
37
|
+
created_at: ts(),
|
|
38
|
+
updated_at: ts(),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const created = (await model.select().where({
|
|
42
|
+
user_id: userId,
|
|
43
|
+
context_type: ctxType,
|
|
44
|
+
context_id: ctxId,
|
|
45
|
+
})) as Record<string, unknown>[];
|
|
46
|
+
return created[0] ?? null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function doCheckin(
|
|
50
|
+
input: { sender?: string; target?: string; metadata?: Readonly<Record<string, unknown>> },
|
|
51
|
+
cfg: GroupSuiteConfig,
|
|
52
|
+
runtime?: GroupSuiteRuntime,
|
|
53
|
+
): Promise<string> {
|
|
54
|
+
const M = getCheckinModel(runtime?.db);
|
|
55
|
+
if (!M) return '签到数据库尚未就绪,请稍后重试';
|
|
56
|
+
|
|
57
|
+
const { id: userId, name: userName } = resolveSender(input);
|
|
58
|
+
if (!userId) return '无法获取用户信息';
|
|
59
|
+
|
|
60
|
+
const { type: ctxType, id: ctxId } = resolveContextKey(input);
|
|
61
|
+
const user = await getOrCreateUser(M, userId, userName, ctxType, ctxId);
|
|
62
|
+
if (!user) return '签到失败,请重试';
|
|
63
|
+
|
|
64
|
+
const today = todayStr();
|
|
65
|
+
if (user.last_checkin === today) {
|
|
66
|
+
return `你今天已经签到过了哦~\n当前积分: ${user.points}\n连续签到: ${user.streak} 天`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const isConsecutive = user.last_checkin === yesterdayStr();
|
|
70
|
+
const newStreak = isConsecutive ? Number(user.streak) + 1 : 1;
|
|
71
|
+
const base = randomInt(cfg.basePointsMin, cfg.basePointsMax);
|
|
72
|
+
const bonus = Math.min(newStreak * cfg.streakBonus, cfg.streakCap);
|
|
73
|
+
const earned = base + bonus;
|
|
74
|
+
const newPoints = Number(user.points || 0) + earned;
|
|
75
|
+
|
|
76
|
+
await M.update({
|
|
77
|
+
points: newPoints,
|
|
78
|
+
total_checkins: Number(user.total_checkins || 0) + 1,
|
|
79
|
+
streak: newStreak,
|
|
80
|
+
max_streak: Math.max(Number(user.max_streak || 0), newStreak),
|
|
81
|
+
last_checkin: today,
|
|
82
|
+
user_name: userName,
|
|
83
|
+
updated_at: ts(),
|
|
84
|
+
}).where({
|
|
85
|
+
user_id: userId,
|
|
86
|
+
context_type: ctxType,
|
|
87
|
+
context_id: ctxId,
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
const lines = [`签到成功!`, `基础积分: +${base}`];
|
|
91
|
+
if (bonus > 0) lines.push(`连续奖励: +${bonus} (连续${newStreak}天)`);
|
|
92
|
+
lines.push(`本次获得: +${earned}`, `当前积分: ${newPoints}`);
|
|
93
|
+
return lines.join('\n');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export async function myPoints(
|
|
97
|
+
input: { sender?: string; target?: string; metadata?: Readonly<Record<string, unknown>> },
|
|
98
|
+
runtime?: GroupSuiteRuntime,
|
|
99
|
+
): Promise<string> {
|
|
100
|
+
const M = getCheckinModel(runtime?.db);
|
|
101
|
+
if (!M) return '签到数据库尚未就绪';
|
|
102
|
+
const { id: userId } = resolveSender(input);
|
|
103
|
+
const { type: ctxType, id: ctxId } = resolveContextKey(input);
|
|
104
|
+
const rows = (await M.select().where({
|
|
105
|
+
user_id: userId,
|
|
106
|
+
context_type: ctxType,
|
|
107
|
+
context_id: ctxId,
|
|
108
|
+
})) as Record<string, unknown>[];
|
|
109
|
+
if (rows.length === 0) return '你还没有签到记录哦,发送「checkin」开始吧!';
|
|
110
|
+
const u = rows[0];
|
|
111
|
+
const checkedToday = u.last_checkin === todayStr();
|
|
112
|
+
return [
|
|
113
|
+
`${u.user_name || '用户'} 的积分信息`,
|
|
114
|
+
`积分: ${u.points}`,
|
|
115
|
+
`累计签到: ${u.total_checkins} 天`,
|
|
116
|
+
`连续签到: ${u.streak} 天`,
|
|
117
|
+
`最长连续: ${u.max_streak} 天`,
|
|
118
|
+
checkedToday ? '今日已签到 ✓' : '今日未签到',
|
|
119
|
+
].join('\n');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export async function pointsRank(
|
|
123
|
+
input: { sender?: string; target?: string; metadata?: Readonly<Record<string, unknown>> },
|
|
124
|
+
cfg: GroupSuiteConfig,
|
|
125
|
+
runtime?: GroupSuiteRuntime,
|
|
126
|
+
): Promise<string> {
|
|
127
|
+
const M = getCheckinModel(runtime?.db);
|
|
128
|
+
if (!M) return '签到数据库尚未就绪';
|
|
129
|
+
const { type: ctxType, id: ctxId } = resolveContextKey(input);
|
|
130
|
+
const all = (await M.select().where(
|
|
131
|
+
ctxType === 'global'
|
|
132
|
+
? { context_type: 'global' }
|
|
133
|
+
: { context_type: ctxType, context_id: ctxId },
|
|
134
|
+
)) as Record<string, unknown>[];
|
|
135
|
+
if (all.length === 0) return '还没有人签到过哦~';
|
|
136
|
+
const sorted = all.sort((a, b) => Number(b.points || 0) - Number(a.points || 0));
|
|
137
|
+
const top = sorted.slice(0, cfg.rankSize);
|
|
138
|
+
const medals = ['🥇', '🥈', '🥉'];
|
|
139
|
+
const lines = top.map((u, i) => {
|
|
140
|
+
const prefix = i < 3 ? medals[i] : `${i + 1}.`;
|
|
141
|
+
return `${prefix} ${u.user_name || u.user_id} — ${u.points}分 (${u.total_checkins}天)`;
|
|
142
|
+
});
|
|
143
|
+
const title = ctxType === 'group' ? '本群积分排行' : '全局积分排行';
|
|
144
|
+
const { id: userId } = resolveSender(input);
|
|
145
|
+
const myRank = sorted.findIndex((u) => u.user_id === userId);
|
|
146
|
+
const footer = myRank >= 0 ? `\n你的排名: 第${myRank + 1}名` : '';
|
|
147
|
+
return `${title}\n${lines.join('\n')}${footer}`;
|
|
148
|
+
}
|
package/src/config.ts
CHANGED
|
@@ -1,68 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/** 扁平 groupSuite 配置(单层字段,默认值覆盖多数场景) */
|
|
4
|
-
export const groupSuiteSchema = Schema.object({
|
|
5
|
-
welcome: Schema.string()
|
|
6
|
-
.default("欢迎新成员加入本群!")
|
|
7
|
-
.description("入群欢迎语"),
|
|
8
|
-
recallNotify: Schema.boolean()
|
|
9
|
-
.default(true)
|
|
10
|
-
.description("群消息撤回时是否提示"),
|
|
11
|
-
keywordReply: Schema.boolean()
|
|
12
|
-
.default(false)
|
|
13
|
-
.description("内置关键词回复(默认关闭,用 teach 问答)"),
|
|
14
|
-
noticeAdapters: Schema.list(Schema.string())
|
|
15
|
-
.default(["icqq"])
|
|
16
|
-
.description("处理 notice 的适配器(欢迎/撤回)"),
|
|
17
|
-
|
|
18
|
-
basePointsMin: Schema.number().default(10).min(1).max(100),
|
|
19
|
-
basePointsMax: Schema.number().default(30).min(1).max(200),
|
|
20
|
-
streakBonus: Schema.number().default(5).min(0).max(100),
|
|
21
|
-
streakCap: Schema.number().default(50).min(0).max(500),
|
|
22
|
-
|
|
23
|
-
rankSize: Schema.number()
|
|
24
|
-
.default(10)
|
|
25
|
-
.min(3)
|
|
26
|
-
.max(50)
|
|
27
|
-
.description("签到排行与消息统计排行显示人数"),
|
|
28
|
-
statsRetentionDays: Schema.number().default(90).min(7).max(365),
|
|
29
|
-
|
|
30
|
-
analysisDays: Schema.number().default(1).min(1).max(30),
|
|
31
|
-
autoAnalysisEnabled: Schema.boolean().default(false),
|
|
32
|
-
autoAnalysisCron: Schema.string().default("0 0 9 * * *"),
|
|
33
|
-
analysisGroups: Schema.list(Schema.string())
|
|
34
|
-
.default([])
|
|
35
|
-
.description("启用群日报的群 ID 白名单(空=不限制)"),
|
|
36
|
-
analysisGroupsBlock: Schema.list(Schema.string())
|
|
37
|
-
.default([])
|
|
38
|
-
.description("禁用群日报的群 ID"),
|
|
39
|
-
analysisMaxMessages: Schema.number().default(500).min(100).max(5000),
|
|
40
|
-
|
|
41
|
-
teachMaxPerGroup: Schema.number().default(200).min(1).max(1000),
|
|
42
|
-
teachCooldownMs: Schema.number().default(3000).min(0).max(60000),
|
|
43
|
-
teachAllowRegex: Schema.boolean().default(true),
|
|
44
|
-
teachPageSize: Schema.number().default(10).min(5).max(50),
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
export type GroupSuiteConfig = {
|
|
48
|
-
welcome: string;
|
|
49
|
-
recallNotify: boolean;
|
|
1
|
+
export interface GroupSuiteConfig {
|
|
50
2
|
keywordReply: boolean;
|
|
51
|
-
noticeAdapters: string[];
|
|
52
3
|
basePointsMin: number;
|
|
53
4
|
basePointsMax: number;
|
|
54
5
|
streakBonus: number;
|
|
55
6
|
streakCap: number;
|
|
56
7
|
rankSize: number;
|
|
57
|
-
statsRetentionDays: number;
|
|
58
|
-
analysisDays: number;
|
|
59
|
-
autoAnalysisEnabled: boolean;
|
|
60
|
-
autoAnalysisCron: string;
|
|
61
|
-
analysisGroups: string[];
|
|
62
|
-
analysisGroupsBlock: string[];
|
|
63
|
-
analysisMaxMessages: number;
|
|
64
8
|
teachMaxPerGroup: number;
|
|
65
9
|
teachCooldownMs: number;
|
|
66
10
|
teachAllowRegex: boolean;
|
|
67
11
|
teachPageSize: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const DEFAULT_GROUP_SUITE_CONFIG: GroupSuiteConfig = {
|
|
15
|
+
keywordReply: false,
|
|
16
|
+
basePointsMin: 10,
|
|
17
|
+
basePointsMax: 30,
|
|
18
|
+
streakBonus: 5,
|
|
19
|
+
streakCap: 50,
|
|
20
|
+
rankSize: 10,
|
|
21
|
+
teachMaxPerGroup: 200,
|
|
22
|
+
teachCooldownMs: 3000,
|
|
23
|
+
teachAllowRegex: true,
|
|
24
|
+
teachPageSize: 10,
|
|
68
25
|
};
|
|
26
|
+
|
|
27
|
+
export function resolveGroupSuiteConfig(
|
|
28
|
+
raw: Partial<GroupSuiteConfig> | undefined,
|
|
29
|
+
): GroupSuiteConfig {
|
|
30
|
+
return {
|
|
31
|
+
...DEFAULT_GROUP_SUITE_CONFIG,
|
|
32
|
+
...raw,
|
|
33
|
+
};
|
|
34
|
+
}
|
package/src/db-store.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CHECKIN_TABLE,
|
|
3
|
+
createInMemoryGroupSuiteDb,
|
|
4
|
+
STATS_TABLE,
|
|
5
|
+
TEACH_TABLE,
|
|
6
|
+
type GroupSuiteMemoryDb,
|
|
7
|
+
type GroupSuiteModel,
|
|
8
|
+
} from './memory-store.js';
|
|
9
|
+
|
|
10
|
+
let _db: GroupSuiteMemoryDb | null = null;
|
|
11
|
+
const registrations: Array<{ readonly value: GroupSuiteMemoryDb }> = [];
|
|
12
|
+
|
|
13
|
+
/** Ensure an in-memory store when Runtime DatabaseFeature Resource is not wired yet. */
|
|
14
|
+
export function ensureGroupSuiteMemoryDb(): GroupSuiteMemoryDb {
|
|
15
|
+
if (!_db) _db = createInMemoryGroupSuiteDb();
|
|
16
|
+
return _db;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getGroupSuiteDb(): GroupSuiteMemoryDb | null {
|
|
20
|
+
return registrations[registrations.length - 1]?.value ?? ensureGroupSuiteMemoryDb();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function setGroupSuiteDb(db: GroupSuiteMemoryDb | null): void {
|
|
24
|
+
_db = db;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function registerGroupSuiteDb(db: GroupSuiteMemoryDb): () => void {
|
|
28
|
+
const registration = Object.freeze({ value: db });
|
|
29
|
+
registrations.push(registration);
|
|
30
|
+
return () => {
|
|
31
|
+
const index = registrations.lastIndexOf(registration);
|
|
32
|
+
if (index >= 0) registrations.splice(index, 1);
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Test helper: clear module store. */
|
|
37
|
+
export function resetGroupSuiteDb(): void {
|
|
38
|
+
_db = null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function getCheckinModel(db = getGroupSuiteDb()): GroupSuiteModel | null {
|
|
42
|
+
return db?.models.get(CHECKIN_TABLE) ?? null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function getTeachModel(db = getGroupSuiteDb()): GroupSuiteModel | null {
|
|
46
|
+
return db?.models.get(TEACH_TABLE) ?? null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function getStatsModel(db = getGroupSuiteDb()): GroupSuiteModel | null {
|
|
50
|
+
return db?.models.get(STATS_TABLE) ?? null;
|
|
51
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,33 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
1
|
+
export {
|
|
2
|
+
DEFAULT_GROUP_SUITE_CONFIG,
|
|
3
|
+
resolveGroupSuiteConfig,
|
|
4
|
+
} from './config.js';
|
|
5
|
+
export type { GroupSuiteConfig } from './config.js';
|
|
6
|
+
export { doCheckin, myPoints, pointsRank } from './checkin-lib.js';
|
|
7
|
+
export {
|
|
8
|
+
addKeyword,
|
|
9
|
+
listKeywords,
|
|
10
|
+
matchKeyword,
|
|
11
|
+
removeKeyword,
|
|
12
|
+
resetKeywords,
|
|
13
|
+
} from './keyword-store.js';
|
|
14
|
+
export {
|
|
15
|
+
ensureGroupSuiteMemoryDb,
|
|
16
|
+
getCheckinModel,
|
|
17
|
+
getGroupSuiteDb,
|
|
18
|
+
getStatsModel,
|
|
19
|
+
getTeachModel,
|
|
20
|
+
resetGroupSuiteDb,
|
|
21
|
+
registerGroupSuiteDb,
|
|
22
|
+
setGroupSuiteDb,
|
|
23
|
+
} from './db-store.js';
|
|
24
|
+
export {
|
|
25
|
+
createGroupSuiteRuntime,
|
|
26
|
+
groupSuiteRuntimeToken,
|
|
27
|
+
resolveGroupSuiteRuntime,
|
|
28
|
+
} from './runtime-state.js';
|
|
29
|
+
export type { GroupSuiteRuntime, PendingStatsIncrement } from './runtime-state.js';
|
|
30
|
+
export {
|
|
31
|
+
parseTeachPair,
|
|
32
|
+
resetTeachCooldown,
|
|
33
|
+
teachAdd,
|
|
34
|
+
teachForget,
|
|
35
|
+
teachList,
|
|
36
|
+
tryTeachReply,
|
|
37
|
+
} from './teach-lib.js';
|
|
38
|
+
export {
|
|
39
|
+
flushStatsBuffer,
|
|
40
|
+
monthStartStr,
|
|
41
|
+
myStatsText,
|
|
42
|
+
queryStats,
|
|
43
|
+
recordMessage,
|
|
44
|
+
resetStatsBuffer,
|
|
45
|
+
statsRankText,
|
|
46
|
+
weekStartStr,
|
|
47
|
+
} from './stats-lib.js';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const keywords = new Map<string, string>();
|
|
2
|
+
|
|
3
|
+
export function addKeyword(keyword: string, reply: string, store = keywords): void {
|
|
4
|
+
store.set(keyword, reply);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function removeKeyword(keyword: string, store = keywords): boolean {
|
|
8
|
+
return store.delete(keyword);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function listKeywords(store = keywords): Array<[string, string]> {
|
|
12
|
+
return Array.from(store.entries());
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function matchKeyword(text: string, store = keywords): string | null {
|
|
16
|
+
for (const [keyword, reply] of store) {
|
|
17
|
+
if (text.includes(keyword)) return reply;
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function resetKeywords(): void {
|
|
23
|
+
keywords.clear();
|
|
24
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In-memory group-suite models (slice-2) until Plugin Runtime DatabaseFeature Resource lands.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface GroupSuiteRow extends Record<string, unknown> {
|
|
6
|
+
id: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface GroupSuiteModel {
|
|
10
|
+
select: () => {
|
|
11
|
+
where: (query: Record<string, unknown>) => Promise<GroupSuiteRow[]>;
|
|
12
|
+
then: <TResult1 = GroupSuiteRow[], TResult2 = never>(
|
|
13
|
+
onfulfilled?: ((value: GroupSuiteRow[]) => TResult1 | PromiseLike<TResult1>) | null,
|
|
14
|
+
onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,
|
|
15
|
+
) => Promise<TResult1 | TResult2>;
|
|
16
|
+
};
|
|
17
|
+
insert: (row: Record<string, unknown>) => Promise<GroupSuiteRow>;
|
|
18
|
+
delete: () => { where: (query: Record<string, unknown>) => Promise<void> };
|
|
19
|
+
update: (patch: Record<string, unknown>) => {
|
|
20
|
+
where: (query: Record<string, unknown>) => Promise<void>;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function createMemoryModel(): GroupSuiteModel {
|
|
25
|
+
const rows: GroupSuiteRow[] = [];
|
|
26
|
+
let nextId = 1;
|
|
27
|
+
|
|
28
|
+
function matches(row: Record<string, unknown>, query: Record<string, unknown>): boolean {
|
|
29
|
+
return Object.entries(query).every(([key, value]) => row[key] === value);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function all(): GroupSuiteRow[] {
|
|
33
|
+
return rows.map((row) => ({ ...row }));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
select: () => {
|
|
38
|
+
const promise = Promise.resolve(all());
|
|
39
|
+
return {
|
|
40
|
+
where: async (query: Record<string, unknown>) =>
|
|
41
|
+
rows.filter((row) => matches(row, query)).map((row) => ({ ...row })),
|
|
42
|
+
then: (onfulfilled, onrejected) => promise.then(onfulfilled, onrejected),
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
insert: async (row: Record<string, unknown>) => {
|
|
46
|
+
const withId: GroupSuiteRow = { id: String(nextId++), ...row };
|
|
47
|
+
rows.push(withId);
|
|
48
|
+
return { ...withId };
|
|
49
|
+
},
|
|
50
|
+
delete: () => ({
|
|
51
|
+
where: async (query: Record<string, unknown>) => {
|
|
52
|
+
for (let i = rows.length - 1; i >= 0; i -= 1) {
|
|
53
|
+
if (matches(rows[i]!, query)) rows.splice(i, 1);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
}),
|
|
57
|
+
update: (patch: Record<string, unknown>) => ({
|
|
58
|
+
where: async (query: Record<string, unknown>) => {
|
|
59
|
+
for (const row of rows) {
|
|
60
|
+
if (matches(row, query)) Object.assign(row, patch);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
}),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface GroupSuiteMemoryDb {
|
|
68
|
+
models: {
|
|
69
|
+
get: (name: string) => GroupSuiteModel | undefined;
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const CHECKIN_TABLE = 'checkin_records';
|
|
74
|
+
export const TEACH_TABLE = 'teach_qa';
|
|
75
|
+
export const STATS_TABLE = 'message_stats';
|
|
76
|
+
|
|
77
|
+
export function createInMemoryGroupSuiteDb(): GroupSuiteMemoryDb {
|
|
78
|
+
const models = new Map<string, GroupSuiteModel>([
|
|
79
|
+
[CHECKIN_TABLE, createMemoryModel()],
|
|
80
|
+
[TEACH_TABLE, createMemoryModel()],
|
|
81
|
+
[STATS_TABLE, createMemoryModel()],
|
|
82
|
+
]);
|
|
83
|
+
return {
|
|
84
|
+
models: {
|
|
85
|
+
get: (name: string) => models.get(name),
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createToken } from '@zhin.js/plugin-runtime';
|
|
2
|
+
import type { GroupSuiteMemoryDb } from './memory-store.js';
|
|
3
|
+
|
|
4
|
+
export interface PendingStatsIncrement {
|
|
5
|
+
user_id: string;
|
|
6
|
+
user_name: string;
|
|
7
|
+
group_id: string;
|
|
8
|
+
date: string;
|
|
9
|
+
count: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** All mutable state owned by one group-suite plugin instance. */
|
|
13
|
+
export interface GroupSuiteRuntime {
|
|
14
|
+
readonly db: GroupSuiteMemoryDb;
|
|
15
|
+
readonly keywords: Map<string, string>;
|
|
16
|
+
readonly teachCooldowns: Map<string, number>;
|
|
17
|
+
readonly statsBuffer: Map<string, PendingStatsIncrement>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const groupSuiteRuntimeToken = createToken<GroupSuiteRuntime>(
|
|
21
|
+
'zhin.group-suite.runtime',
|
|
22
|
+
'Owner-scoped Group Suite database and ephemeral state',
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
export function createGroupSuiteRuntime(db: GroupSuiteMemoryDb): GroupSuiteRuntime {
|
|
26
|
+
return {
|
|
27
|
+
db,
|
|
28
|
+
keywords: new Map(),
|
|
29
|
+
teachCooldowns: new Map(),
|
|
30
|
+
statsBuffer: new Map(),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function resolveGroupSuiteRuntime(context: {
|
|
35
|
+
owner?: { id?: unknown };
|
|
36
|
+
use<T>(token: typeof groupSuiteRuntimeToken): T;
|
|
37
|
+
}): GroupSuiteRuntime | undefined {
|
|
38
|
+
try {
|
|
39
|
+
return context.use(groupSuiteRuntimeToken) as GroupSuiteRuntime;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
if (context.owner?.id) throw error;
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|