如何在不创建新文件的情况下继续在 fs 中写入文件?

How to continue writing the file in fs without creating a new file?

帮助我需要继续记录文件,直到我告诉 discord bot wfileStream.end(); 如何解决这个问题,不工作会抛出错误我做错了什么..

我正在用它来写文件

const fs = require('fs');

代码

if (msg.content === 'cmdrecordvoice' && msg.guild.voiceConnection !== null) {
    var voice_receiver = msg.guild.voiceConnection.createReceiver();
    var wfileStream = fs.createWriteStream('record.opus');
    msg.guild.voiceConnection.on('speaking', (user, speaking) => {
        if (speaking) {
            console.log('recording voice!');
            const audioStream = voice_receiver.createOpusStream(user);
            audioStream.pipe(wfileStream);
            audioStream.on('end', () => {
            console.log('end of recording voice!');
                
            });
        }
    });
}

我知道我必须将文件创建为全局变量 wfileStream,除此之外我应该使用什么 fs."code" 从机器人在文件写入过程中留下的位置开始重写。

只需通过 manual.Then 创建一个名称为 record.opus 的文件,您只需将数据附加到该文件。

if (msg.content === 'cmdrecordvoice' && msg.guild.voiceConnection !== null) {
    var voice_receiver = msg.guild.voiceConnection.createReceiver();
    msg.guild.voiceConnection.on('speaking', (user, speaking) => {
        if (speaking) {
            var wfileStream = fs.createWriteStream('record.opus', {'flags': 'a'}); // creating a write able stream when 'speaking'
            // use {'flags': 'a'} to append and {'flags': 'w'} to erase and write a new file
            console.log('recording voice!');
            const audioStream = voice_receiver.createOpusStream(user);
            audioStream.pipe(wfileStream);
            audioStream.on('end', () => {
               console.log('end of recording voice!');
               wfileStream.end(); // close stream
            });
        }
    });
}