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 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,11 +43,16 @@ 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}...`);
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`);
}
}
}
});
client.login(config.BOT_TOKEN);

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",
"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",