koishi-plugin-aka-60s-api 0.0.4 → 0.0.6
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 -46
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -57,28 +57,28 @@ function apply(ctx, config) {
|
|
|
57
57
|
return true;
|
|
58
58
|
}
|
|
59
59
|
__name(checkCooldown, "checkCooldown");
|
|
60
|
-
async function
|
|
60
|
+
async function get60sNewsImage() {
|
|
61
61
|
try {
|
|
62
|
-
logInfo("60s API:
|
|
62
|
+
logInfo("60s API: 开始获取新闻图片");
|
|
63
63
|
const response = await ctx.http.get("https://192.168.50.55:4399/v2/60s", {
|
|
64
64
|
params: {
|
|
65
|
-
encoding: "
|
|
65
|
+
encoding: "image"
|
|
66
66
|
},
|
|
67
|
-
timeout: 3e4
|
|
67
|
+
timeout: 3e4,
|
|
68
|
+
responseType: "arraybuffer"
|
|
68
69
|
});
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
date: response.data?.date
|
|
70
|
+
const buffer = Buffer.from(response);
|
|
71
|
+
logInfo("60s API: 获取新闻图片成功", {
|
|
72
|
+
size: buffer.length
|
|
73
73
|
});
|
|
74
|
-
return
|
|
74
|
+
return buffer;
|
|
75
75
|
} catch (error) {
|
|
76
|
-
logError("60s API:
|
|
76
|
+
logError("60s API: 获取新闻图片失败", error);
|
|
77
77
|
throw error;
|
|
78
78
|
}
|
|
79
79
|
}
|
|
80
|
-
__name(
|
|
81
|
-
ctx.command("
|
|
80
|
+
__name(get60sNewsImage, "get60sNewsImage");
|
|
81
|
+
ctx.command("新闻", "获取60秒新闻图片").action(async (argv) => {
|
|
82
82
|
const userId = argv.session.userId;
|
|
83
83
|
if (!checkCooldown(userId)) {
|
|
84
84
|
const now = Date.now();
|
|
@@ -87,45 +87,23 @@ function apply(ctx, config) {
|
|
|
87
87
|
return `请等待 ${timeLeft} 秒后再试`;
|
|
88
88
|
}
|
|
89
89
|
try {
|
|
90
|
-
logInfo("60s API:
|
|
91
|
-
await argv.session.send("正在获取60
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
return `获取新闻失败: ${response.message || "未知错误"}`;
|
|
99
|
-
}
|
|
100
|
-
if (!response.data) {
|
|
101
|
-
logError("60s API: 返回数据为空");
|
|
102
|
-
return "获取新闻失败: 未获取到新闻数据";
|
|
103
|
-
}
|
|
104
|
-
const { date, news, weiyu } = response.data;
|
|
105
|
-
let message = `📰 ${date} 每日60秒读懂世界
|
|
106
|
-
|
|
107
|
-
`;
|
|
108
|
-
news.forEach((item, index) => {
|
|
109
|
-
message += `${item}
|
|
110
|
-
`;
|
|
111
|
-
});
|
|
112
|
-
if (weiyu) {
|
|
113
|
-
message += `
|
|
114
|
-
💭 ${weiyu}`;
|
|
115
|
-
}
|
|
116
|
-
logInfo("60s API: 成功发送新闻", {
|
|
117
|
-
date,
|
|
118
|
-
newsCount: news.length,
|
|
119
|
-
hasWeiyu: !!weiyu
|
|
90
|
+
logInfo("60s API: 用户请求新闻图片", { userId });
|
|
91
|
+
await argv.session.send("正在获取60秒新闻图片,请稍候...");
|
|
92
|
+
const imageBuffer = await get60sNewsImage();
|
|
93
|
+
const imageMessage = import_koishi.h.image(imageBuffer, "image/png");
|
|
94
|
+
await argv.session.send(imageMessage);
|
|
95
|
+
logInfo("60s API: 成功发送新闻图片", {
|
|
96
|
+
size: imageBuffer.length,
|
|
97
|
+
userId
|
|
120
98
|
});
|
|
121
|
-
return
|
|
99
|
+
return "📰 60秒读懂世界新闻图片已发送";
|
|
122
100
|
} catch (error) {
|
|
123
|
-
logError("60s API:
|
|
101
|
+
logError("60s API: 处理新闻图片请求失败", {
|
|
124
102
|
error,
|
|
125
103
|
errorMessage: error?.message || "未知错误",
|
|
126
104
|
userId
|
|
127
105
|
});
|
|
128
|
-
return "
|
|
106
|
+
return "获取新闻图片失败,请稍后重试";
|
|
129
107
|
}
|
|
130
108
|
});
|
|
131
109
|
ctx.command("60s重置", "重置60秒新闻冷却时间").action(async (argv) => {
|
|
@@ -133,7 +111,7 @@ function apply(ctx, config) {
|
|
|
133
111
|
const hadCooldown = cooldowns.has(userId);
|
|
134
112
|
cooldowns.delete(userId);
|
|
135
113
|
logInfo("60s API: 手动重置冷却时间", { userId, hadCooldown });
|
|
136
|
-
return hadCooldown ? "
|
|
114
|
+
return hadCooldown ? "已重置冷却时间,可以重新使用新闻指令" : "当前没有冷却时间";
|
|
137
115
|
});
|
|
138
116
|
ctx.on("dispose", () => {
|
|
139
117
|
cooldowns.clear();
|