koishi-plugin-ets2-tools-tmp 3.3.4 → 3.3.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/api/evmOpenApi.js +30 -0
- package/lib/command/tmpQuery/tmpQueryImg.js +6 -22
- package/lib/command/tmpQuery/tmpQueryText.js +3 -27
- package/lib/index.js +3 -2
- package/lib/util/apiLog.js +37 -16
- package/package.json +1 -1
package/lib/api/evmOpenApi.js
CHANGED
|
@@ -223,4 +223,34 @@ module.exports = {
|
|
|
223
223
|
}
|
|
224
224
|
return data
|
|
225
225
|
},
|
|
226
|
+
/**
|
|
227
|
+
* 查询 Steam 游戏时长(ETS2 227300 / ATS 270880)
|
|
228
|
+
* 通过代理访问 Steam IPlayerService,返回格式化后的 { ets2, ats } 字符串
|
|
229
|
+
* 未配置 apiKey 或请求失败时对应字段为 null
|
|
230
|
+
*/
|
|
231
|
+
async steamGameTime(http, steamApiKey, steamId) {
|
|
232
|
+
if (!steamApiKey || !steamId) {
|
|
233
|
+
return { ets2: null, ats: null }
|
|
234
|
+
}
|
|
235
|
+
const path = `/proxy/steam/IPlayerService/GetOwnedGames/v1?key=${encodeURIComponent(steamApiKey)}&steamid=${steamId}&appids_filter[0]=227300&appids_filter[1]=270880&include_played_free_games=1`
|
|
236
|
+
let response
|
|
237
|
+
try {
|
|
238
|
+
response = await apiLog.get(http, 'evm.steam.gameTime', `${BASE_APIS[0]}${path}`)
|
|
239
|
+
} catch {
|
|
240
|
+
return { ets2: null, ats: null }
|
|
241
|
+
}
|
|
242
|
+
const result = { ets2: null, ats: null }
|
|
243
|
+
if (response?.response?.game_count > 0 && Array.isArray(response.response.games)) {
|
|
244
|
+
for (const game of response.response.games) {
|
|
245
|
+
const minutes = game.playtime_forever
|
|
246
|
+
if (minutes == null) continue
|
|
247
|
+
const h = Math.floor(minutes / 60)
|
|
248
|
+
const m = minutes % 60
|
|
249
|
+
const playtime = `${h}小时${m}分钟`
|
|
250
|
+
if (game.appid === 227300) result.ets2 = playtime
|
|
251
|
+
else if (game.appid === 270880) result.ats = playtime
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return result
|
|
255
|
+
},
|
|
226
256
|
}
|
|
@@ -161,28 +161,12 @@ module.exports = async (ctx, cfg, session, tmpId) => {
|
|
|
161
161
|
data.ets2GameTime = null;
|
|
162
162
|
data.atsGameTime = null;
|
|
163
163
|
if (cfg.commands?.tmpQueryGameTime) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
if (response.response && response.response.game_count > 0) {
|
|
171
|
-
for (const game of response.response.games) {
|
|
172
|
-
const playtimeMinutes = game.playtime_forever;
|
|
173
|
-
const hours = Math.floor(playtimeMinutes / 60);
|
|
174
|
-
const minutes = playtimeMinutes % 60;
|
|
175
|
-
const playtime = `${hours}小时${minutes}分钟`;
|
|
176
|
-
if (game.appid === 227300) {
|
|
177
|
-
data.ets2GameTime = playtime;
|
|
178
|
-
} else if (game.appid === 270880) {
|
|
179
|
-
data.atsGameTime = playtime;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
} catch (error) {
|
|
185
|
-
ctx.logger.error(`查询Steam游戏时长出错: ${error}`);
|
|
164
|
+
const steamApiKey = cfg.steamApi?.key;
|
|
165
|
+
if (steamApiKey) {
|
|
166
|
+
const steamId = playerInfo.data.steamId;
|
|
167
|
+
const gameTimes = await evmOpenApi.steamGameTime(ctx.http, steamApiKey, steamId);
|
|
168
|
+
data.ets2GameTime = gameTimes.ets2;
|
|
169
|
+
data.atsGameTime = gameTimes.ats;
|
|
186
170
|
}
|
|
187
171
|
}
|
|
188
172
|
let page;
|
|
@@ -184,35 +184,11 @@ module.exports = async (ctx, cfg, session, tmpId) => {
|
|
|
184
184
|
|
|
185
185
|
// 查询Steam游戏时长
|
|
186
186
|
async function getSteamGameTimes(steamId) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
if (!steamApiKey) {
|
|
190
|
-
return { ets2: null, ats: null };
|
|
191
|
-
}
|
|
192
|
-
const url = `https://evmapi.cxnnn.cn/proxy/steam/IPlayerService/GetOwnedGames/v1?key=${steamApiKey}&steamid=${steamId}&appids_filter[0]=227300&appids_filter[1]=270880&include_played_free_games=1`;
|
|
193
|
-
const response = await ctx.http.get(url);
|
|
194
|
-
const result = { ets2: null, ats: null };
|
|
195
|
-
|
|
196
|
-
if (response.response && response.response.game_count > 0) {
|
|
197
|
-
for (const game of response.response.games) {
|
|
198
|
-
const playtimeMinutes = game.playtime_forever;
|
|
199
|
-
const hours = Math.floor(playtimeMinutes / 60);
|
|
200
|
-
const minutes = playtimeMinutes % 60;
|
|
201
|
-
const playtime = `${hours}小时${minutes}分钟`;
|
|
202
|
-
|
|
203
|
-
if (game.appid === 227300) {
|
|
204
|
-
result.ets2 = playtime;
|
|
205
|
-
} else if (game.appid === 270880) {
|
|
206
|
-
result.ats = playtime;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return result;
|
|
212
|
-
} catch (error) {
|
|
213
|
-
ctx.logger.error(`查询Steam游戏时长出错: ${error}`);
|
|
187
|
+
const steamApiKey = cfg.steamApi?.key;
|
|
188
|
+
if (!steamApiKey) {
|
|
214
189
|
return { ets2: null, ats: null };
|
|
215
190
|
}
|
|
191
|
+
return evmOpenApi.steamGameTime(ctx.http, steamApiKey, steamId);
|
|
216
192
|
}
|
|
217
193
|
|
|
218
194
|
// 获取欧卡和美卡游戏时长
|
package/lib/index.js
CHANGED
|
@@ -67,7 +67,8 @@ exports.inject = {
|
|
|
67
67
|
|
|
68
68
|
exports.Config = koishi_1.Schema.intersect([
|
|
69
69
|
koishi_1.Schema.object({
|
|
70
|
-
debugMode: koishi_1.Schema.boolean().default(false).description('启用调试模式(输出详细日志)')
|
|
70
|
+
debugMode: koishi_1.Schema.boolean().default(false).description('启用调试模式(输出详细日志)'),
|
|
71
|
+
apiRetryCount: koishi_1.Schema.number().default(2).min(0).max(10).step(1).description('API请求失败时的重试次数(0=不重试,默认2次)')
|
|
71
72
|
}).description('基本配置'),
|
|
72
73
|
koishi_1.Schema.object({
|
|
73
74
|
commands: koishi_1.Schema.object({
|
|
@@ -439,7 +440,7 @@ function registerBaseCommands(ctx, cfg) {
|
|
|
439
440
|
|
|
440
441
|
function apply(ctx, cfg) {
|
|
441
442
|
// 初始化 API 请求日志(调试模式开启后,truckersmp/trucky 等接口会输出请求与错误日志)
|
|
442
|
-
apiLog.init(ctx, cfg.debugMode);
|
|
443
|
+
apiLog.init(ctx, cfg.debugMode, cfg.apiRetryCount);
|
|
443
444
|
|
|
444
445
|
try {
|
|
445
446
|
model(ctx);
|
package/lib/util/apiLog.js
CHANGED
|
@@ -3,6 +3,11 @@
|
|
|
3
3
|
// 请求失败日志始终输出,便于排查接口异常
|
|
4
4
|
let logger = null
|
|
5
5
|
let debugMode = false
|
|
6
|
+
let retryCount = 2
|
|
7
|
+
|
|
8
|
+
function sleep(ms) {
|
|
9
|
+
return new Promise(resolve => setTimeout(resolve, ms))
|
|
10
|
+
}
|
|
6
11
|
|
|
7
12
|
// 从异常对象中尽可能提取错误信息(错误名、消息、HTTP 状态码、错误码、底层原因、响应体)
|
|
8
13
|
// 仅用 e.message 时,网络层异常可能只有空消息(如只显示 "Error"),需补充状态码等上下文
|
|
@@ -37,9 +42,10 @@ module.exports = {
|
|
|
37
42
|
/**
|
|
38
43
|
* 初始化(在插件 apply 时调用)
|
|
39
44
|
*/
|
|
40
|
-
init(ctx, enabled) {
|
|
45
|
+
init(ctx, enabled, retries) {
|
|
41
46
|
logger = ctx.logger
|
|
42
47
|
debugMode = !!enabled
|
|
48
|
+
retryCount = retries != null ? Math.max(0, retries) : 2
|
|
43
49
|
},
|
|
44
50
|
/**
|
|
45
51
|
* 记录调试日志(仅调试模式开启时输出)
|
|
@@ -62,28 +68,43 @@ module.exports = {
|
|
|
62
68
|
*/
|
|
63
69
|
formatError,
|
|
64
70
|
/**
|
|
65
|
-
* 发起 GET
|
|
71
|
+
* 发起 GET 请求并记录日志,失败时自动重试(重试次数由配置控制),最终失败抛出异常
|
|
72
|
+
* 重试间隔递增:500ms、1000ms、1500ms...
|
|
66
73
|
*/
|
|
67
74
|
async get(http, tag, url) {
|
|
68
75
|
if (debugMode && logger) {
|
|
69
76
|
logger.info(`[TMP-BOT API] [${tag}] GET ${url}`)
|
|
70
77
|
}
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
78
|
+
const maxAttempts = retryCount + 1
|
|
79
|
+
let lastError = null
|
|
80
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
81
|
+
const start = Date.now()
|
|
82
|
+
try {
|
|
83
|
+
const result = await http.get(url)
|
|
84
|
+
if (debugMode && logger) {
|
|
85
|
+
logger.info(`[TMP-BOT API] [${tag}] 响应成功, 耗时: ${Date.now() - start}ms${attempt > 1 ? ` (第${attempt}次尝试)` : ''}`)
|
|
86
|
+
}
|
|
87
|
+
return result
|
|
88
|
+
} catch (e) {
|
|
89
|
+
lastError = e
|
|
90
|
+
const isLast = attempt >= maxAttempts
|
|
91
|
+
if (logger) {
|
|
92
|
+
if (isLast) {
|
|
93
|
+
// 最终失败,输出错误日志 + 堆栈
|
|
94
|
+
logger.error(`[TMP-BOT API] [${tag}] 请求失败(已重试${retryCount}次): ${url} (${formatError(e)})`)
|
|
95
|
+
if (debugMode && e && e.stack) {
|
|
96
|
+
logger.error(`[TMP-BOT API] [${tag}] 堆栈: ${e.stack}`)
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
// 中间失败,输出警告(始终可见,便于观察重试情况)
|
|
100
|
+
logger.warn(`[TMP-BOT API] [${tag}] 第${attempt}/${maxAttempts}次请求失败, 将重试: ${formatError(e)}`)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (!isLast) {
|
|
104
|
+
await sleep(500 * attempt)
|
|
84
105
|
}
|
|
85
106
|
}
|
|
86
|
-
throw e
|
|
87
107
|
}
|
|
108
|
+
throw lastError
|
|
88
109
|
}
|
|
89
110
|
}
|