diff --git a/index.js b/index.js index f52b949..0eff9b0 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,12 @@ -const Discord = require("discord.js"); +const Discord = require('discord.js'); const client = new Discord.Client(); const fs = require('fs'); const config = require('./config.json'); -const appendChunkToFile = (fileName) => { - const pathToFile = __dirname + `/recordings/${fileName}.pcm`; - return fs.createWriteStream(pathToFile, { flags: 'a' }); +const createNewChunk = () => { + const pathToFile = __dirname + `/recordings/${Date.now()}.pcm`; + return fs.createWriteStream(pathToFile); }; client.on('message', msg => { @@ -14,7 +14,7 @@ client.on('message', msg => { const commandBody = msg.content.substring(config.PREFIX.length).split(' '); const channelName = commandBody[1]; - if (commandBody[0] === ('enter') && commandBody[1] && commandBody[2]) { + if (commandBody[0] === ('enter') && commandBody[1]) { const voiceChannel = msg.guild.channels.cache.find(channel => channel.name === channelName); if (!voiceChannel || voiceChannel.type !== 'voice') @@ -34,7 +34,7 @@ client.on('message', msg => { if (speaking) { console.log(`${user.username} started speaking`); const audioStream = receiver.createStream(user, { mode: 'pcm' }); - audioStream.pipe(appendChunkToFile(commandBody[2])); + audioStream.pipe(createNewChunk()); audioStream.on('end', () => { console.log(`${user.username} stopped speaking`); }); } }); @@ -43,9 +43,14 @@ client.on('message', msg => { } if (commandBody[0] === ('exit') && commandBody[1]) { const voiceChannel = msg.guild.channels.cache.find(channel => channel.name === channelName); - console.log(`Slipping out of ${voiceChannel.name}...`); - voiceChannel.leave(); - console.log(`\nSTOPPED RECORDING\n`); + if (!voiceChannel || voiceChannel.type !== 'voice') { + return msg.reply(`The channel #${channelName} doesn't exist or isn't a voice channel.`); + } + else { + voiceChannel.leave(); + console.log(`Slipping out of ${voiceChannel.name}...`); + console.log(`\nSTOPPED RECORDING\n`); + } } } }); diff --git a/merge.js b/merge.js new file mode 100644 index 0000000..81cd36c --- /dev/null +++ b/merge.js @@ -0,0 +1,26 @@ +var fs = require('fs'), + chunks = fs.readdirSync('./recordings'), + inputStream, + currentfile, + outputStream = fs.createWriteStream('./recordings/merge.pcm'); + +chunks.sort((a, b) => { return a - b; }); + +function appendFiles() { + if (!chunks.length) { + outputStream.end(() => console.log('Finished.')); + return; + } + + currentfile = './recordings/' + chunks.shift(); + inputStream = fs.createReadStream(currentfile); + + inputStream.pipe(outputStream, { end: false }); + + inputStream.on('end', function() { + console.log(currentfile + ' appended'); + appendFiles(); + }); +} + +appendFiles(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index d31ab0c..1f5e774 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "discord-bot", + "name": "discord-voice-recorder", "version": "1.0.0", "lockfileVersion": 1, "requires": true, @@ -343,9 +343,9 @@ "integrity": "sha512-Ntyt4AIXyaLIuMHF6IOoTakB3K+RWxwtsHNRxllEoA6vPwP9o4866g6YWDLUdnucilZhmkxiHwHr11gAENw+QA==" }, "node-fetch": { - "version": "2.6.0", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", - "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", + "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" }, "nopt": { "version": "4.0.3",