koishi-plugin-forward-hime 1.2.0 → 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 ADDED
@@ -0,0 +1,20 @@
1
+ import { Context } from "koishi";
2
+ export interface MsgCache {
3
+ platform: string;
4
+ bot: string;
5
+ guild: string;
6
+ msgid: string;
7
+ uuid: string;
8
+ }
9
+ declare module "@koishijs/cache" {
10
+ interface Tables {
11
+ msgCache: MsgCache;
12
+ }
13
+ }
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,10 @@
1
1
  import { Context } from "koishi";
2
2
  import { ConfigSet } from "./config";
3
- export declare const reusable = true;
4
3
  export declare const name = "forward hime - \u8F6C\u53D1\u59EC";
5
- export declare const usage = "\n# forward hime - \u8F6C\u53D1\u59EC <sub>v1.2.0</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## \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
+ export declare const reusable = true;
6
+ export declare const inject: {
7
+ optional: string[];
8
+ };
6
9
  export declare const Config: import("schemastery")<ConfigSet>;
7
10
  export declare function apply(ctx: Context, cfg: ConfigSet): void;
package/lib/index.js CHANGED
@@ -22,6 +22,7 @@ var src_exports = {};
22
22
  __export(src_exports, {
23
23
  Config: () => Config,
24
24
  apply: () => apply,
25
+ inject: () => inject,
25
26
  name: () => name,
26
27
  reusable: () => reusable,
27
28
  usage: () => usage
@@ -54,6 +55,7 @@ var createConfig = /* @__PURE__ */ __name(() => import_koishi.Schema.intersect([
54
55
  // src/logger.ts
55
56
  var import_koishi2 = require("koishi");
56
57
  var logger = new import_koishi2.Logger("forward-hime");
58
+ logger.level = import_koishi2.Logger.INFO;
57
59
 
58
60
  // src/decorator.ts
59
61
  var import_koishi5 = require("koishi");
@@ -108,6 +110,69 @@ var onebot_default = {
108
110
  Middleware
109
111
  };
110
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
+
111
176
  // src/decorator.ts
112
177
  function defaultDecorator({ head, content }) {
113
178
  let msg = [];
@@ -125,9 +190,9 @@ function defaultMiddleware(session) {
125
190
  return { head, content: session.elements };
126
191
  }
127
192
  __name(defaultMiddleware, "defaultMiddleware");
128
- var localDecorators = [at2Name];
129
- function MsgDecorator(session, node) {
130
- let elems;
193
+ var localDecorators = [atTranslator, quoteTranslator];
194
+ async function MsgDecorator(session, node) {
195
+ let elems = { head: [], content: [] };
131
196
  let _platform_in = decorators_exports[session.platform];
132
197
  let _platform_out = decorators_exports[node.Platform];
133
198
  if (_platform_in && typeof _platform_in.Middleware === "function") {
@@ -135,9 +200,12 @@ function MsgDecorator(session, node) {
135
200
  } else {
136
201
  elems = defaultMiddleware(session);
137
202
  }
138
- localDecorators.forEach((fn) => {
139
- elems = fn(elems);
140
- });
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
+ }
141
209
  if (_platform_out && typeof _platform_out.Decorator === "function") {
142
210
  return _platform_out.Decorator(elems);
143
211
  } else {
@@ -145,49 +213,103 @@ function MsgDecorator(session, node) {
145
213
  }
146
214
  }
147
215
  __name(MsgDecorator, "MsgDecorator");
148
- function at2Name({ head, content }) {
149
- for (const key in content) {
150
- const element = content[key];
151
- if (element.type === "at") {
152
- 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
+ }
230
+ }
231
+ resolve({ head, content: newContent });
232
+ });
233
+ return newMsg;
234
+ }
235
+ __name(atTranslator, "atTranslator");
236
+ async function quoteTranslator(session, node, { head, content }) {
237
+ if (!session.quote || !session.quote.id) {
238
+ return { head, content };
239
+ }
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`);
153
248
  }
154
249
  }
155
250
  return { head, content };
156
251
  }
157
- __name(at2Name, "at2Name");
252
+ __name(quoteTranslator, "quoteTranslator");
158
253
 
159
254
  // src/message.ts
160
- 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);
161
258
  try {
162
- if (ctx.bots[`${node.Platform}:${node.BotID}`] == void 0) {
163
- throw new Error("Bot Not Found");
164
- }
165
- await ctx.bots[`${node.Platform}:${node.BotID}`].sendMessage(
166
- node.Guild,
167
- MsgDecorator(session, node)
168
- );
259
+ botExistsCheck(ctx2, node);
260
+ await ctx2.bots[`${node.Platform}:${node.BotID}`].sendMessage(node.Guild, content).then((res) => {
261
+ let mc = {
262
+ platform: node.Platform,
263
+ bot: node.BotID,
264
+ guild: node.Guild,
265
+ msgid: res[0],
266
+ uuid
267
+ };
268
+ msgCache(mc);
269
+ logger.debug(`[MessageForward] to ${mc.platform} ${mc.uuid}`);
270
+ });
169
271
  } catch (error) {
170
- logger.error(`ERROR:<MessageSend> ${error}`);
272
+ logger.error(`ERROR:<MessageSend> ${error}`, content);
171
273
  }
172
274
  }
173
275
  __name(MessageForward, "MessageForward");
276
+ async function MessageDelete(ctx2, msg) {
277
+ try {
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);
280
+ } catch (error) {
281
+ logger.error(`ERROR:<MessageDelete> ${error}`);
282
+ }
283
+ }
284
+ __name(MessageDelete, "MessageDelete");
285
+ function botExistsCheck(ctx2, node) {
286
+ if (ctx2.bots[`${node.Platform}:${node.BotID}`] == void 0) {
287
+ throw new Error("Bot Not Found");
288
+ }
289
+ }
290
+ __name(botExistsCheck, "botExistsCheck");
174
291
 
175
292
  // src/index.ts
176
- var reusable = true;
177
293
  var name = `forward hime - 转发姬`;
178
- var version = `<sub>v1.2.0</sub>`;
179
294
  var usage = `
180
- # ${name} ${version}
295
+ # ${name}
181
296
  ## 配置说明
182
297
  1. 添加 \`互通转发组\`
183
298
  2. 为添加的 \`互通转发组\` 配置转发节点。每个转发节点对应某个平台的一个频道或群组
299
+
300
+ 注意:当未配置\`cache\`时,跨平台删除、跨平台回复等功能将无法正常工作。
184
301
  ## 特性
185
302
  - 可以配置多个 \`互通转发组\`,每个 \`互通转发组\` 之间相互独立
186
303
  - 启用插件后,当一个 \`互通转发组\` 中的任意节点收到消息时,插件会自动将消息转发给**这个**\`互通转发组\`中的其他节点。
187
304
  ---`;
305
+ var reusable = true;
306
+ var inject = {
307
+ optional: ["cache"]
308
+ };
188
309
  var Config = createConfig();
189
- function apply(ctx, cfg) {
190
- ctx.on("message-created", async (session) => {
310
+ function apply(ctx2, cfg) {
311
+ msgCacheInit(ctx2);
312
+ ctx2.on("message-created", async (session) => {
191
313
  const hitGroup = [];
192
314
  for (const g in cfg.ForwardGroups) {
193
315
  const group = cfg.ForwardGroups[g];
@@ -195,9 +317,16 @@ function apply(ctx, cfg) {
195
317
  if (group.Nodes[k].Guild === session.channelId && group.Nodes[k].Platform === session.platform && group.Nodes[k].BotID !== session.userId) {
196
318
  logger.debug(
197
319
  "[message-created]",
198
- session.messageId,
199
- session.elements
320
+ session.channelId + ":" + session.messageId
200
321
  );
322
+ let uuid = session.channelId + ":" + session.messageId;
323
+ msgCache({
324
+ platform: session.platform,
325
+ bot: group.Nodes[k].BotID,
326
+ guild: session.channelId,
327
+ msgid: session.messageId,
328
+ uuid
329
+ });
201
330
  hitGroup.push(g);
202
331
  break;
203
332
  }
@@ -207,15 +336,33 @@ function apply(ctx, cfg) {
207
336
  const group = cfg.ForwardGroups[hitGroup[g]];
208
337
  for (const k in group.Nodes) {
209
338
  if (group.Nodes[k].Guild !== session.channelId) {
210
- MessageForward(ctx, group.Nodes[k], session);
339
+ MessageForward(ctx2, group.Nodes[k], session);
211
340
  }
212
341
  }
213
342
  }
214
343
  });
215
- ctx.on("message-deleted", async (session) => {
216
- logger.debug("[message-deleted]", session.messageId);
344
+ ctx2.on("message-deleted", async (session) => {
345
+ const cacheKey = session.channelId + ":" + session.messageId;
346
+ logger.debug("[message-deleted]", cacheKey);
347
+ msgCacheFindByKey(cacheKey).then((res) => {
348
+ if (res) {
349
+ logger.debug("[msgCacheFindByKey]", res);
350
+ msgCacheDelete(cacheKey);
351
+ const uuid = res.uuid;
352
+ msgCacheFindByUUID(uuid).then((res2) => {
353
+ if (res2) {
354
+ for (const k in res2) {
355
+ logger.debug("[message-delete]", res2[k][1]);
356
+ msgCacheDelete(res2[k][0]).then(() => {
357
+ MessageDelete(ctx2, res2[k][1]);
358
+ });
359
+ }
360
+ }
361
+ });
362
+ }
363
+ });
217
364
  });
218
- ctx.on("message-updated", async (session) => {
365
+ ctx2.on("message-updated", async (session) => {
219
366
  logger.debug("[message-updated]", session.messageId, session);
220
367
  });
221
368
  }
@@ -224,6 +371,7 @@ __name(apply, "apply");
224
371
  0 && (module.exports = {
225
372
  Config,
226
373
  apply,
374
+ inject,
227
375
  name,
228
376
  reusable,
229
377
  usage
package/lib/message.d.ts CHANGED
@@ -1,3 +1,6 @@
1
1
  import { Context, Session } from "koishi";
2
2
  import { ForwardNode } from "./config";
3
+ import { MsgCache } from "./cache";
3
4
  export declare function MessageForward(ctx: Context, node: ForwardNode, session: Session): Promise<void>;
5
+ export declare function MessageDelete(ctx: Context, msg: MsgCache): Promise<void>;
6
+ export declare function MessageEdit(ctx: Context, node: ForwardNode, session: Session): Promise<void>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-forward-hime",
3
3
  "description": "多群组消息互通",
4
- "version": "1.2.0",
4
+ "version": "1.2.2",
5
5
  "author": {
6
6
  "name": "xianii",
7
7
  "email": "jiyucheng007@gmail.com"
@@ -12,7 +12,6 @@
12
12
  "lib"
13
13
  ],
14
14
  "koishi": {
15
- "preview": true,
16
15
  "browser": true
17
16
  },
18
17
  "license": "MIT",