if (message.body.indexOf('/yt') != -1) {
const url = message.body.replace('/yt ', '')
await ytdl(url).pipe(fs.createWriteStream('video.mp4'))
// the code below does not wait for the download to finish
const media = MessageMedia.fromFilePath('./video.mp4')
client.sendMessage(message.from, media, { sendMediaAsDocument: true })
}我希望ytdl核心在运行剩余代码之前完成下载。
发布于 2022-04-03 12:19:58
下载过程完成后,它将触发close事件。您可以收听close事件。但是,你必须把它包装成一个承诺,然后只是承诺的await。
if (message.body.indexOf('/yt') != -1) {
const url = message.body.replace('/yt ', '')
await new Promise((resolve) => { // wait
ytdl(url)
.pipe(fs.createWriteStream('video.mp4'))
.on('close', () => {
resolve(); // finish
})
})
// the code below does not wait for the download to finish
const media = MessageMedia.fromFilePath('./video.mp4')
client.sendMessage(message.from, media, { sendMediaAsDocument: true })
}https://stackoverflow.com/questions/71723387
复制相似问题