koishi-plugin-forward-hime 1.2.3 → 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 +2 -1
- package/lib/config.d.ts +1 -0
- package/lib/decorator.d.ts +2 -1
- package/lib/index.js +73 -27
- package/package.json +1 -1
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
package/lib/decorator.d.ts
CHANGED
|
@@ -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,
|
|
137
|
+
await ctx.cache.set("msgCache", key, mc, cacheTimeout);
|
|
133
138
|
}
|
|
134
139
|
__name(msgCache, "msgCache");
|
|
135
140
|
async function msgCacheDelete(key) {
|
|
@@ -210,6 +215,30 @@ async function MsgDecorator(session, node) {
|
|
|
210
215
|
}
|
|
211
216
|
}
|
|
212
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");
|
|
213
242
|
async function atTranslator(session, node, { head, content }) {
|
|
214
243
|
const newMsg = await new Promise((resolve, reject) => {
|
|
215
244
|
let newContent = [];
|
|
@@ -249,40 +278,53 @@ async function quoteTranslator(session, node, { head, content }) {
|
|
|
249
278
|
__name(quoteTranslator, "quoteTranslator");
|
|
250
279
|
|
|
251
280
|
// src/message.ts
|
|
252
|
-
async function
|
|
281
|
+
async function MessageSendWithDecorator(ctx2, node, session, Deco) {
|
|
253
282
|
const uuid = session.channelId + ":" + session.messageId;
|
|
254
|
-
const content = await
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
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;
|
|
270
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
|
+
});
|
|
271
311
|
}
|
|
272
312
|
__name(MessageForward, "MessageForward");
|
|
273
313
|
async function MessageDelete(ctx2, msg) {
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
await ctx2.bots[`${msg.platform}:${msg.bot}`].deleteMessage(msg.guild, msg.msgid);
|
|
277
|
-
} catch (error) {
|
|
278
|
-
logger.error(`ERROR:<MessageDelete> ${error}`);
|
|
314
|
+
if (!botExistsCheck(ctx2, { Platform: msg.platform, BotID: msg.bot, Guild: msg.guild })) {
|
|
315
|
+
return;
|
|
279
316
|
}
|
|
317
|
+
await ctx2.bots[`${msg.platform}:${msg.bot}`].deleteMessage(msg.guild, msg.msgid).catch((error) => {
|
|
318
|
+
logger.error(`ERROR:<MessageDelete> ${error}`);
|
|
319
|
+
});
|
|
280
320
|
}
|
|
281
321
|
__name(MessageDelete, "MessageDelete");
|
|
282
322
|
function botExistsCheck(ctx2, node) {
|
|
283
323
|
if (ctx2.bots[`${node.Platform}:${node.BotID}`] == void 0) {
|
|
284
|
-
|
|
324
|
+
logger.error(`ERROR:<BOT not Exist> ${node.Platform}:${node.BotID}`);
|
|
325
|
+
return false;
|
|
285
326
|
}
|
|
327
|
+
return true;
|
|
286
328
|
}
|
|
287
329
|
__name(botExistsCheck, "botExistsCheck");
|
|
288
330
|
|
|
@@ -305,15 +347,19 @@ var inject = {
|
|
|
305
347
|
};
|
|
306
348
|
var Config = createConfig();
|
|
307
349
|
function apply(ctx2, cfg) {
|
|
308
|
-
msgCacheInit(ctx2);
|
|
350
|
+
msgCacheInit(ctx2, cfg);
|
|
309
351
|
ctx2.on("message-created", async (session) => {
|
|
310
352
|
const hitGroup = [];
|
|
311
353
|
for (const g in cfg.ForwardGroups) {
|
|
312
354
|
const group = cfg.ForwardGroups[g];
|
|
313
355
|
for (const k in group.Nodes) {
|
|
314
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
|
+
}
|
|
315
360
|
logger.debug(
|
|
316
361
|
"[message-created]",
|
|
362
|
+
session.platform,
|
|
317
363
|
session.channelId + ":" + session.messageId
|
|
318
364
|
);
|
|
319
365
|
let uuid = session.channelId + ":" + session.messageId;
|