mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 10:33:30 +01:00
v5.0.0! Woo!
This commit is contained in:
@@ -1,129 +1,4 @@
|
||||
"use strict";
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var cpoc = require("child_process");
|
||||
|
||||
var opus;
|
||||
try {
|
||||
opus = require("node-opus");
|
||||
} catch (e) {
|
||||
// no opus!
|
||||
}
|
||||
var VoicePacket = require("./VoicePacket.js");
|
||||
|
||||
var AudioEncoder = (function () {
|
||||
function AudioEncoder() {
|
||||
_classCallCheck(this, AudioEncoder);
|
||||
|
||||
if (opus) {
|
||||
this.opus = new opus.OpusEncoder(48000, 1);
|
||||
}
|
||||
this.choice = false;
|
||||
}
|
||||
|
||||
AudioEncoder.prototype.opusBuffer = function opusBuffer(buffer) {
|
||||
|
||||
return this.opus.encode(buffer, 1920);
|
||||
};
|
||||
|
||||
AudioEncoder.prototype.getCommand = function getCommand(force) {
|
||||
|
||||
if (this.choice && force) return choice;
|
||||
|
||||
var choices = ["avconv", "ffmpeg"];
|
||||
|
||||
for (var _iterator = choices, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
||||
var _ref;
|
||||
|
||||
if (_isArray) {
|
||||
if (_i >= _iterator.length) break;
|
||||
_ref = _iterator[_i++];
|
||||
} else {
|
||||
_i = _iterator.next();
|
||||
if (_i.done) break;
|
||||
_ref = _i.value;
|
||||
}
|
||||
|
||||
var choice = _ref;
|
||||
|
||||
var p = cpoc.spawnSync(choice);
|
||||
if (!p.error) {
|
||||
this.choice = choice;
|
||||
return choice;
|
||||
}
|
||||
}
|
||||
|
||||
return "help";
|
||||
};
|
||||
|
||||
AudioEncoder.prototype.encodeStream = 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", "-"]);
|
||||
|
||||
stream.pipe(enc.stdin);
|
||||
|
||||
enc.stdout.once("readable", function () {
|
||||
callback(null, {
|
||||
proc: enc,
|
||||
stream: enc.stdout,
|
||||
instream: stream
|
||||
});
|
||||
resolve({
|
||||
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");
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
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
|
||||
});
|
||||
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;
|
||||
})();
|
||||
|
||||
module.exports = AudioEncoder;
|
||||
"use strict";function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var cpoc=require("child_process");var opus;try{opus = require("node-opus");}catch(e) { // no opus!
|
||||
}var VoicePacket=require("./VoicePacket.js");var AudioEncoder=(function(){function AudioEncoder(){_classCallCheck(this,AudioEncoder);if(opus){this.opus = new opus.OpusEncoder(48000,1);}this.choice = false;}AudioEncoder.prototype.opusBuffer = function opusBuffer(buffer){return this.opus.encode(buffer,1920);};AudioEncoder.prototype.getCommand = function getCommand(force){if(this.choice && force)return choice;var choices=["avconv","ffmpeg"];for(var _iterator=choices,_isArray=Array.isArray(_iterator),_i=0,_iterator=_isArray?_iterator:_iterator[Symbol.iterator]();;) {var _ref;if(_isArray){if(_i >= _iterator.length)break;_ref = _iterator[_i++];}else {_i = _iterator.next();if(_i.done)break;_ref = _i.value;}var choice=_ref;var p=cpoc.spawnSync(choice);if(!p.error){this.choice = choice;return choice;}}return "help";};AudioEncoder.prototype.encodeStream = 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","-"]);stream.pipe(enc.stdin);enc.stdout.once("readable",function(){callback(null,{proc:enc,stream:enc.stdout,instream:stream});resolve({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");});});};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});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;})();module.exports = AudioEncoder;
|
||||
|
||||
@@ -1,22 +1,2 @@
|
||||
"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 _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");
|
||||
|
||||
var StreamIntent = (function (_EventEmitter) {
|
||||
_inherits(StreamIntent, _EventEmitter);
|
||||
|
||||
function StreamIntent() {
|
||||
_classCallCheck(this, StreamIntent);
|
||||
|
||||
_EventEmitter.call(this);
|
||||
}
|
||||
|
||||
return StreamIntent;
|
||||
})(EventEmitter);
|
||||
|
||||
module.exports = StreamIntent;
|
||||
"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 _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");var StreamIntent=(function(_EventEmitter){_inherits(StreamIntent,_EventEmitter);function StreamIntent(){_classCallCheck(this,StreamIntent);_EventEmitter.call(this);}return StreamIntent;})(EventEmitter);module.exports = StreamIntent;
|
||||
|
||||
@@ -1,332 +1,11 @@
|
||||
"use strict";
|
||||
/*
|
||||
"use strict"; /*
|
||||
Major credit to izy521 who is the creator of
|
||||
https://github.com/izy521/discord.io,
|
||||
|
||||
without his help voice chat in discord.js would not have
|
||||
been possible!
|
||||
*/
|
||||
|
||||
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");
|
||||
var fs = require("fs");
|
||||
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);
|
||||
|
||||
function VoiceConnection(channel, client, session, token, server, endpoint) {
|
||||
_classCallCheck(this, VoiceConnection);
|
||||
|
||||
_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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.playStream = function playStream(stream) {
|
||||
|
||||
var self = this;
|
||||
|
||||
var startTime = Date.now();
|
||||
var sequence = 0;
|
||||
var time = 0;
|
||||
var count = 0;
|
||||
|
||||
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;
|
||||
}
|
||||
try {
|
||||
var buffer = stream.read(1920);
|
||||
if (!buffer) {
|
||||
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;
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
VoiceConnection.prototype.init = function init() {
|
||||
var _this3 = this;
|
||||
|
||||
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");
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
};
|
||||
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);
|
||||
|
||||
module.exports = VoiceConnection;
|
||||
*/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");var fs=require("fs");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);function VoiceConnection(channel,client,session,token,server,endpoint){_classCallCheck(this,VoiceConnection);_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();}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();}};VoiceConnection.prototype.playStream = function playStream(stream){var self=this;var startTime=Date.now();var sequence=0;var time=0;var count=0;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;}try{var buffer=stream.read(1920);if(!buffer){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;};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);}});};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);}});};VoiceConnection.prototype.init = function init(){var _this3=this;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");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
|
||||
}}};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);module.exports = VoiceConnection;
|
||||
|
||||
@@ -1,26 +1 @@
|
||||
"use strict";
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var VoicePacket = function VoicePacket(data, sequence, time, ssrc) {
|
||||
_classCallCheck(this, VoicePacket);
|
||||
|
||||
var audioBuffer = data,
|
||||
returnBuffer = new Buffer(audioBuffer.length + 12);
|
||||
|
||||
returnBuffer.fill(0);
|
||||
returnBuffer[0] = 0x80;
|
||||
returnBuffer[1] = 0x78;
|
||||
|
||||
returnBuffer.writeUIntBE(sequence, 2, 2);
|
||||
returnBuffer.writeUIntBE(time, 4, 4);
|
||||
returnBuffer.writeUIntBE(ssrc, 8, 4);
|
||||
|
||||
for (var i = 0; i < audioBuffer.length; i++) {
|
||||
returnBuffer[i + 12] = audioBuffer[i];
|
||||
}
|
||||
|
||||
return returnBuffer;
|
||||
};
|
||||
|
||||
module.exports = VoicePacket;
|
||||
"use strict";function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var VoicePacket=function VoicePacket(data,sequence,time,ssrc){_classCallCheck(this,VoicePacket);var audioBuffer=data,returnBuffer=new Buffer(audioBuffer.length + 12);returnBuffer.fill(0);returnBuffer[0] = 0x80;returnBuffer[1] = 0x78;returnBuffer.writeUIntBE(sequence,2,2);returnBuffer.writeUIntBE(time,4,4);returnBuffer.writeUIntBE(ssrc,8,4);for(var i=0;i < audioBuffer.length;i++) {returnBuffer[i + 12] = audioBuffer[i];}return returnBuffer;};module.exports = VoicePacket;
|
||||
|
||||
Reference in New Issue
Block a user