mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 17:43:30 +01:00
Fixed compiled version
Downgraded babel until a working solution found
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
"use strict";
|
||||
|
||||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var cpoc = require("child_process");
|
||||
@@ -24,120 +22,106 @@ var AudioEncoder = (function () {
|
||||
this.choice = false;
|
||||
}
|
||||
|
||||
_createClass(AudioEncoder, [{
|
||||
key: "opusBuffer",
|
||||
value: function opusBuffer(buffer) {
|
||||
AudioEncoder.prototype.opusBuffer = function opusBuffer(buffer) {
|
||||
|
||||
return this.opus.encode(buffer, 1920);
|
||||
}
|
||||
}, {
|
||||
key: "getCommand",
|
||||
value: function getCommand(force) {
|
||||
return this.opus.encode(buffer, 1920);
|
||||
};
|
||||
|
||||
if (this.choice && force) return choice;
|
||||
AudioEncoder.prototype.getCommand = function getCommand(force) {
|
||||
|
||||
var choices = ["avconv", "ffmpeg"];
|
||||
if (this.choice && force) return choice;
|
||||
|
||||
var _iteratorNormalCompletion = true;
|
||||
var _didIteratorError = false;
|
||||
var _iteratorError = undefined;
|
||||
var choices = ["avconv", "ffmpeg"];
|
||||
|
||||
try {
|
||||
for (var _iterator = choices[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
|
||||
var choice = _step.value;
|
||||
for (var _iterator = choices, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
||||
var _ref;
|
||||
|
||||
var p = cpoc.spawnSync(choice);
|
||||
if (!p.error) {
|
||||
this.choice = choice;
|
||||
return choice;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError = true;
|
||||
_iteratorError = err;
|
||||
} finally {
|
||||
try {
|
||||
if (!_iteratorNormalCompletion && _iterator.return) {
|
||||
_iterator.return();
|
||||
}
|
||||
} finally {
|
||||
if (_didIteratorError) {
|
||||
throw _iteratorError;
|
||||
}
|
||||
}
|
||||
if (_isArray) {
|
||||
if (_i >= _iterator.length) break;
|
||||
_ref = _iterator[_i++];
|
||||
} else {
|
||||
_i = _iterator.next();
|
||||
if (_i.done) break;
|
||||
_ref = _i.value;
|
||||
}
|
||||
|
||||
return "help";
|
||||
var choice = _ref;
|
||||
|
||||
var p = cpoc.spawnSync(choice);
|
||||
if (!p.error) {
|
||||
this.choice = choice;
|
||||
return choice;
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: "encodeStream",
|
||||
value: function encodeStream(stream) {
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, buffer) {} : arguments[1];
|
||||
|
||||
var self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
var enc = cpoc.spawn(self.getCommand(), ["-f", "s16le", "-ar", "48000", "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth.
|
||||
"-af", "volume=1", "pipe:1", "-i", "-"]);
|
||||
return "help";
|
||||
};
|
||||
|
||||
stream.pipe(enc.stdin);
|
||||
AudioEncoder.prototype.encodeStream = function encodeStream(stream) {
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, buffer) {} : arguments[1];
|
||||
|
||||
enc.stdout.once("readable", function () {
|
||||
callback(null, {
|
||||
proc: enc,
|
||||
stream: enc.stdout,
|
||||
instream: stream
|
||||
});
|
||||
resolve({
|
||||
proc: enc,
|
||||
stream: enc.stdout,
|
||||
instream: stream
|
||||
});
|
||||
var self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
var enc = cpoc.spawn(self.getCommand(), ["-f", "s16le", "-ar", "48000", "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth.
|
||||
"-af", "volume=1", "pipe:1", "-i", "-"]);
|
||||
|
||||
stream.pipe(enc.stdin);
|
||||
|
||||
enc.stdout.once("readable", function () {
|
||||
callback(null, {
|
||||
proc: enc,
|
||||
stream: enc.stdout,
|
||||
instream: stream
|
||||
});
|
||||
|
||||
enc.stdout.on("end", function () {
|
||||
callback("end");
|
||||
reject("end");
|
||||
});
|
||||
|
||||
enc.stdout.on("close", function () {
|
||||
callback("close");
|
||||
reject("close");
|
||||
resolve({
|
||||
proc: enc,
|
||||
stream: enc.stdout,
|
||||
instream: stream
|
||||
});
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: "encodeFile",
|
||||
value: function encodeFile(file) {
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, buffer) {} : arguments[1];
|
||||
|
||||
var self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
var enc = cpoc.spawn(self.getCommand(), ["-f", "s16le", "-ar", "48000", "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth.
|
||||
"-af", "volume=1", "pipe:1", "-i", file]);
|
||||
enc.stdout.on("end", function () {
|
||||
callback("end");
|
||||
reject("end");
|
||||
});
|
||||
|
||||
enc.stdout.once("readable", function () {
|
||||
callback(null, {
|
||||
proc: enc,
|
||||
stream: enc.stdout
|
||||
});
|
||||
resolve({
|
||||
proc: enc,
|
||||
stream: enc.stdout
|
||||
});
|
||||
enc.stdout.on("close", function () {
|
||||
callback("close");
|
||||
reject("close");
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
AudioEncoder.prototype.encodeFile = function encodeFile(file) {
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, buffer) {} : arguments[1];
|
||||
|
||||
var self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
var enc = cpoc.spawn(self.getCommand(), ["-f", "s16le", "-ar", "48000", "-ac", "1", // this can be 2 but there's no point, discord makes it mono on playback, wasted bandwidth.
|
||||
"-af", "volume=1", "pipe:1", "-i", file]);
|
||||
|
||||
enc.stdout.once("readable", function () {
|
||||
callback(null, {
|
||||
proc: enc,
|
||||
stream: enc.stdout
|
||||
});
|
||||
|
||||
enc.stdout.on("end", function () {
|
||||
callback("end");
|
||||
reject("end");
|
||||
});
|
||||
|
||||
enc.stdout.on("close", function () {
|
||||
callback("close");
|
||||
reject("close");
|
||||
resolve({
|
||||
proc: enc,
|
||||
stream: enc.stdout
|
||||
});
|
||||
});
|
||||
}
|
||||
}]);
|
||||
|
||||
enc.stdout.on("end", function () {
|
||||
callback("end");
|
||||
reject("end");
|
||||
});
|
||||
|
||||
enc.stdout.on("close", function () {
|
||||
callback("close");
|
||||
reject("close");
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return AudioEncoder;
|
||||
})();
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
"use strict"
|
||||
"use strict";
|
||||
// represents an intent of streaming music
|
||||
;
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
||||
|
||||
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 EventEmitter = require("events");
|
||||
@@ -16,7 +13,7 @@ var StreamIntent = (function (_EventEmitter) {
|
||||
function StreamIntent() {
|
||||
_classCallCheck(this, StreamIntent);
|
||||
|
||||
return _possibleConstructorReturn(this, Object.getPrototypeOf(StreamIntent).call(this));
|
||||
_EventEmitter.call(this);
|
||||
}
|
||||
|
||||
return StreamIntent;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"use strict"
|
||||
"use strict";
|
||||
/*
|
||||
Major credit to izy521 who is the creator of
|
||||
https://github.com/izy521/discord.io,
|
||||
@@ -7,14 +7,8 @@
|
||||
been possible!
|
||||
*/
|
||||
|
||||
;
|
||||
|
||||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
||||
|
||||
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");
|
||||
@@ -32,320 +26,306 @@ var VoiceConnection = (function (_EventEmitter) {
|
||||
function VoiceConnection(channel, client, session, token, server, endpoint) {
|
||||
_classCallCheck(this, VoiceConnection);
|
||||
|
||||
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(VoiceConnection).call(this));
|
||||
|
||||
_this.id = channel.id;
|
||||
_this.voiceChannel = channel;
|
||||
_this.client = client;
|
||||
_this.session = session;
|
||||
_this.token = token;
|
||||
_this.server = server;
|
||||
_this.endpoint = endpoint.replace(":80", "");
|
||||
_this.vWS = null; // vWS means voice websocket
|
||||
_this.ready = false;
|
||||
_this.vWSData = {};
|
||||
_this.encoder = new AudioEncoder();
|
||||
_this.udp = null;
|
||||
_this.playingIntent = null;
|
||||
_this.playing = false;
|
||||
_this.streamTime = 0;
|
||||
_this.streamProc = null;
|
||||
_this.KAI = null;
|
||||
_this.init();
|
||||
return _this;
|
||||
_EventEmitter.call(this);
|
||||
this.id = channel.id;
|
||||
this.voiceChannel = channel;
|
||||
this.client = client;
|
||||
this.session = session;
|
||||
this.token = token;
|
||||
this.server = server;
|
||||
this.endpoint = endpoint.replace(":80", "");
|
||||
this.vWS = null; // vWS means voice websocket
|
||||
this.ready = false;
|
||||
this.vWSData = {};
|
||||
this.encoder = new AudioEncoder();
|
||||
this.udp = null;
|
||||
this.playingIntent = null;
|
||||
this.playing = false;
|
||||
this.streamTime = 0;
|
||||
this.streamProc = null;
|
||||
this.KAI = null;
|
||||
this.init();
|
||||
}
|
||||
|
||||
_createClass(VoiceConnection, [{
|
||||
key: "destroy",
|
||||
value: function destroy() {
|
||||
this.stopPlaying();
|
||||
if (this.KAI) clearInterval(this.KAI);
|
||||
this.vWS.close();
|
||||
this.udp.close();
|
||||
this.client.internal.sendWS({
|
||||
op: 4,
|
||||
d: {
|
||||
guild_id: null,
|
||||
channel_id: null,
|
||||
self_mute: true,
|
||||
self_deaf: false
|
||||
}
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: "stopPlaying",
|
||||
value: function stopPlaying() {
|
||||
this.playing = false;
|
||||
this.playingIntent = null;
|
||||
if (this.instream) {
|
||||
this.instream.end();
|
||||
this.instream.destroy();
|
||||
VoiceConnection.prototype.destroy = function destroy() {
|
||||
this.stopPlaying();
|
||||
if (this.KAI) clearInterval(this.KAI);
|
||||
this.vWS.close();
|
||||
this.udp.close();
|
||||
this.client.internal.sendWS({
|
||||
op: 4,
|
||||
d: {
|
||||
guild_id: null,
|
||||
channel_id: null,
|
||||
self_mute: true,
|
||||
self_deaf: false
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.stopPlaying = function stopPlaying() {
|
||||
this.playing = false;
|
||||
this.playingIntent = null;
|
||||
if (this.instream) {
|
||||
this.instream.end();
|
||||
this.instream.destroy();
|
||||
}
|
||||
}, {
|
||||
key: "playStream",
|
||||
value: function playStream(stream) {
|
||||
};
|
||||
|
||||
var self = this;
|
||||
VoiceConnection.prototype.playStream = function playStream(stream) {
|
||||
|
||||
var startTime = Date.now();
|
||||
var sequence = 0;
|
||||
var time = 0;
|
||||
var count = 0;
|
||||
var self = this;
|
||||
|
||||
var length = 20;
|
||||
var startTime = Date.now();
|
||||
var sequence = 0;
|
||||
var time = 0;
|
||||
var count = 0;
|
||||
|
||||
if (self.playingIntent) {
|
||||
self.stopPlaying();
|
||||
var length = 20;
|
||||
|
||||
if (self.playingIntent) {
|
||||
self.stopPlaying();
|
||||
}
|
||||
self.playing = true;
|
||||
var retStream = new StreamIntent();
|
||||
var onWarning = false;
|
||||
self.playingIntent = retStream;
|
||||
|
||||
function send() {
|
||||
|
||||
if (!self.playingIntent || !self.playing) {
|
||||
self.setSpeaking(false);
|
||||
retStream.emit("end");
|
||||
self;
|
||||
return;
|
||||
}
|
||||
self.playing = true;
|
||||
var retStream = new StreamIntent();
|
||||
var onWarning = false;
|
||||
self.playingIntent = retStream;
|
||||
|
||||
function send() {
|
||||
|
||||
if (!self.playingIntent || !self.playing) {
|
||||
self.setSpeaking(false);
|
||||
retStream.emit("end");
|
||||
self;
|
||||
try {
|
||||
var buffer = stream.read(1920);
|
||||
if (!buffer) {
|
||||
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
|
||||
return;
|
||||
}
|
||||
try {
|
||||
var buffer = stream.read(1920);
|
||||
if (!buffer) {
|
||||
|
||||
if (buffer.length !== 1920) {
|
||||
if (onWarning) {
|
||||
retStream.emit("end");
|
||||
stream.destroy();
|
||||
self.setSpeaking(false);
|
||||
return;
|
||||
} else {
|
||||
onWarning = true;
|
||||
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
|
||||
return;
|
||||
}
|
||||
|
||||
if (buffer.length !== 1920) {
|
||||
if (onWarning) {
|
||||
retStream.emit("end");
|
||||
stream.destroy();
|
||||
self.setSpeaking(false);
|
||||
return;
|
||||
} else {
|
||||
onWarning = true;
|
||||
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
count++;
|
||||
sequence + 10 < 65535 ? sequence += 1 : sequence = 0;
|
||||
time + 9600 < 4294967295 ? time += 960 : time = 0;
|
||||
|
||||
self.sendBuffer(buffer, sequence, time, function (e) {});
|
||||
|
||||
var nextTime = startTime + count * length;
|
||||
|
||||
self.streamTime = count * length;
|
||||
|
||||
setTimeout(send, length + (nextTime - Date.now()));
|
||||
|
||||
if (!self.playing) self.setSpeaking(true);
|
||||
|
||||
retStream.emit("time", self.streamTime);
|
||||
} catch (e) {
|
||||
retStream.emit("error", e);
|
||||
}
|
||||
}
|
||||
self.setSpeaking(true);
|
||||
send();
|
||||
|
||||
return retStream;
|
||||
}
|
||||
}, {
|
||||
key: "setSpeaking",
|
||||
value: function setSpeaking(value) {
|
||||
this.playing = value;
|
||||
if (this.vWS.readyState === WebSocket.OPEN) this.vWS.send(JSON.stringify({
|
||||
op: 5,
|
||||
d: {
|
||||
speaking: value,
|
||||
delay: 0
|
||||
}
|
||||
}));
|
||||
}
|
||||
}, {
|
||||
key: "sendPacket",
|
||||
value: function sendPacket(packet) {
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err) {} : arguments[1];
|
||||
count++;
|
||||
sequence + 10 < 65535 ? sequence += 1 : sequence = 0;
|
||||
time + 9600 < 4294967295 ? time += 960 : time = 0;
|
||||
|
||||
var self = this;
|
||||
self.playing = true;
|
||||
try {
|
||||
if (self.vWS.readyState === WebSocket.OPEN) self.udp.send(packet, 0, packet.length, self.vWSData.port, self.endpoint, callback);
|
||||
self.sendBuffer(buffer, sequence, time, function (e) {});
|
||||
|
||||
var nextTime = startTime + count * length;
|
||||
|
||||
self.streamTime = count * length;
|
||||
|
||||
setTimeout(send, length + (nextTime - Date.now()));
|
||||
|
||||
if (!self.playing) self.setSpeaking(true);
|
||||
|
||||
retStream.emit("time", self.streamTime);
|
||||
} catch (e) {
|
||||
retStream.emit("error", e);
|
||||
}
|
||||
}
|
||||
self.setSpeaking(true);
|
||||
send();
|
||||
|
||||
return retStream;
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.setSpeaking = function setSpeaking(value) {
|
||||
this.playing = value;
|
||||
if (this.vWS.readyState === WebSocket.OPEN) this.vWS.send(JSON.stringify({
|
||||
op: 5,
|
||||
d: {
|
||||
speaking: value,
|
||||
delay: 0
|
||||
}
|
||||
}));
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.sendPacket = function sendPacket(packet) {
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err) {} : arguments[1];
|
||||
|
||||
var self = this;
|
||||
self.playing = true;
|
||||
try {
|
||||
if (self.vWS.readyState === WebSocket.OPEN) self.udp.send(packet, 0, packet.length, self.vWSData.port, self.endpoint, callback);
|
||||
} catch (e) {
|
||||
self.playing = false;
|
||||
callback(e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.sendBuffer = function sendBuffer(rawbuffer, sequence, timestamp, callback) {
|
||||
var self = this;
|
||||
self.playing = true;
|
||||
try {
|
||||
if (!self.encoder.opus) {
|
||||
self.playing = false;
|
||||
self.emit("error", "No Opus!");
|
||||
self.client.emit("debug", "Tried to use node-opus, but opus not available - install it!");
|
||||
return;
|
||||
}
|
||||
var buffer = self.encoder.opusBuffer(rawbuffer);
|
||||
var packet = new VoicePacket(buffer, sequence, timestamp, self.vWSData.ssrc);
|
||||
return self.sendPacket(packet, callback);
|
||||
} catch (e) {
|
||||
self.playing = false;
|
||||
self.emit("error", e);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.test = function test() {
|
||||
this.playFile("C:/users/amish/desktop/audio.mp3").then(function (stream) {
|
||||
stream.on("time", function (time) {
|
||||
console.log("Time", time);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.playFile = function playFile(stream) {
|
||||
var _this = this;
|
||||
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, str) {} : arguments[1];
|
||||
|
||||
var self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
_this.encoder.encodeFile(stream)["catch"](error).then(function (data) {
|
||||
self.streamProc = data.proc;
|
||||
var intent = self.playStream(data.stream);
|
||||
resolve(intent);
|
||||
callback(null, intent);
|
||||
});
|
||||
function error() {
|
||||
var e = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
|
||||
|
||||
reject(e);
|
||||
callback(e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: "sendBuffer",
|
||||
value: function sendBuffer(rawbuffer, sequence, timestamp, callback) {
|
||||
var self = this;
|
||||
self.playing = true;
|
||||
try {
|
||||
if (!self.encoder.opus) {
|
||||
self.playing = false;
|
||||
self.emit("error", "No Opus!");
|
||||
self.client.emit("debug", "Tried to use node-opus, but opus not available - install it!");
|
||||
return;
|
||||
}
|
||||
var buffer = self.encoder.opusBuffer(rawbuffer);
|
||||
var packet = new VoicePacket(buffer, sequence, timestamp, self.vWSData.ssrc);
|
||||
return self.sendPacket(packet, callback);
|
||||
} catch (e) {
|
||||
self.playing = false;
|
||||
self.emit("error", e);
|
||||
return false;
|
||||
});
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.playRawStream = function playRawStream(stream) {
|
||||
var _this2 = this;
|
||||
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, str) {} : arguments[1];
|
||||
|
||||
var self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
_this2.encoder.encodeStream(stream)["catch"](error).then(function (data) {
|
||||
self.streamProc = data.proc;
|
||||
self.instream = data.instream;
|
||||
var intent = self.playStream(data.stream);
|
||||
resolve(intent);
|
||||
callback(null, intent);
|
||||
});
|
||||
function error() {
|
||||
var e = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
|
||||
|
||||
reject(e);
|
||||
callback(e);
|
||||
}
|
||||
}
|
||||
}, {
|
||||
key: "test",
|
||||
value: function test() {
|
||||
this.playFile("C:/users/amish/desktop/audio.mp3").then(function (stream) {
|
||||
stream.on("time", function (time) {
|
||||
console.log("Time", time);
|
||||
});
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: "playFile",
|
||||
value: function playFile(stream) {
|
||||
var _this2 = this;
|
||||
});
|
||||
};
|
||||
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, str) {} : arguments[1];
|
||||
VoiceConnection.prototype.init = function init() {
|
||||
var _this3 = this;
|
||||
|
||||
var self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
_this2.encoder.encodeFile(stream).catch(error).then(function (data) {
|
||||
self.streamProc = data.proc;
|
||||
var intent = self.playStream(data.stream);
|
||||
resolve(intent);
|
||||
callback(null, intent);
|
||||
});
|
||||
function error() {
|
||||
var e = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
|
||||
var self = this;
|
||||
dns.lookup(this.endpoint, function (err, address, family) {
|
||||
self.endpoint = address;
|
||||
var vWS = self.vWS = new WebSocket("wss://" + _this3.endpoint, null, { rejectUnauthorized: false });
|
||||
var udpClient = self.udp = udp.createSocket("udp4");
|
||||
|
||||
reject(e);
|
||||
callback(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: "playRawStream",
|
||||
value: function playRawStream(stream) {
|
||||
var _this3 = this;
|
||||
var firstPacket = true;
|
||||
|
||||
var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err, str) {} : arguments[1];
|
||||
var discordIP = "",
|
||||
discordPort = "";
|
||||
|
||||
var self = this;
|
||||
return new Promise(function (resolve, reject) {
|
||||
_this3.encoder.encodeStream(stream).catch(error).then(function (data) {
|
||||
self.streamProc = data.proc;
|
||||
self.instream = data.instream;
|
||||
var intent = self.playStream(data.stream);
|
||||
resolve(intent);
|
||||
callback(null, intent);
|
||||
});
|
||||
function error() {
|
||||
var e = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];
|
||||
udpClient.bind({ exclusive: true });
|
||||
udpClient.on('message', function (msg, rinfo) {
|
||||
var buffArr = JSON.parse(JSON.stringify(msg)).data;
|
||||
if (firstPacket === true) {
|
||||
for (var i = 4; i < buffArr.indexOf(0, i); i++) {
|
||||
discordIP += String.fromCharCode(buffArr[i]);
|
||||
}
|
||||
discordPort = msg.readUIntLE(msg.length - 2, 2).toString(10);
|
||||
|
||||
reject(e);
|
||||
callback(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, {
|
||||
key: "init",
|
||||
value: function init() {
|
||||
var _this4 = this;
|
||||
|
||||
var self = this;
|
||||
dns.lookup(this.endpoint, function (err, address, family) {
|
||||
self.endpoint = address;
|
||||
var vWS = self.vWS = new WebSocket("wss://" + _this4.endpoint, null, { rejectUnauthorized: false });
|
||||
var udpClient = self.udp = udp.createSocket("udp4");
|
||||
|
||||
var firstPacket = true;
|
||||
|
||||
var discordIP = "",
|
||||
discordPort = "";
|
||||
|
||||
udpClient.bind({ exclusive: true });
|
||||
udpClient.on('message', function (msg, rinfo) {
|
||||
var buffArr = JSON.parse(JSON.stringify(msg)).data;
|
||||
if (firstPacket === true) {
|
||||
for (var i = 4; i < buffArr.indexOf(0, i); i++) {
|
||||
discordIP += String.fromCharCode(buffArr[i]);
|
||||
}
|
||||
discordPort = msg.readUIntLE(msg.length - 2, 2).toString(10);
|
||||
|
||||
var wsDiscPayload = {
|
||||
"op": 1,
|
||||
"d": {
|
||||
"protocol": "udp",
|
||||
"data": {
|
||||
"address": discordIP,
|
||||
"port": Number(discordPort),
|
||||
"mode": self.vWSData.modes[0] //Plain
|
||||
}
|
||||
var wsDiscPayload = {
|
||||
"op": 1,
|
||||
"d": {
|
||||
"protocol": "udp",
|
||||
"data": {
|
||||
"address": discordIP,
|
||||
"port": Number(discordPort),
|
||||
"mode": self.vWSData.modes[0] //Plain
|
||||
}
|
||||
};
|
||||
vWS.send(JSON.stringify(wsDiscPayload));
|
||||
firstPacket = false;
|
||||
}
|
||||
});
|
||||
|
||||
vWS.on("open", function () {
|
||||
vWS.send(JSON.stringify({
|
||||
op: 0,
|
||||
d: {
|
||||
server_id: self.server.id,
|
||||
user_id: self.client.internal.user.id,
|
||||
session_id: self.session,
|
||||
token: self.token
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
var KAI;
|
||||
|
||||
vWS.on("message", function (msg) {
|
||||
var data = JSON.parse(msg);
|
||||
switch (data.op) {
|
||||
case 2:
|
||||
self.vWSData = data.d;
|
||||
|
||||
KAI = setInterval(function () {
|
||||
if (vWS && vWS.readyState === WebSocket.OPEN) vWS.send(JSON.stringify({
|
||||
op: 3,
|
||||
d: null
|
||||
}));
|
||||
}, data.d.heartbeat_interval);
|
||||
self.KAI = KAI;
|
||||
|
||||
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) {
|
||||
if (err) self.emit("error", err);
|
||||
});
|
||||
break;
|
||||
case 4:
|
||||
|
||||
self.ready = true;
|
||||
self.mode = data.d.mode;
|
||||
self.emit("ready", self);
|
||||
|
||||
break;
|
||||
}
|
||||
});
|
||||
};
|
||||
vWS.send(JSON.stringify(wsDiscPayload));
|
||||
firstPacket = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}]);
|
||||
|
||||
vWS.on("open", function () {
|
||||
vWS.send(JSON.stringify({
|
||||
op: 0,
|
||||
d: {
|
||||
server_id: self.server.id,
|
||||
user_id: self.client.internal.user.id,
|
||||
session_id: self.session,
|
||||
token: self.token
|
||||
}
|
||||
}));
|
||||
});
|
||||
|
||||
var KAI;
|
||||
|
||||
vWS.on("message", function (msg) {
|
||||
var data = JSON.parse(msg);
|
||||
switch (data.op) {
|
||||
case 2:
|
||||
self.vWSData = data.d;
|
||||
|
||||
KAI = setInterval(function () {
|
||||
if (vWS && vWS.readyState === WebSocket.OPEN) vWS.send(JSON.stringify({
|
||||
op: 3,
|
||||
d: null
|
||||
}));
|
||||
}, data.d.heartbeat_interval);
|
||||
self.KAI = KAI;
|
||||
|
||||
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) {
|
||||
if (err) self.emit("error", err);
|
||||
});
|
||||
break;
|
||||
case 4:
|
||||
|
||||
self.ready = true;
|
||||
self.mode = data.d.mode;
|
||||
self.emit("ready", self);
|
||||
|
||||
break;
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return VoiceConnection;
|
||||
})(EventEmitter);
|
||||
|
||||
Reference in New Issue
Block a user