2023-07-09 00:58:50 +08:00
|
|
|
import fs from "node:fs"
|
2023-09-12 13:28:11 +08:00
|
|
|
import path from "node:path"
|
2023-07-09 00:58:50 +08:00
|
|
|
|
2023-07-16 08:55:31 +08:00
|
|
|
function toSegment(type, data) {
|
|
|
|
for (const i in data) {
|
|
|
|
switch (typeof data[i]) {
|
|
|
|
case "string":
|
2023-09-12 13:28:11 +08:00
|
|
|
if ((i == "file" || data[i].match(/^file:\/\//)) && fs.existsSync(data[i].replace(/^file:\/\//, ""))) {
|
|
|
|
if (i == "file" && !data.name)
|
|
|
|
data.name = path.basename(data[i])
|
2023-07-16 08:55:31 +08:00
|
|
|
data[i] = `base64://${fs.readFileSync(data[i].replace(/^file:\/\//, "")).toString("base64")}`
|
2023-09-12 13:28:11 +08:00
|
|
|
}
|
2023-07-16 08:55:31 +08:00
|
|
|
break
|
|
|
|
case "object":
|
|
|
|
if (Buffer.isBuffer(data[i]))
|
|
|
|
data[i] = `base64://${data[i].toString("base64")}`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { type, ...data }
|
|
|
|
}
|
|
|
|
|
2023-07-09 00:58:50 +08:00
|
|
|
const segment = new class segment {
|
|
|
|
custom(type, data) {
|
2023-07-16 08:55:31 +08:00
|
|
|
return toSegment(type, data)
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
2023-09-12 13:28:11 +08:00
|
|
|
image(file, name) {
|
|
|
|
return toSegment("image", { file, name })
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
|
|
|
at(qq, name) {
|
2023-07-16 08:55:31 +08:00
|
|
|
return toSegment("at", { qq, name })
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
2023-09-12 13:28:11 +08:00
|
|
|
record(file, name) {
|
|
|
|
return toSegment("record", { file, name })
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
2023-09-12 13:28:11 +08:00
|
|
|
video(file, name) {
|
|
|
|
return toSegment("video", { file, name })
|
|
|
|
}
|
|
|
|
file(file, name) {
|
|
|
|
return toSegment("file", { file, name })
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
|
|
|
reply(id, text, qq, time, seq) {
|
2023-07-16 08:55:31 +08:00
|
|
|
return toSegment("reply", { id, text, qq, time, seq })
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
|
|
|
face(id) {
|
2023-07-16 08:55:31 +08:00
|
|
|
return toSegment("face", { id })
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
|
|
|
share(url, title, content, image) {
|
2023-07-16 08:55:31 +08:00
|
|
|
return toSegment("share", { url, title, content, image })
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
|
|
|
music(type, id, url, audio, title) {
|
2023-07-16 08:55:31 +08:00
|
|
|
return toSegment("music", { type, id, url, audio, title })
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
|
|
|
poke(qq) {
|
2023-07-16 08:55:31 +08:00
|
|
|
return toSegment("poke", { qq })
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
|
|
|
gift(qq, id) {
|
2023-07-16 08:55:31 +08:00
|
|
|
return toSegment("gift", { qq, id })
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
2023-09-12 13:28:11 +08:00
|
|
|
cardimage(file, name, minwidth, minheight, maxwidth, maxheight, source, icon) {
|
|
|
|
return toSegment("cardimage", { file, name, minwidth, minheight, maxwidth, maxheight, source, icon })
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
|
|
|
tts(text) {
|
2023-07-16 08:55:31 +08:00
|
|
|
return toSegment("tts", { text })
|
2023-07-09 00:58:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export { segment }
|