mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 19:13:31 +01:00
Voice now properly joins
This commit is contained in:
@@ -3,18 +3,111 @@
|
|||||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||||
|
|
||||||
var WebSocket = require("ws");
|
var WebSocket = require("ws");
|
||||||
var dgram = require("dgram");
|
var dns = require("dns");
|
||||||
|
var udp = require("dgram");
|
||||||
|
|
||||||
var VoiceConnection = function VoiceConnection(channel, client, session, token, server, endpoint) {
|
var VoiceConnection = (function () {
|
||||||
_classCallCheck(this, VoiceConnection);
|
function VoiceConnection(channel, client, session, token, server, endpoint) {
|
||||||
|
_classCallCheck(this, VoiceConnection);
|
||||||
|
|
||||||
this.voiceChannel = channel;
|
this.voiceChannel = channel;
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.session = session;
|
this.session = session;
|
||||||
this.token = token;
|
this.token = token;
|
||||||
this.server = server;
|
this.server = server;
|
||||||
this.endpoint = endpoint;
|
this.endpoint = endpoint.replace(":80", "");
|
||||||
console.log("I was instantiated!");
|
this.vWS = null; // vWS means voice websocket
|
||||||
};
|
this.ready = false;
|
||||||
|
this.vWSData = {};
|
||||||
|
this.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
VoiceConnection.prototype.init = function init() {
|
||||||
|
var _this = this;
|
||||||
|
|
||||||
|
var self = this;
|
||||||
|
dns.lookup(this.endpoint, function (err, address, family) {
|
||||||
|
self.endpoint = address;
|
||||||
|
var vWS = self.vWS = new WebSocket("wss://" + _this.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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
console.log("success!!!");
|
||||||
|
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 () {
|
||||||
|
vWS.send(JSON.stringify({
|
||||||
|
op: 3,
|
||||||
|
d: null
|
||||||
|
}));
|
||||||
|
}, data.d.heartbeat_interval);
|
||||||
|
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
|
||||||
|
self.ready = true;
|
||||||
|
self.mode = data.d.mode;
|
||||||
|
console.log("ready!!!");
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return VoiceConnection;
|
||||||
|
})();
|
||||||
|
|
||||||
module.exports = VoiceConnection;
|
module.exports = VoiceConnection;
|
||||||
@@ -1,17 +1,104 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var WebSocket = require("ws");
|
var WebSocket = require("ws");
|
||||||
var dgram = require("dgram");
|
var dns = require("dns");
|
||||||
|
var udp = require("dgram");
|
||||||
|
|
||||||
class VoiceConnection{
|
class VoiceConnection {
|
||||||
constructor(channel, client, session, token, server, endpoint){
|
constructor(channel, client, session, token, server, endpoint) {
|
||||||
this.voiceChannel = channel;
|
this.voiceChannel = channel;
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.session = session;
|
this.session = session;
|
||||||
this.token = token;
|
this.token = token;
|
||||||
this.server = server;
|
this.server = server;
|
||||||
this.endpoint = endpoint;
|
this.endpoint = endpoint.replace(":80", "");
|
||||||
console.log("I was instantiated!");
|
this.vWS = null; // vWS means voice websocket
|
||||||
|
this.ready = false;
|
||||||
|
this.vWSData = {};
|
||||||
|
this.init();
|
||||||
|
}
|
||||||
|
|
||||||
|
init() {
|
||||||
|
var self = this;
|
||||||
|
dns.lookup(this.endpoint, (err, address, family) => {
|
||||||
|
self.endpoint = address;
|
||||||
|
var vWS = self.vWS = new WebSocket("wss://" + this.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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log("success!!!");
|
||||||
|
vWS.send(JSON.stringify(wsDiscPayload));
|
||||||
|
firstPacket = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
vWS.on("open", () => {
|
||||||
|
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", (msg) => {
|
||||||
|
var data = JSON.parse(msg);
|
||||||
|
switch (data.op) {
|
||||||
|
case 2:
|
||||||
|
self.vWSData = data.d;
|
||||||
|
|
||||||
|
KAI = setInterval(() => {
|
||||||
|
vWS.send(JSON.stringify({
|
||||||
|
op : 3,
|
||||||
|
d : null
|
||||||
|
}));
|
||||||
|
}, data.d.heartbeat_interval);
|
||||||
|
|
||||||
|
var udpPacket = new Buffer(70);
|
||||||
|
udpPacket.writeUIntBE(data.d.ssrc, 0, 4);
|
||||||
|
udpClient.send(udpPacket, 0, udpPacket.length, data.d.port, self.endpoint, err => {
|
||||||
|
console.log("err", err);
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
|
||||||
|
self.ready = true;
|
||||||
|
self.mode = data.d.mode;
|
||||||
|
console.log("ready!!!");
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user