koishi-plugin-forward-hime 1.2.2 → 1.3.0

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
@@ -1,4 +1,5 @@
1
1
  import { Context } from "koishi";
2
+ import { ConfigSet } from "./config";
2
3
  export interface MsgCache {
3
4
  platform: string;
4
5
  bot: string;
@@ -11,7 +12,7 @@ declare module "@koishijs/cache" {
11
12
  msgCache: MsgCache;
12
13
  }
13
14
  }
14
- export declare function msgCacheInit(context: Context): void;
15
+ export declare function msgCacheInit(context: Context, cfg: ConfigSet): void;
15
16
  export declare function msgCache(mc: MsgCache): Promise<void>;
16
17
  export declare function msgCacheDelete(key: string): Promise<void>;
17
18
  export declare function msgCacheFindByKey(key: string): Promise<any>;
package/lib/config.d.ts CHANGED
@@ -8,6 +8,7 @@ export interface ForwardNode {
8
8
  export interface ConfigSet {
9
9
  WithChannelName: boolean;
10
10
  WithPlatformName: boolean;
11
+ CacheTimeout: number;
11
12
  ForwardGroups: {
12
13
  Nodes: ForwardNode[];
13
14
  }[];
@@ -1,3 +1,4 @@
1
- import { Session } from "koishi";
1
+ import { Session, Element } from "koishi";
2
2
  import { ForwardNode } from "./config";
3
3
  export declare function MsgDecorator(session: Session, node: ForwardNode): Promise<any>;
4
+ export declare function MsgDecoratorFallback(session: Session, node: ForwardNode): Promise<Element[]>;
package/lib/index.js CHANGED
@@ -35,7 +35,10 @@ var createConfig = /* @__PURE__ */ __name(() => import_koishi.Schema.intersect([
35
35
  import_koishi.Schema.object({
36
36
  WithChannelName: import_koishi.Schema.boolean().description("是否包含频道名称").default(false),
37
37
  WithPlatformName: import_koishi.Schema.boolean().description("是否包含平台名称").default(true)
38
- }).description("转发格式"),
38
+ }).description("转发格式").hidden(),
39
+ import_koishi.Schema.object({
40
+ CacheTimeout: import_koishi.Schema.number().description("消息缓存时间(分钟)- 影响跨平台删除与回复等功能").default(120).min(120).max(1440 * 7)
41
+ }).description("消息缓存"),
39
42
  import_koishi.Schema.object({
40
43
  ForwardGroups: import_koishi.Schema.array(
41
44
  import_koishi.Schema.object({
@@ -112,6 +115,7 @@ var onebot_default = {
112
115
 
113
116
  // src/cache.ts
114
117
  var ctx;
118
+ var cacheTimeout = 86400 * 1e3;
115
119
  function cacheNotEnabled() {
116
120
  if (!ctx.cache) {
117
121
  return true;
@@ -119,8 +123,9 @@ function cacheNotEnabled() {
119
123
  return false;
120
124
  }
121
125
  __name(cacheNotEnabled, "cacheNotEnabled");
122
- function msgCacheInit(context) {
126
+ function msgCacheInit(context, cfg) {
123
127
  ctx = context;
128
+ cacheTimeout = 60 * cfg.CacheTimeout * 1e3;
124
129
  }
125
130
  __name(msgCacheInit, "msgCacheInit");
126
131
  async function msgCache(mc) {
@@ -129,7 +134,7 @@ async function msgCache(mc) {
129
134
  }
130
135
  const key = mc.guild + ":" + mc.msgid;
131
136
  logger.debug(`[message-cache] ${mc.platform} ${key}`);
132
- await ctx.cache.set("msgCache", key, mc, 7200 * 1e3);
137
+ await ctx.cache.set("msgCache", key, mc, cacheTimeout);
133
138
  }
134
139
  __name(msgCache, "msgCache");
135
140
  async function msgCacheDelete(key) {
@@ -203,9 +208,6 @@ async function MsgDecorator(session, node) {
203
208
  for (const fn of localDecorators) {
204
209
  elems = await fn(session, node, elems);
205
210
  }
206
- if (session.quote && session.quote.id) {
207
- elems = await quoteTranslator(session, node, elems);
208
- }
209
211
  if (_platform_out && typeof _platform_out.Decorator === "function") {
210
212
  return _platform_out.Decorator(elems);
211
213
  } else {
@@ -213,6 +215,30 @@ async function MsgDecorator(session, node) {
213
215
  }
214
216
  }
215
217
  __name(MsgDecorator, "MsgDecorator");
218
+ function defaultDecoratorFallback({ head, content }) {
219
+ let msg = [];
220
+ let newContent = [];
221
+ newContent.push((0, import_koishi5.h)("span", `[消息降级] `));
222
+ for (const key in content) {
223
+ if (["img", "audio", "video", "file"].includes(content[key].type)) {
224
+ newContent.push((0, import_koishi5.h)("span", ` [${content[key].type}] `));
225
+ } else {
226
+ newContent.push(content[key]);
227
+ }
228
+ }
229
+ msg = msg.concat(head, (0, import_koishi5.h)("br"), newContent);
230
+ return msg;
231
+ }
232
+ __name(defaultDecoratorFallback, "defaultDecoratorFallback");
233
+ async function MsgDecoratorFallback(session, node) {
234
+ let elems = { head: [], content: [] };
235
+ elems = defaultMiddleware(session);
236
+ for (const fn of localDecorators) {
237
+ elems = await fn(session, node, elems);
238
+ }
239
+ return defaultDecoratorFallback(elems);
240
+ }
241
+ __name(MsgDecoratorFallback, "MsgDecoratorFallback");
216
242
  async function atTranslator(session, node, { head, content }) {
217
243
  const newMsg = await new Promise((resolve, reject) => {
218
244
  let newContent = [];
@@ -252,40 +278,53 @@ async function quoteTranslator(session, node, { head, content }) {
252
278
  __name(quoteTranslator, "quoteTranslator");
253
279
 
254
280
  // src/message.ts
255
- async function MessageForward(ctx2, node, session) {
281
+ async function MessageSendWithDecorator(ctx2, node, session, Deco) {
256
282
  const uuid = session.channelId + ":" + session.messageId;
257
- const content = await MsgDecorator(session, node);
258
- try {
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
- });
271
- } catch (error) {
272
- logger.error(`ERROR:<MessageSend> ${error}`, content);
283
+ const content = await Deco(session, node);
284
+ await ctx2.bots[`${node.Platform}:${node.BotID}`].sendMessage(node.Guild, content).then((res) => {
285
+ let mc = {
286
+ platform: node.Platform,
287
+ bot: node.BotID,
288
+ guild: node.Guild,
289
+ msgid: res[0],
290
+ uuid
291
+ };
292
+ msgCache(mc);
293
+ logger.debug(`[MessageForward] to ${mc.platform} ${mc.uuid}`);
294
+ }).catch((error) => {
295
+ throw error;
296
+ });
297
+ }
298
+ __name(MessageSendWithDecorator, "MessageSendWithDecorator");
299
+ async function MessageForward(ctx2, node, session) {
300
+ if (!botExistsCheck(ctx2, node)) {
301
+ return;
273
302
  }
303
+ MessageSendWithDecorator(ctx2, node, session, MsgDecorator).catch((error) => {
304
+ logger.error(`ERROR:<MessageSend ${node.Platform}> ${error}`);
305
+ MessageSendWithDecorator(ctx2, node, session, MsgDecoratorFallback).catch(
306
+ (error2) => {
307
+ logger.error(`ERROR:<MessageSendFallback ${node.Platform}> ${error2}`);
308
+ }
309
+ );
310
+ });
274
311
  }
275
312
  __name(MessageForward, "MessageForward");
276
313
  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}`);
314
+ if (!botExistsCheck(ctx2, { Platform: msg.platform, BotID: msg.bot, Guild: msg.guild })) {
315
+ return;
282
316
  }
317
+ await ctx2.bots[`${msg.platform}:${msg.bot}`].deleteMessage(msg.guild, msg.msgid).catch((error) => {
318
+ logger.error(`ERROR:<MessageDelete> ${error}`);
319
+ });
283
320
  }
284
321
  __name(MessageDelete, "MessageDelete");
285
322
  function botExistsCheck(ctx2, node) {
286
323
  if (ctx2.bots[`${node.Platform}:${node.BotID}`] == void 0) {
287
- throw new Error("Bot Not Found");
324
+ logger.error(`ERROR:<BOT not Exist> ${node.Platform}:${node.BotID}`);
325
+ return false;
288
326
  }
327
+ return true;
289
328
  }
290
329
  __name(botExistsCheck, "botExistsCheck");
291
330
 
@@ -308,15 +347,19 @@ var inject = {
308
347
  };
309
348
  var Config = createConfig();
310
349
  function apply(ctx2, cfg) {
311
- msgCacheInit(ctx2);
350
+ msgCacheInit(ctx2, cfg);
312
351
  ctx2.on("message-created", async (session) => {
313
352
  const hitGroup = [];
314
353
  for (const g in cfg.ForwardGroups) {
315
354
  const group = cfg.ForwardGroups[g];
316
355
  for (const k in group.Nodes) {
317
356
  if (group.Nodes[k].Guild === session.channelId && group.Nodes[k].Platform === session.platform && group.Nodes[k].BotID !== session.userId) {
357
+ if (!session.channelId || !session.messageId) {
358
+ continue;
359
+ }
318
360
  logger.debug(
319
361
  "[message-created]",
362
+ session.platform,
320
363
  session.channelId + ":" + session.messageId
321
364
  );
322
365
  let uuid = session.channelId + ":" + session.messageId;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-forward-hime",
3
3
  "description": "多群组消息互通",
4
- "version": "1.2.2",
4
+ "version": "1.3.0",
5
5
  "author": {
6
6
  "name": "xianii",
7
7
  "email": "jiyucheng007@gmail.com"