koishi-plugin-forward-hime 1.2.1 → 1.2.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/cache.d.ts CHANGED
@@ -11,7 +11,10 @@ declare module "@koishijs/cache" {
11
11
  msgCache: MsgCache;
12
12
  }
13
13
  }
14
- export declare function msgCache(ctx: Context, mc: MsgCache): Promise<void>;
15
- export declare function msgCacheDelete(ctx: Context, key: string): Promise<void>;
16
- export declare function msgCacheFindByKey(ctx: Context, key: string): Promise<MsgCache>;
17
- export declare function msgCacheFindByUUID(ctx: Context, uuid: string): Promise<any[]>;
14
+ export declare function msgCacheInit(context: Context): void;
15
+ export declare function msgCache(mc: MsgCache): Promise<void>;
16
+ export declare function msgCacheDelete(key: string): Promise<void>;
17
+ export declare function msgCacheFindByKey(key: string): Promise<any>;
18
+ export declare function msgCacheFindByUUID(uuid: string): Promise<any[]>;
19
+ import { ForwardNode } from "./config";
20
+ export declare function msgCacheGetLocalIDByUUID(node: ForwardNode, uuid: string): Promise<any>;
@@ -1,3 +1,3 @@
1
1
  import { Session } from "koishi";
2
2
  import { ForwardNode } from "./config";
3
- export declare function MsgDecorator(session: Session, node: ForwardNode): any;
3
+ export declare function MsgDecorator(session: Session, node: ForwardNode): Promise<any>;
package/lib/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Context } from "koishi";
2
2
  import { ConfigSet } from "./config";
3
3
  export declare const name = "forward hime - \u8F6C\u53D1\u59EC";
4
- export declare const usage = "\n# forward hime - \u8F6C\u53D1\u59EC <sub>v1.2.1</sub>\n## \u914D\u7F6E\u8BF4\u660E\n1. \u6DFB\u52A0 `\u4E92\u901A\u8F6C\u53D1\u7EC4`\n2. \u4E3A\u6DFB\u52A0\u7684 `\u4E92\u901A\u8F6C\u53D1\u7EC4` \u914D\u7F6E\u8F6C\u53D1\u8282\u70B9\u3002\u6BCF\u4E2A\u8F6C\u53D1\u8282\u70B9\u5BF9\u5E94\u67D0\u4E2A\u5E73\u53F0\u7684\u4E00\u4E2A\u9891\u9053\u6216\u7FA4\u7EC4\n\n\u6CE8\u610F\uFF1A\u5F53\u672A\u914D\u7F6E`cache`\u65F6\uFF0C\u8DE8\u5E73\u53F0\u5220\u9664\u3001\u8DE8\u5E73\u53F0\u56DE\u590D\u7B49\u529F\u80FD\u5C06\u65E0\u6CD5\u6B63\u5E38\u5DE5\u4F5C\u3002\n## \u7279\u6027\n- \u53EF\u4EE5\u914D\u7F6E\u591A\u4E2A `\u4E92\u901A\u8F6C\u53D1\u7EC4`\uFF0C\u6BCF\u4E2A `\u4E92\u901A\u8F6C\u53D1\u7EC4` \u4E4B\u95F4\u76F8\u4E92\u72EC\u7ACB\n- \u542F\u7528\u63D2\u4EF6\u540E\uFF0C\u5F53\u4E00\u4E2A `\u4E92\u901A\u8F6C\u53D1\u7EC4` \u4E2D\u7684\u4EFB\u610F\u8282\u70B9\u6536\u5230\u6D88\u606F\u65F6\uFF0C\u63D2\u4EF6\u4F1A\u81EA\u52A8\u5C06\u6D88\u606F\u8F6C\u53D1\u7ED9**\u8FD9\u4E2A**`\u4E92\u901A\u8F6C\u53D1\u7EC4`\u4E2D\u7684\u5176\u4ED6\u8282\u70B9\u3002\n---";
4
+ export declare const usage = "\n# forward hime - \u8F6C\u53D1\u59EC\n## \u914D\u7F6E\u8BF4\u660E\n1. \u6DFB\u52A0 `\u4E92\u901A\u8F6C\u53D1\u7EC4`\n2. \u4E3A\u6DFB\u52A0\u7684 `\u4E92\u901A\u8F6C\u53D1\u7EC4` \u914D\u7F6E\u8F6C\u53D1\u8282\u70B9\u3002\u6BCF\u4E2A\u8F6C\u53D1\u8282\u70B9\u5BF9\u5E94\u67D0\u4E2A\u5E73\u53F0\u7684\u4E00\u4E2A\u9891\u9053\u6216\u7FA4\u7EC4\n\n\u6CE8\u610F\uFF1A\u5F53\u672A\u914D\u7F6E`cache`\u65F6\uFF0C\u8DE8\u5E73\u53F0\u5220\u9664\u3001\u8DE8\u5E73\u53F0\u56DE\u590D\u7B49\u529F\u80FD\u5C06\u65E0\u6CD5\u6B63\u5E38\u5DE5\u4F5C\u3002\n## \u7279\u6027\n- \u53EF\u4EE5\u914D\u7F6E\u591A\u4E2A `\u4E92\u901A\u8F6C\u53D1\u7EC4`\uFF0C\u6BCF\u4E2A `\u4E92\u901A\u8F6C\u53D1\u7EC4` \u4E4B\u95F4\u76F8\u4E92\u72EC\u7ACB\n- \u542F\u7528\u63D2\u4EF6\u540E\uFF0C\u5F53\u4E00\u4E2A `\u4E92\u901A\u8F6C\u53D1\u7EC4` \u4E2D\u7684\u4EFB\u610F\u8282\u70B9\u6536\u5230\u6D88\u606F\u65F6\uFF0C\u63D2\u4EF6\u4F1A\u81EA\u52A8\u5C06\u6D88\u606F\u8F6C\u53D1\u7ED9**\u8FD9\u4E2A**`\u4E92\u901A\u8F6C\u53D1\u7EC4`\u4E2D\u7684\u5176\u4ED6\u8282\u70B9\u3002\n---";
5
5
  export declare const reusable = true;
