@cmach_socket/opencode-qq-bot 0.8.0 → 0.9.2
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/NOTICE +48 -15
- package/README.md +130 -398
- package/config.example.json +12 -0
- package/package.json +11 -1
- package/src/cleanup.ts +1 -0
- package/src/config/fields.ts +780 -0
- package/src/config/live.ts +189 -0
- package/src/config/registry.ts +654 -0
- package/src/config/store.ts +175 -0
- package/src/config/types.ts +253 -0
- package/src/config.ts +77 -1023
- package/src/diary/scheduler.ts +39 -1
- package/src/emoji/collection.ts +289 -0
- package/src/emoji/favorite-index.ts +40 -3
- package/src/emoji/favorites.ts +7 -1
- package/src/home.ts +11 -16
- package/src/index.ts +117 -25
- package/src/logger.ts +104 -1
- package/src/naplink/client.ts +16 -0
- package/src/panel/api.ts +345 -0
- package/src/panel/auth.ts +65 -0
- package/src/panel/models.ts +56 -0
- package/src/panel/runtime.ts +51 -0
- package/src/panel/server.ts +220 -0
- package/src/panel/status.ts +201 -0
- package/src/rate-limit.ts +46 -0
- package/src/sensitive.ts +6 -1
- package/src/session/manager.ts +4 -3
- package/src/tools/qq.ts +83 -10
- package/web/dist/assets/index-7eMQEM7-.css +1 -0
- package/web/dist/assets/index-DCkgUY7y.js +15 -0
- package/web/dist/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
- package/web/dist/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
- package/web/dist/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
- package/web/dist/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
- package/web/dist/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
- package/web/dist/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
- package/web/dist/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
- package/web/dist/index.html +13 -0
package/src/diary/scheduler.ts
CHANGED
|
@@ -58,9 +58,39 @@ export class DiaryScheduler {
|
|
|
58
58
|
private readonly mutex = new Mutex()
|
|
59
59
|
/** 正在生成中的聊天,同一聊天同一时刻只跑一个任务。 */
|
|
60
60
|
private readonly running = new Set<string>()
|
|
61
|
+
/** 上一次调度的时间(毫秒)。 */
|
|
62
|
+
private lastTick: number | undefined
|
|
63
|
+
/** 上一次调度结果的说明。 */
|
|
64
|
+
private lastOutcome = ""
|
|
61
65
|
|
|
62
66
|
constructor(private readonly deps: DiarySchedulerDeps) {}
|
|
63
67
|
|
|
68
|
+
/** 下次轮询的时间(毫秒);还没跑过就是 undefined。 */
|
|
69
|
+
nextRunAt(): number | undefined {
|
|
70
|
+
if (this.lastTick === undefined) return undefined
|
|
71
|
+
const seconds = Math.max(60, this.deps.config.diary.tickSeconds)
|
|
72
|
+
return this.lastTick + seconds * 1000
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 上次轮询的时间(毫秒)。 */
|
|
76
|
+
lastRunAt(): number | undefined {
|
|
77
|
+
return this.lastTick
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 下次总结时刻(毫秒):按配置里的时刻算,今天过了就顺延到明天。 */
|
|
81
|
+
nextSummaryAt(now: Date = new Date()): number {
|
|
82
|
+
const minutes = minutesOfDay(this.deps.config.diary.summaryTime)
|
|
83
|
+
const target = new Date(now)
|
|
84
|
+
target.setHours(Math.floor(minutes / 60), minutes % 60, 0, 0)
|
|
85
|
+
if (target.getTime() <= now.getTime()) target.setDate(target.getDate() + 1)
|
|
86
|
+
return target.getTime()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** 上次轮询结果的说明。 */
|
|
90
|
+
lastResult(): string {
|
|
91
|
+
return this.lastOutcome
|
|
92
|
+
}
|
|
93
|
+
|
|
64
94
|
/** 开始轮询。 */
|
|
65
95
|
start(): void {
|
|
66
96
|
if (this.timer) return
|
|
@@ -91,15 +121,23 @@ export class DiaryScheduler {
|
|
|
91
121
|
if (!this.deps.config.diary.enabled) return
|
|
92
122
|
if (this.mutex.isLocked()) return
|
|
93
123
|
await this.mutex.runExclusive(async () => {
|
|
124
|
+
this.lastTick = now.getTime()
|
|
94
125
|
// 素材是中间产物,过期就清掉;日记成品不动。
|
|
95
126
|
await this.deps.collector.cleanup(this.deps.config.diary.retentionDays)
|
|
96
127
|
const target = minutesOfDay(this.deps.config.diary.summaryTime)
|
|
97
128
|
const current = now.getHours() * 60 + now.getMinutes()
|
|
98
|
-
if (current < target)
|
|
129
|
+
if (current < target) {
|
|
130
|
+
this.lastOutcome = `未到总结时刻(${this.deps.config.diary.summaryTime})`
|
|
131
|
+
return
|
|
132
|
+
}
|
|
99
133
|
const date = dateKey(now)
|
|
134
|
+
let done = 0
|
|
100
135
|
for (const chat of this.targets()) {
|
|
101
136
|
await this.runFor(chat, date)
|
|
137
|
+
done += 1
|
|
102
138
|
}
|
|
139
|
+
this.lastOutcome =
|
|
140
|
+
done > 0 ? `已检查 ${done} 个聊天(${date})` : `没有可总结的聊天(${date})`
|
|
103
141
|
})
|
|
104
142
|
}
|
|
105
143
|
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { createHash } from "node:crypto"
|
|
2
|
+
import { chmod, copyFile, mkdir, open, readFile, unlink } from "node:fs/promises"
|
|
3
|
+
import { extname, join } from "node:path"
|
|
4
|
+
import { fileURLToPath } from "node:url"
|
|
5
|
+
import type { Plugin } from "@opencode/plugin"
|
|
6
|
+
import { Mutex } from "async-mutex"
|
|
7
|
+
import { type ChatTarget, targetKey } from "../chat"
|
|
8
|
+
import type { Logger } from "../logger"
|
|
9
|
+
import type { PromptFile } from "../naplink/media"
|
|
10
|
+
|
|
11
|
+
/** 收藏描述最长字数。 */
|
|
12
|
+
export const MAX_DESCRIPTION_LENGTH = 12
|
|
13
|
+
|
|
14
|
+
/** 收藏描述的规则说明;描述不合规时原样回给模型。 */
|
|
15
|
+
export const DESCRIPTION_RULES = `描述要是一个中文名字,最多 ${MAX_DESCRIPTION_LENGTH} 个字,只能用文字和数字,不要标点、空格、引号、解释。`
|
|
16
|
+
|
|
17
|
+
/** 描述校验结果。 */
|
|
18
|
+
export type DescriptionCheck = { ok: true; value: string } | { ok: false; reason: string }
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 校验收藏描述。
|
|
22
|
+
*
|
|
23
|
+
* 描述会写进收藏列表,模型必须一次给出合规的名字:
|
|
24
|
+
* 只有文字和数字、不带空格标点、不超过 12 个字。
|
|
25
|
+
*/
|
|
26
|
+
export function checkDescription(raw: unknown): DescriptionCheck {
|
|
27
|
+
if (typeof raw !== "string") return { ok: false, reason: "缺少 description(收藏描述)" }
|
|
28
|
+
const value = raw.trim()
|
|
29
|
+
if (!value) return { ok: false, reason: "缺少 description(收藏描述)" }
|
|
30
|
+
if (/\s/u.test(value)) return { ok: false, reason: "描述里不能有空格" }
|
|
31
|
+
const chars = Array.from(value)
|
|
32
|
+
if (chars.some((char) => !/[\p{L}\p{N}]/u.test(char))) {
|
|
33
|
+
return { ok: false, reason: "描述里只能有文字和数字" }
|
|
34
|
+
}
|
|
35
|
+
if (chars.length > MAX_DESCRIPTION_LENGTH) {
|
|
36
|
+
return { ok: false, reason: `描述最多 ${MAX_DESCRIPTION_LENGTH} 个字` }
|
|
37
|
+
}
|
|
38
|
+
return { ok: true, value }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 插件存储里收藏清单的键。 */
|
|
42
|
+
export const COLLECTION_KEY = "emoji-collection"
|
|
43
|
+
/** 收藏文件在媒体目录下的子目录名。 */
|
|
44
|
+
export const COLLECTION_DIR = "favorites"
|
|
45
|
+
/** 本机收藏的表情数量上限,超出后丢掉最早收藏的。 */
|
|
46
|
+
export const MAX_COLLECTED_STICKERS = 200
|
|
47
|
+
|
|
48
|
+
/** 本机收集的表情。 */
|
|
49
|
+
export type CollectedSticker = {
|
|
50
|
+
/** 图片 md5,作为身份键。 */
|
|
51
|
+
md5: string
|
|
52
|
+
/** 收藏描述。 */
|
|
53
|
+
description: string
|
|
54
|
+
/** 本地文件绝对路径。 */
|
|
55
|
+
file: string
|
|
56
|
+
/** 收藏时间(毫秒)。 */
|
|
57
|
+
createdAt: number
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 收藏存储用到的插件存储接口。 */
|
|
61
|
+
export type CollectionStorage = Pick<Plugin.Context["storage"], "get" | "set">
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 本机收集的表情清单。
|
|
65
|
+
*
|
|
66
|
+
* QQ 客户端的收藏表情只有 NapCat 能写,插件收集的动图只能自己存:
|
|
67
|
+
* 图片复制到媒体目录下的收藏子目录,元数据写在插件存储里。
|
|
68
|
+
*/
|
|
69
|
+
export class CollectionStore {
|
|
70
|
+
private readonly mutex = new Mutex()
|
|
71
|
+
private items: CollectedSticker[] = []
|
|
72
|
+
private loaded = false
|
|
73
|
+
private loading: Promise<void> | undefined
|
|
74
|
+
|
|
75
|
+
constructor(
|
|
76
|
+
private readonly storage: CollectionStorage,
|
|
77
|
+
/** 媒体目录:收藏文件放在它的收藏子目录里。 */
|
|
78
|
+
private readonly root: string,
|
|
79
|
+
private readonly logger: Logger,
|
|
80
|
+
) {}
|
|
81
|
+
|
|
82
|
+
/** 收藏清单,按收藏先后返回。 */
|
|
83
|
+
async list(): Promise<CollectedSticker[]> {
|
|
84
|
+
await this.ensureLoaded()
|
|
85
|
+
return [...this.items]
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 收藏一张图片。
|
|
90
|
+
*
|
|
91
|
+
* 图片复制到收藏目录,元数据写进插件存储;
|
|
92
|
+
* 同一张图重复收藏只更新描述,文件不重复存。
|
|
93
|
+
*/
|
|
94
|
+
async add(source: string, description: string): Promise<CollectedSticker> {
|
|
95
|
+
return this.mutex.runExclusive(async () => {
|
|
96
|
+
await this.ensureLoaded()
|
|
97
|
+
const bytes = await readFile(source)
|
|
98
|
+
const md5 = createHash("md5").update(bytes).digest("hex")
|
|
99
|
+
const ext = extname(source).replace(/^\./, "").toLowerCase() || "bin"
|
|
100
|
+
const dir = join(this.root, COLLECTION_DIR)
|
|
101
|
+
await mkdir(dir, { recursive: true, mode: 0o700 })
|
|
102
|
+
const file = join(dir, `${md5}.${ext}`)
|
|
103
|
+
await copyFile(source, file)
|
|
104
|
+
await chmod(file, 0o600)
|
|
105
|
+
const existing = this.items.find((item) => item.md5 === md5)
|
|
106
|
+
const sticker: CollectedSticker = {
|
|
107
|
+
md5,
|
|
108
|
+
description,
|
|
109
|
+
file,
|
|
110
|
+
createdAt: existing?.createdAt ?? Date.now(),
|
|
111
|
+
}
|
|
112
|
+
this.items = existing
|
|
113
|
+
? this.items.map((item) => (item.md5 === md5 ? sticker : item))
|
|
114
|
+
: [...this.items, sticker]
|
|
115
|
+
await this.enforceLimit()
|
|
116
|
+
await this.persist()
|
|
117
|
+
return sticker
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** 超过上限时丢掉最早收藏的,连同它的文件一起删掉。 */
|
|
122
|
+
private async enforceLimit(): Promise<void> {
|
|
123
|
+
while (this.items.length > MAX_COLLECTED_STICKERS) {
|
|
124
|
+
const oldest = this.items.shift()
|
|
125
|
+
if (!oldest) break
|
|
126
|
+
try {
|
|
127
|
+
await unlink(oldest.file)
|
|
128
|
+
} catch {
|
|
129
|
+
// 文件可能已经被删掉,不影响清单。
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** 从插件存储恢复清单;读取失败下次重试。 */
|
|
135
|
+
private ensureLoaded(): Promise<void> {
|
|
136
|
+
if (this.loaded) return Promise.resolve()
|
|
137
|
+
this.loading ??= this.read()
|
|
138
|
+
.then(() => {
|
|
139
|
+
this.loaded = true
|
|
140
|
+
})
|
|
141
|
+
.catch((error: unknown) => {
|
|
142
|
+
this.logger.warn("读取收藏清单失败", error)
|
|
143
|
+
this.loading = undefined
|
|
144
|
+
})
|
|
145
|
+
return this.loading
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private async read(): Promise<void> {
|
|
149
|
+
const stored = await this.storage.get(COLLECTION_KEY)
|
|
150
|
+
const raw = (stored as { items?: unknown } | undefined)?.items
|
|
151
|
+
if (!Array.isArray(raw)) return
|
|
152
|
+
const items: CollectedSticker[] = []
|
|
153
|
+
for (const entry of raw) {
|
|
154
|
+
if (!entry || typeof entry !== "object") continue
|
|
155
|
+
const { md5, description, file, createdAt } = entry as Record<string, unknown>
|
|
156
|
+
if (typeof md5 !== "string" || md5.length === 0) continue
|
|
157
|
+
if (typeof description !== "string" || description.length === 0) continue
|
|
158
|
+
if (typeof file !== "string" || file.length === 0) continue
|
|
159
|
+
items.push({
|
|
160
|
+
md5,
|
|
161
|
+
description,
|
|
162
|
+
file,
|
|
163
|
+
createdAt: typeof createdAt === "number" && Number.isFinite(createdAt) ? createdAt : 0,
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
this.items = items
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** 写回清单;失败只记日志。 */
|
|
170
|
+
private async persist(): Promise<void> {
|
|
171
|
+
try {
|
|
172
|
+
await this.storage.set(COLLECTION_KEY, { version: 1, items: this.items })
|
|
173
|
+
} catch (error) {
|
|
174
|
+
this.logger.error("保存收藏清单失败", error)
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** 最近收到的图片在内存里保留的时间。 */
|
|
180
|
+
export const RECENT_IMAGE_TTL_MS = 30 * 60 * 1000
|
|
181
|
+
/** 每个聊天最多保留多少张最近收到的图片。 */
|
|
182
|
+
export const MAX_RECENT_IMAGES = 12
|
|
183
|
+
|
|
184
|
+
/** 收到的一张图片。 */
|
|
185
|
+
export interface ReceivedImage {
|
|
186
|
+
/** 本地文件绝对路径。 */
|
|
187
|
+
file: string
|
|
188
|
+
/** 扩展名,不含点。 */
|
|
189
|
+
ext: string
|
|
190
|
+
/** 是否动图。 */
|
|
191
|
+
animated: boolean
|
|
192
|
+
/** 收到时间(毫秒)。 */
|
|
193
|
+
at: number
|
|
194
|
+
/** 来源消息 ID。 */
|
|
195
|
+
messageId?: string
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** 把附件引用还原成本地路径;不是本地文件时返回 undefined。 */
|
|
199
|
+
function localPath(uri: string): string | undefined {
|
|
200
|
+
if (uri.startsWith("file:")) {
|
|
201
|
+
try {
|
|
202
|
+
return fileURLToPath(uri)
|
|
203
|
+
} catch {
|
|
204
|
+
return undefined
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return uri.startsWith("/") ? uri : undefined
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* 判断图片是不是动图。
|
|
212
|
+
*
|
|
213
|
+
* GIF 一律当动图;WebP 要看有没有 `ANIM` 块。
|
|
214
|
+
*/
|
|
215
|
+
async function isAnimatedImage(file: string, ext: string): Promise<boolean> {
|
|
216
|
+
if (ext === "gif") return true
|
|
217
|
+
if (ext !== "webp") return false
|
|
218
|
+
try {
|
|
219
|
+
const handle = await open(file, "r")
|
|
220
|
+
try {
|
|
221
|
+
const buffer = Buffer.alloc(256)
|
|
222
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0)
|
|
223
|
+
return buffer.subarray(0, bytesRead).includes("ANIM")
|
|
224
|
+
} finally {
|
|
225
|
+
await handle.close()
|
|
226
|
+
}
|
|
227
|
+
} catch {
|
|
228
|
+
return false
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* 各聊天最近收到的图片。
|
|
234
|
+
*
|
|
235
|
+
* 收藏表情要有个来源:这个表记住每个聊天刚收到过哪些图片,
|
|
236
|
+
* 收藏时从中挑最近收到的那张动图。
|
|
237
|
+
*/
|
|
238
|
+
export class RecentImages {
|
|
239
|
+
private readonly chats = new Map<string, ReceivedImage[]>()
|
|
240
|
+
|
|
241
|
+
constructor(private readonly ttlMs = RECENT_IMAGE_TTL_MS) {}
|
|
242
|
+
|
|
243
|
+
/** 记下一条消息里的图片。 */
|
|
244
|
+
async record(
|
|
245
|
+
target: ChatTarget,
|
|
246
|
+
files: readonly PromptFile[],
|
|
247
|
+
messageId?: string,
|
|
248
|
+
): Promise<void> {
|
|
249
|
+
const images: ReceivedImage[] = []
|
|
250
|
+
for (const file of files) {
|
|
251
|
+
const path = localPath(file.uri)
|
|
252
|
+
if (!path) continue
|
|
253
|
+
const ext = extname(path).replace(/^\./, "").toLowerCase()
|
|
254
|
+
if (!ext) continue
|
|
255
|
+
images.push({
|
|
256
|
+
file: path,
|
|
257
|
+
ext,
|
|
258
|
+
animated: await isAnimatedImage(path, ext),
|
|
259
|
+
at: Date.now(),
|
|
260
|
+
...(messageId ? { messageId } : {}),
|
|
261
|
+
})
|
|
262
|
+
}
|
|
263
|
+
if (images.length === 0) return
|
|
264
|
+
const key = targetKey(target)
|
|
265
|
+
const items = [...(this.chats.get(key) ?? []), ...images]
|
|
266
|
+
while (items.length > MAX_RECENT_IMAGES) items.shift()
|
|
267
|
+
this.chats.set(key, items)
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** 这个聊天最近收到的图片,只返回还在保留期内的。 */
|
|
271
|
+
list(target: ChatTarget): ReceivedImage[] {
|
|
272
|
+
const key = targetKey(target)
|
|
273
|
+
const now = Date.now()
|
|
274
|
+
const items = (this.chats.get(key) ?? []).filter((item) => now - item.at < this.ttlMs)
|
|
275
|
+
if (items.length > 0) this.chats.set(key, items)
|
|
276
|
+
return [...items]
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* 最近收到的一张图片。
|
|
281
|
+
*
|
|
282
|
+
* 要求动图时只在动图里找最新的那张。
|
|
283
|
+
*/
|
|
284
|
+
latest(target: ChatTarget, options: { animated?: boolean } = {}): ReceivedImage | undefined {
|
|
285
|
+
const items = this.list(target)
|
|
286
|
+
const candidates = options.animated ? items.filter((item) => item.animated) : items
|
|
287
|
+
return candidates[candidates.length - 1]
|
|
288
|
+
}
|
|
289
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Plugin } from "@opencode/plugin"
|
|
2
2
|
import { Mutex } from "async-mutex"
|
|
3
3
|
import type { Logger } from "../logger"
|
|
4
|
+
import type { CollectedSticker } from "./collection"
|
|
4
5
|
import { type CustomFace, faceTag, type FavoriteRow } from "./favorites"
|
|
5
6
|
|
|
6
7
|
/** 插件存储里收藏表情编号索引的键。 */
|
|
@@ -31,6 +32,15 @@ interface StoredEntry {
|
|
|
31
32
|
id: number
|
|
32
33
|
}
|
|
33
34
|
|
|
35
|
+
/**
|
|
36
|
+
* 本机收集的表情的身份键。
|
|
37
|
+
*
|
|
38
|
+
* 和收藏表情一样以图片 md5 为准:同一张图只占一个编号。
|
|
39
|
+
*/
|
|
40
|
+
export function collectedKey(sticker: CollectedSticker): string {
|
|
41
|
+
return `md5:${sticker.md5.trim().toLowerCase()}`
|
|
42
|
+
}
|
|
43
|
+
|
|
34
44
|
/** 从插件存储里读回编号记录,格式不对就当没有。 */
|
|
35
45
|
function parseEntries(stored: unknown): StoredEntry[] {
|
|
36
46
|
if (!stored || typeof stored !== "object" || Array.isArray(stored)) return []
|
|
@@ -70,8 +80,12 @@ export class FavoriteIndex {
|
|
|
70
80
|
* 读取收藏表情列表并给每个表情配上编号。
|
|
71
81
|
*
|
|
72
82
|
* 返回的顺序按编号从小到大;`md5` 相同的记录视为同一个表情,只保留一条。
|
|
83
|
+
* 本机收集的表情和 QQ 收藏表情共用一套编号,按图片 md5 认回同一个编号。
|
|
73
84
|
*/
|
|
74
|
-
async sync(
|
|
85
|
+
async sync(
|
|
86
|
+
faces: readonly CustomFace[],
|
|
87
|
+
collected: readonly CollectedSticker[] = [],
|
|
88
|
+
): Promise<FavoriteRow[]> {
|
|
75
89
|
await this.ensureLoaded()
|
|
76
90
|
const rows: FavoriteRow[] = []
|
|
77
91
|
const seen = new Set<string>()
|
|
@@ -94,14 +108,37 @@ export class FavoriteIndex {
|
|
|
94
108
|
}
|
|
95
109
|
rows.push({ id, face, tag: faceTag(face) })
|
|
96
110
|
}
|
|
111
|
+
for (const sticker of collected) {
|
|
112
|
+
const key = collectedKey(sticker)
|
|
113
|
+
// 和 QQ 收藏表情撞上同一张图时以 QQ 收藏表情为准。
|
|
114
|
+
if (seen.has(key)) continue
|
|
115
|
+
seen.add(key)
|
|
116
|
+
let id = this.entries.get(key)
|
|
117
|
+
if (id === undefined) {
|
|
118
|
+
id = nextId
|
|
119
|
+
nextId += 1
|
|
120
|
+
this.entries.set(key, id)
|
|
121
|
+
changed = true
|
|
122
|
+
}
|
|
123
|
+
rows.push({
|
|
124
|
+
id,
|
|
125
|
+
face: { md5: sticker.md5, url: sticker.file, desc: sticker.description },
|
|
126
|
+
tag: sticker.description,
|
|
127
|
+
collected: sticker,
|
|
128
|
+
})
|
|
129
|
+
}
|
|
97
130
|
rows.sort((left, right) => left.id - right.id)
|
|
98
131
|
if (changed) await this.persist()
|
|
99
132
|
return rows
|
|
100
133
|
}
|
|
101
134
|
|
|
102
135
|
/** 按编号找收藏表情;编号不存在时返回 undefined。 */
|
|
103
|
-
async resolve(
|
|
104
|
-
|
|
136
|
+
async resolve(
|
|
137
|
+
faces: readonly CustomFace[],
|
|
138
|
+
id: number,
|
|
139
|
+
collected: readonly CollectedSticker[] = [],
|
|
140
|
+
): Promise<FavoriteRow | undefined> {
|
|
141
|
+
const rows = await this.sync(faces, collected)
|
|
105
142
|
return rows.find((row) => row.id === id)
|
|
106
143
|
}
|
|
107
144
|
|
package/src/emoji/favorites.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Mutex } from "async-mutex"
|
|
2
2
|
import type { Logger } from "../logger"
|
|
3
3
|
import { sanitizeUntrustedText } from "../model-view"
|
|
4
|
+
import type { CollectedSticker } from "./collection"
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* NapCat 收藏表情(自定义表情)的详情。
|
|
@@ -57,6 +58,8 @@ export interface FavoriteRow {
|
|
|
57
58
|
face: CustomFace
|
|
58
59
|
/** 收藏表情当前的标签。 */
|
|
59
60
|
tag: string
|
|
61
|
+
/** 本机收集的表情;QQ 收藏表情没有这个字段。 */
|
|
62
|
+
collected?: CollectedSticker
|
|
60
63
|
}
|
|
61
64
|
|
|
62
65
|
/**
|
|
@@ -66,7 +69,10 @@ export interface FavoriteRow {
|
|
|
66
69
|
*/
|
|
67
70
|
export function formatFavoriteList(rows: readonly FavoriteRow[]): string {
|
|
68
71
|
if (rows.length === 0) return "没有读取到收藏表情。"
|
|
69
|
-
const lines = rows.map((row) =>
|
|
72
|
+
const lines = rows.map((row) => {
|
|
73
|
+
const tag = row.tag || "(未标注)"
|
|
74
|
+
return `${row.id}. ${tag}${row.collected ? "(本机收藏)" : ""}`
|
|
75
|
+
})
|
|
70
76
|
return [`收藏表情(编号即发送用的 id,共 ${rows.length} 个)`, ...lines].join("\n")
|
|
71
77
|
}
|
|
72
78
|
|
package/src/home.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { mkdir } from "node:fs/promises"
|
|
2
2
|
import { homedir } from "node:os"
|
|
3
|
-
import { isAbsolute, join
|
|
3
|
+
import { isAbsolute, join } from "node:path"
|
|
4
4
|
import type { Logger } from "./logger"
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* 机器人固定服务的工作目录。
|
|
8
|
+
*
|
|
9
|
+
* 工作目录不再可配置:`config.json` 自己就放在这个目录里,位置固定才不会自举。
|
|
10
|
+
*/
|
|
11
|
+
|
|
6
12
|
/** 机器人固定服务的工作目录。 */
|
|
7
13
|
export interface BotHome {
|
|
8
14
|
/** 会话统一所在的绝对路径。 */
|
|
@@ -19,14 +25,12 @@ export function defaultHome(): string {
|
|
|
19
25
|
/**
|
|
20
26
|
* 解析机器人工作目录并确保它存在。
|
|
21
27
|
*
|
|
22
|
-
*
|
|
28
|
+
* 目录固定在 XDG 数据目录下的工作区,启动时自动创建。
|
|
23
29
|
*/
|
|
24
|
-
export async function resolveHome(
|
|
25
|
-
|
|
26
|
-
logger: Logger,
|
|
27
|
-
): Promise<BotHome> {
|
|
28
|
-
const directory = configured ? expand(configured, logger) : defaultHome()
|
|
30
|
+
export async function resolveHome(logger: Logger): Promise<BotHome> {
|
|
31
|
+
const directory = defaultHome()
|
|
29
32
|
await mkdir(directory, { recursive: true, mode: 0o700 })
|
|
33
|
+
logger.debug(`机器人工作目录:${directory}`)
|
|
30
34
|
return { directory }
|
|
31
35
|
}
|
|
32
36
|
|
|
@@ -36,12 +40,3 @@ export function expandTilde(value: string): string {
|
|
|
36
40
|
if (value.startsWith("~/")) return join(homedir(), value.slice(2))
|
|
37
41
|
return value
|
|
38
42
|
}
|
|
39
|
-
|
|
40
|
-
function expand(configured: string, logger: Logger): string {
|
|
41
|
-
const expanded = expandTilde(configured)
|
|
42
|
-
if (!isAbsolute(expanded)) {
|
|
43
|
-
logger.warn("机器人工作目录必须是绝对路径,已回退到默认目录", configured)
|
|
44
|
-
return defaultHome()
|
|
45
|
-
}
|
|
46
|
-
return normalize(expanded)
|
|
47
|
-
}
|