From 0113202c2f3b94df3cc04ec067f1b5f2c54b88e7 Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Sat, 26 Dec 2015 17:52:20 +0000 Subject: [PATCH] Fixed voice_connection.stopPlaying Now unpipes before destroying --- lib/Structures/Server.js | 2 ++ lib/Structures/User.js | 1 + lib/Voice/AudioEncoder.js | 4 ---- lib/Voice/VoiceConnection.js | 18 ++++++++++-------- package.json | 1 + src/Structures/Server.js | 2 ++ src/Structures/User.js | 1 + src/Voice/AudioEncoder.js | 4 ---- src/Voice/VoiceConnection.js | 17 ++++++++--------- test/msgbot.js | 16 ++++++++++++---- 10 files changed, 37 insertions(+), 29 deletions(-) diff --git a/lib/Structures/Server.js b/lib/Structures/Server.js index dace8828a..674a427b8 100644 --- a/lib/Structures/Server.js +++ b/lib/Structures/Server.js @@ -182,6 +182,7 @@ var Server = (function (_Equality) { this.eventStopSpeaking(user); channel.speaking.add(user); + user.voiceChannel = channel; return true; } else { // bad @@ -206,6 +207,7 @@ var Server = (function (_Equality) { if (chan.speaking.has(user)) { chan.speaking.remove(user); + user.voiceChannel = null; return chan; } } diff --git a/lib/Structures/User.js b/lib/Structures/User.js index 73bf10cd6..49733ed36 100644 --- a/lib/Structures/User.js +++ b/lib/Structures/User.js @@ -36,6 +36,7 @@ var User = (function (_Equality) { since: null, channel: null }; + this.voiceChannel = null; } User.prototype.mention = function mention() { diff --git a/lib/Voice/AudioEncoder.js b/lib/Voice/AudioEncoder.js index 39c0d6a36..b6e4d5cb2 100644 --- a/lib/Voice/AudioEncoder.js +++ b/lib/Voice/AudioEncoder.js @@ -102,12 +102,10 @@ var AudioEncoder = (function () { }); enc.stdout.on("end", function () { - console.log("end"); reject("end"); }); enc.stdout.on("close", function () { - console.log("close"); reject("close"); }); }); @@ -129,12 +127,10 @@ var AudioEncoder = (function () { }); enc.stdout.on("end", function () { - console.log("end"); reject("end"); }); enc.stdout.on("close", function () { - console.log("close"); reject("close"); }); }); diff --git a/lib/Voice/VoiceConnection.js b/lib/Voice/VoiceConnection.js index 9e4b55af8..96748d2f9 100644 --- a/lib/Voice/VoiceConnection.js +++ b/lib/Voice/VoiceConnection.js @@ -43,6 +43,10 @@ var _events = require("events"); var _events2 = _interopRequireDefault(_events); +var _unpipe = require("unpipe"); + +var _unpipe2 = _interopRequireDefault(_unpipe); + var VoiceConnection = (function (_EventEmitter) { _inherits(VoiceConnection, _EventEmitter); @@ -91,13 +95,10 @@ var VoiceConnection = (function (_EventEmitter) { VoiceConnection.prototype.stopPlaying = function stopPlaying() { this.playing = false; this.playingIntent = null; - if (this.streamProc) { - this.streamProc.stdin.pause(); - this.streamProc.kill("SIGINT"); - } if (this.instream) { //not all streams implement these... //and even file stream don't seem to implement them properly... + _unpipe2["default"](this.instream); if (this.instream.end) { this.instream.end(); } @@ -106,6 +107,11 @@ var VoiceConnection = (function (_EventEmitter) { } this.instream = null; } + if (this.streamProc) { + this.streamProc.stdin.pause(); + this.streamProc.kill("SIGINT"); + this.streamProc = null; + } }; VoiceConnection.prototype.playStream = function playStream(stream) { @@ -118,9 +124,6 @@ var VoiceConnection = (function (_EventEmitter) { var length = 20; - if (self.playingIntent) { - self.stopPlaying(); - } self.playing = true; var retStream = new _StreamIntent2["default"](); var onWarning = false; @@ -318,7 +321,6 @@ var VoiceConnection = (function (_EventEmitter) { var _this4 = this; var self = this; - console.log("\n\nendpoint:", this.endpoint, "\n\n"); _dns2["default"].lookup(this.endpoint, function (err, address, family) { var vWS = self.vWS = new _ws2["default"]("wss://" + _this4.endpoint, null, { rejectUnauthorized: false }); _this4.endpoint = address; diff --git a/package.json b/package.json index a4c39a5a4..7045bbc95 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "dependencies": { "fs-extra": "^0.26.2", "superagent": "^1.5.0", + "unpipe": "^1.0.0", "ws": "^0.8.1" }, "devDependencies": { diff --git a/src/Structures/Server.js b/src/Structures/Server.js index d4520bb24..4a2f1edb6 100644 --- a/src/Structures/Server.js +++ b/src/Structures/Server.js @@ -150,6 +150,7 @@ export default class Server extends Equality { this.eventStopSpeaking(user); channel.speaking.add(user); + user.voiceChannel = channel; return true; }else{ // bad @@ -161,6 +162,7 @@ export default class Server extends Equality { for(let chan of this.channels.getAll("type", "voice")){ if(chan.speaking.has(user)){ chan.speaking.remove(user); + user.voiceChannel = null; return chan; } } diff --git a/src/Structures/User.js b/src/Structures/User.js index 85af14804..d18511233 100644 --- a/src/Structures/User.js +++ b/src/Structures/User.js @@ -18,6 +18,7 @@ export default class User extends Equality{ since : null, channel : null }; + this.voiceChannel = null; } get avatarURL(){ diff --git a/src/Voice/AudioEncoder.js b/src/Voice/AudioEncoder.js index 171a3a10e..588934c39 100644 --- a/src/Voice/AudioEncoder.js +++ b/src/Voice/AudioEncoder.js @@ -97,12 +97,10 @@ export default class AudioEncoder { }); enc.stdout.on("end", function () { - console.log("end"); reject("end"); }); enc.stdout.on("close", function () { - console.log("close"); reject("close"); }); }); @@ -130,12 +128,10 @@ export default class AudioEncoder { }); enc.stdout.on("end", function () { - console.log("end"); reject("end"); }); enc.stdout.on("close", function () { - console.log("close"); reject("close"); }); }); diff --git a/src/Voice/VoiceConnection.js b/src/Voice/VoiceConnection.js index 9932b39ff..c1c4c1528 100644 --- a/src/Voice/VoiceConnection.js +++ b/src/Voice/VoiceConnection.js @@ -14,6 +14,7 @@ import AudioEncoder from "./AudioEncoder"; import VoicePacket from "./VoicePacket"; import StreamIntent from "./StreamIntent"; import EventEmitter from "events"; +import unpipe from "unpipe"; export default class VoiceConnection extends EventEmitter { constructor(channel, client, session, token, server, endpoint) { @@ -62,13 +63,10 @@ export default class VoiceConnection extends EventEmitter { stopPlaying() { this.playing = false; this.playingIntent = null; - if (this.streamProc) { - this.streamProc.stdin.pause(); - this.streamProc.kill("SIGINT"); - } - if(this.instream){ + if (this.instream) { //not all streams implement these... //and even file stream don't seem to implement them properly... + unpipe(this.instream); if(this.instream.end) { this.instream.end(); } @@ -77,6 +75,11 @@ export default class VoiceConnection extends EventEmitter { } this.instream = null; } + if (this.streamProc) { + this.streamProc.stdin.pause(); + this.streamProc.kill("SIGINT"); + this.streamProc = null; + } } playStream(stream, channels=2) { @@ -88,9 +91,6 @@ export default class VoiceConnection extends EventEmitter { var length = 20; - if (self.playingIntent) { - self.stopPlaying(); - } self.playing = true; var retStream = new StreamIntent(); var onWarning = false; @@ -282,7 +282,6 @@ export default class VoiceConnection extends EventEmitter { init() { var self = this; - console.log("\n\nendpoint:", this.endpoint, "\n\n"); dns.lookup(this.endpoint, (err, address, family) => { var vWS = self.vWS = new WebSocket("wss://" + this.endpoint, null, { rejectUnauthorized: false }); this.endpoint = address; diff --git a/test/msgbot.js b/test/msgbot.js index f162a1164..f983a6265 100644 --- a/test/msgbot.js +++ b/test/msgbot.js @@ -3,7 +3,7 @@ var Discord = require("../"); var client = new Discord.Client({revive : true}); -var request = require("superagent"); +var request = require("request"); client.on("ready", () => { console.log("ready - " + client.internal.token); @@ -30,11 +30,19 @@ client.on("message", msg => { msg.channel.server.channels.get("type", "voice").join(); } - if (msg.content === "end") { + if (msg.content === "$end$") { client.destroy(); } - if (msg.content === "who tf is speaking") { + if (msg.content === "$stop_playing") { + client.voiceConnection.stopPlaying(); + } + + if (msg.content === "where am I speaking") { + msg.reply(msg.sender.voiceChannel); + } + + if (msg.content === "who is speaking") { for (var chan of msg.channel.server.channels.getAll("type", "voice")) { msg.channel.send(`${chan} : ${chan.speaking}`); } @@ -52,7 +60,7 @@ client.on("message", msg => { if (msg.content.startsWith("$play")) { var url = msg.content.split(" ")[1]; - client.voiceConnection.playFile(url, { + client.voiceConnection.playRawStream(request(url), { volume : 0.1 });