From d4a9e5ec9cd69478fad05c945d0ff28d1c42cc35 Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Sun, 29 Oct 2017 13:19:38 +0000 Subject: [PATCH] Fix ESLint and handle stream errors --- .../voice/dispatcher/BroadcastDispatcher.js | 1 - .../voice/dispatcher/StreamDispatcher.js | 13 +++- src/client/voice/player/BasePlayer.js | 2 +- .../voice/player/BroadcastAudioPlayer.js | 1 - test/voice.js | 71 ++++++++----------- 5 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/client/voice/dispatcher/BroadcastDispatcher.js b/src/client/voice/dispatcher/BroadcastDispatcher.js index 902935cc4..330d05292 100644 --- a/src/client/voice/dispatcher/BroadcastDispatcher.js +++ b/src/client/voice/dispatcher/BroadcastDispatcher.js @@ -1,4 +1,3 @@ -const Collection = require('../../../util/Collection'); const StreamDispatcher = require('./StreamDispatcher'); /** diff --git a/src/client/voice/dispatcher/StreamDispatcher.js b/src/client/voice/dispatcher/StreamDispatcher.js index eae032c40..ce88524be 100644 --- a/src/client/voice/dispatcher/StreamDispatcher.js +++ b/src/client/voice/dispatcher/StreamDispatcher.js @@ -29,8 +29,8 @@ nonce.fill(0); * @extends {stream.Writable} */ class StreamDispatcher extends Writable { - constructor(player, { seek = 0, volume = 1, passes = 1, fec, plp, bitrate = 96, highWaterMark = 4096 } = {}, streams) { - const streamOptions = { seek, volume, passes, fec, plp, bitrate, highWaterMark }; + constructor(player, { seek = 0, volume = 1, passes = 1, fec, plp, bitrate = 96 } = {}, streams) { + const streamOptions = { seek, volume, passes, fec, plp, bitrate }; super(streamOptions); /** * The Audio Player that controls this dispatcher @@ -67,6 +67,15 @@ class StreamDispatcher extends Writable { if (typeof fec !== 'undefined') this.setFEC(fec); if (typeof plp !== 'undefined') this.setPLP(plp); if (typeof bitrate !== 'undefined') this.setBitrate(bitrate); + + const streamError = err => { + this.emit('warn', err); + this.destroy(); + }; + + if (this.streams.ffmpeg) this.streams.ffmpeg.on('error', streamError); + if (this.streams.opus) this.streams.opus.on('error', streamError); + if (this.streams.volume) this.streams.volume.on('error', streamError); } get _sdata() { diff --git a/src/client/voice/player/BasePlayer.js b/src/client/voice/player/BasePlayer.js index bb0de6bb7..5beb5b033 100644 --- a/src/client/voice/player/BasePlayer.js +++ b/src/client/voice/player/BasePlayer.js @@ -16,7 +16,7 @@ const FFMPEG_ARGUMENTS = [ * @extends {EventEmitter} */ class BasePlayer extends EventEmitter { - constructor(voiceConnection) { + constructor() { super(); this.dispatcher = null; diff --git a/src/client/voice/player/BroadcastAudioPlayer.js b/src/client/voice/player/BroadcastAudioPlayer.js index 41441276d..8a9071692 100644 --- a/src/client/voice/player/BroadcastAudioPlayer.js +++ b/src/client/voice/player/BroadcastAudioPlayer.js @@ -1,4 +1,3 @@ -const prism = require('prism-media'); const BroadcastDispatcher = require('../dispatcher/BroadcastDispatcher'); const BasePlayer = require('./BasePlayer'); diff --git a/test/voice.js b/test/voice.js index 8cdc9dc70..29e1eacc9 100644 --- a/test/voice.js +++ b/test/voice.js @@ -1,6 +1,11 @@ /* eslint no-console: 0 */ 'use strict'; +var profiler = require('gc-profiler'); +profiler.on('gc', function (info) { + console.log(info); +}); + const Discord = require('../'); const ytdl = require('ytdl-core'); const prism = require('prism-media'); @@ -19,6 +24,32 @@ var d, b; client.on('debug', console.log); client.on('error', console.log); +async function wait(time = 1000) { + return new Promise(resolve => { + setTimeout(resolve, time); + }); +} + +var count = 0; + +client.on('ready', async () => { + for (const guild of client.guilds.values()) { + const channels = guild.channels.filter(c => c.type === 'voice' && c.joinable && c.members.size === 0); + const channel = channels.first(); + if (channel) { + channel.join().then(conn => { + conn.playOpusStream(fs.createReadStream('C:/users/amish/downloads/z.ogg').pipe(new prism.OggOpusDemuxer())); + }); + count++; + console.log(`Playing in ${channel.name} in ${channel.guild.name} at count ${count} ${process.memoryUsage().rss / (1024 * 1024)}`); + await wait(); + } + } + console.log('done!'); +}); + +process.on('unhandledRejection', console.log); + client.on('message', m => { if (!m.guild) return; if (m.author.id !== '66564597481480192') return; @@ -35,23 +66,6 @@ client.on('message', m => { } else { m.reply('Specify a voice channel!'); } - } else if (m.content.startsWith('/play')) { - if (connections.has(m.guild.id)) { - const url = m.content.split(' ').slice(1).join(' ') - .replace(//g, ''); - const stream = ytdl(url, { filter: 'audioonly' }, { passes: 3 }); - d = m.guild.voiceConnection.playStream(stream); - d.setBitrate(1); - setTimeout(() => d.setBitrate(320), 5000); - } - } else if (m.content.startsWith('/skip')) { - if (connections.has(m.guild.id)) { - const connData = connections.get(m.guild.id); - if (connData.dispatcher) { - connData.dispatcher.end(); - } - } } else if (m.content.startsWith('#eval') && m.author.id === '66564597481480192') { try { const com = eval(m.content.split(' ').slice(1).join(' ')); @@ -60,28 +74,5 @@ client.on('message', m => { console.log(e); m.channel.send(e, { code: true }); } - } else if (m.content === 'mb') { - b = client.createVoiceBroadcast(); - b.playStream(ytdl('https://www.youtube.com/watch?v=_XXOSf0s2nk', { filter: 'audioonly' }, { passes: 3 })); - } else if (m.content === 'subscribe!') { - m.guild.voiceConnection.playBroadcast(b); } }); - -function doQueue(connData) { - const conn = connData.conn; - const queue = connData.queue; - const item = queue[0]; - if (!item) return; - const stream = ytdl(item.url, { filter: 'audioonly' }, { passes: 3 }); - const dispatcher = conn.playStream(stream); - stream.on('info', info => { - item.m.reply(`OK, playing **${info.title}**`); - }); - dispatcher.on('end', () => { - queue.shift(); - doQueue(connData); - }); - dispatcher.on('error', (...e) => console.log('dispatcher', ...e)); - connData.dispatcher = dispatcher; -}