"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 Logger_1 = require("./Logger");
const WebSocketClient_1 = require("./WebSocketClient");
/**
* Main Shard/Client class
* @see https://abal.moe/Eris/docs/Client
*
* @param {string} token Token to use
* @param {IHibikiOptions} hibikiOptions Options
* @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
*
* @memberof Shard
* @event blocked
*/
this.blocked = blocked((ms) => this.emit("blocked", ms));
/**
* Logger that logs things
*
* @type {Logger}
*/
this.logger = new Logger_1.Logger("shard", this.hibikiOptions.hibiki.debug);
}
/**
* Initializes the framework
*
* @param {number} [timeout] Timeout in ms
* @returns {Promise<void>}
*/
init(timeout) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
const connTimeout = setTimeout(() => {
return Promise.reject(new Error("Connect timed out"));
}, timeout || 10000);
yield this.lm.init();
yield this.pg.connect();
yield this.checkGuilds();
// await this.ws.connect();
yield this.ext.init();
yield this.ch.init();
this.on("guildCreate", (guild) => this.pg.addGuild(guild));
clearTimeout(connTimeout);
return Promise.resolve();
});
}
/**
* Disconnects the shard
*
* @returns {Promise<void>}
*/
disconnectShard() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
this.disconnect({ reconnect: false });
// await this.ext.break();
// await this.ws.disconnect();
yield this.pg.disconnect();
return Promise.resolve();
});
}
/**
* Checks if all guilds are in the database
*
* @returns {Promise<void>}
*/
checkGuilds() {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
for (const guild of this.guilds.map((g) => g)) {
yield this.pg.addGuild(guild);
}
return Promise.resolve();
});
}
}
exports.Shard = Shard;