koishi-plugin-echo-cave 1.10.0 → 1.10.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/index.cjs +29 -26
- package/package.json +1 -1
package/lib/index.cjs
CHANGED
|
@@ -157,10 +157,6 @@ async function getUserName(ctx, session, userId) {
|
|
|
157
157
|
}
|
|
158
158
|
|
|
159
159
|
// src/cave-helper.ts
|
|
160
|
-
var random = (arr) => arr[Math.floor(Math.random() * arr.length)];
|
|
161
|
-
var replacePlaceholders = (template, data) => {
|
|
162
|
-
return template.replace(/{(\w+)}/g, (match, key) => data[key] || match);
|
|
163
|
-
};
|
|
164
160
|
async function sendCaveMsg(ctx, session, caveMsg) {
|
|
165
161
|
const { channelId } = session;
|
|
166
162
|
const content = JSON.parse(caveMsg.content);
|
|
@@ -173,34 +169,41 @@ async function sendCaveMsg(ctx, session, caveMsg) {
|
|
|
173
169
|
originName,
|
|
174
170
|
userName
|
|
175
171
|
};
|
|
176
|
-
const
|
|
172
|
+
const TEMPLATE_COUNT = 5;
|
|
177
173
|
if (caveMsg.type === "forward") {
|
|
178
|
-
const
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
174
|
+
const availableTemplates2 = [];
|
|
175
|
+
for (let i = 0; i < TEMPLATE_COUNT; i++) {
|
|
176
|
+
const template = session.text(`echo-cave.templates.forward.${i}`, templateData);
|
|
177
|
+
if (template.trim() !== "") {
|
|
178
|
+
availableTemplates2.push(template);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (availableTemplates2.length === 0) {
|
|
182
182
|
await session.send(session.text("echo-cave.general.noTemplatesConfigured"));
|
|
183
183
|
return;
|
|
184
184
|
}
|
|
185
|
-
const chosenTemplate2 =
|
|
185
|
+
const chosenTemplate2 = availableTemplates2[Math.floor(Math.random() * availableTemplates2.length)];
|
|
186
186
|
await session.onebot.sendGroupMsg(channelId, [createTextMsg(chosenTemplate2)]);
|
|
187
187
|
await session.onebot.sendGroupForwardMsg(channelId, content);
|
|
188
188
|
return;
|
|
189
189
|
}
|
|
190
|
-
const
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
190
|
+
const availableTemplates = [];
|
|
191
|
+
for (let i = 0; i < TEMPLATE_COUNT; i++) {
|
|
192
|
+
const prefix = session.text(`echo-cave.templates.msg.${i}.prefix`, templateData);
|
|
193
|
+
const suffix = session.text(`echo-cave.templates.msg.${i}.suffix`, templateData);
|
|
194
|
+
if (prefix.trim() !== "" && suffix.trim() !== "") {
|
|
195
|
+
availableTemplates.push({ prefix, suffix });
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (availableTemplates.length === 0) {
|
|
194
199
|
await session.send(session.text("echo-cave.general.noTemplatesConfigured"));
|
|
195
200
|
return;
|
|
196
201
|
}
|
|
197
|
-
const chosenTemplate = random(
|
|
198
|
-
const prefix = replacePlaceholders(chosenTemplate.prefix, templateData);
|
|
199
|
-
const suffix = replacePlaceholders(chosenTemplate.suffix, templateData);
|
|
202
|
+
const chosenTemplate = availableTemplates[Math.floor(Math.random() * availableTemplates.length)];
|
|
200
203
|
const last = content.at(-1);
|
|
201
204
|
const needsNewline = last?.type === "text";
|
|
202
|
-
content.unshift(createTextMsg(prefix));
|
|
203
|
-
content.push(createTextMsg(`${needsNewline ? "\n\n" : ""}${suffix}`));
|
|
205
|
+
content.unshift(createTextMsg(chosenTemplate.prefix));
|
|
206
|
+
content.push(createTextMsg(`${needsNewline ? "\n\n" : ""}${chosenTemplate.suffix}`));
|
|
204
207
|
await session.onebot.sendGroupMsg(channelId, content);
|
|
205
208
|
}
|
|
206
209
|
function formatDate(date) {
|
|
@@ -369,7 +372,7 @@ function apply(ctx, cfg) {
|
|
|
369
372
|
}
|
|
370
373
|
async function getCaveListByUser(ctx, session) {
|
|
371
374
|
if (!session.guildId) {
|
|
372
|
-
return session.text("general.privateChatReminder");
|
|
375
|
+
return session.text("echo-cave.general.privateChatReminder");
|
|
373
376
|
}
|
|
374
377
|
const { userId, channelId } = session;
|
|
375
378
|
const caves = await ctx.database.get("echo_cave", {
|
|
@@ -387,7 +390,7 @@ async function getCaveListByUser(ctx, session) {
|
|
|
387
390
|
}
|
|
388
391
|
async function getCaveListByOriginUser(ctx, session) {
|
|
389
392
|
if (!session.guildId) {
|
|
390
|
-
return session.text("general.privateChatReminder");
|
|
393
|
+
return session.text("echo-cave.general.privateChatReminder");
|
|
391
394
|
}
|
|
392
395
|
const { userId, channelId } = session;
|
|
393
396
|
const caves = await ctx.database.get("echo_cave", {
|
|
@@ -405,7 +408,7 @@ async function getCaveListByOriginUser(ctx, session) {
|
|
|
405
408
|
}
|
|
406
409
|
async function getCave(ctx, session, id) {
|
|
407
410
|
if (!session.guildId) {
|
|
408
|
-
return session.text("general.privateChatReminder");
|
|
411
|
+
return session.text("echo-cave.general.privateChatReminder");
|
|
409
412
|
}
|
|
410
413
|
let caveMsg;
|
|
411
414
|
const { channelId } = session;
|
|
@@ -423,7 +426,7 @@ async function getCave(ctx, session, id) {
|
|
|
423
426
|
channelId
|
|
424
427
|
});
|
|
425
428
|
if (caves.length === 0) {
|
|
426
|
-
return session.text("general.noMsgWithId");
|
|
429
|
+
return session.text("echo-cave.general.noMsgWithId");
|
|
427
430
|
}
|
|
428
431
|
caveMsg = caves[0];
|
|
429
432
|
}
|
|
@@ -431,14 +434,14 @@ async function getCave(ctx, session, id) {
|
|
|
431
434
|
}
|
|
432
435
|
async function deleteCave(ctx, session, cfg, id) {
|
|
433
436
|
if (!session.guildId) {
|
|
434
|
-
return session.text("general.privateChatReminder");
|
|
437
|
+
return session.text("echo-cave.general.privateChatReminder");
|
|
435
438
|
}
|
|
436
439
|
if (!id) {
|
|
437
440
|
return session.text(".noIdProvided");
|
|
438
441
|
}
|
|
439
442
|
const caves = await ctx.database.get("echo_cave", id);
|
|
440
443
|
if (caves.length === 0) {
|
|
441
|
-
return session.text("general.noMsgWithId");
|
|
444
|
+
return session.text("echo-cave.general.noMsgWithId");
|
|
442
445
|
}
|
|
443
446
|
const caveMsg = caves[0];
|
|
444
447
|
const currentUserId = session.userId;
|
|
@@ -470,7 +473,7 @@ async function deleteCave(ctx, session, cfg, id) {
|
|
|
470
473
|
}
|
|
471
474
|
async function addCave(ctx, session) {
|
|
472
475
|
if (!session.guildId) {
|
|
473
|
-
return session.text("general.privateChatReminder");
|
|
476
|
+
return session.text("echo-cave.general.privateChatReminder");
|
|
474
477
|
}
|
|
475
478
|
if (!session.quote) {
|
|
476
479
|
return session.text(".noMsgQuoted");
|