🎈 perf: 更新小蓝鸟的群发算法

This commit is contained in:
zhiyu1998 2023-02-26 18:34:02 +08:00
parent 97684297d7
commit 2cf3702704

View File

@ -384,7 +384,7 @@ export class tools extends plugin {
"duration_ms,height,media_key,preview_image_url,public_metrics,type,url,width,alt_text,variants", "duration_ms,height,media_key,preview_image_url,public_metrics,type,url,width,alt_text,variants",
expansions: [ "entities.mentions.username", "attachments.media_keys" ], expansions: [ "entities.mentions.username", "attachments.media_keys" ],
}) })
.then(resp => { .then(async resp => {
e.reply(`识别:小蓝鸟学习版,${ resp.data.text }`); e.reply(`识别:小蓝鸟学习版,${ resp.data.text }`);
const downloadPath = `${ this.defaultPath }${ this.e.group_id || this.e.user_id }`; const downloadPath = `${ this.defaultPath }${ this.e.group_id || this.e.user_id }`;
// 创建文件夹(如果没有过这个群) // 创建文件夹(如果没有过这个群)
@ -392,14 +392,11 @@ export class tools extends plugin {
mkdirsSync(downloadPath); mkdirsSync(downloadPath);
} }
// 逐个遍历判断 // 逐个遍历判断
let task = []
for (let item of resp.includes.media) { for (let item of resp.includes.media) {
if (item.type === "photo") { if (item.type === "photo") {
// 图片 // 图片
const filePath = `${ downloadPath }/${ item.url.split("/").pop() }`; task.push(this.downloadImg(item.url, downloadPath))
this.downloadImg(item.url, downloadPath).then(_ => {
e.reply(segment.image(fs.readFileSync(filePath)));
fs.unlinkSync(filePath);
});
} else if (item.type === "video") { } else if (item.type === "video") {
// 视频 // 视频
this.downloadVideo(resp.includes.media[0].variants[0].url, true).then(_ => { this.downloadVideo(resp.includes.media[0].variants[0].url, true).then(_ => {
@ -407,6 +404,25 @@ export class tools extends plugin {
}); });
} }
} }
let images = []
let path = []
// 获取所有图片的promise
await Promise.all(task).then(resp => {
// console.log(resp)
resp.forEach(item => {
path.push(item)
images.push({
message: segment.image(fs.readFileSync(item)),
nickname: this.e.sender.card || this.e.user_id,
user_id: this.e.user_id,
});
})
})
await e.reply(await Bot.makeForwardMsg(images))
// 清理文件
path.forEach(item => {
fs.unlinkSync(item);
})
}); });
return true; return true;
} }