koishi-plugin-tmp-bot 1.0.0 → 1.1.0

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,4 +1,5 @@
1
1
  const BASE_API_V3 = 'https://api.truckyapp.com/v3'
2
+ const BASE_API_V2 = 'https://api.truckyapp.com/v2'
2
3
 
3
4
  module.exports = {
4
5
  /**
@@ -22,5 +23,27 @@ module.exports = {
22
23
  data.data = result.response
23
24
  }
24
25
  return data
26
+ },
27
+ /**
28
+ * 查询热门交通数据
29
+ */
30
+ async trafficTop (http, serverName) {
31
+ let result = null
32
+ try {
33
+ result = await http.get(`${BASE_API_V2}/traffic/top?game=ets2&server=${serverName}`)
34
+ } catch {
35
+ return {
36
+ error: true
37
+ }
38
+ }
39
+
40
+ // 拼接返回数据
41
+ let data = {
42
+ error: !result || !result.response || result.response.length <= 0
43
+ }
44
+ if (!data.error) {
45
+ data.data = result.response
46
+ }
47
+ return data
25
48
  }
26
49
  }
@@ -10,7 +10,8 @@ const baiduTranslate = require('../util/baiduTranslate')
10
10
  const userGroup = {
11
11
  'Player': '玩家',
12
12
  'Retired Legend': '退役',
13
- 'Game Developer': '游戏开发者'
13
+ 'Game Developer': '游戏开发者',
14
+ 'Retired Team Member': '退休团队成员'
14
15
  }
15
16
 
16
17
  /**
@@ -0,0 +1,65 @@
1
+ const truckyAppApi = require('../api/truckyAppApi')
2
+ const baiduTranslate = require('../util/baiduTranslate')
3
+
4
+ /**
5
+ * 服务器别名
6
+ */
7
+ const serverNameAlias = {
8
+ 's1': 'sim1',
9
+ 's2': 'sim2',
10
+ 'p': 'eupromods1',
11
+ 'a': 'arc1'
12
+ }
13
+
14
+ /**
15
+ * 路况程度转中文
16
+ */
17
+ const severityToZh = {
18
+ 'Fluid': '🟢畅通',
19
+ 'Moderate': '🟠正常',
20
+ 'Congested': '🔴拥堵'
21
+ }
22
+
23
+ /**
24
+ * 位置类型转中文
25
+ */
26
+ const typeToZh = {
27
+ 'City': '城市',
28
+ 'Road': '公路'
29
+ }
30
+
31
+ /**
32
+ * 查询路况
33
+ */
34
+ module.exports = async (ctx, cfg, serverName) => {
35
+ // 转换服务器别名
36
+ let serverQueryName = serverNameAlias[serverName]
37
+ if (!serverQueryName) {
38
+ return '请输入正确的服务器名称 (s1, s2, p, a)'
39
+ }
40
+
41
+ let trafficData = await truckyAppApi.trafficTop(ctx.http, serverQueryName)
42
+ if (trafficData.error) {
43
+ return '查询路况信息失败'
44
+ }
45
+
46
+ // 构建消息
47
+ let message = ''
48
+ for (const traffic of trafficData.data) {
49
+ // 如果已有内容,换行
50
+ if (message) {
51
+ message += '\n\n'
52
+ }
53
+
54
+ message += await baiduTranslate(ctx, cfg, traffic.country)
55
+ message += ' - '
56
+ let name = traffic.name.substring(0, traffic.name.lastIndexOf('(') - 1)
57
+ let type = traffic.name.substring(traffic.name.lastIndexOf('(') + 1, traffic.name.lastIndexOf(')'))
58
+ message += await baiduTranslate(ctx, cfg, name) + ` (${typeToZh[type] || type})`
59
+ message += '\n路况: ' + (severityToZh[traffic.newSeverity] || traffic.color)
60
+ message += ' | 人数: ' + traffic.players
61
+ console.info(traffic)
62
+ }
63
+
64
+ return message
65
+ }
package/lib/index.js CHANGED
@@ -6,6 +6,7 @@ const model = require('./database/model');
6
6
  const tmpQuery = require('./command/tmpQuery');
7
7
  const tmpServer = require('./command/tmpServer');
8
8
  const tmpBind = require('./command/tmpBind');
9
+ const tmpTraffic = require('./command/tmpTraffic');
9
10
  exports.name = 'tmp-bot';
10
11
  exports.Config = koishi_1.Schema.object({
11
12
  baiduTranslateEnable: koishi_1.Schema.boolean().default(false).description('启用百度翻译'),
@@ -18,6 +19,7 @@ function apply(ctx, cfg) {
18
19
  ctx.command('tmpquery <tmpId>').action(async ({ session }, tmpId) => await tmpQuery(ctx, cfg, session, tmpId));
19
20
  ctx.command('tmpserver').action(async () => await tmpServer(ctx, cfg));
20
21
  ctx.command('tmpbind <tmpId>').action(async ({ session }, tmpId) => await tmpBind(ctx, cfg, session, tmpId));
22
+ ctx.command('tmptraffic <serverName>').action(async ({ session }, serverName) => await tmpTraffic(ctx, cfg, serverName));
21
23
  // 等待数据库模块准备完毕后初始化数据库表
22
24
  let databaseTime = setInterval(() => {
23
25
  if (ctx.database) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-tmp-bot",
3
3
  "description": "欧洲卡车模拟2 TMP查询机器人",
4
- "version": "1.0.0",
4
+ "version": "1.1.0",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -21,7 +21,9 @@
21
21
  ],
22
22
  "koishi": {
23
23
  "service": {
24
- "required": ["database"]
24
+ "required": [
25
+ "database"
26
+ ]
25
27
  }
26
28
  },
27
29
  "peerDependencies": {