koishi-plugin-booknews 0.0.1 → 0.0.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/index.js +34 -36
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -42,6 +42,36 @@ function apply(ctx, config) {
|
|
|
42
42
|
authorName: "string",
|
|
43
43
|
timestamp: "timestamp"
|
|
44
44
|
}, { autoInc: true });
|
|
45
|
+
const sendDailyReport = /* @__PURE__ */ __name(async (isManual = false) => {
|
|
46
|
+
const newsList = await ctx.database.get("daily_book_news", {});
|
|
47
|
+
if (newsList.length === 0) return "今日暂无书讯。";
|
|
48
|
+
const bot = ctx.bots[0];
|
|
49
|
+
if (!bot) return;
|
|
50
|
+
const chunkSize = 10;
|
|
51
|
+
for (let i = 0; i < newsList.length; i += chunkSize) {
|
|
52
|
+
const chunk = newsList.slice(i, i + chunkSize);
|
|
53
|
+
const nodes = chunk.map((item) => (0, import_koishi.h)("message", {
|
|
54
|
+
forward: true,
|
|
55
|
+
userId: bot.selfId,
|
|
56
|
+
nickname: item.authorName
|
|
57
|
+
}, import_koishi.h.parse(item.content)));
|
|
58
|
+
const partNum = Math.floor(i / chunkSize) + 1;
|
|
59
|
+
const totalParts = Math.ceil(newsList.length / chunkSize);
|
|
60
|
+
nodes.unshift((0, import_koishi.h)("message", {
|
|
61
|
+
forward: true,
|
|
62
|
+
userId: bot.selfId,
|
|
63
|
+
nickname: "书讯助手"
|
|
64
|
+
}, `书讯汇总 (${partNum}/${totalParts})
|
|
65
|
+
本段包含 ${chunk.length} 条资讯`));
|
|
66
|
+
try {
|
|
67
|
+
await bot.sendMessage(config.dstGroupIds, nodes);
|
|
68
|
+
await new Promise((resolve) => setTimeout(resolve, 2e3));
|
|
69
|
+
} catch (e) {
|
|
70
|
+
ctx.logger("book").error(`分段 ${partNum} 发送失败`, e);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return `已尝试发送 ${newsList.length} 条书讯(分段处理完毕)。`;
|
|
74
|
+
}, "sendDailyReport");
|
|
45
75
|
ctx.on("message", async (session) => {
|
|
46
76
|
if (session.guildId !== config.sourceGroupId) return;
|
|
47
77
|
if (!config.adminIds.includes(session.userId)) return;
|
|
@@ -50,43 +80,11 @@ function apply(ctx, config) {
|
|
|
50
80
|
authorName: session.username || session.userId,
|
|
51
81
|
timestamp: /* @__PURE__ */ new Date()
|
|
52
82
|
});
|
|
53
|
-
|
|
83
|
+
session.send("已收录书讯");
|
|
54
84
|
});
|
|
55
|
-
ctx.cron(config.sendTime,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
ctx.logger("book").info("书讯数量为0");
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const bot = ctx.bots[0];
|
|
62
|
-
if (!bot) return;
|
|
63
|
-
const nodes = newsList.map((item) => {
|
|
64
|
-
return (0, import_koishi.h)("message", {
|
|
65
|
-
forward: true,
|
|
66
|
-
// 标记这是一个转发节点
|
|
67
|
-
userId: bot.selfId,
|
|
68
|
-
// 节点的发送者 ID
|
|
69
|
-
nickname: item.authorName
|
|
70
|
-
// 节点的发送者昵称
|
|
71
|
-
}, item.content);
|
|
72
|
-
});
|
|
73
|
-
nodes.unshift((0, import_koishi.h)("message", {
|
|
74
|
-
forward: true,
|
|
75
|
-
userId: bot.selfId,
|
|
76
|
-
nickname: "书讯助手"
|
|
77
|
-
}, `${(/* @__PURE__ */ new Date()).toLocaleDateString()} 书讯汇总
|
|
78
|
-
共收录 ${newsList.length} 条资讯`));
|
|
79
|
-
try {
|
|
80
|
-
await bot.sendMessage(config.dstGroupIds, nodes);
|
|
81
|
-
ctx.logger("book").info(`日报发送成功,共 ${newsList.length} 条书讯`);
|
|
82
|
-
await ctx.database.remove("daily_book_news", {});
|
|
83
|
-
} catch (e) {
|
|
84
|
-
ctx.logger("book").error("日报发送失败", e);
|
|
85
|
-
}
|
|
86
|
-
ctx.command("sendNews", "手动触发发送今日书讯 (不清空数据库)").action(async () => {
|
|
87
|
-
await bot.sendMessage(config.dstGroupIds, nodes);
|
|
88
|
-
ctx.logger("book").info(`日报发送成功,共 ${newsList.length} 条书讯`);
|
|
89
|
-
});
|
|
85
|
+
ctx.cron(config.sendTime, () => sendDailyReport(false));
|
|
86
|
+
ctx.command("sendnews", "手动触发发送今日书讯").option("clear", "-c 发送后清空数据库", { fallback: false }).action(async ({ options }) => {
|
|
87
|
+
return await sendDailyReport(!options.clear);
|
|
90
88
|
});
|
|
91
89
|
}
|
|
92
90
|
__name(apply, "apply");
|