galactic-bot/structure/client/components/commands/developer/Evaluate.js

95 lines
2.8 KiB
JavaScript

const { inspect } = require('util');
const { username } = require('os').userInfo();
const { Util } = require('../../../../../util'); //eslint-disable-line no-unused-vars
// eslint-disable-next-line no-unused-vars
const moment = require('moment');
let _storage = null; //eslint-disable-line
const { Command } = require('../../../../interfaces/');
class Evaluate extends Command {
constructor(client) {
super(client, {
name: 'evaluate',
module: 'developer',
aliases: [
'eval',
'e'
],
usage: '<code>',
restricted: true,
arguments: [
{
name: 'log',
type: 'BOOLEAN',
types: ['FLAG'],
default: true
},
{
name: 'hide',
type: 'BOOLEAN',
types: ['FLAG'],
default: true
},
{
name: 'async',
type: 'BOOLEAN',
types: ['FLAG'],
default: true
}
],
showUsage: true,
guarded: true,
archivable: false
});
}
async execute(message, { qParams, args }) {
qParams = qParams.join(' ');
if (args.async) qParams = `(async () => {${qParams}})()`;
const { guild, author, member, client } = message; //eslint-disable-line no-unused-vars
try {
let evaled = eval(qParams); //eslint-disable-line no-eval
if(evaled instanceof Promise) await evaled;
if(typeof evaled !== 'string') evaled = inspect(evaled);
evaled = evaled
.replace(new RegExp(this.client.token, 'gu'), '<redacted>')
.replace(new RegExp(username, 'gu'), '<redacted>');
if(args.log) guild._debugLog(`[${message.author.tag}] Evaluation Success: ${evaled}`);
if (evaled.length > 1850) {
evaled = `${evaled.substring(0, 1850)}...`;
}
await message.respond(
`Evaluation was successful.\`\`\`js\n${args.hide ? '<hidden>' : evaled}\`\`\``,
{ emoji: 'success' }
);
} catch(error) {
let msg = `${error}${error.stack ? `\n${error.stack}` : ''}`;
if(args.log) guild._debugLog(`[${message.author.tag}] Evaluation Fail: ${msg}`);
if(msg.length > 2000) msg = `${msg.substring(0, 1900)}...`;
await message.respond(
`Evaluation failed.\`\`\`js\n${args.hide ? '<hidden>' : msg}\`\`\``,
{ emoji: 'failure' }
);
}
}
}
module.exports = Evaluate;