diff --git a/src/middleware/BaseClient.js b/src/middleware/BaseClient.js index 4626b91..4986c56 100644 --- a/src/middleware/BaseClient.js +++ b/src/middleware/BaseClient.js @@ -59,6 +59,7 @@ class BaseClient extends EventEmitter { _handleMessage(shard, message) { //this.logger.debug(`New message from ${shard ? `${message._api ? 'api-' : ''}shard ${shard.id}`: 'manager'}: ${inspect(message)}`); + if (message._mEval) return this.eval(shard, message); if (message._logger) return this.logger._handleMessage(shard, message); if (message._commands) return this.slashCommandManager._handleMessage(message); if (message._api) return this.apiRequest(shard, message); @@ -77,6 +78,34 @@ class BaseClient extends EventEmitter { } + /** + * @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.info(`Incoming manager eval from shard ${shard.id}:\n${script}`); + let result = null, + error = null; + + try { + // eslint-disable-next-line no-eval + result = await eval(script); + //if(typeof result !== 'string') result = inspect(result); + } catch (err) { + error = err.stack || err; + } + + return shard.send({ + _evalResult: true, + script, + result, + error + }); + + } } diff --git a/src/structure/DiscordClient.js b/src/structure/DiscordClient.js index f9edfa5..ad84045 100644 --- a/src/structure/DiscordClient.js +++ b/src/structure/DiscordClient.js @@ -53,6 +53,7 @@ class DiscordClient extends Client { this._activity = 0; this._options = options; this._built = false; + this._evals = new Collection(); // this.once('ready', () => { // this._setActivity(); @@ -72,6 +73,8 @@ class DiscordClient extends Client { this.logger.error(`Unhandled rejection:\n${err.stack || err}\n${inspect(reason)}`); }); + process.on('message', this._handleMessage.bind(this)); + } async build() { @@ -121,6 +124,31 @@ class DiscordClient extends Client { clearInterval(this._activityInterval); } + async _handleMessage(message) { + //Handle misc. messages. + if (message._evalResult) this.evalResult(message); + } + + async managerEval(script) { + + return new Promise((resolve, reject) => { + + this._evals.set(script, { resolve, reject }); + process.send({ _mEval: true, script }); + + }); + + } + + evalResult({ script, result, error }) { + + const promise = this._evals.get(script); + if (result) promise.resolve(result); + else promise.reject(error); + this._evals.delete(script); + + } + // Wait until the client is actually ready, i.e. all structures from discord are created ready() {