koishi-plugin-ets2-tools-tmp 3.3.2 → 3.3.3

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.
@@ -1,10 +1,15 @@
1
+ const apiLog = require('../util/apiLog')
1
2
  const BASE_APIS = ['https://evmapi.cxnnn.cn']
2
3
 
3
4
  async function requestWithFallback(http, path) {
4
5
  let lastError = null
5
6
  for (const baseUrl of BASE_APIS) {
6
7
  try {
7
- const result = await http.get(`${baseUrl}${path}`)
8
+ const result = await apiLog.get(http, `evm${path}`, `${baseUrl}${path}`)
9
+ // 业务层错误码日志(如 10001 玩家不存在等,仅调试模式输出)
10
+ if (result && result.code !== undefined && result.code !== 200) {
11
+ apiLog.debug(`evm${path}`, `业务响应错误码: code=${result.code}, msg=${result.msg || '无'}`)
12
+ }
8
13
  return result
9
14
  } catch (e) {
10
15
  lastError = e
@@ -21,7 +26,7 @@ module.exports = {
21
26
  let result = null
22
27
  try {
23
28
  result = game === 'ATS'
24
- ? await http.get(`${BASE_APIS[0]}/proxy/truckersmp/servers`)
29
+ ? await apiLog.get(http, 'evm.proxy.truckersmp.servers', `${BASE_APIS[0]}/proxy/truckersmp/servers`)
25
30
  : await requestWithFallback(http, `/server/list`)
26
31
  } catch {
27
32
  return {
@@ -1,5 +1,17 @@
1
+ const apiLog = require('../util/apiLog');
1
2
  const BASE_API = 'https://evmapi.cxnnn.cn/proxy/truckersmp';
2
3
  // const BASE_API = 'https://api.truckersmp.com/v2';
4
+
5
+ // 解析 TMP 接口的 error 字段;响应结构异常时记录日志并按错误处理,避免 JSON.parse 直接崩溃
6
+ function parseError(result, tag) {
7
+ try {
8
+ return !!JSON.parse(result?.error)
9
+ } catch {
10
+ apiLog.error(tag, `响应结构异常: ${JSON.stringify(result).slice(0, 300)}`)
11
+ return true
12
+ }
13
+ }
14
+
3
15
  module.exports = {
4
16
  /**
5
17
  * 查询玩家信息
@@ -7,7 +19,7 @@ module.exports = {
7
19
  async player(http, tmpId) {
8
20
  let result = null;
9
21
  try {
10
- result = await http.get(`${BASE_API}/player/${tmpId}`);
22
+ result = await apiLog.get(http, 'tmp.player', `${BASE_API}/player/${tmpId}`);
11
23
  }
12
24
  catch {
13
25
  return {
@@ -16,7 +28,7 @@ module.exports = {
16
28
  }
17
29
  // 拼接返回数据
18
30
  let data = {
19
- error: JSON.parse(result.error)
31
+ error: parseError(result, 'tmp.player')
20
32
  };
21
33
  if (!data.error) {
22
34
  data.data = result.response;
@@ -29,7 +41,7 @@ module.exports = {
29
41
  async servers(http) {
30
42
  let result = null;
31
43
  try {
32
- result = await http.get(`${BASE_API}/servers`);
44
+ result = await apiLog.get(http, 'tmp.servers', `${BASE_API}/servers`);
33
45
  }
34
46
  catch {
35
47
  return {
@@ -38,7 +50,7 @@ module.exports = {
38
50
  }
39
51
  // 拼接返回数据
40
52
  let data = {
41
- error: JSON.parse(result.error)
53
+ error: parseError(result, 'tmp.servers')
42
54
  };
43
55
  if (!data.error) {
44
56
  data.data = result.response;
@@ -51,7 +63,7 @@ module.exports = {
51
63
  async bans(http, tmpId) {
52
64
  let result = null;
53
65
  try {
54
- result = await http.get(`${BASE_API}/bans/${tmpId}`);
66
+ result = await apiLog.get(http, 'tmp.bans', `${BASE_API}/bans/${tmpId}`);
55
67
  }
56
68
  catch {
57
69
  return {
@@ -60,7 +72,7 @@ module.exports = {
60
72
  }
61
73
  // 拼接返回数据
62
74
  let data = {
63
- error: JSON.parse(result.error)
75
+ error: parseError(result, 'tmp.bans')
64
76
  };
65
77
  if (!data.error) {
66
78
  data.data = result.response;
@@ -73,7 +85,7 @@ module.exports = {
73
85
  async version(http) {
74
86
  let result = null;
75
87
  try {
76
- result = await http.get(`${BASE_API}/version`);
88
+ result = await apiLog.get(http, 'tmp.version', `${BASE_API}/version`);
77
89
  }
78
90
  catch {
79
91
  return {
@@ -92,7 +104,7 @@ module.exports = {
92
104
  async vtcMember(http, vtcId, memberId) {
93
105
  let result = null;
94
106
  try {
95
- result = await http.get(`${BASE_API}/vtc/${vtcId}/member/${memberId}`);
107
+ result = await apiLog.get(http, 'tmp.vtcMember', `${BASE_API}/vtc/${vtcId}/member/${memberId}`);
96
108
  }
97
109
  catch {
98
110
  return {
@@ -101,7 +113,7 @@ module.exports = {
101
113
  }
102
114
  // 拼接返回数据
103
115
  let data = {
104
- error: JSON.parse(result.error)
116
+ error: parseError(result, 'tmp.vtcMember')
105
117
  };
106
118
  if (!data.error) {
107
119
  data.data = result.response;
@@ -114,7 +126,7 @@ module.exports = {
114
126
  async vtc(http, vtcId) {
115
127
  let result = null
116
128
  try {
117
- result = await http.get(`${BASE_API}/vtc/${vtcId}`)
129
+ result = await apiLog.get(http, 'tmp.vtc', `${BASE_API}/vtc/${vtcId}`)
118
130
  } catch {
119
131
  return {
120
132
  error: true
@@ -123,7 +135,7 @@ module.exports = {
123
135
 
124
136
  // 拼接返回数据
125
137
  let data = {
126
- error: JSON.parse(result.error)
138
+ error: parseError(result, 'tmp.vtc')
127
139
  }
128
140
  if (!data.error) {
129
141
  data.data = result.response
@@ -1,3 +1,4 @@
1
+ const apiLog = require('../util/apiLog');
1
2
  const BASE_API = 'https://evmapi.cxnnn.cn/proxy/ets2map';
2
3
  // const BASE_API = 'https://tracker.ets2map.com';
3
4
  module.exports = {
@@ -7,7 +8,7 @@ module.exports = {
7
8
  async area(http, serverId, x1, y1, x2, y2) {
8
9
  let result = null;
9
10
  try {
10
- result = await http.get(`${BASE_API}/v3/area?x1=${x1}&y1=${y1}&x2=${x2}&y2=${y2}&server=${serverId}`);
11
+ result = await apiLog.get(http, 'ets2map.area', `${BASE_API}/v3/area?x1=${x1}&y1=${y1}&x2=${x2}&y2=${y2}&server=${serverId}`);
11
12
  }
12
13
  catch {
13
14
  return {
@@ -1,3 +1,4 @@
1
+ const apiLog = require('../util/apiLog')
1
2
  const BASE_API = 'https://evmapi.cxnnn.cn/proxy/trucky'
2
3
  // const BASE_API = 'https://api.codetabs.com/v1/proxy/?quest=https://api.truckyapp.com'
3
4
 
@@ -8,7 +9,7 @@ module.exports = {
8
9
  async online (http, tmpId) {
9
10
  let result = null
10
11
  try {
11
- result = await http.get(`${BASE_API}/v3/map/online?playerID=${tmpId}`)
12
+ result = await apiLog.get(http, 'trucky.online', `${BASE_API}/v3/map/online?playerID=${tmpId}`)
12
13
  } catch {
13
14
  return {
14
15
  error: true
@@ -30,7 +31,7 @@ module.exports = {
30
31
  async trafficTop (http, serverName) {
31
32
  let result = null
32
33
  try {
33
- result = await http.get(`${BASE_API}/v2/traffic/top?game=ets2&server=${serverName}`)
34
+ result = await apiLog.get(http, 'trucky.trafficTop', `${BASE_API}/v2/traffic/top?game=ets2&server=${serverName}`)
34
35
  } catch {
35
36
  return {
36
37
  error: true
package/lib/index.js CHANGED
@@ -28,6 +28,7 @@ const commands = {
28
28
  tmpFootprint: require('./command/tmpFootprint')
29
29
  };
30
30
  const { ActivityService } = require('./command/tmpActivityService');
31
+ const apiLog = require('./util/apiLog');
31
32
 
32
33
  const __defProp = Object.defineProperty;
33
34
  const __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -437,6 +438,9 @@ function registerBaseCommands(ctx, cfg) {
437
438
  }
438
439
 
439
440
  function apply(ctx, cfg) {
441
+ // 初始化 API 请求日志(调试模式开启后,truckersmp/trucky 等接口会输出请求与错误日志)
442
+ apiLog.init(ctx, cfg.debugMode);
443
+
440
444
  try {
441
445
  model(ctx);
442
446
  if (cfg.debugMode) {
@@ -0,0 +1,52 @@
1
+ // API 请求日志工具:为各 API 模块提供统一的调试/错误日志
2
+ // debug 日志(请求地址、耗时)仅在插件开启调试模式时输出;
3
+ // 请求失败日志始终输出,便于排查接口异常
4
+ let logger = null
5
+ let debugMode = false
6
+
7
+ module.exports = {
8
+ /**
9
+ * 初始化(在插件 apply 时调用)
10
+ */
11
+ init(ctx, enabled) {
12
+ logger = ctx.logger
13
+ debugMode = !!enabled
14
+ },
15
+ /**
16
+ * 记录调试日志(仅调试模式开启时输出)
17
+ */
18
+ debug(tag, message) {
19
+ if (debugMode && logger) {
20
+ logger.info(`[TMP-BOT API] [${tag}] ${message}`)
21
+ }
22
+ },
23
+ /**
24
+ * 记录接口业务层错误日志(始终输出)
25
+ */
26
+ error(tag, message) {
27
+ if (logger) {
28
+ logger.error(`[TMP-BOT API] [${tag}] ${message}`)
29
+ }
30
+ },
31
+ /**
32
+ * 发起 GET 请求并记录日志,失败时记录错误日志后抛出异常(由调用方原有 catch 处理)
33
+ */
34
+ async get(http, tag, url) {
35
+ if (debugMode && logger) {
36
+ logger.info(`[TMP-BOT API] [${tag}] GET ${url}`)
37
+ }
38
+ const start = Date.now()
39
+ try {
40
+ const result = await http.get(url)
41
+ if (debugMode && logger) {
42
+ logger.info(`[TMP-BOT API] [${tag}] 响应成功, 耗时: ${Date.now() - start}ms`)
43
+ }
44
+ return result
45
+ } catch (e) {
46
+ if (logger) {
47
+ logger.error(`[TMP-BOT API] [${tag}] 请求失败: ${url} (${e.message || e})`)
48
+ }
49
+ throw e
50
+ }
51
+ }
52
+ }
@@ -13,8 +13,16 @@ module.exports = async (ctx, cfg, content, cache = true) => {
13
13
  }
14
14
  let randomInt = Math.floor(Math.random() * 10000);
15
15
  let sign = md5(cfg.baiduTranslate.appId + content + randomInt + cfg.baiduTranslate.key);
16
- let result = await ctx.http.get(`${TRANSLATE_API}?q=${encodeURI(content)}&from=auto&to=zh&appid=${cfg.baiduTranslate.appId}&salt=${randomInt}&sign=${sign}`);
16
+ let result;
17
+ try {
18
+ result = await ctx.http.get(`${TRANSLATE_API}?q=${encodeURI(content)}&from=auto&to=zh&appid=${cfg.baiduTranslate.appId}&salt=${randomInt}&sign=${sign}`);
19
+ } catch (error) {
20
+ // 翻译失败时降级返回原文,不影响主流程
21
+ ctx.logger.error(`[TMP-BOT] 百度翻译请求失败: ${error.message || error}`);
22
+ return content;
23
+ }
17
24
  if (result.error_code) {
25
+ ctx.logger.error(`[TMP-BOT] 百度翻译返回错误: error_code=${result.error_code}, error_msg=${result.error_msg || '无'}`);
18
26
  return content;
19
27
  }
20
28
  if (cfg.baiduTranslate.enableCache && cache) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-ets2-tools-tmp",
3
3
  "description": "欧卡2 TruckersMP信息查询、车队平台查询及活动提醒",
4
- "version": "3.3.2",
4
+ "version": "3.3.3",
5
5
  "contributors": [
6
6
  "opwop <slhp1013@qq.com>",
7
7
  "bot_actions <168329908@qq.com>"