const { inspect } = require('util'); const { username } = require('os').userInfo(); const { stripIndents } = require('common-tags'); let _storage = null; //eslint-disable-line no-unused-vars const { Command, Argument } = require('../../../../interfaces/'); class Evaluate extends Command { constructor(client) { super(client, { name: 'evaluate', module: 'developer', aliases: [ 'eval', 'e' ], restricted: true, description: "Evaluates javascript code.", arguments: [ new Argument(client, { name: 'log', type: 'BOOLEAN', types: ['FLAG'], description: "Logs the output in the console." }), new Argument(client, { name: 'hide', type: 'BOOLEAN', types: ['FLAG'], description: "Hides the output from the channel." }) ] }); } async execute(message, { params, args }) { params = params.join(' '); try { let evaled = eval(params); if(evaled instanceof Promise) await evaled; if(typeof evaled !== 'string') evaled = inspect(evaled); evaled = evaled .replace(new RegExp(this.client.token, 'g'), '') .replace(new RegExp(username, 'g'), ''); if(args.log) this.client.logger.debug(`[${message.author.tag}] Evaluation Result: ${evaled}`); if(evaled.length > 2000) evaled = `${evaled.substring(0, 1900)}...`; await message.respond(stripIndents`Evaluation was successful. \`\`\`js ${evaled}\`\`\``, { emoji: 'success' }); } catch(error) { let message = `${error}${error.stack ? `\n${error.stack}` : ''}`; if(args.log) this.client.logger.debug(`[${message.author.tag}] Evaluation Failed: ${message}`); if(message.length > 2000) message = `${message.substring(0, 1900)}...`; await message.respond(stripIndents`Evaluation failed. \`\`\`js ${message}\`\`\``, { emoji: 'failure' }); } } } module.exports = Evaluate;