const { EventEmitter } = require('events'); // eslint-disable-next-line no-unused-vars const { inspect } = require('util'); const ShardManager = require('./middleware/ShardManager.js'); const Logger = require('./middleware/logger/Logger.js'); class Manager extends EventEmitter { constructor(options) { super(); this.shardManager = new ShardManager('./structure/client/DiscordClient.js', options); this.logger = new Logger(this); this._built = false; this.readyAt = null; this.shardManager.on('message', this._handleMessage.bind(this)); // this.on('built', () => { // this.shardManager.broadcast({ _built: true }); // }); } _handleMessage(shard, message) { if (message._mEval) return this.eval(shard, message); if (message._logger) return this.logger._handleMessage(shard, message); if (message._webhook) return undefined; //todo } async build() { await this.shardManager.spawn(); this._built = true; this.emit('built'); this.readyAt = Date.now(); } /** * * * @param {*} shard The shard from which the eval came and to which it will be returned * @param {*} script The script to be executed * @memberof Manager * @private */ async eval(shard, { script }) { this.logger.write('info', `Incoming manager eval from shard ${shard.id}:\n${script}`); let result = null, error = null; try { // eslint-disable-next-line no-eval result = eval(script); if (result instanceof Promise) result = await result; //if(typeof result !== 'string') result = inspect(result); } catch (err) { error = err.stack || err; } return shard.send({ _evalResult: true, script, result, error }); } get uptime() { return this.readyAt !== null ? Date.now() - this.readyAt : 0; } } module.exports = Manager;