koishi-plugin-echo-cave 1.13.0 → 1.14.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/lib/cave-helper.d.ts +2 -2
- package/lib/index.cjs +111 -75
- package/lib/index.d.ts +1 -0
- 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
|
@@ -41,7 +41,8 @@ var require_zh_CN = __commonJS({
|
|
|
41
41
|
maxImageSize: "\u6700\u5927\u56FE\u7247\u5927\u5C0F (MB)",
|
|
42
42
|
maxVideoSize: "\u6700\u5927\u89C6\u9891\u5927\u5C0F (MB)",
|
|
43
43
|
maxFileSize: "\u6700\u5927\u6587\u4EF6\u5927\u5C0F (MB)",
|
|
44
|
-
maxRecordSize: "\u6700\u5927\u5F55\u97F3\u5927\u5C0F (MB)"
|
|
44
|
+
maxRecordSize: "\u6700\u5927\u5F55\u97F3\u5927\u5C0F (MB)",
|
|
45
|
+
useBase64ForMedia: "\u662F\u5426\u4F7F\u7528Base64\u7F16\u7801\u4FDD\u5B58\u5A92\u4F53\u6587\u4EF6\uFF0C\u5F00\u542F\u540E\u5C06\u76F4\u63A5\u5B58\u50A8\u6587\u4EF6\u5185\u5BB9\u800C\u975E\u6587\u4EF6\u8DEF\u5F84"
|
|
45
46
|
},
|
|
46
47
|
"echo-cave": {
|
|
47
48
|
general: {
|
|
@@ -150,76 +151,6 @@ function createTextMsg(content) {
|
|
|
150
151
|
};
|
|
151
152
|
}
|
|
152
153
|
|
|
153
|
-
// src/onebot-helper.ts
|
|
154
|
-
async function getUserName(ctx, session, userId) {
|
|
155
|
-
try {
|
|
156
|
-
const memberInfo = await session.onebot.getGroupMemberInfo(session.channelId, userId);
|
|
157
|
-
return memberInfo.card || memberInfo.nickname || userId;
|
|
158
|
-
} catch (error) {
|
|
159
|
-
ctx.logger.warn(`\u83B7\u53D6\u7FA4\u6210\u5458\u4FE1\u606F\u5931\u8D25\uFF08userId: ${userId}\uFF09\uFF1A`, error);
|
|
160
|
-
return userId;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// src/cave-helper.ts
|
|
165
|
-
async function sendCaveMsg(ctx, session, caveMsg) {
|
|
166
|
-
const { channelId } = session;
|
|
167
|
-
const content = JSON.parse(caveMsg.content);
|
|
168
|
-
const date = formatDate(caveMsg.createTime);
|
|
169
|
-
const originName = await getUserName(ctx, session, caveMsg.originUserId);
|
|
170
|
-
const userName = await getUserName(ctx, session, caveMsg.userId);
|
|
171
|
-
const templateData = {
|
|
172
|
-
id: caveMsg.id.toString(),
|
|
173
|
-
date,
|
|
174
|
-
originName,
|
|
175
|
-
userName,
|
|
176
|
-
nl: "\n"
|
|
177
|
-
};
|
|
178
|
-
const TEMPLATE_COUNT = 5;
|
|
179
|
-
if (caveMsg.type === "forward") {
|
|
180
|
-
const availableTemplates2 = [];
|
|
181
|
-
for (let i = 0; i < TEMPLATE_COUNT; i++) {
|
|
182
|
-
const template = session.text(`echo-cave.templates.forward.${i}`, templateData);
|
|
183
|
-
if (template.trim() !== "") {
|
|
184
|
-
availableTemplates2.push(template);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
if (availableTemplates2.length === 0) {
|
|
188
|
-
await session.send(session.text("echo-cave.general.noTemplatesConfigured"));
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
const chosenTemplate2 = availableTemplates2[Math.floor(Math.random() * availableTemplates2.length)];
|
|
192
|
-
await session.onebot.sendGroupMsg(channelId, [createTextMsg(chosenTemplate2)]);
|
|
193
|
-
await session.onebot.sendGroupForwardMsg(channelId, content);
|
|
194
|
-
return;
|
|
195
|
-
}
|
|
196
|
-
const availableTemplates = [];
|
|
197
|
-
for (let i = 0; i < TEMPLATE_COUNT; i++) {
|
|
198
|
-
const prefix = session.text(`echo-cave.templates.msg.${i}.prefix`, templateData);
|
|
199
|
-
const suffix = session.text(`echo-cave.templates.msg.${i}.suffix`, templateData);
|
|
200
|
-
if (prefix.trim() !== "" && suffix.trim() !== "") {
|
|
201
|
-
availableTemplates.push({ prefix, suffix });
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
if (availableTemplates.length === 0) {
|
|
205
|
-
await session.send(session.text("echo-cave.general.noTemplatesConfigured"));
|
|
206
|
-
return;
|
|
207
|
-
}
|
|
208
|
-
const chosenTemplate = availableTemplates[Math.floor(Math.random() * availableTemplates.length)];
|
|
209
|
-
const last = content.at(-1);
|
|
210
|
-
const needsNewline = last?.type === "text";
|
|
211
|
-
content.unshift(createTextMsg(chosenTemplate.prefix));
|
|
212
|
-
content.push(createTextMsg(`${needsNewline ? "\n\n" : ""}${chosenTemplate.suffix}`));
|
|
213
|
-
await session.onebot.sendGroupMsg(channelId, content);
|
|
214
|
-
}
|
|
215
|
-
function formatDate(date) {
|
|
216
|
-
return date.toLocaleDateString("zh-CN", {
|
|
217
|
-
year: "numeric",
|
|
218
|
-
month: "2-digit",
|
|
219
|
-
day: "2-digit"
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
|
|
223
154
|
// src/media-helper.ts
|
|
224
155
|
var import_axios = __toESM(require("axios"), 1);
|
|
225
156
|
var import_node_fs = require("node:fs");
|
|
@@ -299,6 +230,35 @@ async function processMediaElement(ctx, element, cfg) {
|
|
|
299
230
|
}
|
|
300
231
|
return element;
|
|
301
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);
|
|
239
|
+
const base64 = buffer.toString("base64");
|
|
240
|
+
const mimeTypes = {
|
|
241
|
+
image: "image/jpeg",
|
|
242
|
+
video: "video/mp4",
|
|
243
|
+
record: "audio/mpeg",
|
|
244
|
+
file: "application/octet-stream"
|
|
245
|
+
};
|
|
246
|
+
const mimeType = mimeTypes[element.type] || "application/octet-stream";
|
|
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;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return element;
|
|
261
|
+
}
|
|
302
262
|
async function checkAndCleanMediaFiles(ctx, cfg, type) {
|
|
303
263
|
if (!cfg.enableSizeLimit) {
|
|
304
264
|
return;
|
|
@@ -366,6 +326,81 @@ async function checkAndCleanMediaFiles(ctx, cfg, type) {
|
|
|
366
326
|
}
|
|
367
327
|
}
|
|
368
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
|
+
|
|
369
404
|
// src/forward-helper.ts
|
|
370
405
|
async function reconstructForwardMsg(ctx, session, message, cfg) {
|
|
371
406
|
return Promise.all(
|
|
@@ -433,7 +468,8 @@ var Config = import_koishi.Schema.object({
|
|
|
433
468
|
maxImageSize: import_koishi.Schema.number().default(2048),
|
|
434
469
|
maxVideoSize: import_koishi.Schema.number().default(512),
|
|
435
470
|
maxFileSize: import_koishi.Schema.number().default(512),
|
|
436
|
-
maxRecordSize: import_koishi.Schema.number().default(512)
|
|
471
|
+
maxRecordSize: import_koishi.Schema.number().default(512),
|
|
472
|
+
useBase64ForMedia: import_koishi.Schema.boolean().default(false)
|
|
437
473
|
}).i18n({
|
|
438
474
|
"zh-CN": require_zh_CN()._config
|
|
439
475
|
});
|
|
@@ -456,7 +492,7 @@ function apply(ctx, cfg) {
|
|
|
456
492
|
}
|
|
457
493
|
);
|
|
458
494
|
ctx.command("cave [id:number]").action(
|
|
459
|
-
async ({ session }, id) => await getCave(ctx, session, id)
|
|
495
|
+
async ({ session }, id) => await getCave(ctx, session, cfg, id)
|
|
460
496
|
);
|
|
461
497
|
ctx.command("cave.echo").action(async ({ session }) => await addCave(ctx, session, cfg));
|
|
462
498
|
ctx.command("cave.wipe <id:number>").action(
|
|
@@ -503,7 +539,7 @@ async function getCaveListByOriginUser(ctx, session) {
|
|
|
503
539
|
}
|
|
504
540
|
return response;
|
|
505
541
|
}
|
|
506
|
-
async function getCave(ctx, session, id) {
|
|
542
|
+
async function getCave(ctx, session, cfg, id) {
|
|
507
543
|
if (!session.guildId) {
|
|
508
544
|
return session.text("echo-cave.general.privateChatReminder");
|
|
509
545
|
}
|
|
@@ -527,7 +563,7 @@ async function getCave(ctx, session, id) {
|
|
|
527
563
|
}
|
|
528
564
|
caveMsg = caves[0];
|
|
529
565
|
}
|
|
530
|
-
await sendCaveMsg(ctx, session, caveMsg);
|
|
566
|
+
await sendCaveMsg(ctx, session, caveMsg, cfg);
|
|
531
567
|
}
|
|
532
568
|
async function deleteCave(ctx, session, cfg, id) {
|
|
533
569
|
if (!session.guildId) {
|
package/lib/index.d.ts
CHANGED
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>;
|