koishi-plugin-echo-cave 1.10.0 → 1.10.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.
Files changed (2) hide show
  1. package/lib/index.cjs +22 -19
  2. 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 templates = require_zh_CN()["echo-cave"].templates;
172
+ const TEMPLATE_COUNT = 5;
177
173
  if (caveMsg.type === "forward") {
178
- const forwardTemplates = (templates.forward || []).filter(
179
- (template) => template.trim() !== ""
180
- );
181
- if (forwardTemplates.length === 0) {
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 = replacePlaceholders(random(forwardTemplates), templateData);
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 msgTemplates = (templates.msg || []).filter(
191
- (template) => template && template.prefix?.trim() !== "" && template.suffix?.trim() !== ""
192
- );
193
- if (msgTemplates.length === 0) {
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(msgTemplates);
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) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-echo-cave",
3
3
  "description": "Group echo cave",
4
- "version": "1.10.0",
4
+ "version": "1.10.1",
5
5
  "main": "lib/index.cjs",
6
6
  "typings": "lib/index.d.ts",
7
7
  "type": "module",