node-karin 0.0.3 → 0.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.
- package/LICENSE +674 -674
- package/README.md +57 -57
- package/config/defSet/App.yaml +37 -37
- package/config/defSet/config.yaml +43 -43
- package/config/defSet/group.yaml +18 -18
- package/config/defSet/pm2.yaml +21 -21
- package/config/defSet/server.yaml +42 -42
- package/config/view/App.yaml +74 -74
- package/config/view/config.yaml +100 -100
- package/config/view/group.yaml +62 -62
- package/config/view/pm2.yaml +41 -41
- package/config/view/redis.yaml +25 -25
- package/config/view/server.yaml +93 -93
- package/lib/adapter/onebot/onebot11.d.ts +430 -430
- package/lib/adapter/onebot/onebot11.js +1265 -1302
- package/lib/core/init.js +4 -4
- package/lib/core/karin.d.ts +72 -72
- package/lib/core/karin.js +51 -51
- package/lib/core/listener.d.ts +121 -121
- package/lib/core/listener.js +178 -178
- package/lib/core/plugin.app.d.ts +15 -15
- package/lib/core/plugin.app.js +18 -18
- package/lib/core/plugin.d.ts +182 -182
- package/lib/core/plugin.js +132 -132
- package/lib/core/plugin.loader.d.ts +149 -149
- package/lib/core/plugin.loader.js +451 -451
- package/lib/core/server.d.ts +26 -26
- package/lib/core/server.js +209 -209
- package/lib/db/level.d.ts +20 -20
- package/lib/db/level.js +36 -36
- package/lib/db/redis.d.ts +41 -41
- package/lib/db/redis.js +131 -131
- package/lib/db/redis_level.d.ts +113 -113
- package/lib/db/redis_level.js +274 -274
- package/lib/event/event.d.ts +138 -138
- package/lib/event/event.handler.d.ts +29 -29
- package/lib/event/event.handler.js +138 -138
- package/lib/event/event.js +120 -120
- package/lib/event/message.d.ts +102 -102
- package/lib/event/message.handler.d.ts +25 -25
- package/lib/event/message.handler.js +237 -237
- package/lib/event/message.js +69 -69
- package/lib/event/notice.d.ts +49 -49
- package/lib/event/notice.js +14 -14
- package/lib/event/request.d.ts +49 -49
- package/lib/event/request.js +14 -14
- package/lib/event/review.handler.d.ts +54 -54
- package/lib/event/review.handler.js +374 -374
- package/lib/index.d.ts +23 -23
- package/lib/index.js +39 -40
- package/lib/renderer/app.d.ts +53 -53
- package/lib/renderer/app.js +88 -88
- package/lib/renderer/base.d.ts +30 -30
- package/lib/renderer/base.js +68 -68
- package/lib/renderer/client.d.ts +30 -30
- package/lib/renderer/client.js +155 -155
- package/lib/renderer/http.d.ts +19 -19
- package/lib/renderer/http.js +50 -50
- package/lib/renderer/server.d.ts +42 -42
- package/lib/renderer/server.js +110 -110
- package/lib/renderer/wormhole.d.ts +1 -1
- package/lib/renderer/wormhole.js +154 -154
- package/lib/types/adapter.d.ts +575 -575
- package/lib/types/adapter.js +1 -1
- package/lib/types/config.d.ts +327 -327
- package/lib/types/config.js +1 -1
- package/lib/types/element.d.ts +576 -576
- package/lib/types/element.js +1 -1
- package/lib/types/index.d.ts +8 -8
- package/lib/types/index.js +8 -8
- package/lib/types/logger.d.ts +109 -109
- package/lib/types/logger.js +1 -1
- package/lib/types/onebots11.d.ts +1371 -1371
- package/lib/types/onebots11.js +1 -1
- package/lib/types/plugin.d.ts +282 -282
- package/lib/types/plugin.js +1 -1
- package/lib/types/render.d.ts +111 -111
- package/lib/types/render.js +1 -1
- package/lib/types/reply.d.ts +40 -40
- package/lib/types/reply.js +1 -1
- package/lib/types/types.d.ts +898 -898
- package/lib/types/types.js +1 -1
- package/lib/utils/YamlEditor.d.ts +62 -62
- package/lib/utils/YamlEditor.js +197 -197
- package/lib/utils/button.d.ts +49 -49
- package/lib/utils/button.js +75 -75
- package/lib/utils/common.d.ts +123 -123
- package/lib/utils/common.js +396 -396
- package/lib/utils/config.d.ts +72 -72
- package/lib/utils/config.js +254 -254
- package/lib/utils/exec.d.ts +22 -22
- package/lib/utils/exec.js +36 -36
- package/lib/utils/ffmpeg.d.ts +12 -12
- package/lib/utils/ffmpeg.js +25 -25
- package/lib/utils/handler.d.ts +76 -76
- package/lib/utils/handler.js +98 -98
- package/lib/utils/logger.d.ts +3 -3
- package/lib/utils/logger.js +104 -104
- package/lib/utils/segment.d.ts +276 -276
- package/lib/utils/segment.js +420 -420
- package/lib/utils/update.d.ts +69 -69
- package/lib/utils/update.js +145 -145
- package/lib/utils/updateVersion.d.ts +33 -33
- package/lib/utils/updateVersion.js +140 -140
- package/package.json +92 -91
package/lib/utils/config.d.ts
CHANGED
|
@@ -1,72 +1,72 @@
|
|
|
1
|
-
import { Redis, App, Config, Server, Package, GroupCfg } from '../types/config.js';
|
|
2
|
-
import { Logger } from 'log4js';
|
|
3
|
-
/** 配置文件 */
|
|
4
|
-
declare const _default: {
|
|
5
|
-
dir: string;
|
|
6
|
-
_path: string;
|
|
7
|
-
_pathDef: string;
|
|
8
|
-
change: Map<string, any>;
|
|
9
|
-
watcher: Map<string, any>;
|
|
10
|
-
review: boolean;
|
|
11
|
-
loggger: Logger;
|
|
12
|
-
/** 初始化配置 */
|
|
13
|
-
initCfg(): Promise<void>;
|
|
14
|
-
getPlugins(): string[];
|
|
15
|
-
/**
|
|
16
|
-
* 为每一个插件建立对应的文件夹
|
|
17
|
-
*/
|
|
18
|
-
dirPath(name: string, plugins: string[]): Promise<void>;
|
|
19
|
-
/**
|
|
20
|
-
* 超时时间
|
|
21
|
-
*/
|
|
22
|
-
timeout(type?: 'ws' | 'grpc'): number;
|
|
23
|
-
/**
|
|
24
|
-
* Redis 配置
|
|
25
|
-
* 采用实时读取优化性能
|
|
26
|
-
*/
|
|
27
|
-
readonly redis: Redis;
|
|
28
|
-
/**
|
|
29
|
-
* 主人列表
|
|
30
|
-
* @returns {string[]}
|
|
31
|
-
*/
|
|
32
|
-
readonly master: string[];
|
|
33
|
-
/**
|
|
34
|
-
* 管理员列表
|
|
35
|
-
*/
|
|
36
|
-
readonly admin: string[];
|
|
37
|
-
/** App管理 */
|
|
38
|
-
readonly App: App;
|
|
39
|
-
/**
|
|
40
|
-
* 基本配置
|
|
41
|
-
*/
|
|
42
|
-
readonly Config: Config;
|
|
43
|
-
/**
|
|
44
|
-
* Server 配置文档
|
|
45
|
-
*/
|
|
46
|
-
readonly Server: Server;
|
|
47
|
-
/**
|
|
48
|
-
* packageon
|
|
49
|
-
* 实时获取packageon文件
|
|
50
|
-
*/
|
|
51
|
-
readonly package: Package;
|
|
52
|
-
/**
|
|
53
|
-
* 获取群配置
|
|
54
|
-
*/
|
|
55
|
-
group(group_id?: string): GroupCfg;
|
|
56
|
-
/**
|
|
57
|
-
* 获取配置yaml
|
|
58
|
-
*/
|
|
59
|
-
getYaml(type: 'defSet' | 'config', name: string, isWatch?: boolean): any;
|
|
60
|
-
/**
|
|
61
|
-
* 监听配置文件
|
|
62
|
-
* @param {'defSet'|'config'} type 类型
|
|
63
|
-
* @param {string} name 文件名称 不带后缀
|
|
64
|
-
* @param {string} file 文件路径
|
|
65
|
-
*/
|
|
66
|
-
watch(type: 'defSet' | 'config', name: string, file: string): Promise<true | undefined>;
|
|
67
|
-
change_App(): Promise<void>;
|
|
68
|
-
change_config(): Promise<void>;
|
|
69
|
-
change_group(): Promise<void>;
|
|
70
|
-
"__#1@#review"(): Promise<void>;
|
|
71
|
-
};
|
|
72
|
-
export default _default;
|
|
1
|
+
import { Redis, App, Config, Server, Package, GroupCfg } from '../types/config.js';
|
|
2
|
+
import { Logger } from 'log4js';
|
|
3
|
+
/** 配置文件 */
|
|
4
|
+
declare const _default: {
|
|
5
|
+
dir: string;
|
|
6
|
+
_path: string;
|
|
7
|
+
_pathDef: string;
|
|
8
|
+
change: Map<string, any>;
|
|
9
|
+
watcher: Map<string, any>;
|
|
10
|
+
review: boolean;
|
|
11
|
+
loggger: Logger;
|
|
12
|
+
/** 初始化配置 */
|
|
13
|
+
initCfg(): Promise<void>;
|
|
14
|
+
getPlugins(): string[];
|
|
15
|
+
/**
|
|
16
|
+
* 为每一个插件建立对应的文件夹
|
|
17
|
+
*/
|
|
18
|
+
dirPath(name: string, plugins: string[]): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* 超时时间
|
|
21
|
+
*/
|
|
22
|
+
timeout(type?: 'ws' | 'grpc'): number;
|
|
23
|
+
/**
|
|
24
|
+
* Redis 配置
|
|
25
|
+
* 采用实时读取优化性能
|
|
26
|
+
*/
|
|
27
|
+
readonly redis: Redis;
|
|
28
|
+
/**
|
|
29
|
+
* 主人列表
|
|
30
|
+
* @returns {string[]}
|
|
31
|
+
*/
|
|
32
|
+
readonly master: string[];
|
|
33
|
+
/**
|
|
34
|
+
* 管理员列表
|
|
35
|
+
*/
|
|
36
|
+
readonly admin: string[];
|
|
37
|
+
/** App管理 */
|
|
38
|
+
readonly App: App;
|
|
39
|
+
/**
|
|
40
|
+
* 基本配置
|
|
41
|
+
*/
|
|
42
|
+
readonly Config: Config;
|
|
43
|
+
/**
|
|
44
|
+
* Server 配置文档
|
|
45
|
+
*/
|
|
46
|
+
readonly Server: Server;
|
|
47
|
+
/**
|
|
48
|
+
* packageon
|
|
49
|
+
* 实时获取packageon文件
|
|
50
|
+
*/
|
|
51
|
+
readonly package: Package;
|
|
52
|
+
/**
|
|
53
|
+
* 获取群配置
|
|
54
|
+
*/
|
|
55
|
+
group(group_id?: string): GroupCfg;
|
|
56
|
+
/**
|
|
57
|
+
* 获取配置yaml
|
|
58
|
+
*/
|
|
59
|
+
getYaml(type: 'defSet' | 'config', name: string, isWatch?: boolean): any;
|
|
60
|
+
/**
|
|
61
|
+
* 监听配置文件
|
|
62
|
+
* @param {'defSet'|'config'} type 类型
|
|
63
|
+
* @param {string} name 文件名称 不带后缀
|
|
64
|
+
* @param {string} file 文件路径
|
|
65
|
+
*/
|
|
66
|
+
watch(type: 'defSet' | 'config', name: string, file: string): Promise<true | undefined>;
|
|
67
|
+
change_App(): Promise<void>;
|
|
68
|
+
change_config(): Promise<void>;
|
|
69
|
+
change_group(): Promise<void>;
|
|
70
|
+
"__#1@#review"(): Promise<void>;
|
|
71
|
+
};
|
|
72
|
+
export default _default;
|
package/lib/utils/config.js
CHANGED
|
@@ -1,254 +1,254 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import Yaml from 'yaml'
|
|
3
|
-
import chokidar from 'chokidar'
|
|
4
|
-
/** 配置文件 */
|
|
5
|
-
export default new (class Cfg {
|
|
6
|
-
dir
|
|
7
|
-
_path
|
|
8
|
-
_pathDef
|
|
9
|
-
change
|
|
10
|
-
watcher
|
|
11
|
-
review
|
|
12
|
-
loggger
|
|
13
|
-
constructor () {
|
|
14
|
-
this.dir = process.cwd()
|
|
15
|
-
this._path = this.dir + '/config/config'
|
|
16
|
-
this._pathDef = this.dir + '/config/defSet'
|
|
17
|
-
/** 缓存 */
|
|
18
|
-
this.change = new Map()
|
|
19
|
-
/** 监听文件 */
|
|
20
|
-
this.watcher = new Map()
|
|
21
|
-
/** 拦截器状态 */
|
|
22
|
-
this.review = false
|
|
23
|
-
this.initCfg()
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** 初始化配置 */
|
|
27
|
-
async initCfg () {
|
|
28
|
-
if (!fs.existsSync(this._path)) { fs.mkdirSync(this._path) }
|
|
29
|
-
const files = fs.readdirSync(this._pathDef).filter(file => file.endsWith('.yaml'))
|
|
30
|
-
for (const file of files) {
|
|
31
|
-
const path = `${this._path}/${file}`
|
|
32
|
-
const pathDef = `${this._pathDef}/${file}`
|
|
33
|
-
if (!fs.existsSync(path)) { fs.copyFileSync(pathDef, path) }
|
|
34
|
-
}
|
|
35
|
-
// 创建插件文件夹文件夹
|
|
36
|
-
const plugins = this.getPlugins()
|
|
37
|
-
this.dirPath('data', plugins)
|
|
38
|
-
this.dirPath('temp', plugins)
|
|
39
|
-
this.dirPath('resources', plugins)
|
|
40
|
-
this.dirPath('temp/html', plugins)
|
|
41
|
-
this.loggger = (await import('./logger.js')).default
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
getPlugins () {
|
|
45
|
-
const files = fs.readdirSync('./plugins', { withFileTypes: true })
|
|
46
|
-
// 过滤掉非karin-plugin-开头或karin-adapter-开头的文件夹
|
|
47
|
-
return files.filter(file => file.isDirectory() && (file.name.startsWith('karin-plugin-') || file.name.startsWith('karin-adapter-'))).map(dir => dir.name)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* 为每一个插件建立对应的文件夹
|
|
52
|
-
*/
|
|
53
|
-
async dirPath (name, plugins) {
|
|
54
|
-
name = `./${name}`
|
|
55
|
-
if (!fs.existsSync(name)) { fs.mkdirSync(name) }
|
|
56
|
-
for (const plugin of plugins) {
|
|
57
|
-
const path = `${name}/${plugin}`
|
|
58
|
-
if (!fs.existsSync(path)) { fs.mkdirSync(path) }
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 超时时间
|
|
64
|
-
*/
|
|
65
|
-
timeout (type = 'ws') {
|
|
66
|
-
let timeout = 60
|
|
67
|
-
if (type === 'ws') {
|
|
68
|
-
timeout = this.Server.websocket.timeout
|
|
69
|
-
} else {
|
|
70
|
-
timeout = this.Server.grpc.timeout
|
|
71
|
-
}
|
|
72
|
-
return timeout || 60
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Redis 配置
|
|
77
|
-
* 采用实时读取优化性能
|
|
78
|
-
*/
|
|
79
|
-
get redis () {
|
|
80
|
-
const config = this.getYaml('config', 'redis', false)
|
|
81
|
-
const defSet = this.getYaml('defSet', 'redis', false)
|
|
82
|
-
const data = { ...defSet, ...config }
|
|
83
|
-
return data
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 主人列表
|
|
88
|
-
* @returns {string[]}
|
|
89
|
-
*/
|
|
90
|
-
get master () {
|
|
91
|
-
return this.Config.master || []
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* 管理员列表
|
|
96
|
-
*/
|
|
97
|
-
get admin () {
|
|
98
|
-
return this.Config.admin || []
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** App管理 */
|
|
102
|
-
get App () {
|
|
103
|
-
const key = 'change.App'
|
|
104
|
-
const res = this.change.get(key)
|
|
105
|
-
/** 取缓存 */
|
|
106
|
-
if (res) { return res }
|
|
107
|
-
/** 取配置 */
|
|
108
|
-
const config = this.getYaml('config', 'App', true)
|
|
109
|
-
const defSet = this.getYaml('defSet', 'App', true)
|
|
110
|
-
const data = { ...defSet, ...config }
|
|
111
|
-
/** 缓存 */
|
|
112
|
-
this.change.set(key, data)
|
|
113
|
-
return data
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* 基本配置
|
|
118
|
-
*/
|
|
119
|
-
get Config () {
|
|
120
|
-
const key = 'change.config'
|
|
121
|
-
const res = this.change.get(key)
|
|
122
|
-
/** 取缓存 */
|
|
123
|
-
if (res) { return res }
|
|
124
|
-
/** 取配置 */
|
|
125
|
-
const config = this.getYaml('config', 'config', true)
|
|
126
|
-
const defSet = this.getYaml('defSet', 'config', false)
|
|
127
|
-
const data = { ...defSet, ...config }
|
|
128
|
-
/** 缓存 */
|
|
129
|
-
this.change.set(key, data)
|
|
130
|
-
return data
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Server 配置文档
|
|
135
|
-
*/
|
|
136
|
-
get Server () {
|
|
137
|
-
const key = 'change.server'
|
|
138
|
-
/** 取缓存 */
|
|
139
|
-
const res = this.change.get(key)
|
|
140
|
-
if (res) { return res }
|
|
141
|
-
/** 取配置 */
|
|
142
|
-
const config = this.getYaml('config', 'server', true)
|
|
143
|
-
const defSet = this.getYaml('defSet', 'server', false)
|
|
144
|
-
const data = { ...defSet, ...config }
|
|
145
|
-
/** 缓存 */
|
|
146
|
-
this.change.set(key, data)
|
|
147
|
-
return data
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* packageon
|
|
152
|
-
* 实时获取packageon文件
|
|
153
|
-
*/
|
|
154
|
-
get package () {
|
|
155
|
-
const data = fs.readFileSync('./package.json', 'utf8')
|
|
156
|
-
const pack = JSON.parse(data)
|
|
157
|
-
return pack
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* 获取群配置
|
|
162
|
-
*/
|
|
163
|
-
group (group_id = '') {
|
|
164
|
-
const key = 'change.group'
|
|
165
|
-
/** 取缓存 */
|
|
166
|
-
let res = this.change.get(key)
|
|
167
|
-
if (res) {
|
|
168
|
-
res = { ...res.defCfg.default, ...res.Config.default, ...(res.Config[group_id] || {}) }
|
|
169
|
-
return res
|
|
170
|
-
}
|
|
171
|
-
/** 取配置 */
|
|
172
|
-
const Config = this.getYaml('config', 'group')
|
|
173
|
-
const defCfg = this.getYaml('defSet', 'group')
|
|
174
|
-
const data = { Config, defCfg }
|
|
175
|
-
/** 缓存 */
|
|
176
|
-
res = this.change.set(key, data)
|
|
177
|
-
res = { ...defCfg.default, ...Config.default, ...(Config[group_id] || {}) }
|
|
178
|
-
return res
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* 获取配置yaml
|
|
183
|
-
*/
|
|
184
|
-
getYaml (type, name, isWatch = false) {
|
|
185
|
-
/** 文件路径 */
|
|
186
|
-
const file = `./config/${type}/${name}.yaml`
|
|
187
|
-
/** 读取文件 */
|
|
188
|
-
const data = Yaml.parse(fs.readFileSync(file, 'utf8'))
|
|
189
|
-
/** 监听文件 */
|
|
190
|
-
if (isWatch) { this.watch(type, name, file) }
|
|
191
|
-
return data
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* 监听配置文件
|
|
196
|
-
* @param {'defSet'|'config'} type 类型
|
|
197
|
-
* @param {string} name 文件名称 不带后缀
|
|
198
|
-
* @param {string} file 文件路径
|
|
199
|
-
*/
|
|
200
|
-
async watch (type, name, file) {
|
|
201
|
-
const key = `change.${name}`
|
|
202
|
-
/** 已经监听过了 */
|
|
203
|
-
const res = this.change.get(key)
|
|
204
|
-
if (res) { return true }
|
|
205
|
-
/** 监听文件 */
|
|
206
|
-
const watcher = chokidar.watch(file)
|
|
207
|
-
/** 监听文件变化 */
|
|
208
|
-
watcher.on('change', () => {
|
|
209
|
-
this.change.delete(key)
|
|
210
|
-
this.loggger.mark(`[修改配置文件][${type}][${name}]`)
|
|
211
|
-
/** 文件修改后调用对应的方法 */
|
|
212
|
-
switch (`change_${name}`) {
|
|
213
|
-
case 'change_App':
|
|
214
|
-
this.change_App()
|
|
215
|
-
break
|
|
216
|
-
case 'change_config':
|
|
217
|
-
this.change_config()
|
|
218
|
-
break
|
|
219
|
-
case 'change_group':
|
|
220
|
-
this.change_group()
|
|
221
|
-
break
|
|
222
|
-
}
|
|
223
|
-
})
|
|
224
|
-
/** 缓存 防止重复监听 */
|
|
225
|
-
this.watcher.set(key, watcher)
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
async change_App () {
|
|
229
|
-
await this.#review()
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
async change_config () {
|
|
233
|
-
/** 修改日志等级 */
|
|
234
|
-
this.loggger.level = this.Config.log_level
|
|
235
|
-
await this.#review()
|
|
236
|
-
// if (this.Server.HotUpdate) {
|
|
237
|
-
// const { Bot } = await import('../index.js')
|
|
238
|
-
// Bot.emit('restart_http', {})
|
|
239
|
-
// Bot.emit('restart_grpc', {})
|
|
240
|
-
// }
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
async change_group () {
|
|
244
|
-
await this.#review()
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
async #review () {
|
|
248
|
-
// if (this.review) return
|
|
249
|
-
// this.review = true
|
|
250
|
-
// const review = await import('../event/review')
|
|
251
|
-
// review.default.main()
|
|
252
|
-
// this.review = false
|
|
253
|
-
}
|
|
254
|
-
})()
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import Yaml from 'yaml'
|
|
3
|
+
import chokidar from 'chokidar'
|
|
4
|
+
/** 配置文件 */
|
|
5
|
+
export default new (class Cfg {
|
|
6
|
+
dir
|
|
7
|
+
_path
|
|
8
|
+
_pathDef
|
|
9
|
+
change
|
|
10
|
+
watcher
|
|
11
|
+
review
|
|
12
|
+
loggger
|
|
13
|
+
constructor () {
|
|
14
|
+
this.dir = process.cwd()
|
|
15
|
+
this._path = this.dir + '/config/config'
|
|
16
|
+
this._pathDef = this.dir + '/config/defSet'
|
|
17
|
+
/** 缓存 */
|
|
18
|
+
this.change = new Map()
|
|
19
|
+
/** 监听文件 */
|
|
20
|
+
this.watcher = new Map()
|
|
21
|
+
/** 拦截器状态 */
|
|
22
|
+
this.review = false
|
|
23
|
+
this.initCfg()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 初始化配置 */
|
|
27
|
+
async initCfg () {
|
|
28
|
+
if (!fs.existsSync(this._path)) { fs.mkdirSync(this._path) }
|
|
29
|
+
const files = fs.readdirSync(this._pathDef).filter(file => file.endsWith('.yaml'))
|
|
30
|
+
for (const file of files) {
|
|
31
|
+
const path = `${this._path}/${file}`
|
|
32
|
+
const pathDef = `${this._pathDef}/${file}`
|
|
33
|
+
if (!fs.existsSync(path)) { fs.copyFileSync(pathDef, path) }
|
|
34
|
+
}
|
|
35
|
+
// 创建插件文件夹文件夹
|
|
36
|
+
const plugins = this.getPlugins()
|
|
37
|
+
this.dirPath('data', plugins)
|
|
38
|
+
this.dirPath('temp', plugins)
|
|
39
|
+
this.dirPath('resources', plugins)
|
|
40
|
+
this.dirPath('temp/html', plugins)
|
|
41
|
+
this.loggger = (await import('./logger.js')).default
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
getPlugins () {
|
|
45
|
+
const files = fs.readdirSync('./plugins', { withFileTypes: true })
|
|
46
|
+
// 过滤掉非karin-plugin-开头或karin-adapter-开头的文件夹
|
|
47
|
+
return files.filter(file => file.isDirectory() && (file.name.startsWith('karin-plugin-') || file.name.startsWith('karin-adapter-'))).map(dir => dir.name)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 为每一个插件建立对应的文件夹
|
|
52
|
+
*/
|
|
53
|
+
async dirPath (name, plugins) {
|
|
54
|
+
name = `./${name}`
|
|
55
|
+
if (!fs.existsSync(name)) { fs.mkdirSync(name) }
|
|
56
|
+
for (const plugin of plugins) {
|
|
57
|
+
const path = `${name}/${plugin}`
|
|
58
|
+
if (!fs.existsSync(path)) { fs.mkdirSync(path) }
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 超时时间
|
|
64
|
+
*/
|
|
65
|
+
timeout (type = 'ws') {
|
|
66
|
+
let timeout = 60
|
|
67
|
+
if (type === 'ws') {
|
|
68
|
+
timeout = this.Server.websocket.timeout
|
|
69
|
+
} else {
|
|
70
|
+
timeout = this.Server.grpc.timeout
|
|
71
|
+
}
|
|
72
|
+
return timeout || 60
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Redis 配置
|
|
77
|
+
* 采用实时读取优化性能
|
|
78
|
+
*/
|
|
79
|
+
get redis () {
|
|
80
|
+
const config = this.getYaml('config', 'redis', false)
|
|
81
|
+
const defSet = this.getYaml('defSet', 'redis', false)
|
|
82
|
+
const data = { ...defSet, ...config }
|
|
83
|
+
return data
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 主人列表
|
|
88
|
+
* @returns {string[]}
|
|
89
|
+
*/
|
|
90
|
+
get master () {
|
|
91
|
+
return this.Config.master || []
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* 管理员列表
|
|
96
|
+
*/
|
|
97
|
+
get admin () {
|
|
98
|
+
return this.Config.admin || []
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** App管理 */
|
|
102
|
+
get App () {
|
|
103
|
+
const key = 'change.App'
|
|
104
|
+
const res = this.change.get(key)
|
|
105
|
+
/** 取缓存 */
|
|
106
|
+
if (res) { return res }
|
|
107
|
+
/** 取配置 */
|
|
108
|
+
const config = this.getYaml('config', 'App', true)
|
|
109
|
+
const defSet = this.getYaml('defSet', 'App', true)
|
|
110
|
+
const data = { ...defSet, ...config }
|
|
111
|
+
/** 缓存 */
|
|
112
|
+
this.change.set(key, data)
|
|
113
|
+
return data
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 基本配置
|
|
118
|
+
*/
|
|
119
|
+
get Config () {
|
|
120
|
+
const key = 'change.config'
|
|
121
|
+
const res = this.change.get(key)
|
|
122
|
+
/** 取缓存 */
|
|
123
|
+
if (res) { return res }
|
|
124
|
+
/** 取配置 */
|
|
125
|
+
const config = this.getYaml('config', 'config', true)
|
|
126
|
+
const defSet = this.getYaml('defSet', 'config', false)
|
|
127
|
+
const data = { ...defSet, ...config }
|
|
128
|
+
/** 缓存 */
|
|
129
|
+
this.change.set(key, data)
|
|
130
|
+
return data
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Server 配置文档
|
|
135
|
+
*/
|
|
136
|
+
get Server () {
|
|
137
|
+
const key = 'change.server'
|
|
138
|
+
/** 取缓存 */
|
|
139
|
+
const res = this.change.get(key)
|
|
140
|
+
if (res) { return res }
|
|
141
|
+
/** 取配置 */
|
|
142
|
+
const config = this.getYaml('config', 'server', true)
|
|
143
|
+
const defSet = this.getYaml('defSet', 'server', false)
|
|
144
|
+
const data = { ...defSet, ...config }
|
|
145
|
+
/** 缓存 */
|
|
146
|
+
this.change.set(key, data)
|
|
147
|
+
return data
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* packageon
|
|
152
|
+
* 实时获取packageon文件
|
|
153
|
+
*/
|
|
154
|
+
get package () {
|
|
155
|
+
const data = fs.readFileSync('./package.json', 'utf8')
|
|
156
|
+
const pack = JSON.parse(data)
|
|
157
|
+
return pack
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 获取群配置
|
|
162
|
+
*/
|
|
163
|
+
group (group_id = '') {
|
|
164
|
+
const key = 'change.group'
|
|
165
|
+
/** 取缓存 */
|
|
166
|
+
let res = this.change.get(key)
|
|
167
|
+
if (res) {
|
|
168
|
+
res = { ...res.defCfg.default, ...res.Config.default, ...(res.Config[group_id] || {}) }
|
|
169
|
+
return res
|
|
170
|
+
}
|
|
171
|
+
/** 取配置 */
|
|
172
|
+
const Config = this.getYaml('config', 'group')
|
|
173
|
+
const defCfg = this.getYaml('defSet', 'group')
|
|
174
|
+
const data = { Config, defCfg }
|
|
175
|
+
/** 缓存 */
|
|
176
|
+
res = this.change.set(key, data)
|
|
177
|
+
res = { ...defCfg.default, ...Config.default, ...(Config[group_id] || {}) }
|
|
178
|
+
return res
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 获取配置yaml
|
|
183
|
+
*/
|
|
184
|
+
getYaml (type, name, isWatch = false) {
|
|
185
|
+
/** 文件路径 */
|
|
186
|
+
const file = `./config/${type}/${name}.yaml`
|
|
187
|
+
/** 读取文件 */
|
|
188
|
+
const data = Yaml.parse(fs.readFileSync(file, 'utf8'))
|
|
189
|
+
/** 监听文件 */
|
|
190
|
+
if (isWatch) { this.watch(type, name, file) }
|
|
191
|
+
return data
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 监听配置文件
|
|
196
|
+
* @param {'defSet'|'config'} type 类型
|
|
197
|
+
* @param {string} name 文件名称 不带后缀
|
|
198
|
+
* @param {string} file 文件路径
|
|
199
|
+
*/
|
|
200
|
+
async watch (type, name, file) {
|
|
201
|
+
const key = `change.${name}`
|
|
202
|
+
/** 已经监听过了 */
|
|
203
|
+
const res = this.change.get(key)
|
|
204
|
+
if (res) { return true }
|
|
205
|
+
/** 监听文件 */
|
|
206
|
+
const watcher = chokidar.watch(file)
|
|
207
|
+
/** 监听文件变化 */
|
|
208
|
+
watcher.on('change', () => {
|
|
209
|
+
this.change.delete(key)
|
|
210
|
+
this.loggger.mark(`[修改配置文件][${type}][${name}]`)
|
|
211
|
+
/** 文件修改后调用对应的方法 */
|
|
212
|
+
switch (`change_${name}`) {
|
|
213
|
+
case 'change_App':
|
|
214
|
+
this.change_App()
|
|
215
|
+
break
|
|
216
|
+
case 'change_config':
|
|
217
|
+
this.change_config()
|
|
218
|
+
break
|
|
219
|
+
case 'change_group':
|
|
220
|
+
this.change_group()
|
|
221
|
+
break
|
|
222
|
+
}
|
|
223
|
+
})
|
|
224
|
+
/** 缓存 防止重复监听 */
|
|
225
|
+
this.watcher.set(key, watcher)
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async change_App () {
|
|
229
|
+
await this.#review()
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
async change_config () {
|
|
233
|
+
/** 修改日志等级 */
|
|
234
|
+
this.loggger.level = this.Config.log_level
|
|
235
|
+
await this.#review()
|
|
236
|
+
// if (this.Server.HotUpdate) {
|
|
237
|
+
// const { Bot } = await import('../index.js')
|
|
238
|
+
// Bot.emit('restart_http', {})
|
|
239
|
+
// Bot.emit('restart_grpc', {})
|
|
240
|
+
// }
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async change_group () {
|
|
244
|
+
await this.#review()
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async #review () {
|
|
248
|
+
// if (this.review) return
|
|
249
|
+
// this.review = true
|
|
250
|
+
// const review = await import('../event/review')
|
|
251
|
+
// review.default.main()
|
|
252
|
+
// this.review = false
|
|
253
|
+
}
|
|
254
|
+
})()
|