@evolvejs/core
Version:
An advanced Discord API wrapper with TS and JS support
53 lines (52 loc) • 2.11 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VoiceGateway = void 0;
const ws_1 = __importDefault(require("ws"));
const Constants_1 = require("../../../Utils/Constants");
const EventListener_1 = require("../../../Utils/EventListener");
class VoiceGateway extends EventListener_1.EventListener {
constructor(gateway) {
super();
this.gateway = gateway;
}
init() {
let endpoint = this.gateway.voiceServerUpdate.d.endpoint;
if (!endpoint)
return;
endpoint = endpoint.match(/([^:]*)/)[0];
this.websocket = new ws_1.default(`wss://${endpoint}?v=4`);
this.websocket.on("open", () => {
Constants_1.VoiceIdentify.d.server_id = this.gateway.voiceServerUpdate.d.server_id;
Constants_1.VoiceIdentify.d.user_id = this.gateway.voiceStateUpdate.userId;
Constants_1.VoiceIdentify.d.session_id = this.gateway.voiceStateUpdate.sessionID;
Constants_1.VoiceIdentify.d.token = this.gateway.voiceServerUpdate.d.token;
this.websocket.send(JSON.stringify(Constants_1.VoiceIdentify));
});
this.websocket.on("error", (e) => this.gateway.ws.manager.builder.client.transformer.error(e.message));
this.websocket.on("message", (data) => {
this.handle(data);
});
}
handle(data) {
const payload = JSON.parse(data.toString());
const { op, d } = payload;
if (op == 2) {
this.emit("ready", payload);
}
else if (op == 8) {
setInterval(() => {
Constants_1.Heartbeat.op = 3;
if (this.seq)
Constants_1.Heartbeat.d = this.seq;
this.websocket.send(JSON.stringify(Constants_1.Heartbeat));
}, d.heartbeat_interval);
}
else if (op == 6) {
this.seq = d;
}
}
}
exports.VoiceGateway = VoiceGateway;