UNPKG

rx-discord-client

Version:

Reactive client for receiving discord server messages

120 lines (119 loc) 5.02 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.DiscordClient = void 0; var ws_1 = require("ws"); var rxjs_1 = require("rxjs"); var DiscordClient = /** @class */ (function (_super) { __extends(DiscordClient, _super); function DiscordClient(token, reinitializeOnClose) { if (reinitializeOnClose === void 0) { reinitializeOnClose = true; } var _this = _super.call(this, function (observer) { _this.init(observer); var pingInterval = _this.pingInterval; var ws = _this.ws; return { unsubscribe: function () { ws === null || ws === void 0 ? void 0 : ws.close(); if (pingInterval) { clearInterval(pingInterval); } } }; }) || this; _this.token = token; _this.reinitializeOnClose = reinitializeOnClose; _this.sequence = null; return _this; } DiscordClient.create = function (token) { return new this(token); }; DiscordClient.prototype.parseMessage = function (message) { var parsed = JSON.parse(message.toString()); var type = parsed.t; var operation = parsed.op; var data = parsed.d; var sequence = parsed.s; return { type: type, operation: operation, data: data, sequence: sequence }; }; DiscordClient.prototype.handleWebsocketMessage = function (observer, msg) { var _this = this; var _a; var _b = this.parseMessage(msg), type = _b.type, operation = _b.operation, data = _b.data, sequence = _b.sequence; this.sequence = sequence; switch (operation) { case 10: // Initial message received this.pingInterval = setInterval(function () { var _a; (_a = _this.ws) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({ op: 1, d: _this.sequence })); }, data.heartbeat_interval); break; case 1: // Ping requested (_a = this.ws) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({ op: 1, d: this.sequence })); break; case 0: observer.next({ type: type, data: data }); break; } }; DiscordClient.prototype.initializeConnection = function () { var _a; (_a = this.ws) === null || _a === void 0 ? void 0 : _a.send(JSON.stringify({ op: 2, d: { token: this.token, properties: { $os: 'linux', $browser: 'chrome', $device: 'chrome', } } })); }; DiscordClient.prototype.handleWebsocketClose = function (observer, e) { console.log("[".concat(new Date().toISOString(), "] "), "Connection closed, recreating connection [".concat(e.toString(), "]")); if (this.reinitializeOnClose) { this.init(observer); } else { observer.complete(); } }; DiscordClient.prototype.handleWebsocketError = function (e) { var _a; console.log("[".concat(new Date().toISOString(), "] "), "Connection error, closing connection [".concat(e.toString(), "]")); (_a = this.ws) === null || _a === void 0 ? void 0 : _a.close(); }; DiscordClient.prototype.init = function (observer) { var _this = this; if (!this.token) { var error = 'Token is not set'; observer.error(error); return; } this.ws = new ws_1.WebSocket('wss://gateway.discord.gg/?v=6&encoding=json'); this.ws.on('open', function () { return _this.initializeConnection(); }); this.ws.on('message', function (m) { return _this.handleWebsocketMessage(observer, m); }); this.ws.on('error', function (e) { return _this.handleWebsocketError(e); }); this.ws.on('close', function (e) { return _this.handleWebsocketClose(observer, e); }); }; return DiscordClient; }(rxjs_1.Observable)); exports.DiscordClient = DiscordClient;