node-karin 0.9.0 → 0.10.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/config/defSet/group.yaml +40 -2
- package/lib/adapter/onebot/11/index.d.ts +2 -0
- package/lib/adapter/onebot/11/index.js +3 -1
- package/lib/cli/init.js +1 -1
- package/lib/cli/karin.js +1 -1
- package/lib/core/index.d.ts +9 -9
- package/lib/core/index.js +9 -9
- package/lib/core/init/dir.js +7 -0
- package/lib/core/init/init.js +46 -0
- package/lib/core/{karin.d.ts → karin/karin.d.ts} +1 -1
- package/lib/core/karin/karin.js +194 -0
- package/lib/core/{listener.d.ts → listener/listener.d.ts} +1 -1
- package/lib/core/listener/listener.js +213 -0
- package/lib/core/{plugin.app.d.ts → plugin/app.d.ts} +1 -1
- package/lib/core/plugin/app.js +19 -0
- package/lib/core/{plugin.d.ts → plugin/base.d.ts} +1 -1
- package/lib/core/plugin/base.js +140 -0
- package/lib/core/{plugin.loader.d.ts → plugin/loader.d.ts} +3 -3
- package/lib/core/plugin/loader.js +579 -0
- package/lib/core/process/process.js +100 -0
- package/lib/core/server/server.js +283 -0
- package/lib/db/index.d.ts +2 -2
- package/lib/db/index.js +2 -2
- package/lib/db/level/level.js +36 -0
- package/lib/db/redis/redis.js +135 -0
- package/lib/db/redis/redis_level.js +287 -0
- package/lib/event/{event.handler.d.ts → handler/base.d.ts} +2 -2
- package/lib/event/handler/base.js +173 -0
- package/lib/event/{message.handler.d.ts → handler/message.d.ts} +3 -3
- package/lib/event/handler/message.js +270 -0
- package/lib/event/{notice.handler.d.ts → handler/notice.d.ts} +3 -3
- package/lib/event/handler/notice.js +212 -0
- package/lib/event/{request.handler.d.ts → handler/request.d.ts} +3 -3
- package/lib/event/handler/request.js +118 -0
- package/lib/event/{review.handler.d.ts → handler/review.d.ts} +3 -3
- package/lib/event/handler/review.js +391 -0
- package/lib/event/index.d.ts +5 -5
- package/lib/event/index.js +5 -5
- package/lib/render/base.d.ts +1 -1
- package/lib/render/client.d.ts +1 -1
- package/lib/render/client.js +5 -7
- package/lib/render/client_even.d.ts +30 -0
- package/lib/render/client_even.js +153 -0
- package/lib/render/http.d.ts +1 -1
- package/lib/render/http.js +1 -1
- package/lib/render/server.js +1 -1
- package/lib/types/adapter/api.d.ts +5 -1
- package/lib/types/adapter/{adapter.d.ts → base.d.ts} +2 -2
- package/lib/types/config/config.js +1 -0
- package/lib/types/element/element.js +1 -0
- package/lib/types/event/message.d.ts +1 -1
- package/lib/types/event/reply.d.ts +1 -1
- package/lib/types/index.d.ts +6 -6
- package/lib/types/index.js +6 -6
- package/lib/types/logger/logger.js +1 -0
- package/lib/types/{plugin.d.ts → plugin/plugin.d.ts} +3 -3
- package/lib/types/plugin/plugin.js +1 -0
- package/lib/types/render/render.js +1 -0
- package/lib/utils/{common.d.ts → common/common.d.ts} +14 -13
- package/lib/utils/common/common.js +591 -0
- package/lib/utils/{config.d.ts → config/config.d.ts} +37 -19
- package/lib/utils/config/config.js +328 -0
- package/lib/utils/config/updateVersion.js +145 -0
- package/lib/utils/config/yamlEditor.js +292 -0
- package/lib/utils/{handler.d.ts → core/handler.d.ts} +1 -1
- package/lib/utils/core/handler.js +115 -0
- package/lib/utils/core/init.js +213 -0
- package/lib/utils/core/logger.js +105 -0
- package/lib/utils/{segment.d.ts → core/segment.d.ts} +1 -1
- package/lib/utils/core/segment.js +441 -0
- package/lib/utils/index.d.ts +11 -11
- package/lib/utils/index.js +11 -11
- package/lib/utils/{button.d.ts → tools/button.d.ts} +1 -1
- package/lib/utils/tools/button.js +38 -0
- package/lib/utils/tools/exec.js +37 -0
- package/lib/utils/tools/ffmpeg.js +25 -0
- package/lib/utils/tools/update.js +139 -0
- package/package.json +1 -1
- package/lib/core/dir.js +0 -7
- package/lib/core/init.js +0 -42
- package/lib/core/karin.js +0 -194
- package/lib/core/listener.js +0 -217
- package/lib/core/plugin.app.js +0 -19
- package/lib/core/plugin.js +0 -145
- package/lib/core/plugin.loader.js +0 -561
- package/lib/core/process.js +0 -98
- package/lib/core/server.js +0 -269
- package/lib/db/level.js +0 -37
- package/lib/db/redis.js +0 -134
- package/lib/db/redis_level.js +0 -293
- package/lib/event/event.handler.js +0 -167
- package/lib/event/message.handler.js +0 -254
- package/lib/event/notice.handler.js +0 -204
- package/lib/event/request.handler.js +0 -110
- package/lib/event/review.handler.js +0 -386
- package/lib/types/config.js +0 -1
- package/lib/types/element.js +0 -1
- package/lib/types/logger.js +0 -1
- package/lib/types/plugin.js +0 -1
- package/lib/types/render.js +0 -1
- package/lib/utils/button.js +0 -34
- package/lib/utils/common.js +0 -596
- package/lib/utils/config.js +0 -318
- package/lib/utils/exec.js +0 -36
- package/lib/utils/ffmpeg.js +0 -25
- package/lib/utils/handler.js +0 -109
- package/lib/utils/init.js +0 -208
- package/lib/utils/logger.js +0 -104
- package/lib/utils/segment.js +0 -470
- package/lib/utils/update.js +0 -135
- package/lib/utils/updateVersion.js +0 -145
- package/lib/utils/yamlEditor.js +0 -279
- /package/lib/core/{dir.d.ts → init/dir.d.ts} +0 -0
- /package/lib/core/{init.d.ts → init/init.d.ts} +0 -0
- /package/lib/core/{process.d.ts → process/process.d.ts} +0 -0
- /package/lib/core/{server.d.ts → server/server.d.ts} +0 -0
- /package/lib/db/{level.d.ts → level/level.d.ts} +0 -0
- /package/lib/db/{redis.d.ts → redis/redis.d.ts} +0 -0
- /package/lib/db/{redis_level.d.ts → redis/redis_level.d.ts} +0 -0
- /package/lib/types/adapter/{adapter.js → base.js} +0 -0
- /package/lib/types/{config.d.ts → config/config.d.ts} +0 -0
- /package/lib/types/{element.d.ts → element/element.d.ts} +0 -0
- /package/lib/types/{logger.d.ts → logger/logger.d.ts} +0 -0
- /package/lib/types/{render.d.ts → render/render.d.ts} +0 -0
- /package/lib/utils/{updateVersion.d.ts → config/updateVersion.d.ts} +0 -0
- /package/lib/utils/{yamlEditor.d.ts → config/yamlEditor.d.ts} +0 -0
- /package/lib/utils/{init.d.ts → core/init.d.ts} +0 -0
- /package/lib/utils/{logger.d.ts → core/logger.d.ts} +0 -0
- /package/lib/utils/{exec.d.ts → tools/exec.d.ts} +0 -0
- /package/lib/utils/{ffmpeg.d.ts → tools/ffmpeg.d.ts} +0 -0
- /package/lib/utils/{update.d.ts → tools/update.d.ts} +0 -0
package/lib/types/element.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {}
|
package/lib/types/logger.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {}
|
package/lib/types/plugin.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {}
|
package/lib/types/render.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {}
|
package/lib/utils/button.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import logger from './logger.js'
|
|
2
|
-
import { pluginLoader as loader } from '../core/index.js'
|
|
3
|
-
export const button = async (e) => {
|
|
4
|
-
const button = []
|
|
5
|
-
for (const v of loader.buttonIds) {
|
|
6
|
-
const info = loader.PluginList[v]
|
|
7
|
-
for (const v of info.button) {
|
|
8
|
-
const reg = v.reg
|
|
9
|
-
if (reg.test(e.msg)) {
|
|
10
|
-
try {
|
|
11
|
-
let res
|
|
12
|
-
let done = true
|
|
13
|
-
/**
|
|
14
|
-
* 标记函数 如果调用则继续执行 循环下一个按钮插件处理
|
|
15
|
-
*/
|
|
16
|
-
const reject = () => { done = false }
|
|
17
|
-
if (typeof v.fnc === 'function') {
|
|
18
|
-
res = await v.fnc(e, reject)
|
|
19
|
-
} else {
|
|
20
|
-
const cla = new info.file.Fnc()
|
|
21
|
-
cla.e = e
|
|
22
|
-
res = await cla[v.fnc](e, reject)
|
|
23
|
-
}
|
|
24
|
-
if (res) { button.push(res) }
|
|
25
|
-
if (done) { return res }
|
|
26
|
-
} catch (error) {
|
|
27
|
-
logger.error(error)
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
/** 理论上不会走到这里,但是还是要稳一手,不排除有所有插件都false... */
|
|
33
|
-
return button
|
|
34
|
-
}
|
package/lib/utils/common.js
DELETED
|
@@ -1,596 +0,0 @@
|
|
|
1
|
-
import fs from 'fs'
|
|
2
|
-
import path from 'path'
|
|
3
|
-
import Yaml from 'yaml'
|
|
4
|
-
import axios from 'axios'
|
|
5
|
-
import lodash from 'lodash'
|
|
6
|
-
import { promisify } from 'util'
|
|
7
|
-
import { fileURLToPath } from 'url'
|
|
8
|
-
import { pipeline, Readable } from 'stream'
|
|
9
|
-
import { logger, segment, YamlEditor } from '../utils/index.js'
|
|
10
|
-
/**
|
|
11
|
-
* 常用方法
|
|
12
|
-
*/
|
|
13
|
-
class Common {
|
|
14
|
-
streamPipeline
|
|
15
|
-
constructor () {
|
|
16
|
-
this.streamPipeline = promisify(pipeline)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 休眠函数
|
|
21
|
-
* @param ms 毫秒
|
|
22
|
-
*/
|
|
23
|
-
sleep (ms) {
|
|
24
|
-
return new Promise(resolve => setTimeout(resolve, ms))
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 下载保存文件
|
|
29
|
-
* @param fileUrl 下载地址
|
|
30
|
-
* @param savePath 保存路径
|
|
31
|
-
* @param param axios参数
|
|
32
|
-
*/
|
|
33
|
-
async downFile (fileUrl, savePath, param = {}) {
|
|
34
|
-
try {
|
|
35
|
-
this.mkdir(path.dirname(savePath))
|
|
36
|
-
logger && logger.info(`[下载文件] ${fileUrl}`)
|
|
37
|
-
const response = await axios.get(fileUrl, { ...param, responseType: 'stream' })
|
|
38
|
-
await this.streamPipeline(response.data, fs.createWriteStream(savePath))
|
|
39
|
-
return true
|
|
40
|
-
} catch (err) {
|
|
41
|
-
logger && logger.error(`下载文件错误:${err}`)
|
|
42
|
-
return false
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* 对axios进行简单封装,超时、错误后返回null,不会抛出异常
|
|
48
|
-
* @param url 请求地址
|
|
49
|
-
* @param type 请求类型
|
|
50
|
-
* @param param axios参数
|
|
51
|
-
*/
|
|
52
|
-
async axios (url, type = 'get', param = {}) {
|
|
53
|
-
try {
|
|
54
|
-
if (type === 'post') { return await axios.post(url, param.data, param) }
|
|
55
|
-
return await axios.get(url, param)
|
|
56
|
-
} catch (error) {
|
|
57
|
-
logger.debug(error)
|
|
58
|
-
return null
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 递归创建目录
|
|
64
|
-
* @param dirname - 要创建的文件夹路径
|
|
65
|
-
*/
|
|
66
|
-
mkdir (dirname) {
|
|
67
|
-
if (fs.existsSync(dirname)) { return true }
|
|
68
|
-
/** 递归自调用 */
|
|
69
|
-
if (this.mkdir(path.dirname(dirname))) { fs.mkdirSync(dirname) }
|
|
70
|
-
return true
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* 标准化文件路径
|
|
75
|
-
* @param file - 相对路径
|
|
76
|
-
* @param isDir - 返回绝对路径
|
|
77
|
-
* @param isFile - 添加file://前缀
|
|
78
|
-
* @returns 标准化后的路径
|
|
79
|
-
*/
|
|
80
|
-
absPath (file, isDir = true, isFile = false) {
|
|
81
|
-
file = file.replace(/\\/g, '/')
|
|
82
|
-
if (file.startsWith('file://')) {
|
|
83
|
-
/** linux */
|
|
84
|
-
if (path.sep === '/') {
|
|
85
|
-
file = file.replace('file://', '')
|
|
86
|
-
} else {
|
|
87
|
-
/** windows */
|
|
88
|
-
file = file.replace(/^file:[/]{2,3}/g, '')
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
file = path.normalize(file)
|
|
92
|
-
/** 判断路径是否为绝对路径 否则转为绝对路径 */
|
|
93
|
-
if (isDir && !path.isAbsolute(file)) {
|
|
94
|
-
file = path.resolve(file)
|
|
95
|
-
}
|
|
96
|
-
if (isFile) { file = 'file://' + file }
|
|
97
|
-
file = file.replace(/\\/g, '/')
|
|
98
|
-
return file
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* 判断是否为文件夹
|
|
103
|
-
* @param path - 路径
|
|
104
|
-
* @returns 返回true为文件夹
|
|
105
|
-
*/
|
|
106
|
-
isDir (path) {
|
|
107
|
-
try {
|
|
108
|
-
return fs.statSync(path).isDirectory()
|
|
109
|
-
} catch {
|
|
110
|
-
return false
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* 判断是否为插件包
|
|
116
|
-
* @param path - 路径
|
|
117
|
-
* @returns 返回true为插件包
|
|
118
|
-
*/
|
|
119
|
-
isPlugin (path) {
|
|
120
|
-
return this.exists(`${path}/package.json`)
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* 判断路径是否存在
|
|
125
|
-
* @param path - 路径
|
|
126
|
-
* @returns 返回true为存在
|
|
127
|
-
*/
|
|
128
|
-
exists (path) {
|
|
129
|
-
try {
|
|
130
|
-
return fs.existsSync(path)
|
|
131
|
-
} catch {
|
|
132
|
-
return false
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* - 解析json文件
|
|
138
|
-
*/
|
|
139
|
-
readJson (file) {
|
|
140
|
-
try {
|
|
141
|
-
return JSON.parse(fs.readFileSync(file, 'utf8'))
|
|
142
|
-
} catch (error) {
|
|
143
|
-
logger.error('[common] 读取json文件错误:' + error)
|
|
144
|
-
return null
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* - 写入json文件
|
|
150
|
-
*/
|
|
151
|
-
writeJson (file, data) {
|
|
152
|
-
try {
|
|
153
|
-
fs.writeFileSync(file, JSON.stringify(data, null, 2))
|
|
154
|
-
return true
|
|
155
|
-
} catch (error) {
|
|
156
|
-
logger.error('[common] 写入json文件错误:' + error)
|
|
157
|
-
return false
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* - 解析yaml文件
|
|
163
|
-
*/
|
|
164
|
-
readYaml (file) {
|
|
165
|
-
try {
|
|
166
|
-
return Yaml.parse(fs.readFileSync(file, 'utf8'))
|
|
167
|
-
} catch (error) {
|
|
168
|
-
logger.error('[common] 读取yaml文件错误:' + error)
|
|
169
|
-
return null
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* - 写入yaml文件
|
|
175
|
-
*/
|
|
176
|
-
writeYaml (file, data) {
|
|
177
|
-
try {
|
|
178
|
-
fs.writeFileSync(file, Yaml.stringify(data))
|
|
179
|
-
return true
|
|
180
|
-
} catch (error) {
|
|
181
|
-
logger.error('[common] 写入yaml文件错误:' + error)
|
|
182
|
-
return false
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* 根据文件后缀名从指定路径下读取符合要求的文件
|
|
188
|
-
* @param path - 路径
|
|
189
|
-
* @param ext - 后缀名、或后缀名列表
|
|
190
|
-
* @example common.readDir('./plugins', '.js')
|
|
191
|
-
* @example common.readDir('./plugins', ['.js', '.ts'])
|
|
192
|
-
*/
|
|
193
|
-
readDir (_path, ext) {
|
|
194
|
-
if (!this.isDir(_path)) { return null }
|
|
195
|
-
const files = fs.readdirSync(_path, { withFileTypes: true })
|
|
196
|
-
const list = []
|
|
197
|
-
if (!Array.isArray(ext)) { ext = [ext] }
|
|
198
|
-
// 排除文件夹 和不符合后缀名的文件
|
|
199
|
-
files.forEach(v => {
|
|
200
|
-
if (v.isDirectory()) { return }
|
|
201
|
-
if (ext.includes(path.extname(v.name))) { list.push(v.name) }
|
|
202
|
-
})
|
|
203
|
-
return list
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* 根据传入的 import.meta.url 计算相对于项目根目录的路径,返回需要的 '../' 层级。
|
|
208
|
-
* @param url - import.meta.url
|
|
209
|
-
* @returns 相对路径的层级数量,用 '../' 表示
|
|
210
|
-
* @example
|
|
211
|
-
* // 在 plugins/karin-plugin-example/index.ts 中使用
|
|
212
|
-
* common.urlToPath(import.meta.url) // 返回 '../../'
|
|
213
|
-
*/
|
|
214
|
-
urlToPath (url) {
|
|
215
|
-
/** 当前文件的绝对路径 */
|
|
216
|
-
const filePath = fileURLToPath(url)
|
|
217
|
-
/** 当前文件所在目录的绝对路径 */
|
|
218
|
-
const dirPath = path.dirname(filePath)
|
|
219
|
-
/** 项目根目录 */
|
|
220
|
-
const rootPath = process.cwd()
|
|
221
|
-
/** 当前文件到项目根目录的相对路径 */
|
|
222
|
-
const relativePath = path.relative(dirPath, rootPath)
|
|
223
|
-
/** 相对路径的层级数量 */
|
|
224
|
-
const upLevelsCount = relativePath.split(path.sep).length
|
|
225
|
-
/** 返回构建的路径 */
|
|
226
|
-
const upPath = lodash.repeat('../', upLevelsCount)
|
|
227
|
-
return upPath
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* 传入相对路径 分割为两部分 1为文件夹路径 2为文件名 文件夹路径无前缀开头
|
|
232
|
-
* @param _path - 路径
|
|
233
|
-
*/
|
|
234
|
-
splitPath (_path) {
|
|
235
|
-
const list = _path.replace(/\\/g, '/').split('/')
|
|
236
|
-
if (list[0] === '.') { list.shift() }
|
|
237
|
-
const pop = list.pop() || ''
|
|
238
|
-
const dir = path.join(...list)
|
|
239
|
-
return { dir, pop }
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* 将文件转换为不带前缀的base64字符串
|
|
244
|
-
* @param file - 文件路径或Buffer对象、可读流对象、http地址、base64://字符串
|
|
245
|
-
* @param - 附加数据
|
|
246
|
-
* @param - 为true时,http地址会直接返回,否则会下载文件并转换为base64字符串
|
|
247
|
-
* @returns 返回base64字符串
|
|
248
|
-
*/
|
|
249
|
-
async base64 (file, options = { http: false }) {
|
|
250
|
-
/** 先判断是否非字符串情况 */
|
|
251
|
-
if (typeof file !== 'string') {
|
|
252
|
-
/** buffer */
|
|
253
|
-
if (Buffer.isBuffer(file)) {
|
|
254
|
-
return file.toString('base64')
|
|
255
|
-
}
|
|
256
|
-
/** 可读流 */
|
|
257
|
-
if (file instanceof Readable) {
|
|
258
|
-
const data_1 = await this.stream(file)
|
|
259
|
-
return data_1.toString('base64')
|
|
260
|
-
}
|
|
261
|
-
/** 未知类型 */
|
|
262
|
-
throw new Error('未知类型')
|
|
263
|
-
}
|
|
264
|
-
/** base64:// */
|
|
265
|
-
if (file.startsWith('base64://')) {
|
|
266
|
-
return file.replace('base64://', '')
|
|
267
|
-
}
|
|
268
|
-
/** url */
|
|
269
|
-
if (file.startsWith('http://') || file.startsWith('https://')) {
|
|
270
|
-
if (options.http) { return file }
|
|
271
|
-
const response = await axios.get(file, { responseType: 'arraybuffer' })
|
|
272
|
-
return Buffer.from(response.data, 'binary').toString('base64')
|
|
273
|
-
}
|
|
274
|
-
/** file:/// */
|
|
275
|
-
if (fs.existsSync(file.replace(/^file:\/\/\//, ''))) {
|
|
276
|
-
file = file.replace(/^file:\/\/\//, '')
|
|
277
|
-
return fs.readFileSync(file).toString('base64')
|
|
278
|
-
}
|
|
279
|
-
/** file:// */
|
|
280
|
-
if (fs.existsSync(file.replace(/^file:\/\//, ''))) {
|
|
281
|
-
file = file.replace(/^file:\/\//, '')
|
|
282
|
-
return fs.readFileSync(file).toString('base64')
|
|
283
|
-
}
|
|
284
|
-
/** 无前缀base64:// */
|
|
285
|
-
const buffer = Buffer.from(file, 'base64').toString('base64') === file
|
|
286
|
-
if (buffer) { return file }
|
|
287
|
-
throw new Error('未知类型')
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* 将数据流对象转换为Buffer对象
|
|
292
|
-
* @param {stream.Readable} stream - 要转换的数据流对象
|
|
293
|
-
* @returns {Promise<Buffer>} - 返回Buffer
|
|
294
|
-
*/
|
|
295
|
-
stream (stream) {
|
|
296
|
-
return new Promise((resolve, reject) => {
|
|
297
|
-
const chunks = []
|
|
298
|
-
stream.on('data', chunk => chunks.push(chunk))
|
|
299
|
-
stream.on('end', () => resolve(Buffer.concat(chunks)))
|
|
300
|
-
stream.on('error', error => reject(error))
|
|
301
|
-
})
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* 将文件转换为Buffer对象
|
|
306
|
-
* @param file - 文件路径或Buffer对象、可读流对象、http地址、base64://字符串
|
|
307
|
-
* @param options - 选项
|
|
308
|
-
* @returns - 返回Buffer对象
|
|
309
|
-
*/
|
|
310
|
-
async buffer (file, options = { http: false }) {
|
|
311
|
-
if (typeof file !== 'string') {
|
|
312
|
-
if (Buffer.isBuffer(file)) { return file }
|
|
313
|
-
if (file instanceof Readable) { return this.stream(file) }
|
|
314
|
-
throw new Error('未知文件类型:' + file)
|
|
315
|
-
}
|
|
316
|
-
if (file.startsWith('base64://')) { return Buffer.from(file.replace('base64://', ''), 'base64') }
|
|
317
|
-
if (file.startsWith('http://') || file.startsWith('https://')) {
|
|
318
|
-
if (options.http) { return file }
|
|
319
|
-
const response = await axios.get(file, { responseType: 'arraybuffer' })
|
|
320
|
-
return Buffer.from(response.data)
|
|
321
|
-
}
|
|
322
|
-
/** file:/// */
|
|
323
|
-
if (fs.existsSync(file.replace(/^file:\/\/\//, ''))) {
|
|
324
|
-
file = file.replace(/^file:\/\/\//, '')
|
|
325
|
-
return fs.readFileSync(file)
|
|
326
|
-
}
|
|
327
|
-
/** file:// */
|
|
328
|
-
if (fs.existsSync(file.replace(/^file:\/\//, ''))) {
|
|
329
|
-
file = file.replace(/^file:\/\//, '')
|
|
330
|
-
return fs.readFileSync(file)
|
|
331
|
-
}
|
|
332
|
-
/** 无前缀base64:// */
|
|
333
|
-
const buffer = Buffer.from(file, 'base64')
|
|
334
|
-
if (buffer.toString('base64') === file) { return buffer }
|
|
335
|
-
throw new Error('未知类型')
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
/**
|
|
339
|
-
* 标准化发送的消息内容
|
|
340
|
-
* @param elements - 消息内容
|
|
341
|
-
*/
|
|
342
|
-
makeMessage (elements) {
|
|
343
|
-
/** 将msg格式化为数组 */
|
|
344
|
-
if (!Array.isArray(elements)) { elements = [elements] }
|
|
345
|
-
const message = []
|
|
346
|
-
elements.forEach(v => {
|
|
347
|
-
if (typeof v === 'string') {
|
|
348
|
-
message.push(segment.text(v))
|
|
349
|
-
} else {
|
|
350
|
-
message.push(v)
|
|
351
|
-
}
|
|
352
|
-
})
|
|
353
|
-
return message
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* 制作简单转发,返回segment.node[]。仅简单包装node,也可以自己组装
|
|
358
|
-
* @param elements
|
|
359
|
-
* @param fakeUin 转发用户的QQ号 必填
|
|
360
|
-
* @param fakeNick 转发用户显示的昵称 必填
|
|
361
|
-
*/
|
|
362
|
-
makeForward (elements, fakeUin, fakeNick) {
|
|
363
|
-
if (!Array.isArray(elements)) { elements = [elements] }
|
|
364
|
-
return elements.map(element => {
|
|
365
|
-
const NodeElement = this.makeMessage(element)
|
|
366
|
-
return segment.node(fakeUin, fakeNick, NodeElement)
|
|
367
|
-
})
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
/**
|
|
371
|
-
* 获取git插件列表
|
|
372
|
-
* @param isPack - 是否屏蔽不带package.json的插件,默认为false
|
|
373
|
-
*/
|
|
374
|
-
getPlugins (isPack = false) {
|
|
375
|
-
const dir = this.absPath('./plugins', false)
|
|
376
|
-
let list = fs.readdirSync(dir, { withFileTypes: true })
|
|
377
|
-
/** 忽略非文件夹、非 karin-plugin-开头的文件夹 */
|
|
378
|
-
list = list.filter(v => v.isDirectory() && v.name.startsWith('karin-plugin-'))
|
|
379
|
-
if (isPack) { list = list.filter(v => fs.existsSync(`${dir}/${v.name}/package.json`)) }
|
|
380
|
-
const arr = []
|
|
381
|
-
list.map(v => arr.push(v.name))
|
|
382
|
-
return arr
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
/**
|
|
386
|
-
* 获取npm插件列表
|
|
387
|
-
* @param showDetails - 是否返回详细信息,默认为false
|
|
388
|
-
* 默认只返回插件npm包名,为true时返回详细的{dir, name}[]
|
|
389
|
-
*/
|
|
390
|
-
async getNpmPlugins (showDetails) {
|
|
391
|
-
/** 屏蔽的依赖包列表 */
|
|
392
|
-
const pkgdependencies = [
|
|
393
|
-
'@grpc/grpc-js',
|
|
394
|
-
'@grpc/proto-loader',
|
|
395
|
-
'art-template',
|
|
396
|
-
'axios',
|
|
397
|
-
'chalk',
|
|
398
|
-
'chokidar',
|
|
399
|
-
'express',
|
|
400
|
-
'kritor-proto',
|
|
401
|
-
'level',
|
|
402
|
-
'lodash',
|
|
403
|
-
'log4js',
|
|
404
|
-
'moment',
|
|
405
|
-
'node-karin',
|
|
406
|
-
'node-schedule',
|
|
407
|
-
'redis',
|
|
408
|
-
'ws',
|
|
409
|
-
'yaml',
|
|
410
|
-
]
|
|
411
|
-
const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8'))
|
|
412
|
-
const dependencies = Object.keys(pkg.dependencies).filter((name) => !pkgdependencies.includes(name))
|
|
413
|
-
if (!showDetails) {
|
|
414
|
-
const list = []
|
|
415
|
-
// 检查pkg是否存在karin字段
|
|
416
|
-
const readPackageJson = async (name) => {
|
|
417
|
-
try {
|
|
418
|
-
const pkgPath = path.join(process.cwd(), 'node_modules', name, 'package.json')
|
|
419
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
|
|
420
|
-
if (pkg?.karin) { list.push(name) }
|
|
421
|
-
} catch { }
|
|
422
|
-
}
|
|
423
|
-
await Promise.all(dependencies.map(readPackageJson))
|
|
424
|
-
return list
|
|
425
|
-
} else {
|
|
426
|
-
const list = []
|
|
427
|
-
const readPackageJson = async (files) => {
|
|
428
|
-
try {
|
|
429
|
-
const pkgPath = path.join(process.cwd(), 'node_modules', files, 'package.json')
|
|
430
|
-
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
|
|
431
|
-
if (pkg?.karin) {
|
|
432
|
-
if (pkg?.main) {
|
|
433
|
-
const dir = `${files}/${path.dirname(pkg.main).replace(/\.\//, '')}`
|
|
434
|
-
list.push({ dir, name: path.basename(pkg.main), isMain: true })
|
|
435
|
-
}
|
|
436
|
-
if (pkg?.karin?.apps?.length) {
|
|
437
|
-
pkg.karin.apps.forEach((app) => {
|
|
438
|
-
fs.readdirSync(`./node_modules/${files}/${app}`).forEach((filename) => {
|
|
439
|
-
/** 忽略非js */
|
|
440
|
-
if (!filename.endsWith('.js')) { return }
|
|
441
|
-
const dir = `${files}/${app}`
|
|
442
|
-
list.push({ dir, name: filename, isMain: false })
|
|
443
|
-
})
|
|
444
|
-
})
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
} catch { }
|
|
448
|
-
}
|
|
449
|
-
await Promise.all(dependencies.map(readPackageJson))
|
|
450
|
-
return list
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
/**
|
|
455
|
-
* 获取运行时间
|
|
456
|
-
*/
|
|
457
|
-
uptime () {
|
|
458
|
-
const time = process.uptime()
|
|
459
|
-
const day = Math.floor(time / 86400)
|
|
460
|
-
const hour = Math.floor((time % 86400) / 3600)
|
|
461
|
-
const min = Math.floor((time % 3600) / 60)
|
|
462
|
-
const sec = Math.floor(time % 60)
|
|
463
|
-
const parts = [day ? `${day}天` : '', hour ? `${hour}小时` : '', min ? `${min}分钟` : '', !day && sec ? `${sec}秒` : '']
|
|
464
|
-
return parts.filter(Boolean).join('')
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
/**
|
|
468
|
-
* 构建消息体日志
|
|
469
|
-
* @param - 消息体
|
|
470
|
-
*/
|
|
471
|
-
makeMessageLog (message) {
|
|
472
|
-
const logs = []
|
|
473
|
-
for (const val of message) {
|
|
474
|
-
switch (val.type) {
|
|
475
|
-
case 'text':
|
|
476
|
-
logs.push(val.text)
|
|
477
|
-
break
|
|
478
|
-
case 'face':
|
|
479
|
-
logs.push(`[face:${val.id}]`)
|
|
480
|
-
break
|
|
481
|
-
case 'video':
|
|
482
|
-
case 'image':
|
|
483
|
-
case 'voice':
|
|
484
|
-
case 'record':
|
|
485
|
-
case 'file': {
|
|
486
|
-
let file
|
|
487
|
-
if (Buffer.isBuffer(val.file)) {
|
|
488
|
-
file = 'Buffer://...'
|
|
489
|
-
} else if (/^http|^file/.test(val.file)) {
|
|
490
|
-
file = val.file
|
|
491
|
-
} else {
|
|
492
|
-
file = 'base64://...'
|
|
493
|
-
}
|
|
494
|
-
logs.push(`[${val.type}:${file}]`)
|
|
495
|
-
break
|
|
496
|
-
}
|
|
497
|
-
case 'at':
|
|
498
|
-
logs.push(`[at:${val.uid}]`)
|
|
499
|
-
break
|
|
500
|
-
case 'rps':
|
|
501
|
-
logs.push(`[rps:${val.id}]`)
|
|
502
|
-
break
|
|
503
|
-
case 'dice':
|
|
504
|
-
logs.push(`[dice:${val.id}]`)
|
|
505
|
-
break
|
|
506
|
-
case 'poke':
|
|
507
|
-
logs.push(`[poke:${val.id}]`)
|
|
508
|
-
break
|
|
509
|
-
case 'share':
|
|
510
|
-
logs.push(`[share:${JSON.stringify(val)}]`)
|
|
511
|
-
break
|
|
512
|
-
case 'contact':
|
|
513
|
-
logs.push(`[contact:${JSON.stringify(val)}]`)
|
|
514
|
-
break
|
|
515
|
-
case 'location':
|
|
516
|
-
logs.push(`[location:${JSON.stringify(val)}]`)
|
|
517
|
-
break
|
|
518
|
-
case 'music':
|
|
519
|
-
logs.push(`[music:${JSON.stringify(val)}]`)
|
|
520
|
-
break
|
|
521
|
-
case 'reply':
|
|
522
|
-
logs.push(`[reply:${val.message_id}]`)
|
|
523
|
-
break
|
|
524
|
-
case 'forward':
|
|
525
|
-
logs.push(`[forward:${val.res_id}]`)
|
|
526
|
-
break
|
|
527
|
-
case 'xml':
|
|
528
|
-
logs.push(`[xml:${val.data}]`)
|
|
529
|
-
break
|
|
530
|
-
case 'json':
|
|
531
|
-
logs.push(`[json:${val.data}]`)
|
|
532
|
-
break
|
|
533
|
-
case 'markdown': {
|
|
534
|
-
logs.push(`[markdown:${val.content}]`)
|
|
535
|
-
break
|
|
536
|
-
}
|
|
537
|
-
case 'markdown_tpl': {
|
|
538
|
-
const params = val.params
|
|
539
|
-
if (!params) { break }
|
|
540
|
-
const content = { id: val.custom_template_id }
|
|
541
|
-
for (const v of params) { content[v.key] = v.values[0] }
|
|
542
|
-
logs.push(`[markdown_tpl:${JSON.stringify(content)}]`)
|
|
543
|
-
break
|
|
544
|
-
}
|
|
545
|
-
case 'rows': {
|
|
546
|
-
const rows = []
|
|
547
|
-
for (const v of val.rows) { rows.push(JSON.stringify(v.data)) }
|
|
548
|
-
logs.push(`[rows:${JSON.stringify(rows)}]`)
|
|
549
|
-
break
|
|
550
|
-
}
|
|
551
|
-
case 'button':
|
|
552
|
-
logs.push(`[button:${JSON.stringify(val.data)}]`)
|
|
553
|
-
break
|
|
554
|
-
case 'long_msg':
|
|
555
|
-
logs.push(`[long_msg:${val.id}]`)
|
|
556
|
-
break
|
|
557
|
-
default:
|
|
558
|
-
logs.push(`[未知:${JSON.stringify(val)}]`)
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
return logs.join('')
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
/**
|
|
565
|
-
* 更新yaml文件
|
|
566
|
-
* @param filePath - 文件路径
|
|
567
|
-
* @param settings - 设置项
|
|
568
|
-
*/
|
|
569
|
-
updateYaml (filePath, settings) {
|
|
570
|
-
let yaml = new YamlEditor(filePath)
|
|
571
|
-
/** 先添加内容 */
|
|
572
|
-
settings.forEach(({ key, val }) => {
|
|
573
|
-
try {
|
|
574
|
-
if (!yaml.has(key)) { yaml.set(key, val) }
|
|
575
|
-
} catch (error) {
|
|
576
|
-
logger.error(`[common] 更新yaml文件时出错:${error.stack || error.message || error}`)
|
|
577
|
-
}
|
|
578
|
-
})
|
|
579
|
-
/** 先保存 */
|
|
580
|
-
yaml.save()
|
|
581
|
-
/** 重新解析 再次写入注释 直接写入注释会报错 写入的不是node节点模式 */
|
|
582
|
-
yaml = new YamlEditor(filePath)
|
|
583
|
-
settings.forEach(({ key, comment }) => {
|
|
584
|
-
try {
|
|
585
|
-
yaml.comment(key, comment, true)
|
|
586
|
-
} catch (error) {
|
|
587
|
-
logger.error(`[common] 更新yaml文件时出错:${error.stack || error.message || error}`)
|
|
588
|
-
}
|
|
589
|
-
})
|
|
590
|
-
yaml.save()
|
|
591
|
-
}
|
|
592
|
-
}
|
|
593
|
-
/**
|
|
594
|
-
* 常用方法
|
|
595
|
-
*/
|
|
596
|
-
export const common = new Common()
|