manager evals

This commit is contained in:
Erik 2022-05-11 20:23:47 +03:00
parent 9240686aff
commit 79c302ce04
Signed by untrusted user: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB
2 changed files with 57 additions and 0 deletions

View File

@ -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
});
}
}

View File

@ -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() {