koishi-plugin-aka-60s-api 0.0.6 → 0.0.8
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.d.ts +3 -0
- package/lib/index.js +79 -10
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -3,6 +3,9 @@ export declare const name = "aka-60s-api";
|
|
|
3
3
|
export interface Config {
|
|
4
4
|
cooldownTime: number;
|
|
5
5
|
enableLog: boolean;
|
|
6
|
+
enableSchedule: boolean;
|
|
7
|
+
scheduleTime: string;
|
|
8
|
+
scheduleChannels: string[];
|
|
6
9
|
}
|
|
7
10
|
export declare const Config: Schema<Config>;
|
|
8
11
|
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
CHANGED
|
@@ -29,11 +29,15 @@ var import_koishi = require("koishi");
|
|
|
29
29
|
var name = "aka-60s-api";
|
|
30
30
|
var Config = import_koishi.Schema.object({
|
|
31
31
|
cooldownTime: import_koishi.Schema.number().default(30).min(5).max(300).description("冷却时间(秒)"),
|
|
32
|
-
enableLog: import_koishi.Schema.boolean().default(true).description("启用日志记录")
|
|
32
|
+
enableLog: import_koishi.Schema.boolean().default(true).description("启用日志记录"),
|
|
33
|
+
enableSchedule: import_koishi.Schema.boolean().default(false).description("启用定时发送新闻"),
|
|
34
|
+
scheduleTime: import_koishi.Schema.string().default("08:00 / 1d").description("定时发送时间 (格式: HH:MM / 1d)"),
|
|
35
|
+
scheduleChannels: import_koishi.Schema.array(String).default([]).description("定时发送的频道ID列表")
|
|
33
36
|
});
|
|
34
37
|
function apply(ctx, config) {
|
|
35
38
|
const logger = ctx.logger("aka-60s-api");
|
|
36
39
|
const cooldowns = /* @__PURE__ */ new Map();
|
|
40
|
+
let scheduleInterval = null;
|
|
37
41
|
function logInfo(message, data) {
|
|
38
42
|
if (config.enableLog && logger) {
|
|
39
43
|
logger.info(message, data);
|
|
@@ -60,7 +64,7 @@ function apply(ctx, config) {
|
|
|
60
64
|
async function get60sNewsImage() {
|
|
61
65
|
try {
|
|
62
66
|
logInfo("60s API: 开始获取新闻图片");
|
|
63
|
-
const response = await ctx.http.get("
|
|
67
|
+
const response = await ctx.http.get("http://192.168.50.55:4399/v2/60s", {
|
|
64
68
|
params: {
|
|
65
69
|
encoding: "image"
|
|
66
70
|
},
|
|
@@ -78,6 +82,73 @@ function apply(ctx, config) {
|
|
|
78
82
|
}
|
|
79
83
|
}
|
|
80
84
|
__name(get60sNewsImage, "get60sNewsImage");
|
|
85
|
+
async function sendNewsToChannels() {
|
|
86
|
+
if (config.scheduleChannels.length === 0) {
|
|
87
|
+
logInfo("60s API: 没有配置定时发送频道");
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const imageBuffer = await get60sNewsImage();
|
|
92
|
+
const imageMessage = import_koishi.h.image(imageBuffer, "image/png");
|
|
93
|
+
for (const channelId of config.scheduleChannels) {
|
|
94
|
+
try {
|
|
95
|
+
await ctx.broadcast([channelId], imageMessage);
|
|
96
|
+
logInfo("60s API: 定时发送新闻成功", { channelId });
|
|
97
|
+
} catch (error) {
|
|
98
|
+
logError("60s API: 定时发送新闻到频道失败", { channelId, error });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
} catch (error) {
|
|
102
|
+
logError("60s API: 定时发送新闻失败", error);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
__name(sendNewsToChannels, "sendNewsToChannels");
|
|
106
|
+
function parseScheduleTime(timeStr) {
|
|
107
|
+
if (timeStr.includes("/")) {
|
|
108
|
+
const [timePart] = timeStr.split(" / ");
|
|
109
|
+
const [hours, minutes] = timePart.split(":").map(Number);
|
|
110
|
+
const now = /* @__PURE__ */ new Date();
|
|
111
|
+
const targetTime = /* @__PURE__ */ new Date();
|
|
112
|
+
targetTime.setHours(hours, minutes, 0, 0);
|
|
113
|
+
if (targetTime <= now) {
|
|
114
|
+
targetTime.setDate(targetTime.getDate() + 1);
|
|
115
|
+
}
|
|
116
|
+
return targetTime.getTime() - now.getTime();
|
|
117
|
+
} else if (timeStr.includes("h")) {
|
|
118
|
+
const hours = parseInt(timeStr.replace("h", ""));
|
|
119
|
+
return hours * 60 * 60 * 1e3;
|
|
120
|
+
} else if (timeStr.includes("m")) {
|
|
121
|
+
const minutes = parseInt(timeStr.replace("m", ""));
|
|
122
|
+
return minutes * 60 * 1e3;
|
|
123
|
+
} else {
|
|
124
|
+
return 60 * 60 * 1e3;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
__name(parseScheduleTime, "parseScheduleTime");
|
|
128
|
+
function setupSchedule() {
|
|
129
|
+
if (!config.enableSchedule) {
|
|
130
|
+
logInfo("60s API: 定时发送功能已禁用");
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (scheduleInterval) {
|
|
134
|
+
clearInterval(scheduleInterval);
|
|
135
|
+
scheduleInterval = null;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
const interval = parseScheduleTime(config.scheduleTime);
|
|
139
|
+
scheduleInterval = setInterval(async () => {
|
|
140
|
+
await sendNewsToChannels();
|
|
141
|
+
}, interval);
|
|
142
|
+
logInfo("60s API: 定时任务设置成功", {
|
|
143
|
+
scheduleTime: config.scheduleTime,
|
|
144
|
+
interval,
|
|
145
|
+
channels: config.scheduleChannels
|
|
146
|
+
});
|
|
147
|
+
} catch (error) {
|
|
148
|
+
logError("60s API: 设置定时任务失败", error);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
__name(setupSchedule, "setupSchedule");
|
|
81
152
|
ctx.command("新闻", "获取60秒新闻图片").action(async (argv) => {
|
|
82
153
|
const userId = argv.session.userId;
|
|
83
154
|
if (!checkCooldown(userId)) {
|
|
@@ -88,7 +159,6 @@ function apply(ctx, config) {
|
|
|
88
159
|
}
|
|
89
160
|
try {
|
|
90
161
|
logInfo("60s API: 用户请求新闻图片", { userId });
|
|
91
|
-
await argv.session.send("正在获取60秒新闻图片,请稍候...");
|
|
92
162
|
const imageBuffer = await get60sNewsImage();
|
|
93
163
|
const imageMessage = import_koishi.h.image(imageBuffer, "image/png");
|
|
94
164
|
await argv.session.send(imageMessage);
|
|
@@ -96,7 +166,6 @@ function apply(ctx, config) {
|
|
|
96
166
|
size: imageBuffer.length,
|
|
97
167
|
userId
|
|
98
168
|
});
|
|
99
|
-
return "📰 60秒读懂世界新闻图片已发送";
|
|
100
169
|
} catch (error) {
|
|
101
170
|
logError("60s API: 处理新闻图片请求失败", {
|
|
102
171
|
error,
|
|
@@ -106,15 +175,15 @@ function apply(ctx, config) {
|
|
|
106
175
|
return "获取新闻图片失败,请稍后重试";
|
|
107
176
|
}
|
|
108
177
|
});
|
|
109
|
-
ctx.
|
|
110
|
-
|
|
111
|
-
const hadCooldown = cooldowns.has(userId);
|
|
112
|
-
cooldowns.delete(userId);
|
|
113
|
-
logInfo("60s API: 手动重置冷却时间", { userId, hadCooldown });
|
|
114
|
-
return hadCooldown ? "已重置冷却时间,可以重新使用新闻指令" : "当前没有冷却时间";
|
|
178
|
+
ctx.on("ready", async () => {
|
|
179
|
+
await setupSchedule();
|
|
115
180
|
});
|
|
116
181
|
ctx.on("dispose", () => {
|
|
117
182
|
cooldowns.clear();
|
|
183
|
+
if (scheduleInterval) {
|
|
184
|
+
clearInterval(scheduleInterval);
|
|
185
|
+
scheduleInterval = null;
|
|
186
|
+
}
|
|
118
187
|
});
|
|
119
188
|
}
|
|
120
189
|
__name(apply, "apply");
|