@evolvejs/evolava
Version:
a lavalink client for evolvejs
138 lines • 5.15 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.EvoLavaNode = void 0;
const ws_1 = __importDefault(require("ws"));
class EvoLavaNode {
constructor(evolava, node) {
this.evolava = evolava;
this.node = node;
this.stats = {
players: 0,
memory: {
reserved: 0,
used: 0,
free: 0,
allocated: 0
},
activePlayers: 0,
cpu: {
cores: 0,
load: 0,
lavaload: 0
},
uptime: 0
};
this.connect();
}
connect() {
const headers = {
Authorization: this.node.password,
"Num-Shards": this.evolava.client.shardConnections.size,
"User-Id": this.evolava.client.user.id
};
this.ws = new ws_1.default(`ws://${this.node.host}:${this.node.port}`, { headers });
this.ws.on("open", (d) => this.evolava.emit("newNode", d));
this.ws.on("error", (e) => this.evolava.emit("nodeError", e));
this.ws.on("close", (r) => this.evolava.emit("nodeDestroy", r));
this.ws.on("message", (d) => this.handle(d));
}
handle(data) {
var _a;
const payload = JSON.parse(data.toString());
const { op, type, code, guildID, state } = payload;
if (!op)
return;
if (op == "event") {
if (op == "stats") {
this.stats = Object.assign({}, payload);
delete this.stats.op;
}
else if (op == "playerUpdate") {
const player = this.evolava.players.get(guildID);
if (player)
player.position = state.position || 0;
}
}
else if (op == "event") {
const player = this.evolava.players.get(guildID);
if (!player)
return;
player.state = false;
const track = player.queue.get(0);
if (type == "TrackStartEvent") {
player.state = true;
this.evolava.emit("trackStart", track, player);
}
else if (type == "TrackEndEvent") {
if (!track)
return;
if (track && player.queue.repeatQueue) {
const cache = player.queue;
player.queue.clear();
cache.forEach(o => {
if (cache)
player.queue.add(o);
});
}
else if (track && player.queue.repeatTrack) {
player.play();
}
else if (track && player.queue.repeatQueue) {
const toAdd = player.queue.remove();
if (toAdd)
player.queue.add(toAdd);
player.play();
}
else if (track && player.queue.size > 1) {
player.queue.remove();
player.play();
}
else if (track && player.queue.size === 1) {
player.queue.remove();
this.evolava.emit("queueOver", player);
}
}
else if (type === "TrackStuckEvent") {
if (!track)
return;
player.queue.remove();
if (player.queue.skipOnError)
player.play();
this.evolava.emit("trackStuck", track, player, payload);
}
else if (type == "TrackExceptionEvent") {
if (!track)
return;
player.queue.remove();
if (player.queue.skipOnError)
player.play();
this.evolava.emit("trackError", track, player, payload);
}
else if (type == "WebsocketClosedEvent") {
if ([4009, 4015].includes(code))
(_a = this.evolava.client.shardConnections.get(0)) === null || _a === void 0 ? void 0 : _a.gateway.sendVoiceStateUpdate(guildID, player.options.voiceChannel.id, {
self_deaf: player.options.self.deaf,
self_mute: player.options.self.mute
});
this.evolava.emit("socketClosed", this, payload);
}
}
}
wsSend(data) {
return new Promise((res, rej) => {
if (!this.ws)
res(false);
const formattedData = JSON.stringify(data);
if (!formattedData || !formattedData.startsWith("{"))
rej(`The data was not in the proper format.`);
this.ws.send(formattedData, (err) => {
err ? rej(err) : res(true);
});
});
}
}
exports.EvoLavaNode = EvoLavaNode;
//# sourceMappingURL=EvoLavaNode.js.map