Add owner-only eval and reload commands

This commit is contained in:
1computer1 2019-03-12 12:20:02 -04:00
parent 3b2a3c4f78
commit b2e3eca02b
2 changed files with 157 additions and 0 deletions

111
src/commands/eval.js Normal file
View file

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

46
src/commands/reload.js Normal file
View file

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