koishi-plugin-aka-60s-api 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.d.ts +9 -0
- package/lib/index.js +148 -0
- package/package.json +1 -1
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Context, Schema } from 'koishi';
|
|
2
|
+
export declare const name = "aka-60s-api";
|
|
3
|
+
export interface Config {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
cooldownTime: number;
|
|
6
|
+
enableLog: boolean;
|
|
7
|
+
}
|
|
8
|
+
export declare const Config: Schema<Config>;
|
|
9
|
+
export declare function apply(ctx: Context, config: Config): void;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name2 in all)
|
|
8
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
Config: () => Config,
|
|
24
|
+
apply: () => apply,
|
|
25
|
+
name: () => name
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(src_exports);
|
|
28
|
+
var import_koishi = require("koishi");
|
|
29
|
+
var name = "aka-60s-api";
|
|
30
|
+
var Config = import_koishi.Schema.object({
|
|
31
|
+
apiKey: import_koishi.Schema.string().required().description("API密钥"),
|
|
32
|
+
cooldownTime: import_koishi.Schema.number().default(30).min(5).max(300).description("冷却时间(秒)"),
|
|
33
|
+
enableLog: import_koishi.Schema.boolean().default(true).description("启用日志记录")
|
|
34
|
+
});
|
|
35
|
+
function apply(ctx, config) {
|
|
36
|
+
const logger = ctx.logger("aka-60s-api");
|
|
37
|
+
const cooldowns = /* @__PURE__ */ new Map();
|
|
38
|
+
function logInfo(message, data) {
|
|
39
|
+
if (config.enableLog && logger) {
|
|
40
|
+
logger.info(message, data);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
__name(logInfo, "logInfo");
|
|
44
|
+
function logError(message, error) {
|
|
45
|
+
if (config.enableLog && logger) {
|
|
46
|
+
logger.error(message, error);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
__name(logError, "logError");
|
|
50
|
+
function checkCooldown(userId) {
|
|
51
|
+
const now = Date.now();
|
|
52
|
+
const lastTime = cooldowns.get(userId) || 0;
|
|
53
|
+
const timeLeft = Math.ceil((lastTime + config.cooldownTime * 1e3 - now) / 1e3);
|
|
54
|
+
if (timeLeft > 0) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
cooldowns.set(userId, now);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
__name(checkCooldown, "checkCooldown");
|
|
61
|
+
async function get60sNews() {
|
|
62
|
+
try {
|
|
63
|
+
logInfo("60s API: 开始获取新闻");
|
|
64
|
+
const response = await ctx.http.get("https://v2.xxapi.cn/api/get60sNews", {
|
|
65
|
+
params: {
|
|
66
|
+
key: config.apiKey
|
|
67
|
+
},
|
|
68
|
+
timeout: 3e4
|
|
69
|
+
});
|
|
70
|
+
logInfo("60s API: 获取新闻成功", {
|
|
71
|
+
code: response.code,
|
|
72
|
+
hasData: !!response.data,
|
|
73
|
+
requestId: response.request_id
|
|
74
|
+
});
|
|
75
|
+
return response;
|
|
76
|
+
} catch (error) {
|
|
77
|
+
logError("60s API: 获取新闻失败", error);
|
|
78
|
+
throw error;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
__name(get60sNews, "get60sNews");
|
|
82
|
+
ctx.command("60s", "获取60秒新闻").action(async (argv) => {
|
|
83
|
+
const userId = argv.session.userId;
|
|
84
|
+
if (!checkCooldown(userId)) {
|
|
85
|
+
const now = Date.now();
|
|
86
|
+
const lastTime = cooldowns.get(userId) || 0;
|
|
87
|
+
const timeLeft = Math.ceil((lastTime + config.cooldownTime * 1e3 - now) / 1e3);
|
|
88
|
+
return `请等待 ${timeLeft} 秒后再试`;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
logInfo("60s API: 用户请求新闻", { userId });
|
|
92
|
+
await argv.session.send("正在获取60秒新闻,请稍候...");
|
|
93
|
+
const response = await get60sNews();
|
|
94
|
+
if (response.code !== 200) {
|
|
95
|
+
logError("60s API: 返回错误", {
|
|
96
|
+
code: response.code,
|
|
97
|
+
msg: response.msg,
|
|
98
|
+
requestId: response.request_id
|
|
99
|
+
});
|
|
100
|
+
return `获取新闻失败: ${response.msg || "未知错误"}`;
|
|
101
|
+
}
|
|
102
|
+
if (!response.data) {
|
|
103
|
+
logError("60s API: 返回数据为空");
|
|
104
|
+
return "获取新闻失败: 未获取到新闻数据";
|
|
105
|
+
}
|
|
106
|
+
const { title, content, image, time } = response.data;
|
|
107
|
+
let message = `📰 ${title}
|
|
108
|
+
|
|
109
|
+
${content}
|
|
110
|
+
|
|
111
|
+
⏰ ${time}`;
|
|
112
|
+
if (image) {
|
|
113
|
+
const imageMessage = import_koishi.h.image(image);
|
|
114
|
+
await argv.session.send(imageMessage);
|
|
115
|
+
}
|
|
116
|
+
logInfo("60s API: 成功发送新闻", {
|
|
117
|
+
title: title.substring(0, 50) + "...",
|
|
118
|
+
hasImage: !!image,
|
|
119
|
+
requestId: response.request_id
|
|
120
|
+
});
|
|
121
|
+
return message;
|
|
122
|
+
} catch (error) {
|
|
123
|
+
logError("60s API: 处理请求失败", {
|
|
124
|
+
error,
|
|
125
|
+
errorMessage: error?.message || "未知错误",
|
|
126
|
+
userId
|
|
127
|
+
});
|
|
128
|
+
return "获取新闻失败,请稍后重试";
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
ctx.command("60s重置", "重置60秒新闻冷却时间").action(async (argv) => {
|
|
132
|
+
const userId = argv.session.userId;
|
|
133
|
+
const hadCooldown = cooldowns.has(userId);
|
|
134
|
+
cooldowns.delete(userId);
|
|
135
|
+
logInfo("60s API: 手动重置冷却时间", { userId, hadCooldown });
|
|
136
|
+
return hadCooldown ? "已重置冷却时间,可以重新使用60s指令" : "当前没有冷却时间";
|
|
137
|
+
});
|
|
138
|
+
ctx.on("dispose", () => {
|
|
139
|
+
cooldowns.clear();
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
__name(apply, "apply");
|
|
143
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
144
|
+
0 && (module.exports = {
|
|
145
|
+
Config,
|
|
146
|
+
apply,
|
|
147
|
+
name
|
|
148
|
+
});
|