koishi-plugin-forward-hime 1.2.1 → 1.2.3
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 +7 -4
- package/lib/decorator.d.ts +1 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +127 -81
- package/package.json +1 -1
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
|
|
15
|
-
export declare function
|
|
16
|
-
export declare function
|
|
17
|
-
export declare function
|
|
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>;
|
package/lib/decorator.d.ts
CHANGED
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
|
|
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 = [
|
|
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,9 @@ function MsgDecorator(session, node) {
|
|
|
136
200
|
} else {
|
|
137
201
|
elems = defaultMiddleware(session);
|
|
138
202
|
}
|
|
139
|
-
|
|
140
|
-
elems = fn(elems);
|
|
141
|
-
}
|
|
203
|
+
for (const fn of localDecorators) {
|
|
204
|
+
elems = await fn(session, node, elems);
|
|
205
|
+
}
|
|
142
206
|
if (_platform_out && typeof _platform_out.Decorator === "function") {
|
|
143
207
|
return _platform_out.Decorator(elems);
|
|
144
208
|
} else {
|
|
@@ -146,69 +210,51 @@ function MsgDecorator(session, node) {
|
|
|
146
210
|
}
|
|
147
211
|
}
|
|
148
212
|
__name(MsgDecorator, "MsgDecorator");
|
|
149
|
-
function
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
content[key]
|
|
213
|
+
async function atTranslator(session, node, { head, content }) {
|
|
214
|
+
const newMsg = await new Promise((resolve, reject) => {
|
|
215
|
+
let newContent = [];
|
|
216
|
+
for (const key in content) {
|
|
217
|
+
const element = content[key];
|
|
218
|
+
if (element.type === "at") {
|
|
219
|
+
if (element.attrs.id !== session.selfId) {
|
|
220
|
+
newContent.push(
|
|
221
|
+
(0, import_koishi5.h)("span", `@${element.attrs.name || element.attrs.id}`)
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
} else {
|
|
225
|
+
newContent.push(element);
|
|
226
|
+
}
|
|
154
227
|
}
|
|
155
|
-
|
|
156
|
-
|
|
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);
|
|
228
|
+
resolve({ head, content: newContent });
|
|
229
|
+
});
|
|
230
|
+
return newMsg;
|
|
175
231
|
}
|
|
176
|
-
__name(
|
|
177
|
-
async function
|
|
178
|
-
if (
|
|
179
|
-
return;
|
|
232
|
+
__name(atTranslator, "atTranslator");
|
|
233
|
+
async function quoteTranslator(session, node, { head, content }) {
|
|
234
|
+
if (!session.quote || !session.quote.id) {
|
|
235
|
+
return { head, content };
|
|
180
236
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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);
|
|
237
|
+
const key = session.channelId + ":" + session.quote.id;
|
|
238
|
+
const cache = await msgCacheFindByKey(key);
|
|
239
|
+
if (cache) {
|
|
240
|
+
let msgid = await msgCacheGetLocalIDByUUID(node, cache.uuid);
|
|
241
|
+
if (msgid) {
|
|
242
|
+
head.unshift((0, import_koishi5.h)("quote", { id: msgid }));
|
|
243
|
+
} else {
|
|
244
|
+
logger.error(`[msgCacheGetLocalIDByUUID] ${cache.uuid} not found`);
|
|
200
245
|
}
|
|
201
246
|
}
|
|
202
|
-
return
|
|
247
|
+
return { head, content };
|
|
203
248
|
}
|
|
204
|
-
__name(
|
|
249
|
+
__name(quoteTranslator, "quoteTranslator");
|
|
205
250
|
|
|
206
251
|
// src/message.ts
|
|
207
|
-
async function MessageForward(
|
|
252
|
+
async function MessageForward(ctx2, node, session) {
|
|
253
|
+
const uuid = session.channelId + ":" + session.messageId;
|
|
254
|
+
const content = await MsgDecorator(session, node);
|
|
208
255
|
try {
|
|
209
|
-
botExistsCheck(
|
|
210
|
-
|
|
211
|
-
await ctx.bots[`${node.Platform}:${node.BotID}`].sendMessage(node.Guild, MsgDecorator(session, node)).then((res) => {
|
|
256
|
+
botExistsCheck(ctx2, node);
|
|
257
|
+
await ctx2.bots[`${node.Platform}:${node.BotID}`].sendMessage(node.Guild, content).then((res) => {
|
|
212
258
|
let mc = {
|
|
213
259
|
platform: node.Platform,
|
|
214
260
|
bot: node.BotID,
|
|
@@ -216,25 +262,25 @@ async function MessageForward(ctx, node, session) {
|
|
|
216
262
|
msgid: res[0],
|
|
217
263
|
uuid
|
|
218
264
|
};
|
|
219
|
-
msgCache(
|
|
220
|
-
logger.debug(
|
|
265
|
+
msgCache(mc);
|
|
266
|
+
logger.debug(`[MessageForward] to ${mc.platform} ${mc.uuid}`);
|
|
221
267
|
});
|
|
222
268
|
} catch (error) {
|
|
223
|
-
logger.error(`ERROR:<MessageSend> ${error}
|
|
269
|
+
logger.error(`ERROR:<MessageSend> ${error}`, content);
|
|
224
270
|
}
|
|
225
271
|
}
|
|
226
272
|
__name(MessageForward, "MessageForward");
|
|
227
|
-
async function MessageDelete(
|
|
273
|
+
async function MessageDelete(ctx2, msg) {
|
|
228
274
|
try {
|
|
229
|
-
botExistsCheck(
|
|
230
|
-
await
|
|
275
|
+
botExistsCheck(ctx2, { Platform: msg.platform, BotID: msg.bot, Guild: msg.guild });
|
|
276
|
+
await ctx2.bots[`${msg.platform}:${msg.bot}`].deleteMessage(msg.guild, msg.msgid);
|
|
231
277
|
} catch (error) {
|
|
232
278
|
logger.error(`ERROR:<MessageDelete> ${error}`);
|
|
233
279
|
}
|
|
234
280
|
}
|
|
235
281
|
__name(MessageDelete, "MessageDelete");
|
|
236
|
-
function botExistsCheck(
|
|
237
|
-
if (
|
|
282
|
+
function botExistsCheck(ctx2, node) {
|
|
283
|
+
if (ctx2.bots[`${node.Platform}:${node.BotID}`] == void 0) {
|
|
238
284
|
throw new Error("Bot Not Found");
|
|
239
285
|
}
|
|
240
286
|
}
|
|
@@ -242,9 +288,8 @@ __name(botExistsCheck, "botExistsCheck");
|
|
|
242
288
|
|
|
243
289
|
// src/index.ts
|
|
244
290
|
var name = `forward hime - 转发姬`;
|
|
245
|
-
var version = `<sub>v1.2.1</sub>`;
|
|
246
291
|
var usage = `
|
|
247
|
-
# ${name}
|
|
292
|
+
# ${name}
|
|
248
293
|
## 配置说明
|
|
249
294
|
1. 添加 \`互通转发组\`
|
|
250
295
|
2. 为添加的 \`互通转发组\` 配置转发节点。每个转发节点对应某个平台的一个频道或群组
|
|
@@ -259,8 +304,9 @@ var inject = {
|
|
|
259
304
|
optional: ["cache"]
|
|
260
305
|
};
|
|
261
306
|
var Config = createConfig();
|
|
262
|
-
function apply(
|
|
263
|
-
|
|
307
|
+
function apply(ctx2, cfg) {
|
|
308
|
+
msgCacheInit(ctx2);
|
|
309
|
+
ctx2.on("message-created", async (session) => {
|
|
264
310
|
const hitGroup = [];
|
|
265
311
|
for (const g in cfg.ForwardGroups) {
|
|
266
312
|
const group = cfg.ForwardGroups[g];
|
|
@@ -271,7 +317,7 @@ function apply(ctx, cfg) {
|
|
|
271
317
|
session.channelId + ":" + session.messageId
|
|
272
318
|
);
|
|
273
319
|
let uuid = session.channelId + ":" + session.messageId;
|
|
274
|
-
msgCache(
|
|
320
|
+
msgCache({
|
|
275
321
|
platform: session.platform,
|
|
276
322
|
bot: group.Nodes[k].BotID,
|
|
277
323
|
guild: session.channelId,
|
|
@@ -287,25 +333,25 @@ function apply(ctx, cfg) {
|
|
|
287
333
|
const group = cfg.ForwardGroups[hitGroup[g]];
|
|
288
334
|
for (const k in group.Nodes) {
|
|
289
335
|
if (group.Nodes[k].Guild !== session.channelId) {
|
|
290
|
-
MessageForward(
|
|
336
|
+
MessageForward(ctx2, group.Nodes[k], session);
|
|
291
337
|
}
|
|
292
338
|
}
|
|
293
339
|
}
|
|
294
340
|
});
|
|
295
|
-
|
|
341
|
+
ctx2.on("message-deleted", async (session) => {
|
|
296
342
|
const cacheKey = session.channelId + ":" + session.messageId;
|
|
297
343
|
logger.debug("[message-deleted]", cacheKey);
|
|
298
|
-
msgCacheFindByKey(
|
|
344
|
+
msgCacheFindByKey(cacheKey).then((res) => {
|
|
299
345
|
if (res) {
|
|
300
346
|
logger.debug("[msgCacheFindByKey]", res);
|
|
301
|
-
msgCacheDelete(
|
|
347
|
+
msgCacheDelete(cacheKey);
|
|
302
348
|
const uuid = res.uuid;
|
|
303
|
-
msgCacheFindByUUID(
|
|
349
|
+
msgCacheFindByUUID(uuid).then((res2) => {
|
|
304
350
|
if (res2) {
|
|
305
351
|
for (const k in res2) {
|
|
306
352
|
logger.debug("[message-delete]", res2[k][1]);
|
|
307
|
-
msgCacheDelete(
|
|
308
|
-
MessageDelete(
|
|
353
|
+
msgCacheDelete(res2[k][0]).then(() => {
|
|
354
|
+
MessageDelete(ctx2, res2[k][1]);
|
|
309
355
|
});
|
|
310
356
|
}
|
|
311
357
|
}
|
|
@@ -313,7 +359,7 @@ function apply(ctx, cfg) {
|
|
|
313
359
|
}
|
|
314
360
|
});
|
|
315
361
|
});
|
|
316
|
-
|
|
362
|
+
ctx2.on("message-updated", async (session) => {
|
|
317
363
|
logger.debug("[message-updated]", session.messageId, session);
|
|
318
364
|
});
|
|
319
365
|
}
|