koishi-plugin-booknews 0.0.1 → 0.0.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/index.js +24 -32
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -42,34 +42,16 @@ function apply(ctx, config) {
|
|
|
42
42
|
authorName: "string",
|
|
43
43
|
timestamp: "timestamp"
|
|
44
44
|
}, { autoInc: true });
|
|
45
|
-
|
|
46
|
-
if (session.guildId !== config.sourceGroupId) return;
|
|
47
|
-
if (!config.adminIds.includes(session.userId)) return;
|
|
48
|
-
await ctx.database.create("daily_book_news", {
|
|
49
|
-
content: session.content,
|
|
50
|
-
authorName: session.username || session.userId,
|
|
51
|
-
timestamp: /* @__PURE__ */ new Date()
|
|
52
|
-
});
|
|
53
|
-
ctx.logger("booknews").info(`收录一条来自 ${session.username} 的书讯`);
|
|
54
|
-
});
|
|
55
|
-
ctx.cron(config.sendTime, async () => {
|
|
45
|
+
const sendDailyReport = /* @__PURE__ */ __name(async (isManual = false) => {
|
|
56
46
|
const newsList = await ctx.database.get("daily_book_news", {});
|
|
57
|
-
if (newsList.length
|
|
58
|
-
ctx.logger("book").info("书讯数量为0");
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
47
|
+
if (newsList.length === 0) return "今日暂无书讯收录。";
|
|
61
48
|
const bot = ctx.bots[0];
|
|
62
|
-
if (!bot) return;
|
|
63
|
-
const nodes = newsList.map((item) => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
// 节点的发送者 ID
|
|
69
|
-
nickname: item.authorName
|
|
70
|
-
// 节点的发送者昵称
|
|
71
|
-
}, item.content);
|
|
72
|
-
});
|
|
49
|
+
if (!bot) return "未找到可用的机器人实例。";
|
|
50
|
+
const nodes = newsList.map((item) => (0, import_koishi.h)("message", {
|
|
51
|
+
forward: true,
|
|
52
|
+
userId: bot.selfId,
|
|
53
|
+
nickname: item.authorName
|
|
54
|
+
}, import_koishi.h.parse(item.content)));
|
|
73
55
|
nodes.unshift((0, import_koishi.h)("message", {
|
|
74
56
|
forward: true,
|
|
75
57
|
userId: bot.selfId,
|
|
@@ -78,15 +60,25 @@ function apply(ctx, config) {
|
|
|
78
60
|
共收录 ${newsList.length} 条资讯`));
|
|
79
61
|
try {
|
|
80
62
|
await bot.sendMessage(config.dstGroupIds, nodes);
|
|
81
|
-
|
|
82
|
-
await ctx.database.remove("daily_book_news", {});
|
|
63
|
+
return `发送成功,共 ${newsList.length} 条。`;
|
|
83
64
|
} catch (e) {
|
|
84
|
-
ctx.logger("book").error("
|
|
65
|
+
ctx.logger("book").error("发送失败", e);
|
|
66
|
+
return "发送失败,请检查日志。";
|
|
85
67
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
68
|
+
}, "sendDailyReport");
|
|
69
|
+
ctx.on("message", async (session) => {
|
|
70
|
+
if (session.guildId !== config.sourceGroupId) return;
|
|
71
|
+
if (!config.adminIds.includes(session.userId)) return;
|
|
72
|
+
await ctx.database.create("daily_book_news", {
|
|
73
|
+
content: session.content,
|
|
74
|
+
authorName: session.username || session.userId,
|
|
75
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
89
76
|
});
|
|
77
|
+
session.send("已收录书讯");
|
|
78
|
+
});
|
|
79
|
+
ctx.cron(config.sendTime, () => sendDailyReport(false));
|
|
80
|
+
ctx.command("sendnews", "手动触发发送今日书讯").option("clear", "-c 发送后清空数据库", { fallback: false }).action(async ({ options }) => {
|
|
81
|
+
return await sendDailyReport(!options.clear);
|
|
90
82
|
});
|
|
91
83
|
}
|
|
92
84
|
__name(apply, "apply");
|