koishi-plugin-tmp-bot 1.0.0 → 1.1.1
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/truckyAppApi.js +23 -0
- package/lib/command/tmpQuery.js +2 -1
- package/lib/command/tmpTraffic.js +67 -0
- package/lib/index.js +2 -0
- package/package.json +4 -2
package/lib/api/truckyAppApi.js
CHANGED
|
@@ -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
|
}
|
package/lib/command/tmpQuery.js
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
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
|
+
'Heavy': '🟣拥堵'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 位置类型转中文
|
|
26
|
+
*/
|
|
27
|
+
const typeToZh = {
|
|
28
|
+
'City': '城市',
|
|
29
|
+
'Road': '公路',
|
|
30
|
+
'Intersection': '十字路口'
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 查询路况
|
|
35
|
+
*/
|
|
36
|
+
module.exports = async (ctx, cfg, serverName) => {
|
|
37
|
+
// 转换服务器别名
|
|
38
|
+
let serverQueryName = serverNameAlias[serverName]
|
|
39
|
+
if (!serverQueryName) {
|
|
40
|
+
return '请输入正确的服务器名称 (s1, s2, p, a)'
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let trafficData = await truckyAppApi.trafficTop(ctx.http, serverQueryName)
|
|
44
|
+
if (trafficData.error) {
|
|
45
|
+
return '查询路况信息失败'
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 构建消息
|
|
49
|
+
let message = ''
|
|
50
|
+
for (const traffic of trafficData.data) {
|
|
51
|
+
// 如果已有内容,换行
|
|
52
|
+
if (message) {
|
|
53
|
+
message += '\n\n'
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
message += await baiduTranslate(ctx, cfg, traffic.country)
|
|
57
|
+
message += ' - '
|
|
58
|
+
let name = traffic.name.substring(0, traffic.name.lastIndexOf('(') - 1)
|
|
59
|
+
let type = traffic.name.substring(traffic.name.lastIndexOf('(') + 1, traffic.name.lastIndexOf(')'))
|
|
60
|
+
message += await baiduTranslate(ctx, cfg, name) + ` (${typeToZh[type] || type})`
|
|
61
|
+
message += '\n路况: ' + (severityToZh[traffic.newSeverity] || traffic.color)
|
|
62
|
+
message += ' | 人数: ' + traffic.players
|
|
63
|
+
console.info(traffic)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
return message
|
|
67
|
+
}
|
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.
|
|
4
|
+
"version": "1.1.1",
|
|
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": [
|
|
24
|
+
"required": [
|
|
25
|
+
"database"
|
|
26
|
+
]
|
|
25
27
|
}
|
|
26
28
|
},
|
|
27
29
|
"peerDependencies": {
|