client/Shard.js

"use strict";
// Shard.ts - Shard class (noud02)
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const blocked = require("blocked");
const Eris = require("eris");
const PostgreSQL_1 = require("../db/PostgreSQL");
const CommandHandler_1 = require("../ext/CommandHandler");
const ExtensionManager_1 = require("../ext/ExtensionManager");
const LocaleManager_1 = require("../locale/LocaleManager");
const WebSocketClient_1 = require("./WebSocketClient");
/**
 * Main Shard/Client class
 * @see https://abal.moe/Eris/docs/Client
 *
 * @export
 * @class Shard
 * @extends {Eris.Client}
 */
class Shard extends Eris.Client {
    constructor(token, hibikiOptions) {
        super(token, hibikiOptions.eris);
        this.hibikiOptions = hibikiOptions;
        /**
         * PostgreSQL client
         *
         * @type {PostgreSQL}
         */
        this.pg = new PostgreSQL_1.PostgreSQL(this.hibikiOptions.postgres);
        /**
         * WebSocket client
         *
         * @type {WebSocketClient}
         */
        this.ws = new WebSocketClient_1.WebSocketClient(`${this.hibikiOptions.ws.ssl && "wss" || "ws"}://${this.hibikiOptions.ws.host}:${this.hibikiOptions.ws.port || 8080}`, {
            headers: {
                token: this.token,
            },
        });
        /**
         * Extension manager
         *
         * @type {ExtensionManager}
         */
        this.ext = new ExtensionManager_1.ExtensionManager(this.hibikiOptions.ext);
        /**
         * Command handler
         *
         * @type {CommandHandler}
         */
        this.ch = new CommandHandler_1.CommandHandler(this);
        /**
         * Locale manager
         *
         * @type {LocaleManager}
         */
        this.lm = new LocaleManager_1.LocaleManager();
        /**
         * Event loop block detector
         *
         * @type {NodeJS.Timer}
         */
        /**
         * Emitted when the event loop is blocked
         *
         * @event blocked
         */
        this.blocked = blocked((ms) => this.emit("blocked", ms));
    }
    /**
     * Connects the shard.
     *
     * @param {number} [timeout] Timeout in ms
     * @returns {Promise<void>}
     */
    connectShard(timeout) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const connTimeout = setTimeout(() => {
                return Promise.reject(new Error("Connect timed out"));
            }, timeout || 10000);
            try {
                yield this.lm.init();
                yield this.connect();
                yield this.pg.connect();
                // await this.ws.connect();
                yield this.ext.init();
                yield this.ch.init();
                clearTimeout(connTimeout);
                return Promise.resolve();
            }
            catch (e) {
                return Promise.reject(e);
            }
        });
    }
    /**
     * Disconnects the shard
     *
     * @returns {Promise<void>}
     */
    disconnectShard() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            try {
                this.disconnect({ reconnect: false });
                // await this.ext.break();
                // await this.ws.disconnect();
                yield this.pg.disconnect();
                return Promise.resolve();
            }
            catch (e) {
                return Promise.reject(e);
            }
        });
    }
}
exports.Shard = Shard;