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

87 lines
2.4 KiB
JavaScript
Raw Normal View History

const { inspect } = require('util');
const { username } = require('os').userInfo();
2020-06-04 19:59:09 +02:00
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'],
2020-05-21 10:51:44 +02:00
description: 'Logs the output in the console.'
},
{
name: 'hide',
type: 'BOOLEAN',
types: ['FLAG'],
2020-05-21 10:51:44 +02:00
description: 'Hides the output from the channel.'
}
],
2020-06-04 19:59:09 +02:00
showUsage: true,
keepQuotes: true
});
}
async execute(message, { params, args }) {
params = params.join(' ');
2020-05-24 23:40:28 +02:00
// eslint-disable-next-line no-unused-vars
const { guild, author, member } = message;
2020-06-04 19:59:09 +02:00
console.log(params);
try {
let evaled = eval(params); //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) this.client.logger.debug(`[${message.author.tag}] Evaluation Result: ${evaled}`);
2020-05-01 16:12:45 +02:00
if (evaled.length > 1850) {
evaled = `${evaled.substring(0, 1850)}...`;
}
await message.respond(
`Evaluation was successful.\`\`\`js\n${evaled}\`\`\``,
{ emoji: 'success' }
);
} catch(error) {
2020-05-01 16:12:45 +02:00
let msg = `${error}${error.stack ? `\n${error.stack}` : ''}`;
2020-05-01 16:12:45 +02:00
if(args.log) this.client.logger.debug(`[${message.author.tag}] Evaluation Failed: ${msg}`);
2020-05-01 16:12:45 +02:00
if(msg.length > 2000) msg = `${msg.substring(0, 1900)}...`;
await message.respond(
`Evaluation failed.\`\`\`js\n${msg}\`\`\``,
{ emoji: 'failure' }
);
}
}
}
module.exports = Evaluate;