UNPKG

@evolvejs/core

Version:

An advanced Discord API wrapper with TS and JS support

156 lines (155 loc) 6.76 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Gateway = void 0; /* eslint-disable @typescript-eslint/ban-types */ /* eslint-disable no-mixed-spaces-and-tabs */ const Websocket_1 = require("./Websocket"); const __1 = require("../.."); const VoiceGateway_1 = require("./Voice/VoiceGateway"); const Constants_1 = require("../../Utils/Constants"); class Gateway { constructor() { this.launchedShards = new Set(); } init(data, ws) { return __awaiter(this, void 0, void 0, function* () { this.data = data; this.ws = ws; this.shard = this.ws.shard; try { let payload; if (this.ws.manager.builder.encoding == "json") { payload = JSON.parse(data.toString()); } else { try { payload = require("erlpack").unpack(Buffer.from(data.toString(), "binary")); } catch (e) { throw this.ws.manager.builder.client.transformer.error(e); } } const { op, t, d } = payload; if (!d) return; if (op === __1.OPCODE.Hello) { // Command: Heartbeat this._spawn(this.shard); setInterval(() => { this.lastPingTimeStamp = Date.now(); this.ws.send(__1.Heartbeat); }, d.heartbeat_interval); } else if (op === __1.OPCODE.Reconnect) { this.ws.manager.connections.set(this.shard, new Websocket_1.EvolveSocket(this.ws.manager, this.shard)); this.reconnect(); this.ws.close(); } else if (t) { this.ws.manager.builder.client.emit(Constants_1.EVENTS.RAW, { name: t, payload, shard: this.shard, }); try { const { default: handler } = yield Promise.resolve().then(() => __importStar(require(`./Handlers/${t}`))); new handler(this.ws.manager.builder.client, payload, this.shard); } catch (e) { this.ws.manager.builder.client.transformer.error(e); } } } catch (e) { this.ws.manager.builder.client.transformer.error(e); } }); } _spawn(shard) { __1.Identify.d.token = this.ws.manager.builder.client.token; __1.Identify.d.activity = this.ws.manager.builder.activity; __1.Identify.d.shard = [shard, this.ws.manager.builder.shards]; __1.Identify.d.intents = this.ws.manager.builder.intents; if (this._debug(shard)) { this.ws.send(__1.Identify); } } destroy() { this.ws.manager.connections.delete(this.shard); this.ws.manager.emit(Constants_1.EVENTS.SHARD_DESTROY, this.shard); this.ws.close(); } _debug(shard) { this.ws.manager.emit(Constants_1.EVENTS.SHARD_SPAWN, shard); return true; } reconnect() { const payload = { op: __1.OPCODE.Resume, d: { token: this.ws.manager.builder.client.token, session_id: this.ws.manager.builder.client.sessionID, seq: this.ws.seq, }, }; this.ws.send(payload); } sendVoiceStateUpdate(guildID, channelID, options, initialize = false) { __1.VoiceStateUpdate.d.guild_id = guildID; __1.VoiceStateUpdate.d.channel_id = channelID; if (options) { __1.VoiceStateUpdate.d.self_deaf = options.self_deaf; __1.VoiceStateUpdate.d.self_mute = options.self_mute; } this.ws.send(__1.VoiceStateUpdate); this.ws.manager.builder.client.on(Constants_1.EVENTS.VOICE_STATE_UPDATE, (pk) => { if (pk.member.user.id !== this.ws.manager.builder.client.user.id) return; this.voiceStateUpdate = pk; if (this.voiceStateUpdate && this.voiceServerUpdate && !this.voice) { this.voice = new VoiceGateway_1.VoiceGateway(this); this.voice.emit(Constants_1.EVENTS.PACKET_READY, (this.voiceStateUpdate, this.voiceServerUpdate)); if (initialize) this.voice.init(); } }); this.ws.manager.builder.client.on(Constants_1.EVENTS.VOICE_SERVER_UPDATE, (pk) => { this.voiceServerUpdate = pk; if (this.voiceStateUpdate && this.voiceServerUpdate && !this.voice) { this.voice = new VoiceGateway_1.VoiceGateway(this); this.voice.emit(Constants_1.EVENTS.PACKET_READY, (this.voiceStateUpdate, this.voiceServerUpdate, this.shard)); if (initialize) this.voice.init(); } }); } } exports.Gateway = Gateway;