@karinjs/adapter-qqbot 2.0.0 → 2.2.1
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 +112 -255
- package/dist/apps/cleanup.d.ts +5 -0
- package/dist/apps/cleanup.js +22 -0
- package/dist/apps/login.d.ts +1 -3
- package/dist/apps/login.js +65 -16
- package/dist/common-by1GpV0b.js +49 -0
- package/dist/config-B_Gc9JKZ.js +253 -0
- package/dist/index.d.ts +511 -25
- package/dist/index.js +2327 -242
- package/dist/message-Dt4QkDYn.js +1739 -0
- package/dist/{onboard-BX8g8Wik.js → onboard-BXQ1cG8h.js} +10 -7
- package/dist/utils-DIq0lGMt.js +4 -0
- package/dist/web.config.d.ts +21 -19
- package/dist/web.config.js +232 -88
- package/package.json +6 -11
- package/dist/config-H7LrvlNU.js +0 -197
- package/dist/utils-DO4gEkVN.js +0 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { AdapterBase, AdapterType, Contact, CreateGroupFolderResponse, DownloadFileOptions, DownloadFileResponse, ElementTypes, ForwardOptions, GetAiCharactersResponse, GetAtAllCountResponse, GetGroupFileListResponse, GetGroupFileSystemInfoResponse, GetGroupHighlightsResponse, GetGroupMuteListResponse, GetRkeyResponse, GroupInfo, GroupMemberInfo, LogMethodNames, Message, MessageResponse, NodeElement, QQGroupHonorInfo, SendMsgResults, UserInfo, karinToQQBot } from "node-karin";
|
|
1
|
+
import { AdapterBase, AdapterType, Contact, CreateGroupFolderResponse, DownloadFileOptions, DownloadFileResponse, ElementTypes, ForwardOptions, GetAiCharactersResponse, GetAtAllCountResponse, GetGroupFileListResponse, GetGroupFileSystemInfoResponse, GetGroupHighlightsResponse, GetGroupMuteListResponse, GetRkeyResponse, GroupInfo, GroupMemberInfo, LogMethodNames, Message, MessageResponse, NodeElement, QQGroupHonorInfo, SendMsgResults, Sender, UserInfo, karinToQQBot } from "node-karin";
|
|
2
2
|
import FormData from "form-data";
|
|
3
|
-
import * as _$node_karin_axios0 from "node-karin/axios";
|
|
4
3
|
|
|
5
4
|
//#region src/core/api/types.d.ts
|
|
6
5
|
/** 上传富媒体文件场景 */
|
|
@@ -87,8 +86,6 @@ interface SendQQMediaMessageRequest extends SendQQMessageRequest, QQMessageID {
|
|
|
87
86
|
media: {
|
|
88
87
|
/** 文件信息,用于发消息接口的 media 字段使用 */file_info: string;
|
|
89
88
|
};
|
|
90
|
-
/** 当 msg_type = 7 时,content 字段需要填入一个值,譬如一个空格 “ ”,后续版本会修复该问题。 */
|
|
91
|
-
content: string;
|
|
92
89
|
}
|
|
93
90
|
/** 发送QQ消息请求参数 */
|
|
94
91
|
type SendQQMsg = SendQQTextMessageRequest | SendQQMarkdownMessageRequest | SendQQArkMessageRequest | SendQQMediaMessageRequest;
|
|
@@ -215,7 +212,11 @@ interface SendQQMsgResponse {
|
|
|
215
212
|
/** 消息唯一 ID */
|
|
216
213
|
id: string;
|
|
217
214
|
/** 发送时间 */
|
|
218
|
-
timestamp: number;
|
|
215
|
+
timestamp: number | string;
|
|
216
|
+
/** 扩展信息;部分 QQ 消息发送响应会返回可用于 message_reference 的 REFIDX。 */
|
|
217
|
+
ext_info?: {
|
|
218
|
+
/** 当前发送消息可被引用时使用的索引。 */ref_idx?: string;
|
|
219
|
+
};
|
|
219
220
|
}
|
|
220
221
|
/** 发送频道消息后响应 */
|
|
221
222
|
interface SendGuildResponse {
|
|
@@ -240,7 +241,7 @@ interface UploadMediaResponse {
|
|
|
240
241
|
/**
|
|
241
242
|
* 创建 axios 实例,自动从 tokens map 取最新 access_token
|
|
242
243
|
*/
|
|
243
|
-
declare const createAxiosInstance: (baseURL: string, appId: string) =>
|
|
244
|
+
declare const createAxiosInstance: (baseURL: string, appId: string) => import("node-karin/axios").AxiosInstance;
|
|
244
245
|
//#endregion
|
|
245
246
|
//#region src/core/api/http.d.ts
|
|
246
247
|
type AxiosInstance = ReturnType<typeof createAxiosInstance>;
|
|
@@ -251,8 +252,8 @@ declare class Http {
|
|
|
251
252
|
readonly axios: AxiosInstance;
|
|
252
253
|
constructor(axios: AxiosInstance);
|
|
253
254
|
protected get<T>(path: string): Promise<T>;
|
|
254
|
-
protected post<T>(path: string, body?: unknown, headers?: Record<string, string
|
|
255
|
-
protected put<T>(path: string, body?: unknown, headers?: Record<string, string
|
|
255
|
+
protected post<T>(path: string, body?: unknown, headers?: Record<string, string>, timeout?: number): Promise<T>;
|
|
256
|
+
protected put<T>(path: string, body?: unknown, headers?: Record<string, string>, timeout?: number): Promise<T>;
|
|
256
257
|
protected delete<T>(path: string): Promise<T>;
|
|
257
258
|
}
|
|
258
259
|
//#endregion
|
|
@@ -292,10 +293,69 @@ declare class MediaApi extends Http {
|
|
|
292
293
|
* @param scene user(单聊) / group(群聊)
|
|
293
294
|
* @param peer openid 或 group_openid
|
|
294
295
|
* @param type 文件类型
|
|
295
|
-
* @param source
|
|
296
|
+
* @param source http url、本地路径或 base64 字符串
|
|
296
297
|
* @param srvSendMsg 上传后是否由服务端直接发送
|
|
298
|
+
* @param fileName 文件消息的展示文件名
|
|
299
|
+
*/
|
|
300
|
+
upload(scene: Scene, peer: string, type: MediaType, source: string, srvSendMsg?: boolean, fileName?: string): Promise<UploadMediaResponse>;
|
|
301
|
+
/**
|
|
302
|
+
* 上传没有公网 URL 处理器可用时的 fallback 富媒体资源。
|
|
303
|
+
*
|
|
304
|
+
* 较大的 fallback 资源会优先使用 QQ 分片上传,提高直接发送成功率。
|
|
305
|
+
* `srvSendMsg` 为 true 时保留普通上传路径,避免分片完成接口与服务端直接发送语义冲突。
|
|
306
|
+
*
|
|
307
|
+
* @param scene user(单聊) / group(群聊)
|
|
308
|
+
* @param peer openid 或 group_openid
|
|
309
|
+
* @param type 文件类型
|
|
310
|
+
* @param source 本地路径、http url、data URL、base64:// 或裸 base64
|
|
311
|
+
* @param srvSendMsg 上传后是否由服务端直接发送
|
|
312
|
+
* @param fileName 文件消息的展示文件名
|
|
313
|
+
* @returns 上传结果,包含后续发送消息所需的 file_info
|
|
314
|
+
*/
|
|
315
|
+
uploadFallback(scene: Scene, peer: string, type: MediaType, source: string, srvSendMsg?: boolean, fileName?: string): Promise<UploadMediaResponse>;
|
|
316
|
+
/**
|
|
317
|
+
* 确认单个分片已完成上传。
|
|
318
|
+
*
|
|
319
|
+
* QQ 有时会在分片刚 PUT 完时短暂返回 40093001,此时需要在服务端给出的
|
|
320
|
+
* retry_timeout 窗口内持续重试,而不是直接判定整个大文件上传失败。
|
|
321
|
+
*
|
|
322
|
+
* @param scene user(单聊) / group(群聊)
|
|
323
|
+
* @param peer openid 或 group_openid
|
|
324
|
+
* @param body upload_part_finish 请求体
|
|
325
|
+
* @param retryTimeout 服务端返回的持续重试秒数
|
|
326
|
+
*/
|
|
327
|
+
private finishUploadPart;
|
|
328
|
+
/**
|
|
329
|
+
* 持续重试 upload_part_finish 的短暂服务端状态。
|
|
330
|
+
*
|
|
331
|
+
* @param scene user(单聊) / group(群聊)
|
|
332
|
+
* @param peer openid 或 group_openid
|
|
333
|
+
* @param body upload_part_finish 请求体
|
|
334
|
+
* @param retryTimeout 服务端返回的持续重试秒数
|
|
335
|
+
*/
|
|
336
|
+
private retryUploadPartFinish;
|
|
337
|
+
/**
|
|
338
|
+
* 完成 QQ 分片上传,并对短暂失败做有限重试。
|
|
339
|
+
*
|
|
340
|
+
* @param scene user(单聊) / group(群聊)
|
|
341
|
+
* @param peer openid 或 group_openid
|
|
342
|
+
* @param uploadId upload_prepare 返回的上传任务 ID
|
|
343
|
+
* @returns 上传结果
|
|
297
344
|
*/
|
|
298
|
-
|
|
345
|
+
private completeChunkedUpload;
|
|
346
|
+
/**
|
|
347
|
+
* 使用 QQ 官方分片协议上传 fallback 大资源。
|
|
348
|
+
*
|
|
349
|
+
* 流程为:准备上传 -> PUT 每个分片 -> 确认每个分片 -> 完成上传。
|
|
350
|
+
*
|
|
351
|
+
* @param scene user(单聊) / group(群聊)
|
|
352
|
+
* @param peer openid 或 group_openid
|
|
353
|
+
* @param type 文件类型
|
|
354
|
+
* @param source 已归一化的 fallback 来源
|
|
355
|
+
* @param preparedHashes 预计算的文件 hash
|
|
356
|
+
* @returns 上传结果,包含后续发送消息所需的 file_info
|
|
357
|
+
*/
|
|
358
|
+
private uploadChunked;
|
|
299
359
|
}
|
|
300
360
|
//#endregion
|
|
301
361
|
//#region src/core/api/interaction.d.ts
|
|
@@ -362,7 +422,416 @@ declare class QQBotApi {
|
|
|
362
422
|
constructor(axios: AxiosInstance);
|
|
363
423
|
}
|
|
364
424
|
//#endregion
|
|
425
|
+
//#region src/core/storage/message-store/types.d.ts
|
|
426
|
+
interface CachedMessage {
|
|
427
|
+
messageId: string;
|
|
428
|
+
messageSeq: number;
|
|
429
|
+
time: number;
|
|
430
|
+
contact: Contact;
|
|
431
|
+
sender: Sender;
|
|
432
|
+
elements: ElementTypes[];
|
|
433
|
+
}
|
|
434
|
+
//#endregion
|
|
435
|
+
//#region src/core/storage/message-store/store.d.ts
|
|
436
|
+
/**
|
|
437
|
+
* QQBot 收到的消息缓存。
|
|
438
|
+
*
|
|
439
|
+
* 热路径只写内存并入队,不等待 SQLite:
|
|
440
|
+
* - `getMsg` 优先查内存,插件在同一事件回调内可立即命中;
|
|
441
|
+
* - SQLite 在后台按批次事务落库,避免一条消息一个事务拖慢事件分发;
|
|
442
|
+
* - 元素表只保存 `type + value`,避免宽表 NULL 和多详情表多次 INSERT。
|
|
443
|
+
*/
|
|
444
|
+
declare class MessageStore {
|
|
445
|
+
private db?;
|
|
446
|
+
private readonly ready;
|
|
447
|
+
private lastCleanup;
|
|
448
|
+
private shouldVacuum;
|
|
449
|
+
private flushTimer?;
|
|
450
|
+
private flushing;
|
|
451
|
+
private mediaTimer?;
|
|
452
|
+
private mediaActive;
|
|
453
|
+
private readonly pending;
|
|
454
|
+
private readonly pendingMedia;
|
|
455
|
+
private readonly pendingMediaKeys;
|
|
456
|
+
private readonly memory;
|
|
457
|
+
private readonly directIndex;
|
|
458
|
+
private readonly aliasIndex;
|
|
459
|
+
private readonly scopedAliasIndex;
|
|
460
|
+
private readonly botRefCache;
|
|
461
|
+
private readonly contactRefCache;
|
|
462
|
+
private readonly senderRefCache;
|
|
463
|
+
constructor();
|
|
464
|
+
/**
|
|
465
|
+
* 立即执行一次过期消息和本地媒体缓存清理。
|
|
466
|
+
*
|
|
467
|
+
* 该方法交给 Karin `task` 调度调用;类内部只保留清理能力,不自行维护常驻定时器。
|
|
468
|
+
*
|
|
469
|
+
* @returns 清理完成 Promise。
|
|
470
|
+
*/
|
|
471
|
+
cleanupExpired(): Promise<void>;
|
|
472
|
+
/**
|
|
473
|
+
* 保存实际消息,并为 QQ 的 `msg_idx` 等引用索引建立轻量映射。
|
|
474
|
+
*
|
|
475
|
+
* 该方法只同步写入内存并投递后台队列,不等待 SQLite。这样消息事件创建不会被
|
|
476
|
+
* 数据库写锁或小事务阻塞。
|
|
477
|
+
*
|
|
478
|
+
* @param botId 当前 QQBot appId。
|
|
479
|
+
* @param message 已转换为 Karin elements 的消息缓存。
|
|
480
|
+
* @param aliases 可查询到该消息的额外消息索引,例如 QQ `msg_idx`。
|
|
481
|
+
*/
|
|
482
|
+
save(botId: string, message: CachedMessage, aliases?: string[]): Promise<void>;
|
|
483
|
+
/**
|
|
484
|
+
* 仅当内存中没有该引用索引时保存 QQ 下发的引用上下文。
|
|
485
|
+
* 数据库是否已存在由后台 flush 再判断,避免阻塞事件热路径。
|
|
486
|
+
*
|
|
487
|
+
* @param botId 当前 QQBot appId。
|
|
488
|
+
* @param message 以 `ref_msg_idx` 作为 messageId 的引用上下文。
|
|
489
|
+
*/
|
|
490
|
+
saveReferenceIfAbsent(botId: string, message: CachedMessage): Promise<void>;
|
|
491
|
+
/**
|
|
492
|
+
* 读取缓存消息。
|
|
493
|
+
*
|
|
494
|
+
* OneBot v11 标准调用只传 messageId;contact 参数仅用于额外限定范围。
|
|
495
|
+
*
|
|
496
|
+
* @param botId 当前 QQBot appId。
|
|
497
|
+
* @param messageId 真实消息 ID 或 QQ `REFIDX` 引用索引。
|
|
498
|
+
* @param contact 可选会话范围,传入后只在该会话下查询。
|
|
499
|
+
* @returns 命中的消息缓存;未命中或已过期时返回 null。
|
|
500
|
+
*/
|
|
501
|
+
get(botId: string, messageId: string, contact?: Contact): Promise<MessageResponse | null>;
|
|
502
|
+
/**
|
|
503
|
+
* 按起始消息 ID 读取同一会话内的历史缓存消息。
|
|
504
|
+
*
|
|
505
|
+
* QQ 官方 Bot 没有 seq 历史查询能力;这里以本地缓存中命中的起始消息为锚点,
|
|
506
|
+
* 按时间倒序返回最近的 `count` 条消息,包含起始消息本身。
|
|
507
|
+
*
|
|
508
|
+
* @param botId 当前 QQBot appId。
|
|
509
|
+
* @param contact 查询限定的会话。
|
|
510
|
+
* @param startMsgId 起始消息 ID 或 QQ `REFIDX` 引用索引。
|
|
511
|
+
* @param count 获取消息数量。
|
|
512
|
+
* @returns 命中的历史消息数组;未命中或已过期时返回空数组。
|
|
513
|
+
*/
|
|
514
|
+
getHistory(botId: string, contact: Contact, startMsgId: string, count: number): Promise<MessageResponse[]>;
|
|
515
|
+
/**
|
|
516
|
+
* 初始化 SQLite 连接、表结构,并执行启动清理和媒体本地化补救。
|
|
517
|
+
*
|
|
518
|
+
* @returns 初始化完成 Promise。
|
|
519
|
+
*/
|
|
520
|
+
private initialize;
|
|
521
|
+
/**
|
|
522
|
+
* 投递待落库消息。
|
|
523
|
+
*
|
|
524
|
+
* @param item 待写入 SQLite 的缓存任务。
|
|
525
|
+
*/
|
|
526
|
+
private enqueue;
|
|
527
|
+
/**
|
|
528
|
+
* 安排一次后台批量落库。
|
|
529
|
+
*
|
|
530
|
+
* 已有 timer 或正在 flush 时不会重复安排。
|
|
531
|
+
*/
|
|
532
|
+
private scheduleFlush;
|
|
533
|
+
/**
|
|
534
|
+
* 批量落库队列中的消息。
|
|
535
|
+
*
|
|
536
|
+
* 单次最多处理 `FLUSH_BATCH_SIZE` 条,并在一个事务内完成写入。
|
|
537
|
+
*
|
|
538
|
+
* @returns flush 完成 Promise。
|
|
539
|
+
*/
|
|
540
|
+
private flushQueue;
|
|
541
|
+
/**
|
|
542
|
+
* 写入一条待落库消息。
|
|
543
|
+
*
|
|
544
|
+
* @param item 待写入的消息、alias 和引用上下文标记。
|
|
545
|
+
*/
|
|
546
|
+
private writeItem;
|
|
547
|
+
/**
|
|
548
|
+
* 清理待写队列中已经过期的消息。
|
|
549
|
+
*/
|
|
550
|
+
private prunePending;
|
|
551
|
+
/**
|
|
552
|
+
* 投递媒体本地化任务。
|
|
553
|
+
*
|
|
554
|
+
* 原消息会先保留短期 URL 写入缓存;本地化成功后再覆盖同一条消息。队列会按
|
|
555
|
+
* message scoped key 去重,避免高频更新同一条消息时重复下载。
|
|
556
|
+
*
|
|
557
|
+
* @param botId 当前 QQBot appId。
|
|
558
|
+
* @param message 可能包含远程媒体 URL 的消息快照。
|
|
559
|
+
* @param aliases 该消息可被查询到的额外 alias。
|
|
560
|
+
*/
|
|
561
|
+
private enqueueMediaLocalize;
|
|
562
|
+
/**
|
|
563
|
+
* 安排媒体本地化队列处理。
|
|
564
|
+
*
|
|
565
|
+
* @returns 已存在定时器或队列为空时直接返回。
|
|
566
|
+
*/
|
|
567
|
+
private scheduleMediaLocalize;
|
|
568
|
+
/**
|
|
569
|
+
* 按固定并发处理媒体本地化任务。
|
|
570
|
+
*
|
|
571
|
+
* @returns 队列为空或达到并发上限时返回。
|
|
572
|
+
*/
|
|
573
|
+
private drainMediaLocalizeQueue;
|
|
574
|
+
/**
|
|
575
|
+
* 本地化单条消息中的远程媒体字段,并在成功后覆盖内存与 SQLite。
|
|
576
|
+
*
|
|
577
|
+
* @param item 待处理的媒体本地化任务。
|
|
578
|
+
* @returns 本地化与回写完成 Promise。
|
|
579
|
+
*/
|
|
580
|
+
private localizeMediaItem;
|
|
581
|
+
/**
|
|
582
|
+
* 清理已经过期的媒体本地化待处理任务。
|
|
583
|
+
*
|
|
584
|
+
* @returns 无返回值;会同步维护 pendingMediaKeys。
|
|
585
|
+
*/
|
|
586
|
+
private prunePendingMedia;
|
|
587
|
+
/**
|
|
588
|
+
* 启动时扫描 SQLite 中尚未本地化的远程媒体 URL,并重新投递队列补救。
|
|
589
|
+
*
|
|
590
|
+
* @returns 扫描和投递完成 Promise。
|
|
591
|
+
*/
|
|
592
|
+
private recoverRemoteMedia;
|
|
593
|
+
/**
|
|
594
|
+
* 写入内存热缓存。
|
|
595
|
+
*
|
|
596
|
+
* @param botId 当前 QQBot appId。
|
|
597
|
+
* @param message 消息缓存。
|
|
598
|
+
* @param aliases 可查询到该消息的额外消息索引。
|
|
599
|
+
*/
|
|
600
|
+
private putMemory;
|
|
601
|
+
/**
|
|
602
|
+
* 从内存热缓存读取消息。
|
|
603
|
+
*
|
|
604
|
+
* @param botId 当前 QQBot appId。
|
|
605
|
+
* @param messageId 真实消息 ID 或 alias。
|
|
606
|
+
* @param contact 可选会话范围。
|
|
607
|
+
* @returns 命中的缓存消息;未命中或过期时返回 null。
|
|
608
|
+
*/
|
|
609
|
+
private getFromMemory;
|
|
610
|
+
/**
|
|
611
|
+
* 到达清理间隔时清理内存缓存。
|
|
612
|
+
*/
|
|
613
|
+
private cleanupMemoryIfDue;
|
|
614
|
+
/**
|
|
615
|
+
* 清理一天前的内存消息,并同步清理失效索引。
|
|
616
|
+
*/
|
|
617
|
+
private cleanupMemory;
|
|
618
|
+
/**
|
|
619
|
+
* 清理指向已删除内存消息的索引。
|
|
620
|
+
*
|
|
621
|
+
* @param index 需要清理的索引 Map。
|
|
622
|
+
*/
|
|
623
|
+
private cleanIndex;
|
|
624
|
+
/**
|
|
625
|
+
* 浅拷贝消息缓存,避免后续事件对象被插件侧修改后污染缓存。
|
|
626
|
+
*
|
|
627
|
+
* @param message 原始缓存消息。
|
|
628
|
+
* @returns 可安全放入缓存的消息副本。
|
|
629
|
+
*/
|
|
630
|
+
private cloneMessage;
|
|
631
|
+
/**
|
|
632
|
+
* 将内存缓存转换为 Karin `MessageResponse`。
|
|
633
|
+
*
|
|
634
|
+
* @param message 内存缓存消息。
|
|
635
|
+
* @returns Karin 标准消息查询结果。
|
|
636
|
+
*/
|
|
637
|
+
private toResponse;
|
|
638
|
+
/**
|
|
639
|
+
* 执行数据库版本迁移。
|
|
640
|
+
*
|
|
641
|
+
* 使用 SQLite `PRAGMA user_version` 记录已应用的 schema 版本。
|
|
642
|
+
* 版本 0 只表示“未初始化或开发期旧库”,首个 alpha 发布基线为 v1。
|
|
643
|
+
*
|
|
644
|
+
* @returns 迁移完成 Promise。
|
|
645
|
+
*/
|
|
646
|
+
private migrate;
|
|
647
|
+
/**
|
|
648
|
+
* 获取或创建 bot 映射 ID。
|
|
649
|
+
*
|
|
650
|
+
* @param botId 当前 QQBot appId。
|
|
651
|
+
* @returns `qqbot_bots.id`。
|
|
652
|
+
*/
|
|
653
|
+
private ensureBot;
|
|
654
|
+
/**
|
|
655
|
+
* 获取或创建会话映射 ID。
|
|
656
|
+
*
|
|
657
|
+
* @param botRef `qqbot_bots.id`。
|
|
658
|
+
* @param contact Karin 会话对象。
|
|
659
|
+
* @returns `qqbot_contacts.id`。
|
|
660
|
+
*/
|
|
661
|
+
private ensureContact;
|
|
662
|
+
/**
|
|
663
|
+
* 获取或创建发送者映射 ID。
|
|
664
|
+
*
|
|
665
|
+
* @param botRef `qqbot_bots.id`。
|
|
666
|
+
* @param sender Karin sender 对象。
|
|
667
|
+
* @returns `qqbot_senders.id`。
|
|
668
|
+
*/
|
|
669
|
+
private ensureSender;
|
|
670
|
+
/**
|
|
671
|
+
* 获取或创建消息主记录。
|
|
672
|
+
*
|
|
673
|
+
* @param botRef `qqbot_bots.id`。
|
|
674
|
+
* @param contactRef `qqbot_contacts.id`。
|
|
675
|
+
* @param senderRef `qqbot_senders.id`。
|
|
676
|
+
* @param message 消息缓存。
|
|
677
|
+
* @returns `qqbot_messages.id`。
|
|
678
|
+
*/
|
|
679
|
+
private ensureMessage;
|
|
680
|
+
/**
|
|
681
|
+
* 从查询行还原 Karin Contact。
|
|
682
|
+
*
|
|
683
|
+
* @param row 消息主查询结果。
|
|
684
|
+
* @returns Karin Contact。
|
|
685
|
+
*/
|
|
686
|
+
private contactFromRow;
|
|
687
|
+
/**
|
|
688
|
+
* 将数据库行还原为 Karin `MessageResponse`。
|
|
689
|
+
*
|
|
690
|
+
* @param row 消息主查询结果。
|
|
691
|
+
* @returns Karin 标准消息查询结果。
|
|
692
|
+
*/
|
|
693
|
+
private rowToResponse;
|
|
694
|
+
/**
|
|
695
|
+
* 在指定会话中解析消息 ID 或 alias 到消息主表行。
|
|
696
|
+
*
|
|
697
|
+
* @param botId 当前 QQBot appId。
|
|
698
|
+
* @param contact Karin 会话对象。
|
|
699
|
+
* @param messageId 真实消息 ID 或 alias。
|
|
700
|
+
* @returns 命中的消息行;未命中时返回 null。
|
|
701
|
+
*/
|
|
702
|
+
private resolveMessageRow;
|
|
703
|
+
/**
|
|
704
|
+
* 构造会话级查询键。
|
|
705
|
+
*
|
|
706
|
+
* @param botId 当前 QQBot appId。
|
|
707
|
+
* @param contact Karin 会话对象。
|
|
708
|
+
* @param messageId 消息 ID 或 alias。
|
|
709
|
+
* @returns 用于 SQL 参数或内存 key 的字段数组。
|
|
710
|
+
*/
|
|
711
|
+
private contactKey;
|
|
712
|
+
/**
|
|
713
|
+
* 构造内存会话级 key。
|
|
714
|
+
*
|
|
715
|
+
* @param botId 当前 QQBot appId。
|
|
716
|
+
* @param contact Karin 会话对象。
|
|
717
|
+
* @param messageId 消息 ID 或 alias。
|
|
718
|
+
* @returns 内存 Map 使用的稳定 key。
|
|
719
|
+
*/
|
|
720
|
+
private scopedKey;
|
|
721
|
+
/**
|
|
722
|
+
* 构造内存全局 key。
|
|
723
|
+
*
|
|
724
|
+
* @param botId 当前 QQBot appId。
|
|
725
|
+
* @param messageId 消息 ID 或 alias。
|
|
726
|
+
* @returns 不限定会话时使用的内存 key。
|
|
727
|
+
*/
|
|
728
|
+
private globalKey;
|
|
729
|
+
/**
|
|
730
|
+
* 删除指定会话下的 alias。
|
|
731
|
+
*
|
|
732
|
+
* @param botId 当前 QQBot appId。
|
|
733
|
+
* @param contact Karin 会话对象。
|
|
734
|
+
* @param messageId 需要删除的 alias ID。
|
|
735
|
+
*/
|
|
736
|
+
private deleteAlias;
|
|
737
|
+
/**
|
|
738
|
+
* 判断某条消息是否包含指向指定 ID 的 reply 段。
|
|
739
|
+
*
|
|
740
|
+
* @param messageRef `qqbot_messages.id`。
|
|
741
|
+
* @param replyId 被引用的消息 ID。
|
|
742
|
+
* @returns true 表示该消息包含对应 reply 段。
|
|
743
|
+
*/
|
|
744
|
+
private hasReplyTo;
|
|
745
|
+
/**
|
|
746
|
+
* 判断消息是否已经超过本地缓存 TTL。
|
|
747
|
+
*
|
|
748
|
+
* @param row 带 `time` 字段的消息对象。
|
|
749
|
+
* @returns true 表示已经超过一天缓存窗口。
|
|
750
|
+
*/
|
|
751
|
+
private isExpired;
|
|
752
|
+
/**
|
|
753
|
+
* 到达清理间隔时清理内存和 SQLite 过期消息。
|
|
754
|
+
*/
|
|
755
|
+
private cleanupIfDue;
|
|
756
|
+
/**
|
|
757
|
+
* 清理一天前的缓存消息。
|
|
758
|
+
*
|
|
759
|
+
* SQLite 只删除主消息表,消息段和 alias 通过外键级联删除。
|
|
760
|
+
*/
|
|
761
|
+
private cleanup;
|
|
762
|
+
/**
|
|
763
|
+
* 判断 SQLite 表是否存在。
|
|
764
|
+
*
|
|
765
|
+
* @param name 表名。
|
|
766
|
+
* @returns true 表示表存在。
|
|
767
|
+
*/
|
|
768
|
+
private tableExists;
|
|
769
|
+
/**
|
|
770
|
+
* 读取 SQLite 表字段名。
|
|
771
|
+
*
|
|
772
|
+
* @param name 表名。
|
|
773
|
+
* @returns 字段名数组;表不存在时返回空数组。
|
|
774
|
+
*/
|
|
775
|
+
private tableColumns;
|
|
776
|
+
/**
|
|
777
|
+
* 读取 SQLite schema 版本。
|
|
778
|
+
*
|
|
779
|
+
* @returns 当前 `PRAGMA user_version`;新库默认为 0。
|
|
780
|
+
*/
|
|
781
|
+
private getUserVersion;
|
|
782
|
+
/**
|
|
783
|
+
* 写入 SQLite schema 版本。
|
|
784
|
+
*
|
|
785
|
+
* @param version 已成功应用的数据库版本号。
|
|
786
|
+
*/
|
|
787
|
+
private setUserVersion;
|
|
788
|
+
/**
|
|
789
|
+
* 在 `BEGIN IMMEDIATE` 事务中执行写操作。
|
|
790
|
+
*
|
|
791
|
+
* @param fn 事务内执行的异步函数。
|
|
792
|
+
*/
|
|
793
|
+
private transaction;
|
|
794
|
+
/**
|
|
795
|
+
* 执行不返回行的 SQL。
|
|
796
|
+
*
|
|
797
|
+
* @param sql SQL 字符串。
|
|
798
|
+
* @param params SQL 参数。
|
|
799
|
+
*/
|
|
800
|
+
private run;
|
|
801
|
+
/**
|
|
802
|
+
* 查询单行数据。
|
|
803
|
+
*
|
|
804
|
+
* @param sql SQL 字符串。
|
|
805
|
+
* @param params SQL 参数。
|
|
806
|
+
* @returns 查询结果;未命中时返回 undefined。
|
|
807
|
+
*/
|
|
808
|
+
private getRow;
|
|
809
|
+
/**
|
|
810
|
+
* 查询多行数据。
|
|
811
|
+
*
|
|
812
|
+
* @param sql SQL 字符串。
|
|
813
|
+
* @param params SQL 参数。
|
|
814
|
+
* @returns 查询结果数组。
|
|
815
|
+
*/
|
|
816
|
+
private getRows;
|
|
817
|
+
}
|
|
818
|
+
//#endregion
|
|
365
819
|
//#region src/types/config.d.ts
|
|
820
|
+
/**
|
|
821
|
+
* 自定义连接代理配置
|
|
822
|
+
*/
|
|
823
|
+
interface QQBotProxyConfig {
|
|
824
|
+
/** 正式环境 API */
|
|
825
|
+
prodApi: string;
|
|
826
|
+
/** 测试环境 API */
|
|
827
|
+
sandboxApi: string;
|
|
828
|
+
/** 调用凭证 API */
|
|
829
|
+
tokenApi: string;
|
|
830
|
+
/** 正式环境 WebSocket 网关 */
|
|
831
|
+
prodWs: string;
|
|
832
|
+
/** 测试环境 WebSocket 网关 */
|
|
833
|
+
sandboxWs: string;
|
|
834
|
+
}
|
|
366
835
|
/**
|
|
367
836
|
* 单个 QQBot 配置
|
|
368
837
|
*/
|
|
@@ -373,12 +842,8 @@ interface QQBotConfig {
|
|
|
373
842
|
appId: string;
|
|
374
843
|
/** 机器人密钥 */
|
|
375
844
|
secret: string;
|
|
376
|
-
/**
|
|
377
|
-
|
|
378
|
-
/** 测试环境 API */
|
|
379
|
-
sandboxApi: string;
|
|
380
|
-
/** 调用凭证 API */
|
|
381
|
-
tokenApi: string;
|
|
845
|
+
/** 自定义连接代理 */
|
|
846
|
+
proxy: QQBotProxyConfig;
|
|
382
847
|
/** 开启沙盒环境 */
|
|
383
848
|
sandbox: boolean;
|
|
384
849
|
/** 开启 QQ 场景能力(单聊 / 群聊) */
|
|
@@ -396,6 +861,15 @@ interface QQBotConfig {
|
|
|
396
861
|
keyboard: {
|
|
397
862
|
/** 是否将文本中的 URL 自动转换为 keyboard 按钮 */enable: boolean;
|
|
398
863
|
};
|
|
864
|
+
/** Markdown 通道 */
|
|
865
|
+
markdown: {
|
|
866
|
+
/** 是否启用 Markdown 通道发送消息 */enable: boolean;
|
|
867
|
+
};
|
|
868
|
+
/** 消息缓存配置 */
|
|
869
|
+
messageCache: {
|
|
870
|
+
/** 是否启用数据库消息缓存,用于 bot.getMsg */enable: boolean; /** 是否缓存机器人自己发送的消息 */
|
|
871
|
+
self: boolean;
|
|
872
|
+
};
|
|
399
873
|
/** 事件接收配置 */
|
|
400
874
|
event: {
|
|
401
875
|
/** 接收方式 0 关闭 / 1 webhook / 2 ws */type: 0 | 1 | 2;
|
|
@@ -412,11 +886,13 @@ interface QQBotConfig {
|
|
|
412
886
|
declare class AdapterQQBot extends AdapterBase implements AdapterType {
|
|
413
887
|
/** 与官方 API 交互 */
|
|
414
888
|
super: QQBotApi;
|
|
415
|
-
/** 当前 bot 配置 */
|
|
416
|
-
cfg: QQBotConfig;
|
|
417
889
|
/** openId / member_openid -> 昵称 缓存 */
|
|
418
890
|
nicknameCache: Map<string, string>;
|
|
419
891
|
constructor(cfg: QQBotConfig, api: QQBotApi);
|
|
892
|
+
/** 当前 bot 配置。真实配置存放在 WeakMap,避免被系统接口序列化整个适配器时带出。 */
|
|
893
|
+
get cfg(): QQBotConfig;
|
|
894
|
+
/** 接收消息的一天热缓存 + SQLite 缓存,用于实现 Karin 标准 `getMsg`。 */
|
|
895
|
+
get messageStore(): MessageStore;
|
|
420
896
|
logger(level: LogMethodNames, ...args: any[]): void;
|
|
421
897
|
/**
|
|
422
898
|
* 主消息回复入口(karin 调用)
|
|
@@ -428,6 +904,8 @@ declare class AdapterQQBot extends AdapterBase implements AdapterType {
|
|
|
428
904
|
sendMsg(contact: Contact, elements: Array<ElementTypes>, _retryCount?: number): Promise<SendMsgResults>;
|
|
429
905
|
/**
|
|
430
906
|
* 撤回消息
|
|
907
|
+
*
|
|
908
|
+
* 群聊中机器人被群主设为管理员后,也可撤回成员消息;平台仍限制消息发送后两分钟内。
|
|
431
909
|
*/
|
|
432
910
|
recallMsg(contact: Contact, messageId: string): Promise<void>;
|
|
433
911
|
/**
|
|
@@ -461,17 +939,25 @@ declare class AdapterQQBot extends AdapterBase implements AdapterType {
|
|
|
461
939
|
*/
|
|
462
940
|
getGroupAvatarUrl(_groupId: string, _size?: 0 | 40 | 100 | 140, _history?: number): Promise<string>;
|
|
463
941
|
/**
|
|
464
|
-
*
|
|
942
|
+
* 按消息 ID 获取已接收的消息。
|
|
943
|
+
*
|
|
944
|
+
* QQ 官方 Bot API 没有历史消息查询接口,因此仅从本地缓存读取。
|
|
945
|
+
* 消息在接收时已转换为 Karin elements,最长保留一天;引用消息的 `REFIDX`
|
|
946
|
+
* 索引同样可作为 `messageId` 查询。
|
|
947
|
+
*
|
|
948
|
+
* @param input OneBot v11 标准单参 messageId,或用于限定查询范围的 Contact。
|
|
949
|
+
* @param messageId 当 input 是 Contact 时传入的目标消息 ID。
|
|
950
|
+
* @returns 命中的消息;未命中时返回空消息结构。
|
|
465
951
|
*/
|
|
466
|
-
getMsg(
|
|
952
|
+
getMsg(input: Contact | string, messageId?: string): Promise<MessageResponse>;
|
|
467
953
|
/**
|
|
468
|
-
*
|
|
954
|
+
* 获取本地缓存历史消息(仅支持 message_id)
|
|
469
955
|
*/
|
|
470
|
-
getHistoryMsg(
|
|
956
|
+
getHistoryMsg(contact: Contact, startMsgId: string, count: number): Promise<Array<MessageResponse>>;
|
|
471
957
|
/**
|
|
472
|
-
*
|
|
958
|
+
* 获取本地缓存历史消息(message_seq 暂不支持)
|
|
473
959
|
*/
|
|
474
|
-
getHistoryMsg(
|
|
960
|
+
getHistoryMsg(contact: Contact, startMsgSeq: number, count: number): Promise<Array<MessageResponse>>;
|
|
475
961
|
/**
|
|
476
962
|
* 获取合并转发消息
|
|
477
963
|
*/
|
|
@@ -667,4 +1153,4 @@ declare class AdapterQQBot extends AdapterBase implements AdapterType {
|
|
|
667
1153
|
}>;
|
|
668
1154
|
}
|
|
669
1155
|
//#endregion
|
|
670
|
-
export {
|
|
1156
|
+
export type { AdapterQQBot };
|