From ae75c4962150dedef170c9b0b757e60c5500fe98 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Sat, 7 Nov 2015 21:24:56 +0000 Subject: [PATCH] Minor fixes --- lib/Client/Client.js | 18 ++++++++++++++++++ lib/Client/InternalClient.js | 6 ++++-- lib/Voice/AudioEncoder.js | 2 -- lib/Voice/VoiceConnection.js | 23 ++++++++++++----------- lib/index.js | 10 ++++++---- src/Client/Client.js | 18 ++++++++++++++++++ src/Client/InternalClient.js | 6 ++++-- src/Voice/AudioEncoder.js | 2 -- src/index.js | 11 +++++++---- 9 files changed, 69 insertions(+), 27 deletions(-) diff --git a/lib/Client/Client.js b/lib/Client/Client.js index 862d6311f..97ec4df12 100644 --- a/lib/Client/Client.js +++ b/lib/Client/Client.js @@ -532,6 +532,24 @@ var Client = (function (_EventEmitter) { }); }; + //def joinVoiceChannel + + Client.prototype.joinVoiceChannel = function joinVoiceChannel(channel) { + var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err) {} : arguments[1]; + + var self = this; + return new Promise(function (resolve, reject) { + + self.internal.joinVoiceChannel(channel).then(function (chan) { + callback(null, chan); + resolve(chan); + })["catch"](function (err) { + callback(err); + reject(err); + }); + }); + }; + _createClass(Client, [{ key: "users", get: function get() { diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index 556fa336e..027d7cc2e 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -76,7 +76,10 @@ var InternalClient = (function () { token = data.d.token; endpoint = data.d.endpoint; - self.voiceConnections[channel] = new VoiceConnection(channel, self.client, session, token, server, endpoint); + var chan = self.voiceConnections[channel] = new VoiceConnection(channel, self.client, session, token, server, endpoint); + + chan.on("ready", resolve); + chan.on("error", reject); } if (fired >= 2) { self.websocket.removeListener('message', check); @@ -774,7 +777,6 @@ var InternalClient = (function () { } request.put(Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id).set("authorization", self.token).send(data).end(function (err) { - console.log(err); if (err) { reject(err); } else { diff --git a/lib/Voice/AudioEncoder.js b/lib/Voice/AudioEncoder.js index 1343bf448..8ae098286 100644 --- a/lib/Voice/AudioEncoder.js +++ b/lib/Voice/AudioEncoder.js @@ -25,8 +25,6 @@ var AudioEncoder = (function () { return new Promise(function (resolve, reject) { var enc = cpoc.spawn("ffmpeg", ["-i", file, "-f", "s16le", "-ar", "48000", "-ac", "1", "-af", "volume=1", "pipe:1"]); - var rcvd = 0; - enc.stdout.on("readable", function () { callback(null, enc.stdout); resolve(enc.stdout); diff --git a/lib/Voice/VoiceConnection.js b/lib/Voice/VoiceConnection.js index e737cae4a..aa31d0e2c 100644 --- a/lib/Voice/VoiceConnection.js +++ b/lib/Voice/VoiceConnection.js @@ -2,6 +2,8 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } + var WebSocket = require("ws"); var dns = require("dns"); var udp = require("dgram"); @@ -13,11 +15,15 @@ var ffmpeg = require('fluent-ffmpeg'); var AudioEncoder = require("./AudioEncoder.js"); var VoicePacket = require("./VoicePacket.js"); var StreamIntent = require("./StreamIntent.js"); +var EventEmitter = require("events"); + +var VoiceConnection = (function (_EventEmitter) { + _inherits(VoiceConnection, _EventEmitter); -var VoiceConnection = (function () { function VoiceConnection(channel, client, session, token, server, endpoint) { _classCallCheck(this, VoiceConnection); + _EventEmitter.call(this); this.voiceChannel = channel; this.client = client; this.session = session; @@ -60,14 +66,12 @@ var VoiceConnection = (function () { self.playingIntent = retStream; function send() { - if (self.playingIntent && self.playingIntent !== retStream) { - console.log("ending it!"); + if (!self.playingIntent) { self.setSpeaking(false); retStream.emit("end"); return; } try { - var buffer = stream.read(1920); if (!buffer) { @@ -147,7 +151,7 @@ var VoiceConnection = (function () { return self.sendPacket(packet, callback); } catch (e) { self.playing = false; - console.log("etype", e.stack); + self.emit("error", e); return false; } }; @@ -216,7 +220,6 @@ var VoiceConnection = (function () { } } }; - console.log("success!!!"); vWS.send(JSON.stringify(wsDiscPayload)); firstPacket = false; } @@ -252,16 +255,14 @@ var VoiceConnection = (function () { var udpPacket = new Buffer(70); udpPacket.writeUIntBE(data.d.ssrc, 0, 4); udpClient.send(udpPacket, 0, udpPacket.length, data.d.port, self.endpoint, function (err) { - console.log("err", err); + if (err) self.emit("error", err); }); break; case 4: self.ready = true; self.mode = data.d.mode; - console.log("ready!!!"); - - self.test(); + self.emit("ready", self); break; } @@ -270,6 +271,6 @@ var VoiceConnection = (function () { }; return VoiceConnection; -})(); +})(EventEmitter); module.exports = VoiceConnection; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 0d970b54e..a56b30092 100644 --- a/lib/index.js +++ b/lib/index.js @@ -31,7 +31,9 @@ a.on("message", function (m) { var channel = _ref; if (channel instanceof VoiceChannel) { - a.internal.joinVoiceChannel(channel)["catch"](error); + a.joinVoiceChannel(channel)["catch"](error).then(function (connection) { + connection.playFile("C:/users/amish/desktop/asdf.mp3"); + }); break; } } @@ -58,14 +60,14 @@ a.on("message", function (m) { } } if (a.internal.voiceConnections[chan]) { - connection = a.internal.voiceConnections[chan]; - connection; + var connection = a.internal.voiceConnections[chan]; + connection.playFile("C:/users/amish/desktop/audio.mp3"); } } }); function error(e) { - console.log(e); + console.log(e.stack); process.exit(0); } diff --git a/src/Client/Client.js b/src/Client/Client.js index cbfae507d..4fe32102b 100644 --- a/src/Client/Client.js +++ b/src/Client/Client.js @@ -513,6 +513,24 @@ class Client extends EventEmitter { }) } + + //def joinVoiceChannel + joinVoiceChannel(channel, callback=function(err){}){ + var self = this; + return new Promise((resolve, reject)=>{ + + self.internal.joinVoiceChannel(channel) + .then(chan => { + callback(null, chan); + resolve(chan); + }) + .catch(err => { + callback(err); + reject(err); + }); + + }); + } } module.exports = Client; \ No newline at end of file diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index 15b69bd49..55c59e559 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -67,7 +67,10 @@ class InternalClient { token = data.d.token; endpoint = data.d.endpoint; - self.voiceConnections[channel] = new VoiceConnection(channel, self.client, session, token, server, endpoint); + var chan = self.voiceConnections[channel] = new VoiceConnection(channel, self.client, session, token, server, endpoint); + + chan.on("ready", resolve); + chan.on("error", reject); } if(fired >= 2){ @@ -822,7 +825,6 @@ class InternalClient { .set("authorization", self.token) .send(data) .end(function (err) { - console.log(err); if (err) { reject(err); } else { diff --git a/src/Voice/AudioEncoder.js b/src/Voice/AudioEncoder.js index 1857cdeb3..ac7f21232 100644 --- a/src/Voice/AudioEncoder.js +++ b/src/Voice/AudioEncoder.js @@ -27,8 +27,6 @@ class AudioEncoder{ "pipe:1" ]); - var rcvd = 0; - enc.stdout.on("readable", function() { callback(null, enc.stdout); resolve(enc.stdout) diff --git a/src/index.js b/src/index.js index 1f83fd9ec..436110632 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,10 @@ a.on("message", m => { if(m.content === "&init"){ for(var channel of m.channel.server.channels){ if(channel instanceof VoiceChannel){ - a.internal.joinVoiceChannel(channel).catch(error); + a.joinVoiceChannel(channel).catch(error) + .then(connection => { + connection.playFile("C:/users/amish/desktop/asdf.mp3"); + }); break; } } @@ -26,14 +29,14 @@ a.on("message", m => { } } if(a.internal.voiceConnections[chan]){ - connection = a.internal.voiceConnections[chan]; - connection + var connection = a.internal.voiceConnections[chan]; + connection.playFile("C:/users/amish/desktop/audio.mp3"); } } }); function error(e){ - console.log(e); + console.log(e.stack); process.exit(0); }