koishi-plugin-echo-cave 1.14.0 → 1.14.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/lib/cave-helper.d.ts +2 -2
- package/lib/index.cjs +109 -90
- package/lib/media-helper.d.ts +1 -0
- package/package.json +1 -1
package/lib/cave-helper.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EchoCave } from './index';
|
|
1
|
+
import { Config, EchoCave } from './index';
|
|
2
2
|
import { Context, Session } from 'koishi';
|
|
3
|
-
export declare function sendCaveMsg(ctx: Context, session: Session, caveMsg: EchoCave): Promise<void>;
|
|
3
|
+
export declare function sendCaveMsg(ctx: Context, session: Session, caveMsg: EchoCave, cfg: Config): Promise<void>;
|
|
4
4
|
export declare function formatDate(date: Date): string;
|
package/lib/index.cjs
CHANGED
|
@@ -42,7 +42,7 @@ var require_zh_CN = __commonJS({
|
|
|
42
42
|
maxVideoSize: "\u6700\u5927\u89C6\u9891\u5927\u5C0F (MB)",
|
|
43
43
|
maxFileSize: "\u6700\u5927\u6587\u4EF6\u5927\u5C0F (MB)",
|
|
44
44
|
maxRecordSize: "\u6700\u5927\u5F55\u97F3\u5927\u5C0F (MB)",
|
|
45
|
-
useBase64ForMedia: "\u662F\u5426\u4F7F\
|
|
45
|
+
useBase64ForMedia: "\u662F\u5426\u4F7F\u7528 Base64 \u7F16\u7801\u53D1\u9001\u5A92\u4F53\u6587\u4EF6\uFF0C\u5F00\u542F\u540E\u5C06\u8BFB\u53D6 base64 \u7F16\u7801\u53D1\u9001\u800C\u4E0D\u662F\u4F7F\u7528 file uri"
|
|
46
46
|
},
|
|
47
47
|
"echo-cave": {
|
|
48
48
|
general: {
|
|
@@ -151,76 +151,6 @@ function createTextMsg(content) {
|
|
|
151
151
|
};
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
-
// src/onebot-helper.ts
|
|
155
|
-
async function getUserName(ctx, session, userId) {
|
|
156
|
-
try {
|
|
157
|
-
const memberInfo = await session.onebot.getGroupMemberInfo(session.channelId, userId);
|
|
158
|
-
return memberInfo.card || memberInfo.nickname || userId;
|
|
159
|
-
} catch (error) {
|
|
160
|
-
ctx.logger.warn(`\u83B7\u53D6\u7FA4\u6210\u5458\u4FE1\u606F\u5931\u8D25\uFF08userId: ${userId}\uFF09\uFF1A`, error);
|
|
161
|
-
return userId;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// src/cave-helper.ts
|
|
166
|
-
async function sendCaveMsg(ctx, session, caveMsg) {
|
|
167
|
-
const { channelId } = session;
|
|
168
|
-
const content = JSON.parse(caveMsg.content);
|
|
169
|
-
const date = formatDate(caveMsg.createTime);
|
|
170
|
-
const originName = await getUserName(ctx, session, caveMsg.originUserId);
|
|
171
|
-
const userName = await getUserName(ctx, session, caveMsg.userId);
|
|
172
|
-
const templateData = {
|
|
173
|
-
id: caveMsg.id.toString(),
|
|
174
|
-
date,
|
|
175
|
-
originName,
|
|
176
|
-
userName,
|
|
177
|
-
nl: "\n"
|
|
178
|
-
};
|
|
179
|
-
const TEMPLATE_COUNT = 5;
|
|
180
|
-
if (caveMsg.type === "forward") {
|
|
181
|
-
const availableTemplates2 = [];
|
|
182
|
-
for (let i = 0; i < TEMPLATE_COUNT; i++) {
|
|
183
|
-
const template = session.text(`echo-cave.templates.forward.${i}`, templateData);
|
|
184
|
-
if (template.trim() !== "") {
|
|
185
|
-
availableTemplates2.push(template);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
if (availableTemplates2.length === 0) {
|
|
189
|
-
await session.send(session.text("echo-cave.general.noTemplatesConfigured"));
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
const chosenTemplate2 = availableTemplates2[Math.floor(Math.random() * availableTemplates2.length)];
|
|
193
|
-
await session.onebot.sendGroupMsg(channelId, [createTextMsg(chosenTemplate2)]);
|
|
194
|
-
await session.onebot.sendGroupForwardMsg(channelId, content);
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
const availableTemplates = [];
|
|
198
|
-
for (let i = 0; i < TEMPLATE_COUNT; i++) {
|
|
199
|
-
const prefix = session.text(`echo-cave.templates.msg.${i}.prefix`, templateData);
|
|
200
|
-
const suffix = session.text(`echo-cave.templates.msg.${i}.suffix`, templateData);
|
|
201
|
-
if (prefix.trim() !== "" && suffix.trim() !== "") {
|
|
202
|
-
availableTemplates.push({ prefix, suffix });
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
if (availableTemplates.length === 0) {
|
|
206
|
-
await session.send(session.text("echo-cave.general.noTemplatesConfigured"));
|
|
207
|
-
return;
|
|
208
|
-
}
|
|
209
|
-
const chosenTemplate = availableTemplates[Math.floor(Math.random() * availableTemplates.length)];
|
|
210
|
-
const last = content.at(-1);
|
|
211
|
-
const needsNewline = last?.type === "text";
|
|
212
|
-
content.unshift(createTextMsg(chosenTemplate.prefix));
|
|
213
|
-
content.push(createTextMsg(`${needsNewline ? "\n\n" : ""}${chosenTemplate.suffix}`));
|
|
214
|
-
await session.onebot.sendGroupMsg(channelId, content);
|
|
215
|
-
}
|
|
216
|
-
function formatDate(date) {
|
|
217
|
-
return date.toLocaleDateString("zh-CN", {
|
|
218
|
-
year: "numeric",
|
|
219
|
-
month: "2-digit",
|
|
220
|
-
day: "2-digit"
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
|
|
224
154
|
// src/media-helper.ts
|
|
225
155
|
var import_axios = __toESM(require("axios"), 1);
|
|
226
156
|
var import_node_fs = require("node:fs");
|
|
@@ -287,9 +217,25 @@ async function processMediaElement(ctx, element, cfg) {
|
|
|
287
217
|
element.type,
|
|
288
218
|
cfg
|
|
289
219
|
);
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
220
|
+
const fileUri = `file:///${savedPath.replace(/\\/g, "/")}`;
|
|
221
|
+
return {
|
|
222
|
+
...element,
|
|
223
|
+
data: {
|
|
224
|
+
...element.data,
|
|
225
|
+
file: fileUri,
|
|
226
|
+
// Remove the url field
|
|
227
|
+
url: void 0
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
return element;
|
|
232
|
+
}
|
|
233
|
+
async function convertFileUriToBase64(ctx, element) {
|
|
234
|
+
if (element.type === "image" || element.type === "video" || element.type === "file" || element.type === "record") {
|
|
235
|
+
const fileUri = element.data.file;
|
|
236
|
+
const filePath = decodeURIComponent(fileUri.replace("file:///", ""));
|
|
237
|
+
try {
|
|
238
|
+
const buffer = await import_node_fs.promises.readFile(filePath);
|
|
293
239
|
const base64 = buffer.toString("base64");
|
|
294
240
|
const mimeTypes = {
|
|
295
241
|
image: "image/jpeg",
|
|
@@ -298,20 +244,18 @@ async function processMediaElement(ctx, element, cfg) {
|
|
|
298
244
|
file: "application/octet-stream"
|
|
299
245
|
};
|
|
300
246
|
const mimeType = mimeTypes[element.type] || "application/octet-stream";
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
247
|
+
const dataUrl = `data:${mimeType};base64,${base64}`;
|
|
248
|
+
return {
|
|
249
|
+
...element,
|
|
250
|
+
data: {
|
|
251
|
+
...element.data,
|
|
252
|
+
file: dataUrl
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
} catch (err) {
|
|
256
|
+
ctx.logger.error(`Failed to convert ${element.type} to base64: ${err}`);
|
|
257
|
+
return element;
|
|
305
258
|
}
|
|
306
|
-
return {
|
|
307
|
-
...element,
|
|
308
|
-
data: {
|
|
309
|
-
...element.data,
|
|
310
|
-
file: fileValue,
|
|
311
|
-
// Remove the url field
|
|
312
|
-
url: void 0
|
|
313
|
-
}
|
|
314
|
-
};
|
|
315
259
|
}
|
|
316
260
|
return element;
|
|
317
261
|
}
|
|
@@ -382,6 +326,81 @@ async function checkAndCleanMediaFiles(ctx, cfg, type) {
|
|
|
382
326
|
}
|
|
383
327
|
}
|
|
384
328
|
|
|
329
|
+
// src/onebot-helper.ts
|
|
330
|
+
async function getUserName(ctx, session, userId) {
|
|
331
|
+
try {
|
|
332
|
+
const memberInfo = await session.onebot.getGroupMemberInfo(session.channelId, userId);
|
|
333
|
+
return memberInfo.card || memberInfo.nickname || userId;
|
|
334
|
+
} catch (error) {
|
|
335
|
+
ctx.logger.warn(`\u83B7\u53D6\u7FA4\u6210\u5458\u4FE1\u606F\u5931\u8D25\uFF08userId: ${userId}\uFF09\uFF1A`, error);
|
|
336
|
+
return userId;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// src/cave-helper.ts
|
|
341
|
+
async function sendCaveMsg(ctx, session, caveMsg, cfg) {
|
|
342
|
+
const { channelId } = session;
|
|
343
|
+
let content = JSON.parse(caveMsg.content);
|
|
344
|
+
if (cfg.useBase64ForMedia) {
|
|
345
|
+
content = await Promise.all(
|
|
346
|
+
content.map(async (element) => await convertFileUriToBase64(ctx, element))
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
const date = formatDate(caveMsg.createTime);
|
|
350
|
+
const originName = await getUserName(ctx, session, caveMsg.originUserId);
|
|
351
|
+
const userName = await getUserName(ctx, session, caveMsg.userId);
|
|
352
|
+
const templateData = {
|
|
353
|
+
id: caveMsg.id.toString(),
|
|
354
|
+
date,
|
|
355
|
+
originName,
|
|
356
|
+
userName,
|
|
357
|
+
nl: "\n"
|
|
358
|
+
};
|
|
359
|
+
const TEMPLATE_COUNT = 5;
|
|
360
|
+
if (caveMsg.type === "forward") {
|
|
361
|
+
const availableTemplates2 = [];
|
|
362
|
+
for (let i = 0; i < TEMPLATE_COUNT; i++) {
|
|
363
|
+
const template = session.text(`echo-cave.templates.forward.${i}`, templateData);
|
|
364
|
+
if (template.trim() !== "") {
|
|
365
|
+
availableTemplates2.push(template);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
if (availableTemplates2.length === 0) {
|
|
369
|
+
await session.send(session.text("echo-cave.general.noTemplatesConfigured"));
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
const chosenTemplate2 = availableTemplates2[Math.floor(Math.random() * availableTemplates2.length)];
|
|
373
|
+
await session.onebot.sendGroupMsg(channelId, [createTextMsg(chosenTemplate2)]);
|
|
374
|
+
await session.onebot.sendGroupForwardMsg(channelId, content);
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const availableTemplates = [];
|
|
378
|
+
for (let i = 0; i < TEMPLATE_COUNT; i++) {
|
|
379
|
+
const prefix = session.text(`echo-cave.templates.msg.${i}.prefix`, templateData);
|
|
380
|
+
const suffix = session.text(`echo-cave.templates.msg.${i}.suffix`, templateData);
|
|
381
|
+
if (prefix.trim() !== "" && suffix.trim() !== "") {
|
|
382
|
+
availableTemplates.push({ prefix, suffix });
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
if (availableTemplates.length === 0) {
|
|
386
|
+
await session.send(session.text("echo-cave.general.noTemplatesConfigured"));
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
const chosenTemplate = availableTemplates[Math.floor(Math.random() * availableTemplates.length)];
|
|
390
|
+
const last = content.at(-1);
|
|
391
|
+
const needsNewline = last?.type === "text";
|
|
392
|
+
content.unshift(createTextMsg(chosenTemplate.prefix));
|
|
393
|
+
content.push(createTextMsg(`${needsNewline ? "\n\n" : ""}${chosenTemplate.suffix}`));
|
|
394
|
+
await session.onebot.sendGroupMsg(channelId, content);
|
|
395
|
+
}
|
|
396
|
+
function formatDate(date) {
|
|
397
|
+
return date.toLocaleDateString("zh-CN", {
|
|
398
|
+
year: "numeric",
|
|
399
|
+
month: "2-digit",
|
|
400
|
+
day: "2-digit"
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
|
|
385
404
|
// src/forward-helper.ts
|
|
386
405
|
async function reconstructForwardMsg(ctx, session, message, cfg) {
|
|
387
406
|
return Promise.all(
|
|
@@ -473,7 +492,7 @@ function apply(ctx, cfg) {
|
|
|
473
492
|
}
|
|
474
493
|
);
|
|
475
494
|
ctx.command("cave [id:number]").action(
|
|
476
|
-
async ({ session }, id) => await getCave(ctx, session, id)
|
|
495
|
+
async ({ session }, id) => await getCave(ctx, session, cfg, id)
|
|
477
496
|
);
|
|
478
497
|
ctx.command("cave.echo").action(async ({ session }) => await addCave(ctx, session, cfg));
|
|
479
498
|
ctx.command("cave.wipe <id:number>").action(
|
|
@@ -520,7 +539,7 @@ async function getCaveListByOriginUser(ctx, session) {
|
|
|
520
539
|
}
|
|
521
540
|
return response;
|
|
522
541
|
}
|
|
523
|
-
async function getCave(ctx, session, id) {
|
|
542
|
+
async function getCave(ctx, session, cfg, id) {
|
|
524
543
|
if (!session.guildId) {
|
|
525
544
|
return session.text("echo-cave.general.privateChatReminder");
|
|
526
545
|
}
|
|
@@ -544,7 +563,7 @@ async function getCave(ctx, session, id) {
|
|
|
544
563
|
}
|
|
545
564
|
caveMsg = caves[0];
|
|
546
565
|
}
|
|
547
|
-
await sendCaveMsg(ctx, session, caveMsg);
|
|
566
|
+
await sendCaveMsg(ctx, session, caveMsg, cfg);
|
|
548
567
|
}
|
|
549
568
|
async function deleteCave(ctx, session, cfg, id) {
|
|
550
569
|
if (!session.guildId) {
|
package/lib/media-helper.d.ts
CHANGED
|
@@ -2,4 +2,5 @@ import { Config } from './index';
|
|
|
2
2
|
import { Context } from 'koishi';
|
|
3
3
|
export declare function saveMedia(ctx: Context, mediaElement: Record<string, any>, type: 'image' | 'video' | 'file' | 'record', cfg: Config): Promise<string>;
|
|
4
4
|
export declare function processMediaElement(ctx: Context, element: any, cfg: Config): Promise<any>;
|
|
5
|
+
export declare function convertFileUriToBase64(ctx: Context, element: any): Promise<any>;
|
|
5
6
|
export declare function checkAndCleanMediaFiles(ctx: Context, cfg: Config, type: 'image' | 'video' | 'file' | 'record'): Promise<void>;
|