Fixed multi user issue. merge.js now merges all chunks.

This commit is contained in:
Sravanth C 2020-10-24 21:49:58 +05:30
parent 0dc6feaa2e
commit 1586f948cc
3 changed files with 44 additions and 13 deletions

View file

@ -1,12 +1,12 @@
const Discord = require("discord.js"); const Discord = require('discord.js');
const client = new Discord.Client(); const client = new Discord.Client();
const fs = require('fs'); const fs = require('fs');
const config = require('./config.json'); const config = require('./config.json');
const appendChunkToFile = (fileName) => { const createNewChunk = () => {
const pathToFile = __dirname + `/recordings/${fileName}.pcm`; const pathToFile = __dirname + `/recordings/${Date.now()}.pcm`;
return fs.createWriteStream(pathToFile, { flags: 'a' }); return fs.createWriteStream(pathToFile);
}; };
client.on('message', msg => { client.on('message', msg => {
@ -14,7 +14,7 @@ client.on('message', msg => {
const commandBody = msg.content.substring(config.PREFIX.length).split(' '); const commandBody = msg.content.substring(config.PREFIX.length).split(' ');
const channelName = commandBody[1]; 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); const voiceChannel = msg.guild.channels.cache.find(channel => channel.name === channelName);
if (!voiceChannel || voiceChannel.type !== 'voice') if (!voiceChannel || voiceChannel.type !== 'voice')
@ -34,7 +34,7 @@ client.on('message', msg => {
if (speaking) { if (speaking) {
console.log(`${user.username} started speaking`); console.log(`${user.username} started speaking`);
const audioStream = receiver.createStream(user, { mode: 'pcm' }); const audioStream = receiver.createStream(user, { mode: 'pcm' });
audioStream.pipe(appendChunkToFile(commandBody[2])); audioStream.pipe(createNewChunk());
audioStream.on('end', () => { console.log(`${user.username} stopped speaking`); }); audioStream.on('end', () => { console.log(`${user.username} stopped speaking`); });
} }
}); });
@ -43,9 +43,14 @@ client.on('message', msg => {
} }
if (commandBody[0] === ('exit') && commandBody[1]) { if (commandBody[0] === ('exit') && commandBody[1]) {
const voiceChannel = msg.guild.channels.cache.find(channel => channel.name === channelName); const voiceChannel = msg.guild.channels.cache.find(channel => channel.name === channelName);
console.log(`Slipping out of ${voiceChannel.name}...`); if (!voiceChannel || voiceChannel.type !== 'voice') {
voiceChannel.leave(); return msg.reply(`The channel #${channelName} doesn't exist or isn't a voice channel.`);
console.log(`\nSTOPPED RECORDING\n`); }
else {
voiceChannel.leave();
console.log(`Slipping out of ${voiceChannel.name}...`);
console.log(`\nSTOPPED RECORDING\n`);
}
} }
} }
}); });

26
merge.js Normal file
View file

@ -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();

8
package-lock.json generated
View file

@ -1,5 +1,5 @@
{ {
"name": "discord-bot", "name": "discord-voice-recorder",
"version": "1.0.0", "version": "1.0.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
@ -343,9 +343,9 @@
"integrity": "sha512-Ntyt4AIXyaLIuMHF6IOoTakB3K+RWxwtsHNRxllEoA6vPwP9o4866g6YWDLUdnucilZhmkxiHwHr11gAENw+QA==" "integrity": "sha512-Ntyt4AIXyaLIuMHF6IOoTakB3K+RWxwtsHNRxllEoA6vPwP9o4866g6YWDLUdnucilZhmkxiHwHr11gAENw+QA=="
}, },
"node-fetch": { "node-fetch": {
"version": "2.6.0", "version": "2.6.1",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
"integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw=="
}, },
"nopt": { "nopt": {
"version": "4.0.3", "version": "4.0.3",