From b2e3eca02b2ddae59425331022292c18429e962f Mon Sep 17 00:00:00 2001 From: 1computer1 Date: Tue, 12 Mar 2019 12:20:02 -0400 Subject: [PATCH] Add owner-only eval and reload commands --- src/commands/eval.js | 111 +++++++++++++++++++++++++++++++++++++++++ src/commands/reload.js | 46 +++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 src/commands/eval.js create mode 100644 src/commands/reload.js diff --git a/src/commands/eval.js b/src/commands/eval.js new file mode 100644 index 0000000..29e7b35 --- /dev/null +++ b/src/commands/eval.js @@ -0,0 +1,111 @@ +const { Command } = require('discord-akairo'); +const util = require('util'); + +class EvalCommand extends Command { + constructor() { + super('eval', { + aliases: ['eval', 'e'], + ownerOnly: true, + quoted: false, + args: [ + { + id: 'code', + match: 'content' + } + ] + }); + } + + async exec(message, { code }) { + if (!code) { + return message.util.send('No code provided!'); + } + + const evaled = {}; + const logs = []; + const token = this.client.token.split('').join('[^]{0,2}'); + const rev = this.client.token.split('').reverse().join('[^]{0,2}'); + const tokenRegex = new RegExp(`${token}|${rev}`, 'g'); + const cb = '```'; + const print = (...a) => { // eslint-disable-line no-unused-vars + const cleaned = a.map(obj => { + if (typeof o !== 'string') obj = util.inspect(obj, { depth: 1 }); + return obj.replace(tokenRegex, '[TOKEN]'); + }); + + if (!evaled.output) { + logs.push(...cleaned); + return; + } + + evaled.output += evaled.output.endsWith('\n') ? cleaned.join(' ') : `\n${cleaned.join(' ')}`; + const title = evaled.errored ? '☠\u2000**Error**' : '📤\u2000**Output**'; + if (evaled.output.length + code.length > 1900) { + evaled.output = 'Output too long.'; + } + + evaled.message.edit([ + `📥\u2000**Input**${cb}js`, + code, + cb, + `${title}${cb}js`, + evaled.output, + cb + ]); + }; + + try { + // eslint-disable-next-line no-eval + let output = eval(code); + + // eslint-disable-next-line eqeqeq + if (output != null && typeof output.then === 'function') { + output = await output; + } + + if (typeof output !== 'string') { + output = util.inspect(output, { depth: 0 }); + } + + output = `${logs.join('\n')}\n${logs.length && output === 'undefined' ? '' : output}`; + output = output.replace(tokenRegex, '[TOKEN]'); + if (output.length + code.length > 1900) { + output = 'Output too long.'; + } + + const sent = await message.util.send([ + `📥\u2000**Input**${cb}js`, + code, + cb, + `📤\u2000**Output**${cb}js`, + output, + cb + ]); + + evaled.message = sent; + evaled.errored = false; + evaled.output = output; + return sent; + } catch (err) { + let error = err; + error = error.toString(); + error = `${logs.join('\n')}\n${logs.length && error === 'undefined' ? '' : error}`; + error = error.replace(tokenRegex, '[TOKEN]'); + const sent = await message.util.send([ + `📥\u2000**Input**${cb}js`, + code, + cb, + `☠\u2000**Error**${cb}js`, + error, + cb + ]); + + evaled.message = sent; + evaled.errored = true; + evaled.output = error; + return sent; + } + } +} + +module.exports = EvalCommand; diff --git a/src/commands/reload.js b/src/commands/reload.js new file mode 100644 index 0000000..6092206 --- /dev/null +++ b/src/commands/reload.js @@ -0,0 +1,46 @@ +const { Command } = require('discord-akairo'); + +class ReloadCommand extends Command { + constructor() { + super('reload', { + aliases: ['reload', 'r'], + ownerOnly: true, + quoted: false, + args: [ + { + id: 'type', + match: 'option', + flag: ['type:'], + type: [['command', 'c'], ['listener', 'l']], + default: 'command' + }, + { + id: 'module', + type: (phrase, message, { type }) => { + if (!phrase) return null; + const resolver = this.handler.resolver.type({ + command: 'commandAlias', + listener: 'listener' + }[type]); + return resolver(phrase); + } + } + ] + }); + } + + exec(message, { type, module: mod }) { + if (!mod) { + return message.util.send(`Invalid ${type} ${type === 'command' ? 'alias' : 'ID'} specified to reload.`); + } + + try { + mod.reload(); + return message.util.send(`Sucessfully reloaded ${type} \`${mod.id}\`.`); + } catch (err) { + return message.util.send(`Failed to reload ${type} \`${mod.id}\`.`); + } + } +} + +module.exports = ReloadCommand;