103 lines
2.8 KiB
JavaScript
103 lines
2.8 KiB
JavaScript
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() {
|
|
|
|
this.info('Spawning shards...');
|
|
await this.shardManager.spawn();
|
|
// G:\Documents\My programs\GBot\New GBot\api\index.cjs
|
|
this.info('Shards done, booting up API...');
|
|
const { default: APIManager } = await import('./api/index.js').catch((err) => this.error(`Import of API Manager failed.\n${err.stack}`));
|
|
if (APIManager) {
|
|
this.apiManager = new APIManager(this);
|
|
await this.apiManager.init();
|
|
this.info('API done. Manager built.');
|
|
} else this.info('Missing API source, skipping');
|
|
|
|
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.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;
|
|
}
|
|
|
|
info(message) {
|
|
this.logger.write('info', message);
|
|
}
|
|
|
|
warn(message) {
|
|
this.logger.write('warn', message);
|
|
}
|
|
|
|
error(message) {
|
|
this.logger.write('error', message);
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = Manager; |