6
6
  export declare const inject: {
7
7
  optional: string[];
package/lib/index.js CHANGED
@@ -55,6 +55,7 @@ var createConfig = /* @__PURE__ */ __name(() => import_koishi.Schema.intersect([
55
55
  // src/logger.ts
56
56
  var import_koishi2 = require("koishi");
57
57
  var logger = new import_koishi2.Logger("forward-hime");
58
+ logger.level = import_koishi2.Logger.INFO;
58
59
 
59
60
  // src/decorator.ts
60
61
  var import_koishi5 = require("koishi");
@@ -109,6 +110,69 @@ var onebot_default = {
109
110
  Middleware
110
111
  };
111
112
 
113
+ // src/cache.ts
114
+ var ctx;
115
+ function cacheNotEnabled() {
116
+ if (!ctx.cache) {
117
+ return true;
118
+ }
119
+ return false;
120
+ }
121
+ __name(cacheNotEnabled, "cacheNotEnabled");
122
+ function msgCacheInit(context) {
123
+ ctx = context;
124
+ }
125
+ __name(msgCacheInit, "msgCacheInit");
126
+ async function msgCache(mc) {
127
+ if (cacheNotEnabled()) {
128
+ return;
129
+ }
130
+ const key = mc.guild + ":" + mc.msgid;
131
+ logger.debug(`[message-cache] ${mc.platform} ${key}`);
132
+ await ctx.cache.set("msgCache", key, mc, 7200 * 1e3);
133
+ }
134
+ __name(msgCache, "msgCache");
135
+ async function msgCacheDelete(key) {
136
+ if (cacheNotEnabled()) {
137
+ return;
138
+ }
139
+ await ctx.cache.delete("msgCache", key);
140
+ }
141
+ __name(msgCacheDelete, "msgCacheDelete");
142
+ async function msgCacheFindByKey(key) {
143
+ if (cacheNotEnabled()) {
144
+ return;
145
+ }
146
+ return await ctx.cache.get("msgCache", key);
147
+ }
148
+ __name(msgCacheFindByKey, "msgCacheFindByKey");
149
+ async function msgCacheFindByUUID(uuid) {
150
+ if (cacheNotEnabled()) {
151
+ return;
152
+ }
153
+ let caches = await ctx.cache.entries("msgCache");
154
+ let result = [];
155
+ for await (const item of caches) {
156
+ if (item[1].uuid === uuid) {
157
+ result.push(item);
158
+ }
159
+ }
160
+ return result;
161
+ }
162
+ __name(msgCacheFindByUUID, "msgCacheFindByUUID");
163
+ async function msgCacheGetLocalIDByUUID(node, uuid) {
164
+ if (cacheNotEnabled()) {
165
+ return;
166
+ }
167
+ let caches = await ctx.cache.entries("msgCache");
168
+ for await (const item of caches) {
169
+ if (item[1].uuid === uuid && item[1].platform === node.Platform && item[1].guild === node.Guild && item[1].bot === node.BotID) {
170
+ return item[1].msgid;
171
+ }
172
+ }
173
+ }
174
+ __name(msgCacheGetLocalIDByUUID, "msgCacheGetLocalIDByUUID");
175
+
112
176
  // src/decorator.ts
113
177
  function defaultDecorator({ head, content }) {
114
178
  let msg = [];
@@ -126,9 +190,9 @@ function defaultMiddleware(session) {
126
190
  return { head, content: session.elements };
127
191
  }
128
192
  __name(defaultMiddleware, "defaultMiddleware");
129
- var localDecorators = [at2Name];
130
- function MsgDecorator(session, node) {
131
- let elems;
193
+ var localDecorators = [atTranslator, quoteTranslator];
194
+ async function MsgDecorator(session, node) {
195
+ let elems = { head: [], content: [] };
132
196
  let _platform_in = decorators_exports[session.platform];
133
197
  let _platform_out = decorators_exports[node.Platform];
134
198
  if (_platform_in && typeof _platform_in.Middleware === "function") {
@@ -136,9 +200,12 @@ function MsgDecorator(session, node) {
136
200
  } else {
137
201
  elems = defaultMiddleware(session);
138
202
  }
139
- localDecorators.forEach((fn) => {
140
- elems = fn(elems);
141
- });
203
+ for (const fn of localDecorators) {
204
+ elems = await fn(session, node, elems);
205
+ }
206
+ if (session.quote && session.quote.id) {
207
+ elems = await quoteTranslator(session, node, elems);
208
+ }
142
209
  if (_platform_out && typeof _platform_out.Decorator === "function") {
143
210
  return _platform_out.Decorator(elems);
144
211
  } else {
@@ -146,69 +213,51 @@ function MsgDecorator(session, node) {
146
213
  }
147
214
  }
148
215
  __name(MsgDecorator, "MsgDecorator");
149
- function at2Name({ head, content }) {
150
- for (const key in content) {
151
- const element = content[key];
152
- if (element.type === "at") {
153
- content[key] = (0, import_koishi5.h)("span", `@${element.attrs.name || element.attrs.id}`);
216
+ async function atTranslator(session, node, { head, content }) {
217
+ const newMsg = await new Promise((resolve, reject) => {
218
+ let newContent = [];
219
+ for (const key in content) {
220
+ const element = content[key];
221
+ if (element.type === "at") {
222
+ if (element.attrs.id !== session.selfId) {
223
+ newContent.push(
224
+ (0, import_koishi5.h)("span", `@${element.attrs.name || element.attrs.id}`)
225
+ );
226
+ }
227
+ } else {
228
+ newContent.push(element);
229
+ }
154
230
  }
155
- }
156
- return { head, content };
157
- }
158
- __name(at2Name, "at2Name");
159
-
160
- // src/cache.ts
161
- function cacheNotEnabled(ctx) {
162
- if (!ctx.cache) {
163
- return true;
164
- }
165
- return false;
166
- }
167
- __name(cacheNotEnabled, "cacheNotEnabled");
168
- async function msgCache(ctx, mc) {
169
- if (cacheNotEnabled(ctx)) {
170
- return;
171
- }
172
- const key = mc.guild + ":" + mc.msgid;
173
- logger.debug("[message-cache]", key, mc);
174
- await ctx.cache.set("msgCache", key, mc, 7200 * 1e3);
231
+ resolve({ head, content: newContent });
232
+ });
233
+ return newMsg;
175
234
  }
176
- __name(msgCache, "msgCache");
177
- async function msgCacheDelete(ctx, key) {
178
- if (cacheNotEnabled(ctx)) {
179
- return;
235
+ __name(atTranslator, "atTranslator");
236
+ async function quoteTranslator(session, node, { head, content }) {
237
+ if (!session.quote || !session.quote.id) {
238
+ return { head, content };
180
239
  }
181
- await ctx.cache.delete("msgCache", key);
182
- }
183
- __name(msgCacheDelete, "msgCacheDelete");
184
- async function msgCacheFindByKey(ctx, key) {
185
- if (cacheNotEnabled(ctx)) {
186
- return;
187
- }
188
- return await ctx.cache.get("msgCache", key);
189
- }
190
- __name(msgCacheFindByKey, "msgCacheFindByKey");
191
- async function msgCacheFindByUUID(ctx, uuid) {
192
- if (cacheNotEnabled(ctx)) {
193
- return;
194
- }
195
- let caches = await ctx.cache.entries("msgCache");
196
- let result = [];
197
- for await (const item of caches) {
198
- if (item[1].uuid === uuid) {
199
- result.push(item);
240
+ const key = session.channelId + ":" + session.quote.id;
241
+ const cache = await msgCacheFindByKey(key);
242
+ if (cache) {
243
+ let msgid = await msgCacheGetLocalIDByUUID(node, cache.uuid);
244
+ if (msgid) {
245
+ head.unshift((0, import_koishi5.h)("quote", { id: msgid }));
246
+ } else {
247
+ logger.error(`[msgCacheGetLocalIDByUUID] ${cache.uuid} not found`);
200
248
  }
201
249
  }
202
- return result;
250
+ return { head, content };
203
251
  }
204
- __name(msgCacheFindByUUID, "msgCacheFindByUUID");
252
+ __name(quoteTranslator, "quoteTranslator");
205
253
 
206
254
  // src/message.ts
207
- async function MessageForward(ctx, node, session) {
255
+ async function MessageForward(ctx2, node, session) {
256
+ const uuid = session.channelId + ":" + session.messageId;
257
+ const content = await MsgDecorator(session, node);
208
258
  try {
209
- botExistsCheck(ctx, node);
210
- const uuid = session.channelId + ":" + session.messageId;
211
- await ctx.bots[`${node.Platform}:${node.BotID}`].sendMessage(node.Guild, MsgDecorator(session, node)).then((res) => {
259
+ botExistsCheck(ctx2, node);
260
+ await ctx2.bots[`${node.Platform}:${node.BotID}`].sendMessage(node.Guild, content).then((res) => {
212
261
  let mc = {
213
262
  platform: node.Platform,
214
263
  bot: node.BotID,
@@ -216,25 +265,25 @@ async function MessageForward(ctx, node, session) {
216
265
  msgid: res[0],
217
266
  uuid
218
267
  };
219
- msgCache(ctx, mc);
220
- logger.debug("Forward", mc);
268
+ msgCache(mc);
269
+ logger.debug(`[MessageForward] to ${mc.platform} ${mc.uuid}`);
221
270
  });
222
271
  } catch (error) {
223
- logger.error(`ERROR:<MessageSend> ${error}`);
272
+ logger.error(`ERROR:<MessageSend> ${error}`, content);
224
273
  }
225
274
  }
226
275
  __name(MessageForward, "MessageForward");
227
- async function MessageDelete(ctx, msg) {
276
+ async function MessageDelete(ctx2, msg) {
228
277
  try {
229
- botExistsCheck(ctx, { Platform: msg.platform, BotID: msg.bot, Guild: msg.guild });
230
- await ctx.bots[`${msg.platform}:${msg.bot}`].deleteMessage(msg.guild, msg.msgid);
278
+ botExistsCheck(ctx2, { Platform: msg.platform, BotID: msg.bot, Guild: msg.guild });
279
+ await ctx2.bots[`${msg.platform}:${msg.bot}`].deleteMessage(msg.guild, msg.msgid);
231
280
  } catch (error) {
232
281
  logger.error(`ERROR:<MessageDelete> ${error}`);
233
282
  }
234
283
  }
235
284
  __name(MessageDelete, "MessageDelete");
236
- function botExistsCheck(ctx, node) {
237
- if (ctx.bots[`${node.Platform}:${node.BotID}`] == void 0) {
285
+ function botExistsCheck(ctx2, node) {
286
+ if (ctx2.bots[`${node.Platform}:${node.BotID}`] == void 0) {
238
287
  throw new Error("Bot Not Found");
239
288
  }
240
289
  }
@@ -242,9 +291,8 @@ __name(botExistsCheck, "botExistsCheck");
242
291
 
243
292
  // src/index.ts
244
293
  var name = `forward hime - 转发姬`;
245
- var version = `<sub>v1.2.1</sub>`;
246
294
  var usage = `
247
- # ${name} ${version}
295
+ # ${name}
248
296
  ## 配置说明
249
297
  1. 添加 \`互通转发组\`
250
298
  2. 为添加的 \`互通转发组\` 配置转发节点。每个转发节点对应某个平台的一个频道或群组
@@ -259,8 +307,9 @@ var inject = {
259
307
  optional: ["cache"]
260
308
  };
261
309
  var Config = createConfig();
262
- function apply(ctx, cfg) {
263
- ctx.on("message-created", async (session) => {
310
+ function apply(ctx2, cfg) {
311
+ msgCacheInit(ctx2);
312
+ ctx2.on("message-created", async (session) => {
264
313
  const hitGroup = [];
265
314
  for (const g in cfg.ForwardGroups) {
266
315
  const group = cfg.ForwardGroups[g];
@@ -271,7 +320,7 @@ function apply(ctx, cfg) {
271
320
  session.channelId + ":" + session.messageId
272
321
  );
273
322
  let uuid = session.channelId + ":" + session.messageId;
274
- msgCache(ctx, {
323
+ msgCache({
275
324
  platform: session.platform,
276
325
  bot: group.Nodes[k].BotID,
277
326
  guild: session.channelId,
@@ -287,25 +336,25 @@ function apply(ctx, cfg) {
287
336
  const group = cfg.ForwardGroups[hitGroup[g]];
288
337
  for (const k in group.Nodes) {
289
338
  if (group.Nodes[k].Guild !== session.channelId) {
290
- MessageForward(ctx, group.Nodes[k], session);
339
+ MessageForward(ctx2, group.Nodes[k], session);
291
340
  }
292
341
  }
293
342
  }
294
343
  });
295
- ctx.on("message-deleted", async (session) => {
344
+ ctx2.on("message-deleted", async (session) => {
296
345
  const cacheKey = session.channelId + ":" + session.messageId;
297
346
  logger.debug("[message-deleted]", cacheKey);
298
- msgCacheFindByKey(ctx, cacheKey).then((res) => {
347
+ msgCacheFindByKey(cacheKey).then((res) => {
299
348
  if (res) {
300
349
  logger.debug("[msgCacheFindByKey]", res);
301
- msgCacheDelete(ctx, cacheKey);
350
+ msgCacheDelete(cacheKey);
302
351
  const uuid = res.uuid;
303
- msgCacheFindByUUID(ctx, uuid).then((res2) => {
352
+ msgCacheFindByUUID(uuid).then((res2) => {
304
353
  if (res2) {
305
354
  for (const k in res2) {
306
355
  logger.debug("[message-delete]", res2[k][1]);
307
- msgCacheDelete(ctx, res2[k][0]).then(() => {
308
- MessageDelete(ctx, res2[k][1]);
356
+ msgCacheDelete(res2[k][0]).then(() => {
357
+ MessageDelete(ctx2, res2[k][1]);
309
358
  });
310
359
  }
311
360
  }
@@ -313,7 +362,7 @@ function apply(ctx, cfg) {
313
362
  }
314
363
  });
315
364
  });
316
- ctx.on("message-updated", async (session) => {
365
+ ctx2.on("message-updated", async (session) => {
317
366
  logger.debug("[message-updated]", session.messageId, session);
318
367
  });
319
368
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-forward-hime",
3
3
  "description": "多群组消息互通",
4
- "version": "1.2.1",
4
+ "version": "1.2.2",
5
5
  "author": {
6
6
  "name": "xianii",
7
7
  "email": "jiyucheng007@gmail.com"