Avon97
Avon97

Reputation: 47

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

Help I need to keep recording the file until I tell the discord bot to wfileStream.end(); how to resolve this problem, dose not work throws errors what am i doing wrong..

I am using this to wrtie files

const fs = require('fs');

code

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!');
                
            });
        }
    });
}

I know I have to create the file as a global variable wfileStream other than that what should I use fs."code" to start rewriting from the place the bot has left in file writing process.

Upvotes: 0

Views: 606

Answers (1)

hoangdv
hoangdv

Reputation: 16127

Just create a file with name record.opus by manual.Then You only append data to the file.

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
            });
        }
    });
}

Upvotes: 1

Related Questions