koishi-plugin-kkk 3.7.0 → 3.7.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/CHANGELOG.md +44 -0
- package/lib/build-metadata.json +5 -5
- package/lib/compat/imageMarkdown.js +280 -0
- package/lib/compat/node-karin.js +40 -9
- package/lib/karin/module/utils/Base.js +21 -4
- package/lib/karin/platform/bilibili/bilibili.js +15 -41
- package/lib/karin/platform/bilibili/interactive-story.js +46 -1
- package/lib/ktr/template/bilibili/interactive/components/interactive.js +99 -5
- package/package.json +1 -1
- package/scripts/probe-chart-wrap.cjs +33 -0
- package/scripts/smoke-interactive-story.cjs +27 -0
- package/scripts/smoke-md-image.cjs +206 -0
- package/src/compat/imageMarkdown.ts +263 -0
- package/src/compat/node-karin.ts +43 -9
- package/src/karin/module/utils/Base.ts +26 -4
- package/src/karin/platform/bilibili/bilibili.ts +20 -39
- package/src/karin/platform/bilibili/interactive-story.ts +44 -0
- package/src/ktr/template/bilibili/interactive/components/interactive.tsx +102 -12
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,49 @@
|
|
|
1
1
|
# 更新日志
|
|
2
2
|
|
|
3
|
+
## 3.7.2
|
|
4
|
+
|
|
5
|
+
> 群里发的图片**统一改走 markdown** 了 —— 普通图片消息会被 QQ 压得发糊,markdown 图片不会;
|
|
6
|
+
> 顺手把互动视频那张**剧情图的字放大**了。
|
|
7
|
+
|
|
8
|
+
### 图片不再被压糊
|
|
9
|
+
|
|
10
|
+
- 卡片、评论图、图集、推送里的图……以前都是当「普通图片」发出去的,QQ 会二次压缩,
|
|
11
|
+
卡片上的小字糊成一团。现在**图片一律按 markdown 图片发**:先传到机器人自己的图床,
|
|
12
|
+
再用带尺寸的图片写法发出去,字是清楚的。
|
|
13
|
+
- markdown 图片**必须写死尺寸**(``)—— 不写尺寸的话手机端根本不显示这张图,
|
|
14
|
+
所以每一张都会先把真实的宽高读出来(JPEG / PNG / GIF / WebP 都认)。
|
|
15
|
+
- **平台不一样,处理不一样**(客户端能力决定的,不是选择):
|
|
16
|
+
- QQ 官方机器人:走 markdown 图片;
|
|
17
|
+
- OneBot 系(NapCat、Lagrange、go-cqhttp、Chronocat 等):客户端不认 markdown,
|
|
18
|
+
收到只会显示成一串网址、图片全丢,所以这条链路**照旧发普通图片**;
|
|
19
|
+
- QQ 频道(qqguild)同理,也不改写。
|
|
20
|
+
- 一条消息里**同时有视频 / 语音 / 文件**时不改写:那种消息会按媒体发出去,markdown 内容会被丢掉。
|
|
21
|
+
- 转发里的图、合并转发收集中的图也不改写,保持原样。
|
|
22
|
+
- 传图床失败(或者宿主没装 assets 服务)时**照旧发普通图片**,不会因为改写不了就把图弄丢。
|
|
23
|
+
- 同一张图反复发(推送封面、面板卡片)不会再反复上传。
|
|
24
|
+
|
|
25
|
+
### 互动视频:剧情图的字大了
|
|
26
|
+
|
|
27
|
+
- 剧情图里每个选项 / 结局的**字号调大**了(大约 1/3),手机上看得清多了。
|
|
28
|
+
- 长文字不再按「14 个字」硬切,改成**按这一列还能放多少像素**来截断,
|
|
29
|
+
字大了也不会压到下一列的圆圈上;最后一列右边本来空着,会多显示几个字。
|
|
30
|
+
- 「已选」「图例」「提示」这些说明文字也跟着放大了一点。
|
|
31
|
+
|
|
32
|
+
## 3.7.1
|
|
33
|
+
|
|
34
|
+
> 修一个 3.7.0 改过头的地方:**视频直接发到群里的那种互动视频,照旧要在群里发带按钮的选择面板**;
|
|
35
|
+
> 而且点完选项之后,**上一段视频会撤回**,群里只留当前这一段。
|
|
36
|
+
|
|
37
|
+
### 互动视频:两种玩法各归各位
|
|
38
|
+
|
|
39
|
+
- **选了在线播放**(或者视频太大、自动转成在线播放):选项在网页播放器里贴出来,群里不再问一遍。
|
|
40
|
+
- **视频直接发到群里**(没有播放页):照旧在群里发**带按钮的选择面板** ——
|
|
41
|
+
QQ 官方机器人是按钮,其它平台是一行一个选项的文字,点一下接着播下一段。
|
|
42
|
+
- **点完选项会撤回上一段视频**:不然玩几段之后,群里会躺着一串视频。
|
|
43
|
+
顺序是先发新的、再撤旧的,中间不会出现「一段视频都没有」的空档;
|
|
44
|
+
选择面板照旧是发新的时候撤掉旧的。
|
|
45
|
+
- 剧情图(勾了「流程图」时)照常:第一段出一张,走到结局再出一张。
|
|
46
|
+
|
|
3
47
|
## 3.7.0
|
|
4
48
|
|
|
5
49
|
> 互动视频这一版基本重做了:**选剧情搬到了网页播放器里**(选项就贴在画面上、点完能看到下载进度),
|
package/lib/build-metadata.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "3.7.
|
|
3
|
-
"buildTime": "2026-09-
|
|
4
|
-
"buildTimestamp":
|
|
2
|
+
"version": "3.7.2",
|
|
3
|
+
"buildTime": "2026-09-26T02:01:03.950Z",
|
|
4
|
+
"buildTimestamp": 1790388063951,
|
|
5
5
|
"name": "koishi-plugin-kkk",
|
|
6
6
|
"description": "抖音 / B站 / 快手 / 小红书 视频解析与动态推送,带 QQ 交互面板与弹幕烧录(karin-plugin-kkk 的 Koishi 移植版)",
|
|
7
7
|
"homepage": "https://github.com/maimai993/koishi-plugin-kkk#readme",
|
|
8
|
-
"commitHash": "
|
|
9
|
-
"shortCommitHash": "
|
|
8
|
+
"commitHash": "41e817b5ec90f1b56475866f553b33c15a0b4e94",
|
|
9
|
+
"shortCommitHash": "41e817b"
|
|
10
10
|
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.imagesToMarkdown = exports.clearMarkdownImageCache = exports.markdownImageOf = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* 图片段 → markdown 图片(``)。
|
|
9
|
+
*
|
|
10
|
+
* 用户反馈两条,这里一起解决:
|
|
11
|
+
* 1. **「所有的图片发送都要走 md,不然太模糊了」** —— QQ 的普通图片消息(image 段 → 适配器按
|
|
12
|
+
* MEDIA 上传)会被客户端二次压缩,卡片上的小字糊成一团;markdown 里的图片不会被压,字是清楚的。
|
|
13
|
+
* 2. **「![#px大小#px大小]() 必须要 #px,不然手机不会显示」** —— md 图片必须写死尺寸,
|
|
14
|
+
* 不写尺寸手机端干脆不显示这张图。
|
|
15
|
+
*
|
|
16
|
+
* 所以这里在**发送出口**统一改写:compat/node-karin 的 reply / sendMsg / sendMaster 四条路径都过一遍,
|
|
17
|
+
* 业务代码照旧 e.reply(segment.image(...)),不用一处处改。
|
|
18
|
+
*
|
|
19
|
+
* 只在「认 markdown 的平台」上生效(官方 QQ:`qq`):
|
|
20
|
+
* - OneBot 系(NapCat / Lagrange / go-cqhttp / Chronocat…)收到 markdown 只显示成一串文字、
|
|
21
|
+
* 图片全丢(用户实测),所以那条链路继续发图片段;
|
|
22
|
+
* - QQ 频道(`qqguild`)的适配器没有 markdown 分支,md 元素会被当纯文本渲染 → 也排除。
|
|
23
|
+
*
|
|
24
|
+
* 另外三种情况**不动**(动了只会更糟):
|
|
25
|
+
* - 消息里还有视频 / 语音 / 文件:适配器遇到附件会把整条消息转成 MEDIA,markdown 内容会被丢掉;
|
|
26
|
+
* - 合并转发收集中的消息:转发节点里塞 markdown 客户端不认;
|
|
27
|
+
* - 上传拿不到公网地址(没装 assets 服务等)、或者**读不出图片尺寸**:保持原来的图片段。
|
|
28
|
+
* 读不出尺寸是可以退回普通图片的,但**绝不能发一张不带尺寸的 markdown 图片** ——
|
|
29
|
+
* 用户实测「必须要 #px,不然手机不会显示」,那种图上手机就是一片空白。
|
|
30
|
+
*/
|
|
31
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
32
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
33
|
+
const node_url_1 = require("node:url");
|
|
34
|
+
const logger_1 = require("./logger");
|
|
35
|
+
const runtime_1 = require("./runtime");
|
|
36
|
+
const segment_1 = require("./segment");
|
|
37
|
+
/** OneBot 系:不渲染 markdown(见文件头) */
|
|
38
|
+
const ONEBOT_LIKE = /onebot|napcat|lagrange|go-?cqhttp|chronocat|mirai/i;
|
|
39
|
+
/**
|
|
40
|
+
* 会渲染 markdown 图片的平台。
|
|
41
|
+
*
|
|
42
|
+
* 只认 `qq` 这一个平台名 —— QQ 的 markdown 是**官方机器人**能力,
|
|
43
|
+
* `qqguild`(频道)走的是另一套编码器,md 元素在那里会被当成纯文本原样发出去。
|
|
44
|
+
*/
|
|
45
|
+
const canUseMarkdownImage = (platform) => {
|
|
46
|
+
const name = String(platform || '').toLowerCase();
|
|
47
|
+
if (!name)
|
|
48
|
+
return false;
|
|
49
|
+
if (ONEBOT_LIKE.test(name))
|
|
50
|
+
return false;
|
|
51
|
+
if (name.startsWith('qqguild'))
|
|
52
|
+
return false;
|
|
53
|
+
return name === 'qq' || name.startsWith('qq-') || name.startsWith('qqbot');
|
|
54
|
+
};
|
|
55
|
+
/** 媒体段:md 和附件不能塞进同一条消息(会被适配器吃掉),所以要跳过整条改写 */
|
|
56
|
+
const ATTACHMENT_TYPES = new Set(['video', 'audio', 'record', 'file']);
|
|
57
|
+
const isImageElement = (element) => !!element && typeof element === 'object' && (element.type === 'img' || element.type === 'image');
|
|
58
|
+
const srcOf = (element) => {
|
|
59
|
+
const attrs = element?.attrs ?? {};
|
|
60
|
+
const src = attrs.src ?? attrs.url ?? element?.data?.file ?? element?.data?.url;
|
|
61
|
+
return typeof src === 'string' ? src : '';
|
|
62
|
+
};
|
|
63
|
+
/** 按二进制头认 mime(发给 assets 用;标错会让适配器按错格式处理) */
|
|
64
|
+
const mimeOfBuffer = (buffer) => {
|
|
65
|
+
if (buffer.length >= 8 && buffer[0] === 0x89 && buffer.toString('latin1', 1, 4) === 'PNG')
|
|
66
|
+
return 'image/png';
|
|
67
|
+
if (buffer.length >= 3 && buffer[0] === 0xff && buffer[1] === 0xd8)
|
|
68
|
+
return 'image/jpeg';
|
|
69
|
+
if (buffer.length >= 6 && buffer.toString('latin1', 0, 3) === 'GIF')
|
|
70
|
+
return 'image/gif';
|
|
71
|
+
if (buffer.length >= 12 && buffer.toString('latin1', 0, 4) === 'RIFF' && buffer.toString('latin1', 8, 12) === 'WEBP')
|
|
72
|
+
return 'image/webp';
|
|
73
|
+
return 'image/jpeg';
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* 读出图片的**真实像素尺寸** —— md 里的 `#宽px #高px` 就靠它。
|
|
77
|
+
*
|
|
78
|
+
* 不引第三方库:卡片是 JPEG、图集是 JPEG、二维码是 PNG、实况图封面是 WebP,四种头都自己认,
|
|
79
|
+
* 免得又出现「取不到尺寸 → 正方形拉伸」或「没写尺寸 → 手机不显示」。
|
|
80
|
+
*/
|
|
81
|
+
const readImageSize = (buffer) => {
|
|
82
|
+
// PNG:IHDR 就在固定偏移
|
|
83
|
+
if (buffer.length >= 24 && buffer[0] === 0x89 && buffer.toString('latin1', 1, 4) === 'PNG') {
|
|
84
|
+
return { width: buffer.readUInt32BE(16), height: buffer.readUInt32BE(20) };
|
|
85
|
+
}
|
|
86
|
+
// GIF:逻辑屏幕描述符,小端
|
|
87
|
+
if (buffer.length >= 10 && buffer.toString('latin1', 0, 3) === 'GIF') {
|
|
88
|
+
return { width: buffer.readUInt16LE(6), height: buffer.readUInt16LE(8) };
|
|
89
|
+
}
|
|
90
|
+
// JPEG:扫到 SOF 段(SOF0..SOF15,跳过 DHT/DAC 这些非 SOF 标记)
|
|
91
|
+
if (buffer.length > 4 && buffer[0] === 0xff && buffer[1] === 0xd8) {
|
|
92
|
+
let offset = 2;
|
|
93
|
+
while (offset + 9 < buffer.length) {
|
|
94
|
+
if (buffer[offset] !== 0xff) {
|
|
95
|
+
offset++;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const marker = buffer[offset + 1];
|
|
99
|
+
if (marker >= 0xc0 && marker <= 0xcf && marker !== 0xc4 && marker !== 0xc8 && marker !== 0xcc) {
|
|
100
|
+
return { height: buffer.readUInt16BE(offset + 5), width: buffer.readUInt16BE(offset + 7) };
|
|
101
|
+
}
|
|
102
|
+
const length = buffer.readUInt16BE(offset + 2);
|
|
103
|
+
if (length <= 0)
|
|
104
|
+
break;
|
|
105
|
+
offset += 2 + length;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
// WebP:VP8X(扩展)/ VP8L(无损)/ VP8(有损)三种头的写法都不一样
|
|
109
|
+
if (buffer.length >= 30 && buffer.toString('latin1', 0, 4) === 'RIFF' && buffer.toString('latin1', 8, 12) === 'WEBP') {
|
|
110
|
+
const format = buffer.toString('latin1', 12, 16);
|
|
111
|
+
if (format === 'VP8X') {
|
|
112
|
+
return { width: 1 + buffer.readUIntLE(24, 3), height: 1 + buffer.readUIntLE(27, 3) };
|
|
113
|
+
}
|
|
114
|
+
if (format === 'VP8L') {
|
|
115
|
+
const bits = buffer.readUInt32LE(21);
|
|
116
|
+
return { width: 1 + (bits & 0x3fff), height: 1 + ((bits >> 14) & 0x3fff) };
|
|
117
|
+
}
|
|
118
|
+
if (format === 'VP8 ') {
|
|
119
|
+
return { width: buffer.readUInt16LE(26) & 0x3fff, height: buffer.readUInt16LE(28) & 0x3fff };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return { width: 0, height: 0 };
|
|
123
|
+
};
|
|
124
|
+
/** 把各种写法读成 Buffer:data URI / base64:// / file:// / 本地路径 / http(s) */
|
|
125
|
+
const loadImageBuffer = async (src) => {
|
|
126
|
+
try {
|
|
127
|
+
if (src.startsWith('base64://'))
|
|
128
|
+
return Buffer.from(src.slice('base64://'.length), 'base64');
|
|
129
|
+
if (src.startsWith('data:')) {
|
|
130
|
+
const comma = src.indexOf(',');
|
|
131
|
+
if (comma < 0)
|
|
132
|
+
return null;
|
|
133
|
+
return Buffer.from(src.slice(comma + 1), 'base64');
|
|
134
|
+
}
|
|
135
|
+
const localPath = src.startsWith('file://') ? (0, node_url_1.fileURLToPath)(src) : src;
|
|
136
|
+
if (!/^https?:\/\//i.test(src)) {
|
|
137
|
+
if (node_fs_1.default.existsSync(localPath))
|
|
138
|
+
return node_fs_1.default.readFileSync(localPath);
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
const response = await fetch(src);
|
|
142
|
+
if (!response.ok) {
|
|
143
|
+
logger_1.logger.mark('[图片md] 下载失败 HTTP ' + response.status + ': ' + src.slice(0, 80));
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
return Buffer.from(await response.arrayBuffer());
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
logger_1.logger.mark('[图片md] 读取图片失败: ' + String(error?.message ?? error).slice(0, 120));
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* 上传到宿主的 assets,拿到 QQ 能直接取的 https 地址。
|
|
155
|
+
*
|
|
156
|
+
* md 里的图片地址**必须是公网 http(s)**:适配器只对 data: 图片做特殊处理
|
|
157
|
+
* (见 qq-crack 的 renderMarkdownDataImages —— data 图片会被它转回 MEDIA 上传,等于白改),
|
|
158
|
+
* 相对路径/本地路径手机端也取不到。
|
|
159
|
+
*/
|
|
160
|
+
let warnedNoAssets = false;
|
|
161
|
+
const uploadImage = async (buffer) => {
|
|
162
|
+
const assets = (0, runtime_1.tryGetRuntime)()?.ctx?.assets;
|
|
163
|
+
if (!assets?.upload) {
|
|
164
|
+
/** 没装 assets 服务就整条链路都用不了,提示一次就够,别每张图刷一遍日志 */
|
|
165
|
+
if (!warnedNoAssets) {
|
|
166
|
+
warnedNoAssets = true;
|
|
167
|
+
logger_1.logger.mark('[图片md] 宿主没有 assets 服务(拿不到公网图片地址),图片继续按普通图片段发送');
|
|
168
|
+
}
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
const mime = mimeOfBuffer(buffer);
|
|
172
|
+
const ext = mime === 'image/png' ? '.png' : mime === 'image/gif' ? '.gif' : mime === 'image/webp' ? '.webp' : '.jpg';
|
|
173
|
+
try {
|
|
174
|
+
const uploaded = await assets.upload('data:' + mime + ';base64,' + buffer.toString('base64'), 'kkk-md' + ext);
|
|
175
|
+
const url = typeof uploaded === 'string' ? uploaded : uploaded?.url;
|
|
176
|
+
return url && /^https?:\/\//i.test(String(url)) ? String(url) : null;
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
logger_1.logger.mark('[图片md] 上传失败: ' + String(error?.message ?? error).slice(0, 120));
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* 同一张图反复发(推送里的封面、面板卡片…)不用反复上传:
|
|
185
|
+
* 按图片内容摘要缓存结果,顺带省掉重复的下载与解码。
|
|
186
|
+
*/
|
|
187
|
+
const imageCache = new Map();
|
|
188
|
+
const IMAGE_CACHE_MAX = 200;
|
|
189
|
+
/** 图片段 → md 图片字符串;拿不到尺寸/地址就返回 null(调用方保持原样,别把图弄丢) */
|
|
190
|
+
const markdownImageOf = async (src) => {
|
|
191
|
+
if (!src)
|
|
192
|
+
return null;
|
|
193
|
+
const key = node_crypto_1.default.createHash('md5').update(src).digest('hex');
|
|
194
|
+
const cached = imageCache.get(key);
|
|
195
|
+
if (cached)
|
|
196
|
+
return cached;
|
|
197
|
+
const buffer = await loadImageBuffer(src);
|
|
198
|
+
let result = null;
|
|
199
|
+
if (buffer && buffer.length) {
|
|
200
|
+
const { width, height } = readImageSize(buffer);
|
|
201
|
+
/**
|
|
202
|
+
* **读不出尺寸就不换成 markdown**(退回普通图片段)。
|
|
203
|
+
*
|
|
204
|
+
* 用户实测:「必须要 #px,不然手机不会显示」—— 不带尺寸的 md 图片在手机端干脆是空白,
|
|
205
|
+
* 比糊更糟。所以这里宁可发一张(会被压糊但看得见的)普通图片,
|
|
206
|
+
* 也不发一张手机上什么都不显示的 markdown 图片。
|
|
207
|
+
*/
|
|
208
|
+
if (width > 0 && height > 0) {
|
|
209
|
+
const url = await uploadImage(buffer);
|
|
210
|
+
if (url)
|
|
211
|
+
result = '';
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* **只缓存成功的结果**:网络 / 图床抖动导致的失败要是也缓存下来,
|
|
216
|
+
* 这张图之后就一直按普通图片发(糊),得等重启才能恢复。
|
|
217
|
+
*/
|
|
218
|
+
if (result) {
|
|
219
|
+
if (imageCache.size >= IMAGE_CACHE_MAX)
|
|
220
|
+
imageCache.delete(imageCache.keys().next().value);
|
|
221
|
+
imageCache.set(key, result);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
logger_1.logger.debug('[图片md] 这张图这次没转成 markdown,按普通图片发送: ' + src.slice(0, 60));
|
|
225
|
+
}
|
|
226
|
+
return result;
|
|
227
|
+
};
|
|
228
|
+
exports.markdownImageOf = markdownImageOf;
|
|
229
|
+
/** 清空缓存(测试用) */
|
|
230
|
+
const clearMarkdownImageCache = () => { imageCache.clear(); };
|
|
231
|
+
exports.clearMarkdownImageCache = clearMarkdownImageCache;
|
|
232
|
+
/**
|
|
233
|
+
* 把一条消息里的图片段改写成 markdown 图片(其它段原样保留、顺序不变)。
|
|
234
|
+
*
|
|
235
|
+
* 连续的图片合成**一个** markdown 元素(换行分隔)—— 和官方 QQ 的渲染习惯一致:
|
|
236
|
+
* 连续图片紧贴显示,视觉上仍是一整张卡片,而不是一张一张孤立的图。
|
|
237
|
+
*
|
|
238
|
+
* @param elements 已归一化的消息元素数组
|
|
239
|
+
* @param platform 适配器平台名(kompat 层从 bot.platform / session.platform 取)
|
|
240
|
+
* @returns 改写后的数组;不该改或改不动时**原样返回**,绝不让消息发不出去
|
|
241
|
+
*/
|
|
242
|
+
const imagesToMarkdown = async (elements, platform) => {
|
|
243
|
+
const list = Array.isArray(elements) ? elements : [];
|
|
244
|
+
if (!list.length)
|
|
245
|
+
return list;
|
|
246
|
+
if (!canUseMarkdownImage(platform))
|
|
247
|
+
return list;
|
|
248
|
+
if (!list.some(isImageElement))
|
|
249
|
+
return list;
|
|
250
|
+
if (list.some((element) => ATTACHMENT_TYPES.has(String(element?.type ?? ''))))
|
|
251
|
+
return list;
|
|
252
|
+
const out = [];
|
|
253
|
+
/** 攒着的连续图片(遇到非图片段就冲出去) */
|
|
254
|
+
let run = [];
|
|
255
|
+
const flushRun = () => {
|
|
256
|
+
// 有图转不动(上传失败等)时 run 里会是空串占位,这里只冲有内容的
|
|
257
|
+
if (run.length)
|
|
258
|
+
out.push(segment_1.segment.markdown(run.join(String.fromCharCode(10))));
|
|
259
|
+
run = [];
|
|
260
|
+
};
|
|
261
|
+
for (const element of list) {
|
|
262
|
+
if (!isImageElement(element)) {
|
|
263
|
+
flushRun();
|
|
264
|
+
out.push(element);
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
const markdown = await (0, exports.markdownImageOf)(srcOf(element));
|
|
268
|
+
if (markdown) {
|
|
269
|
+
run.push(markdown);
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
// 转不了就把它自己按图片段发出去,别丢
|
|
273
|
+
flushRun();
|
|
274
|
+
out.push(element);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
flushRun();
|
|
278
|
+
return out;
|
|
279
|
+
};
|
|
280
|
+
exports.imagesToMarkdown = imagesToMarkdown;
|
package/lib/compat/node-karin.js
CHANGED
|
@@ -34,6 +34,7 @@ const adapter_info_1 = require("./adapter-info");
|
|
|
34
34
|
const logger_1 = require("./logger");
|
|
35
35
|
Object.defineProperty(exports, "logger", { enumerable: true, get: function () { return logger_1.logger; } });
|
|
36
36
|
const forward_collect_1 = require("./forward-collect");
|
|
37
|
+
const imageMarkdown_1 = require("./imageMarkdown");
|
|
37
38
|
const sendError_1 = require("./sendError");
|
|
38
39
|
const runtime_1 = require("./runtime");
|
|
39
40
|
const segment_1 = require("./segment");
|
|
@@ -240,14 +241,16 @@ class KkkBot {
|
|
|
240
241
|
logger_1.logger.debug('当前适配器(' + this.bot.platform + ')不支持合并转发,改为直接发送内容');
|
|
241
242
|
}
|
|
242
243
|
// 退化路径:整条发一次;失败再逐个元素发,尽量把内容送出去
|
|
244
|
+
/** 退化发送 = 直接发消息,所以图片同样要改走 markdown(见 compat/imageMarkdown) */
|
|
245
|
+
const fallback = await (0, imageMarkdown_1.imagesToMarkdown)(payload.elements, platformOfBot(this.bot));
|
|
243
246
|
try {
|
|
244
|
-
const ids = await this.bot.sendMessage(channelId,
|
|
247
|
+
const ids = await this.bot.sendMessage(channelId, fallback);
|
|
245
248
|
return { messageId: ids[ids.length - 1] ?? '' };
|
|
246
249
|
}
|
|
247
250
|
catch (error) {
|
|
248
251
|
logger_1.logger.warn('整条发送失败,改为逐个元素发送: ' + String(error?.message ?? error));
|
|
249
252
|
let lastId = '';
|
|
250
|
-
for (const item of
|
|
253
|
+
for (const item of fallback) {
|
|
251
254
|
const ids = await this.bot.sendMessage(channelId, [item]);
|
|
252
255
|
lastId = ids[ids.length - 1] ?? lastId;
|
|
253
256
|
}
|
|
@@ -346,11 +349,14 @@ class KkkBot {
|
|
|
346
349
|
}
|
|
347
350
|
/** 发送消息(karin 的 bot.sendMsg) */
|
|
348
351
|
async sendMsg(contact, content, _options) {
|
|
352
|
+
const elements = normalizeContent(content);
|
|
349
353
|
/** 解析结果合并转发:正在收集就攒起来(见 compat/forward-collect) */
|
|
350
|
-
if ((0, forward_collect_1.collectForward)(peerOf(contact),
|
|
354
|
+
if ((0, forward_collect_1.collectForward)(peerOf(contact), elements))
|
|
351
355
|
return { messageId: forward_collect_1.COLLECTED_MESSAGE_ID };
|
|
352
356
|
const channelId = typeof contact === 'string' ? contact : contact.peer;
|
|
353
|
-
|
|
357
|
+
/** 图片统一改走 markdown(见 compat/imageMarkdown) */
|
|
358
|
+
const outgoing = await (0, imageMarkdown_1.imagesToMarkdown)(elements, platformOfBot(this.bot));
|
|
359
|
+
const ids = await this.bot.sendMessage(channelId, outgoing);
|
|
354
360
|
// 同 reply/uploadFile:**没拿到消息 ID 就是没发出去**,别当成功返回
|
|
355
361
|
const id = ids?.[ids.length - 1] ?? '';
|
|
356
362
|
if (!id) {
|
|
@@ -471,13 +477,21 @@ class Message {
|
|
|
471
477
|
* 这时自动改用**主动消息**(不带引用,直接往频道里发),失败才把原错误抛出去。
|
|
472
478
|
*/
|
|
473
479
|
async reply(content, _options) {
|
|
474
|
-
const
|
|
480
|
+
const raw = normalizeContent(content);
|
|
475
481
|
/**
|
|
476
482
|
* 解析结果合并转发:正在收集就攒起来,等解析结束发一条转发(见 compat/forward-collect)。
|
|
477
483
|
* 回一个假的消息 ID:调用方普遍只看「有没有拿到 ID」(例如 uploadFile 判断发送成没成功)。
|
|
478
484
|
*/
|
|
479
|
-
if ((0, forward_collect_1.collectForward)(this.contact?.peer ?? '',
|
|
485
|
+
if ((0, forward_collect_1.collectForward)(this.contact?.peer ?? '', raw))
|
|
480
486
|
return { messageId: forward_collect_1.COLLECTED_MESSAGE_ID };
|
|
487
|
+
/**
|
|
488
|
+
* **图片统一改走 markdown**(上传拿公网地址 + 写死 `#宽px #高px`),见 compat/imageMarkdown。
|
|
489
|
+
*
|
|
490
|
+
* 放在这里是因为它是**所有回复的唯一出口** —— 业务代码一律 `e.reply(...)`,
|
|
491
|
+
* 不用一处处改业务代码。转换不成功会退回原来的图片段(绝不把图弄丢)。
|
|
492
|
+
* 上面的合并转发分支已经返回了,所以转发节点里仍然是普通图片段。
|
|
493
|
+
*/
|
|
494
|
+
const elements = await (0, imageMarkdown_1.imagesToMarkdown)(raw, platformOfMessage(this));
|
|
481
495
|
/**
|
|
482
496
|
* 没拿到消息 ID 就是**没发出去**(对齐 qq-chat 的判法)。
|
|
483
497
|
*
|
|
@@ -570,6 +584,19 @@ function normalizeContent(content) {
|
|
|
570
584
|
function peerOf(contact) {
|
|
571
585
|
return typeof contact === 'string' ? contact : String(contact?.peer ?? '');
|
|
572
586
|
}
|
|
587
|
+
/**
|
|
588
|
+
* 适配器平台名。
|
|
589
|
+
*
|
|
590
|
+
* 发送出口要用它判断「这个平台认不认 markdown 图片」(见 compat/imageMarkdown):
|
|
591
|
+
* KkkBot 包装上直接转发真实 Bot 的 platform,传 KkkBot 或裸 Bot 都能取到。
|
|
592
|
+
*/
|
|
593
|
+
function platformOfBot(bot) {
|
|
594
|
+
return String(bot?.platform ?? bot?.bot?.platform ?? '');
|
|
595
|
+
}
|
|
596
|
+
/** 同上,从消息事件取(会话上就带 platform,拿不到再问 bot) */
|
|
597
|
+
function platformOfMessage(message) {
|
|
598
|
+
return String(message.session?.platform ?? platformOfBot(message.bot));
|
|
599
|
+
}
|
|
573
600
|
/**
|
|
574
601
|
* 合并转发的载荷。
|
|
575
602
|
*
|
|
@@ -707,18 +734,22 @@ async function sendMsg(selfId, contact, content, _options) {
|
|
|
707
734
|
+ '),这条消息发不出去。等它重新上线后下次推送会正常发出;如果是长期不在线,检查一下适配器连接');
|
|
708
735
|
}
|
|
709
736
|
const channelId = peerOf(contact);
|
|
737
|
+
const elements = normalizeContent(content);
|
|
710
738
|
/** 解析结果合并转发:这条也走漏斗(karin.sendMsg 是业务代码另一条常用出口) */
|
|
711
|
-
if ((0, forward_collect_1.collectForward)(channelId,
|
|
739
|
+
if ((0, forward_collect_1.collectForward)(channelId, elements)) {
|
|
712
740
|
return { messageId: forward_collect_1.COLLECTED_MESSAGE_ID, rawData: [] };
|
|
713
741
|
}
|
|
714
|
-
|
|
742
|
+
/** 图片统一改走 markdown(见 compat/imageMarkdown) */
|
|
743
|
+
const outgoing = await (0, imageMarkdown_1.imagesToMarkdown)(elements, platformOfBot(bot.bot));
|
|
744
|
+
const ids = await bot.bot.sendMessage(channelId, outgoing);
|
|
715
745
|
return { messageId: ids[ids.length - 1] ?? '', rawData: ids };
|
|
716
746
|
}
|
|
717
747
|
async function sendMaster(botId, master, content, _options) {
|
|
718
748
|
const bot = resolveBot(botId);
|
|
719
749
|
if (!bot)
|
|
720
750
|
throw new Error('[kkk] 没有可用的机器人实例,无法发送消息');
|
|
721
|
-
|
|
751
|
+
// 图片统一改走 markdown(见 compat/imageMarkdown):私聊和群里一样,md 图片才不会被压糊
|
|
752
|
+
const elements = await (0, imageMarkdown_1.imagesToMarkdown)(normalizeContent(content), platformOfBot(bot.bot));
|
|
722
753
|
const target = bot.bot;
|
|
723
754
|
// Satori 的 Bot 有 sendPrivateMessage;个别适配器没有,退化成私聊频道 id
|
|
724
755
|
const ids = typeof target.sendPrivateMessage === 'function'
|
|
@@ -51,6 +51,19 @@ const runtime_1 = require("../../../compat/runtime");
|
|
|
51
51
|
const player_1 = require("../../../player");
|
|
52
52
|
// 解析阶段(「下载进度」指令读的就是这里登记的状态)
|
|
53
53
|
const Downloader_1 = require("./Network/Downloader");
|
|
54
|
+
/** 发送结果里把消息 ID 抠出来(不同适配器返回的形状不一样:对象带 messageId、或者就是字符串数组) */
|
|
55
|
+
const reportSent = (options, sent) => {
|
|
56
|
+
if (!options?.onSent)
|
|
57
|
+
return;
|
|
58
|
+
try {
|
|
59
|
+
const id = typeof sent === 'string'
|
|
60
|
+
? sent
|
|
61
|
+
: (sent?.messageId ?? (Array.isArray(sent) ? sent[0] : ''));
|
|
62
|
+
if (id)
|
|
63
|
+
options.onSent(String(id));
|
|
64
|
+
}
|
|
65
|
+
catch { /* 拿不到 ID 不影响发送本身 */ }
|
|
66
|
+
};
|
|
54
67
|
/**
|
|
55
68
|
* 基础基类
|
|
56
69
|
* @remarks 提供事件上下文与通用请求头,继承自 `AmagiBase` 以复用 Amagi 客户端。
|
|
@@ -259,13 +272,15 @@ const uploadFile = async (event, file, videoUrl, options) => {
|
|
|
259
272
|
// 是群文件
|
|
260
273
|
const bot = node_karin_1.default.getBot(String(options.activeOption?.uin));
|
|
261
274
|
node_karin_1.logger.mark(`${node_karin_1.logger.blue('主动消息:')} 视频大小: ${newFileSize.toFixed(1)}MB 正在通过${node_karin_1.logger.yellow('bot.uploadFile')}回复...`);
|
|
262
|
-
await bot.uploadFile(contact, File, file.originTitle ? `${file.originTitle}.mp4` : `${File.split('/').pop()}`);
|
|
275
|
+
const sent = await bot.uploadFile(contact, File, file.originTitle ? `${file.originTitle}.mp4` : `${File.split('/').pop()}`);
|
|
276
|
+
reportSent(options, sent);
|
|
263
277
|
}
|
|
264
278
|
else {
|
|
265
279
|
// 不是群文件
|
|
266
280
|
node_karin_1.logger.mark(`${node_karin_1.logger.blue('主动消息:')} 视频大小: ${newFileSize.toFixed(1)}MB 正在通过${node_karin_1.logger.yellow('karin.sendMsg')}回复...`);
|
|
267
281
|
// 兼容层已经把「没拿到消息 ID」当成失败抛出(见 compat/sendError),所以这里没有异常就是发出去了
|
|
268
|
-
await node_karin_1.default.sendMsg(selfId, contact, [node_karin_1.segment.video(File)], { retryCount: 0 });
|
|
282
|
+
const sent = await node_karin_1.default.sendMsg(selfId, contact, [node_karin_1.segment.video(File)], { retryCount: 0 });
|
|
283
|
+
reportSent(options, sent);
|
|
269
284
|
sendStatus = true;
|
|
270
285
|
}
|
|
271
286
|
}
|
|
@@ -274,13 +289,15 @@ const uploadFile = async (event, file, videoUrl, options) => {
|
|
|
274
289
|
if (useGroupFile) {
|
|
275
290
|
// 是文件
|
|
276
291
|
node_karin_1.logger.mark(`${node_karin_1.logger.blue('被动消息:')} 视频大小: ${newFileSize.toFixed(1)}MB 正在通过${node_karin_1.logger.yellow('e.bot.uploadFile')}回复...`);
|
|
277
|
-
await event.bot.uploadFile(event.contact, File, file.originTitle ? `${file.originTitle}.mp4` : `${File.split('/').pop()}`);
|
|
292
|
+
const sent = await event.bot.uploadFile(event.contact, File, file.originTitle ? `${file.originTitle}.mp4` : `${File.split('/').pop()}`);
|
|
293
|
+
reportSent(options, sent);
|
|
278
294
|
}
|
|
279
295
|
else {
|
|
280
296
|
// 不是文件
|
|
281
297
|
node_karin_1.logger.mark(`${node_karin_1.logger.blue('被动消息:')} 视频大小: ${newFileSize.toFixed(1)}MB 正在通过${node_karin_1.logger.yellow('e.reply')}回复...`);
|
|
282
298
|
// 同上:异常才是失败(没拿到消息 ID 已经由兼容层抛出来了)
|
|
283
|
-
await event.reply(node_karin_1.segment.video(File) || videoUrl);
|
|
299
|
+
const sent = await event.reply(node_karin_1.segment.video(File) || videoUrl);
|
|
300
|
+
reportSent(options, sent);
|
|
284
301
|
sendStatus = true;
|
|
285
302
|
}
|
|
286
303
|
}
|
|
@@ -588,15 +588,14 @@ class Bilibili extends utils_1.Base {
|
|
|
588
588
|
await downloadTask;
|
|
589
589
|
await this.sendPreparedVideo();
|
|
590
590
|
/**
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
* 用户至少能看到这张图里有几条分支。
|
|
591
|
+
* 互动视频的剧情怎么走,看这次的视频发到哪儿:
|
|
592
|
+
* · **走在线播放**(视频没发进群里,用户在看播放页)→ 选项在播放页上贴出来,
|
|
593
|
+
* 群里不再问一遍,免得两处各问一次(见 sendPreparedVideo 里的 story);
|
|
594
|
+
* · **直接发到群里**(没有播放页)→ 照旧在群里发**带按钮的选择面板**,
|
|
595
|
+
* 用户点一下播下一段,并撤回上一段视频(用户要求)。
|
|
597
596
|
*/
|
|
598
597
|
if (this.interactive && !this.publishedStory)
|
|
599
|
-
void this.
|
|
598
|
+
void this.startInteractiveStory();
|
|
600
599
|
});
|
|
601
600
|
}
|
|
602
601
|
}
|
|
@@ -1720,38 +1719,6 @@ class Bilibili extends utils_1.Base {
|
|
|
1720
1719
|
}
|
|
1721
1720
|
return this.preparedVideo !== null;
|
|
1722
1721
|
}
|
|
1723
|
-
/**
|
|
1724
|
-
* 互动视频发到群里时:只补一张**剧情图**,不问选项(用户要求:群里不要选择按钮)。
|
|
1725
|
-
*
|
|
1726
|
-
* 选项流程在播放页里(见 sendPreparedVideo 的 story);这里只把「有几条分支」画出来。
|
|
1727
|
-
* 失败只记日志 —— 出不了图不影响视频已经发出去这件事。
|
|
1728
|
-
*/
|
|
1729
|
-
async sendInteractiveChartOnly() {
|
|
1730
|
-
const interactive = this.interactive;
|
|
1731
|
-
if (!interactive)
|
|
1732
|
-
return;
|
|
1733
|
-
/** 没勾「流程图」就什么都不做(配置说了算) */
|
|
1734
|
-
if (!Config_1.Config.bilibili.sendContent.some((item) => item === 'chart'))
|
|
1735
|
-
return;
|
|
1736
|
-
try {
|
|
1737
|
-
const info = await (0, InteractiveVideo_1.fetchInteractiveInfo)({ bvid: interactive.bvid, cid: interactive.cid, headers: this.headers });
|
|
1738
|
-
if (!info)
|
|
1739
|
-
return;
|
|
1740
|
-
await (0, interactive_story_1.renderInteractiveChart)({
|
|
1741
|
-
e: this.e,
|
|
1742
|
-
bvid: interactive.bvid,
|
|
1743
|
-
graphVersion: info.graphVersion,
|
|
1744
|
-
cid: interactive.cid,
|
|
1745
|
-
title: this.workInfo?.title ?? '互动视频',
|
|
1746
|
-
notice: info.notice,
|
|
1747
|
-
headers: this.headers,
|
|
1748
|
-
full: true
|
|
1749
|
-
});
|
|
1750
|
-
}
|
|
1751
|
-
catch (error) {
|
|
1752
|
-
node_karin_1.logger.debug('[互动视频] 群里补剧情图失败(不影响视频): ' + String(error?.message ?? error));
|
|
1753
|
-
}
|
|
1754
|
-
}
|
|
1755
1722
|
/**
|
|
1756
1723
|
* 起一次互动剧情(后台长跑,不阻塞解析主流程)。
|
|
1757
1724
|
*
|
|
@@ -1923,11 +1890,18 @@ class Bilibili extends utils_1.Base {
|
|
|
1923
1890
|
}
|
|
1924
1891
|
node_karin_1.logger.warn('[在线播放] 播放会话登记失败,退回直接发送视频文件');
|
|
1925
1892
|
}
|
|
1893
|
+
/**
|
|
1894
|
+
* 互动视频(发到群里的那种):把这条视频的消息 ID 记下来 ——
|
|
1895
|
+
* 用户点了下一个选项之后,这条要撤回,群里只留当前这一段。
|
|
1896
|
+
*/
|
|
1897
|
+
const sentOptions = this.interactive
|
|
1898
|
+
? { onSent: (messageId) => (0, interactive_story_1.rememberStoryVideo)(this.e, messageId) }
|
|
1899
|
+
: undefined;
|
|
1926
1900
|
if (totalBytes > Config_1.Config.app.groupfilevalue) {
|
|
1927
|
-
await (0, utils_1.uploadFile)(this.e, { filepath, totalBytes, originTitle }, videoUrl ?? '', { useGroupFile: true });
|
|
1901
|
+
await (0, utils_1.uploadFile)(this.e, { filepath, totalBytes, originTitle }, videoUrl ?? '', { useGroupFile: true, ...sentOptions });
|
|
1928
1902
|
}
|
|
1929
1903
|
else {
|
|
1930
|
-
await (0, utils_1.uploadFile)(this.e, { filepath, totalBytes, originTitle }, videoUrl ?? '');
|
|
1904
|
+
await (0, utils_1.uploadFile)(this.e, { filepath, totalBytes, originTitle }, videoUrl ?? '', sentOptions);
|
|
1931
1905
|
}
|
|
1932
1906
|
return true;
|
|
1933
1907
|
}
|