Discord Сохранить голос пользователя с VoiceReceiver - PullRequest
0 голосов
/ 20 февраля 2019


Когда я говорю по голосовому каналу, бот говорит: «Я слушаю ххх»,
Но когда я проверяю папку / recordss, там есть файл .pcm, но его длина равна 0поэтому в основном сохраняем пустой файл .pcm.

Вот код:

const Discord = require("discord.js");

const fs = require('fs');
const client = new Discord.Client();
// const config = require('./auth.json');
// make a new stream for each time someone starts to talk
function generateOutputFile(channel, member) {
  // use IDs instead of username cause some people have stupid emojis in their name
  const fileName = `./recordings/${Date.now()}.pcm`;
  return fs.createWriteStream(fileName);
}
client.on('message', msg => {
  if (msg.content.startsWith('!join')) {
    let [command, ...channelName] = msg.content.split(" ");
    if (!msg.guild) {
      return msg.reply('no private service is available in your area at the moment. Please contact a service representative for more details.');
    }
    const voiceChannel = msg.guild.channels.find("name", channelName.join(" "));
    //console.log(voiceChannel.id);
    if (!voiceChannel || voiceChannel.type !== 'voice') {
      return msg.reply(`I couldn't find the channel ${channelName}. Can you spell?`);
    }
    voiceChannel.join()
      .then(conn => {
        msg.reply('ready!');
        // create our voice receiver
        const receiver = conn.createReceiver();
        conn.on('speaking', (user, speaking) => {
          if (speaking) {
            msg.channel.sendMessage(`I'm listening to ${user}`);
            // this creates a 16-bit signed PCM, stereo 48KHz PCM stream.
            const audioStream = receiver.createPCMStream(user);
            const outputStream = generateOutputFile(voiceChannel, user);
            audioStream.pipe(outputStream);
            outputStream.on("data", console.log);
            audioStream.on('end', () => {
              msg.channel.sendMessage(`I'm no longer listening to ${user}`);
            });
          }
        });
      })
      .catch(console.log);
  }
  if(msg.content.startsWith('!leave')) {
    let [command, ...channelName] = msg.content.split(" ");
    let voiceChannel = msg.guild.channels.find("name", channelName.join(" "));
    voiceChannel.leave();
  }
});
client.login("discord token");
client.on('ready', () => {
  console.log('ready!');
});

Есть ли способ исправить эту проблему или альтернативы, чтобы сохранить голос пользователя при разговоре.

Спасибо.

...