manager eval

This commit is contained in:
Erik 2020-08-14 17:13:39 +03:00
parent ff544dd57b
commit 01c826a58d

View File

@ -1,4 +1,5 @@
const { EventEmitter } = require('events');
const { inspect } = require('util');
const ShardManager = require('./middleware/ShardManager.js');
const Logger = require('./middleware/logger/Logger.js');
@ -13,6 +14,7 @@ class Manager extends EventEmitter {
this.logger = new Logger(this);
this._built = false;
this.readyAt = null;
this.shardManager.on('message', this._handleMessage.bind(this));
// this.on('built', () => {
@ -23,8 +25,9 @@ class Manager extends EventEmitter {
_handleMessage(shard, message) {
if(message._logger) return this.logger._handleMessage(shard, message);
if(message._webhook) return undefined; //todo
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() {
@ -33,9 +36,46 @@ class Manager extends EventEmitter {
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;