细节优化
This commit is contained in:
parent
4b6b547c6c
commit
0634aa026b
|
@ -1,11 +1,15 @@
|
||||||
import fs from "node:fs"
|
import fs from "node:fs"
|
||||||
|
import path from "node:path"
|
||||||
|
|
||||||
function toSegment(type, data) {
|
function toSegment(type, data) {
|
||||||
for (const i in data) {
|
for (const i in data) {
|
||||||
switch (typeof data[i]) {
|
switch (typeof data[i]) {
|
||||||
case "string":
|
case "string":
|
||||||
if ((i == "file" || data[i].match(/^file:\/\//)) && fs.existsSync(data[i].replace(/^file:\/\//, "")))
|
if ((i == "file" || data[i].match(/^file:\/\//)) && fs.existsSync(data[i].replace(/^file:\/\//, ""))) {
|
||||||
|
if (i == "file" && !data.name)
|
||||||
|
data.name = path.basename(data[i])
|
||||||
data[i] = `base64://${fs.readFileSync(data[i].replace(/^file:\/\//, "")).toString("base64")}`
|
data[i] = `base64://${fs.readFileSync(data[i].replace(/^file:\/\//, "")).toString("base64")}`
|
||||||
|
}
|
||||||
break
|
break
|
||||||
case "object":
|
case "object":
|
||||||
if (Buffer.isBuffer(data[i]))
|
if (Buffer.isBuffer(data[i]))
|
||||||
|
@ -19,17 +23,20 @@ const segment = new class segment {
|
||||||
custom(type, data) {
|
custom(type, data) {
|
||||||
return toSegment(type, data)
|
return toSegment(type, data)
|
||||||
}
|
}
|
||||||
image(file) {
|
image(file, name) {
|
||||||
return toSegment("image", { file })
|
return toSegment("image", { file, name })
|
||||||
}
|
}
|
||||||
at(qq, name) {
|
at(qq, name) {
|
||||||
return toSegment("at", { qq, name })
|
return toSegment("at", { qq, name })
|
||||||
}
|
}
|
||||||
record(file) {
|
record(file, name) {
|
||||||
return toSegment("record", { file })
|
return toSegment("record", { file, name })
|
||||||
}
|
}
|
||||||
video(file) {
|
video(file, name) {
|
||||||
return toSegment("video", { file })
|
return toSegment("video", { file, name })
|
||||||
|
}
|
||||||
|
file(file, name) {
|
||||||
|
return toSegment("file", { file, name })
|
||||||
}
|
}
|
||||||
reply(id, text, qq, time, seq) {
|
reply(id, text, qq, time, seq) {
|
||||||
return toSegment("reply", { id, text, qq, time, seq })
|
return toSegment("reply", { id, text, qq, time, seq })
|
||||||
|
@ -49,8 +56,8 @@ const segment = new class segment {
|
||||||
gift(qq, id) {
|
gift(qq, id) {
|
||||||
return toSegment("gift", { qq, id })
|
return toSegment("gift", { qq, id })
|
||||||
}
|
}
|
||||||
cardimage(file, minwidth, minheight, maxwidth, maxheight, source, icon) {
|
cardimage(file, name, minwidth, minheight, maxwidth, maxheight, source, icon) {
|
||||||
return toSegment("cardimage", { file, minwidth, minheight, maxwidth, maxheight, source, icon })
|
return toSegment("cardimage", { file, name, minwidth, minheight, maxwidth, maxheight, source, icon })
|
||||||
}
|
}
|
||||||
tts(text) {
|
tts(text) {
|
||||||
return toSegment("tts", { text })
|
return toSegment("tts", { text })
|
||||||
|
|
|
@ -34,16 +34,16 @@
|
||||||
"pm2": "^5.3.0",
|
"pm2": "^5.3.0",
|
||||||
"puppeteer": "^21.1.1",
|
"puppeteer": "^21.1.1",
|
||||||
"redis": "^4.6.8",
|
"redis": "^4.6.8",
|
||||||
"sequelize": "^6.32.1",
|
"sequelize": "^6.33.0",
|
||||||
"sqlite3": "^5.1.6",
|
"sqlite3": "^5.1.6",
|
||||||
"ws": "^8.14.0",
|
"ws": "^8.14.1",
|
||||||
"yaml": "^2.3.2"
|
"yaml": "^2.3.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint": "^8.48.0",
|
"eslint": "^8.49.0",
|
||||||
"eslint-config-standard": "^17.1.0",
|
"eslint-config-standard": "^17.1.0",
|
||||||
"eslint-plugin-import": "^2.28.1",
|
"eslint-plugin-import": "^2.28.1",
|
||||||
"eslint-plugin-n": "^16.0.2",
|
"eslint-plugin-n": "^16.1.0",
|
||||||
"eslint-plugin-promise": "^6.1.1"
|
"eslint-plugin-promise": "^6.1.1"
|
||||||
},
|
},
|
||||||
"imports": {
|
"imports": {
|
||||||
|
|
|
@ -26,7 +26,7 @@ Bot.adapter.push(new class ComWeChatAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
makeLog(msg) {
|
makeLog(msg) {
|
||||||
return this.toStr(msg).replace(/(base64:\/\/|"type":"data","data":").*?(,|]|")/g, "$1...$2")
|
return this.toStr(msg).replace(/(base64:\/\/|"type":"data","data":").*?"/g, '$1..."')
|
||||||
}
|
}
|
||||||
|
|
||||||
sendApi(ws, action, params = {}) {
|
sendApi(ws, action, params = {}) {
|
||||||
|
@ -63,7 +63,7 @@ Bot.adapter.push(new class ComWeChatAdapter {
|
||||||
opts.type = "data"
|
opts.type = "data"
|
||||||
opts.data = file.replace(/^base64:\/\//, "")
|
opts.data = file.replace(/^base64:\/\//, "")
|
||||||
} else if (fs.existsSync(file)) {
|
} else if (fs.existsSync(file)) {
|
||||||
opts.type = "data",
|
opts.type = "data"
|
||||||
opts.data = fs.readFileSync(file).toString("base64")
|
opts.data = fs.readFileSync(file).toString("base64")
|
||||||
} else {
|
} else {
|
||||||
opts.type = "path"
|
opts.type = "path"
|
||||||
|
@ -84,16 +84,16 @@ Bot.adapter.push(new class ComWeChatAdapter {
|
||||||
else if (!i.data)
|
else if (!i.data)
|
||||||
i = { type: i.type, data: { ...i, type: undefined }}
|
i = { type: i.type, data: { ...i, type: undefined }}
|
||||||
if (i.data.file)
|
if (i.data.file)
|
||||||
i.data = { file_id: (await this.uploadFile(data, i.data.file)).file_id }
|
i.data = { file_id: (await this.uploadFile(data, i.data.file, i.data.name)).file_id }
|
||||||
|
|
||||||
switch (i.type) {
|
switch (i.type) {
|
||||||
case "text":
|
case "text":
|
||||||
break
|
|
||||||
case "image":
|
case "image":
|
||||||
|
case "file":
|
||||||
|
case "wx.emoji":
|
||||||
|
case "wx.link":
|
||||||
break
|
break
|
||||||
case "record":
|
case "record":
|
||||||
i.type = "file"
|
|
||||||
break
|
|
||||||
case "video":
|
case "video":
|
||||||
i.type = "file"
|
i.type = "file"
|
||||||
break
|
break
|
||||||
|
@ -105,11 +105,6 @@ Bot.adapter.push(new class ComWeChatAdapter {
|
||||||
break
|
break
|
||||||
case "reply":
|
case "reply":
|
||||||
continue
|
continue
|
||||||
break
|
|
||||||
case "wx.emoji":
|
|
||||||
break
|
|
||||||
case "wx.link":
|
|
||||||
break
|
|
||||||
default:
|
default:
|
||||||
i = { type: "text", data: { text: JSON.stringify(i) }}
|
i = { type: "text", data: { text: JSON.stringify(i) }}
|
||||||
}
|
}
|
||||||
|
@ -223,13 +218,6 @@ Bot.adapter.push(new class ComWeChatAdapter {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendFile(data, send, file, name) {
|
|
||||||
logger.info(`${logger.blue(`[${data.self_id}]`)} 发送文件:${name}(${file})`)
|
|
||||||
return send(segment.custom("file", {
|
|
||||||
file_id: (await this.uploadFile(data, file, name)).file_id
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
pickFriend(data, user_id) {
|
pickFriend(data, user_id) {
|
||||||
const i = {
|
const i = {
|
||||||
...data.bot.fl.get(user_id),
|
...data.bot.fl.get(user_id),
|
||||||
|
@ -239,7 +227,7 @@ Bot.adapter.push(new class ComWeChatAdapter {
|
||||||
return {
|
return {
|
||||||
...i,
|
...i,
|
||||||
sendMsg: msg => this.sendFriendMsg(i, msg),
|
sendMsg: msg => this.sendFriendMsg(i, msg),
|
||||||
sendFile: (file, name) => this.sendFile(i, msg => this.sendFriendMsg(i, msg), file, name),
|
sendFile: (file, name) => this.sendFriendMsg(i, segment.file(file, name)),
|
||||||
getInfo: () => this.getFriendInfo(i),
|
getInfo: () => this.getFriendInfo(i),
|
||||||
getAvatarUrl: async () => (await this.getFriendInfo(i))["wx.avatar"],
|
getAvatarUrl: async () => (await this.getFriendInfo(i))["wx.avatar"],
|
||||||
}
|
}
|
||||||
|
@ -269,7 +257,7 @@ Bot.adapter.push(new class ComWeChatAdapter {
|
||||||
return {
|
return {
|
||||||
...i,
|
...i,
|
||||||
sendMsg: msg => this.sendGroupMsg(i, msg),
|
sendMsg: msg => this.sendGroupMsg(i, msg),
|
||||||
sendFile: (file, name) => this.sendFile(i, msg => this.sendGroupMsg(i, msg), file, name),
|
sendFile: (file, name) => this.sendGroupMsg(i, segment.file(file, name)),
|
||||||
getInfo: () => this.getGroupInfo(i),
|
getInfo: () => this.getGroupInfo(i),
|
||||||
getAvatarUrl: async () => (await this.getGroupInfo(i))["wx.avatar"],
|
getAvatarUrl: async () => (await this.getGroupInfo(i))["wx.avatar"],
|
||||||
getMemberArray: () => this.getMemberArray(i),
|
getMemberArray: () => this.getMemberArray(i),
|
||||||
|
|
|
@ -204,7 +204,7 @@ export class update extends plugin {
|
||||||
|
|
||||||
if (!logAll) return false
|
if (!logAll) return false
|
||||||
|
|
||||||
logAll = logAll.split('\n')
|
logAll = logAll.trim().split('\n')
|
||||||
|
|
||||||
let log = []
|
let log = []
|
||||||
for (let str of logAll) {
|
for (let str of logAll) {
|
||||||
|
|
Loading…
Reference in New Issue