"use strict";

const Module = require('../Core/API/Module');

const PREFIX = 'Result:\n```javascript\n';
const SUFFIX = '\n```';
const NO_SPACE = 'This response is too long. Shortening to ~2000 characters:\n```javascript';

class EvalCommand extends Module {
  constructor(container) {
    super(container);

    this.hears(/^eval/i, this.onEvalCommand.bind(this));
  }

  onEvalCommand(m, args) {
    if (m.author.id !== '142098955818369024') {
      this.logger.info(`User ${m.author.username} (${m.author.id}) tried to eval ${args}`);
      m.channel.sendMessage(`${m.author.mention} You are not authorized to perform this action.`);
      return;
    }

    this.logger.debug(`EXECUTE_JS ${args}`);

    let res = null;
    const text = args.slice(1).join(' ');

    this.logger.debug(`EXECUTE_JS 2 ${text}`);

    try {
      res = eval(text);
      this.logger.debug('EXECUTE_JS res', res);

      const inspect = util.inspect(res);
      let string = `${prefix}${inspect}${suffix}`;
      this.logger.debug(`EXECUTE_JS length ${string.length}`);

      // max length of message is 2000 characters
      if (string.length > 2000) {
        const stringTwo = `${prefix}${inspect.substring(0, 2000 - prefix.length - suffix.length - 3)}...${suffix}`;
        m.channel.sendMessage(stringTwo);
      } else {
        m.channel.sendMessage(string);
      }
    } catch (errr) {
      const errMsg = err ? err.stack : 'no error message';
      m.channel.sendMessage('Something went wrong:\n```\n' + errMsg + '\n```');
    }
  }
}

module.exports = EvalCommand;

@@ -8,16 +8,47 @@
 rict%22;%0A%0A
+const util = require('util');%0A%0A
 const Mo
@@ -81,24 +81,24 @@
 I/Module');%0A
-
 %0Aconst PREFI
@@ -353,9 +353,8 @@
 rs(/
-%5E
 eval
@@ -980,30 +980,30 @@
 string = %60$%7B
-prefix
+PREFIX
 %7D$%7Binspect%7D$
@@ -1003,22 +1003,22 @@
 spect%7D$%7B
-suffix
+SUFFIX
 %7D%60;%0A    
@@ -1190,22 +1190,22 @@
 wo = %60$%7B
-prefix
+PREFIX
 %7D$%7Binspe
@@ -1231,14 +1231,14 @@
 0 - 
-prefix
+PREFIX
 .len
@@ -1243,22 +1243,22 @@
 ength - 
-suffix
+SUFFIX
 .length 
@@ -1271,14 +1271,14 @@
 ..$%7B
-suffix
+SUFFIX
 %7D%60;%0A
@@ -1397,17 +1397,16 @@
 tch (err
-r
 ) %7B%0A    
@@ -1468,35 +1468,21 @@
       m.
-channel.sendMessage
+reply
 ('Someth

refactor variable names and change regex