koishi-plugin-aka-60s-api 0.0.6 → 0.0.7

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 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("https://192.168.50.55:4399/v2/60s", {
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)) {
@@ -113,8 +184,55 @@ function apply(ctx, config) {
113
184
  logInfo("60s API: 手动重置冷却时间", { userId, hadCooldown });
114
185
  return hadCooldown ? "已重置冷却时间,可以重新使用新闻指令" : "当前没有冷却时间";
115
186
  });
187
+ ctx.command("新闻定时", "管理定时发送新闻").subcommand(".设置 <time>", "设置定时发送时间").action(async (argv, time) => {
188
+ if (!time) {
189
+ return "请提供时间格式,例如: 08:00 / 1d";
190
+ }
191
+ config.scheduleTime = time;
192
+ await setupSchedule();
193
+ return `定时发送时间已设置为: ${time}`;
194
+ }).subcommand(".频道 <channels...>", "设置定时发送频道").action(async (argv, ...channels) => {
195
+ if (!channels || channels.length === 0) {
196
+ return "请提供频道ID,例如: 新闻定时.频道 123456789 987654321";
197
+ }
198
+ config.scheduleChannels = channels;
199
+ await setupSchedule();
200
+ return `定时发送频道已设置为: ${channels.join(", ")}`;
201
+ }).subcommand(".启用", "启用定时发送").action(async (argv) => {
202
+ config.enableSchedule = true;
203
+ await setupSchedule();
204
+ return "定时发送已启用";
205
+ }).subcommand(".禁用", "禁用定时发送").action(async (argv) => {
206
+ config.enableSchedule = false;
207
+ if (scheduleInterval) {
208
+ clearInterval(scheduleInterval);
209
+ scheduleInterval = null;
210
+ }
211
+ return "定时发送已禁用";
212
+ }).subcommand(".状态", "查看定时发送状态").action(async (argv) => {
213
+ const status = config.enableSchedule ? "启用" : "禁用";
214
+ const time = config.scheduleTime;
215
+ const channels = config.scheduleChannels.length > 0 ? config.scheduleChannels.join(", ") : "未设置";
216
+ return `定时发送状态: ${status}
217
+ 发送时间: ${time}
218
+ 发送频道: ${channels}`;
219
+ }).subcommand(".测试", "测试定时发送功能").action(async (argv) => {
220
+ try {
221
+ await sendNewsToChannels();
222
+ return "测试发送完成";
223
+ } catch (error) {
224
+ return `测试发送失败: ${error.message}`;
225
+ }
226
+ });
227
+ ctx.on("ready", async () => {
228
+ await setupSchedule();
229
+ });
116
230
  ctx.on("dispose", () => {
117
231
  cooldowns.clear();
232
+ if (scheduleInterval) {
233
+ clearInterval(scheduleInterval);
234
+ scheduleInterval = null;
235
+ }
118
236
  });
119
237
  }
120
238
  __name(apply, "apply");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-aka-60s-api",
3
3
  "description": "调用60s API转发信息的工具 - 个人用",
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [