Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* REPLACE <ID> WITH YOUR USER ID AND <TOKEN> WITH YOUR USER TOKEN. */
  2.  
  3. var discord = require("discord.js");
  4. var client = new discord.Client();
  5.  
  6. var fs = require("fs"),
  7.     time = 10000,
  8.     settings = {}
  9.  
  10. var runtimes = {};
  11.  
  12. fs.readFile(__dirname + "/settings.json", (error, data) => {
  13.     if (error) return fs.writeFile(__dirname + "/settings.json", "{\"tags\":{}}", e => {});
  14.  
  15.     settings = JSON.parse(data);
  16. });
  17.  
  18.  
  19. client.on("message", message => {
  20.     if (message.author.id != <id>) return;
  21.  
  22.     var args = message.content.split(" ");
  23.  
  24.      /* ///////////////// NODE.JS EXECUTION ///////////////// */
  25.     if (args[0] == "::eval") {
  26.         message.delete();
  27.        
  28.         message.channel.send({
  29.             embed: {
  30.                 title: "Executing Javascript",
  31.                 color: 0x336699,
  32.                 description: "Executing Javascript. Please wait."
  33.             }
  34.         }).then(messag => {
  35.             var arg2 = args.splice(1).join(" ");
  36.  
  37.             var code = arg2.replace(/\`\`\`(js|javascript)/, "");
  38.            
  39.             if (code == arg2) {
  40.                 code = arg2.replace("```", "");
  41.    
  42.                 if (code == args.splice(1).join(" ")) {
  43.                     return messag.edit({
  44.                         embed: {
  45.                             title: "Node.js Runtime Environment",
  46.                             color: 0x336699,
  47.                             description: "Failed to execute code: Invalid command syntax."
  48.                         }
  49.                     });
  50.                 }
  51.             }
  52.             code = code.substring(0, code.length - 3);
  53.  
  54.             var id = Math.random().toString(36).substr(7, 3), output = "\n";
  55.  
  56.             fs.writeFile(`${__dirname}/nodejs_execute_${id}.js`, code, err => {
  57.                 if (err) return messag.edit({
  58.                     embed: {
  59.                         title: "Executing Javascript",
  60.                         color: 0x336699,
  61.                         description: "Failed to execute code, could not write file."
  62.                     }
  63.                 });
  64.                 messag.edit({
  65.                     embed: {
  66.                         title: "Executing Javascript",
  67.                         color: 0x336699,
  68.                         description: "**Code:**```js\n" + code + "```**Output:**``` ```"
  69.                     }
  70.                 });
  71.  
  72.                 var child_process = require("child_process");
  73.                
  74.                 var _ = child_process.exec(`node "${__dirname}/nodejs_execute_${id}.js"`, (error, stdout, stdin) => {
  75.                     if (!error) messag.edit({
  76.                         embed: {
  77.                             title: "Executing Javascript",
  78.                             color: 0x336699,
  79.                             description: "**Code:**```js\n" + code + "```**Output:**```" + stdout.toString() + "```"
  80.                         }
  81.                     }); else messag.edit({
  82.                         embed: {
  83.                             title: "Executing Javascript",
  84.                             color: 0x336699,
  85.                             description: "**Code:**```js\n" + code + "```**Error:**```" + error.toString().replace(/C:\\Users\\.+(?:\\)/g, "<path>/") + "```"
  86.                         }
  87.                     });
  88.                     fs.unlink(`${__dirname}/nodejs_execute_${id}.js`, () => {});
  89.                 });
  90.             });
  91.         });
  92.     }
  93.  
  94.     /* ///////////////// NODE.JS ENVIRONMENT ///////////////// */
  95.     if (args[0] == "::nodejs-runtime" || args[0] == "::nrt") {
  96.         message.delete();
  97.  
  98.         message.channel.send({
  99.             embed: {
  100.                 title: "Node.js Runtime Environment",
  101.                 color: 0x336699,
  102.                 description: "Preparing Node.js Runtime Environment, please stand by."
  103.             }
  104.         }).then(messag => {
  105.             var code = args.splice(1).join(" ").replace(/\`\`\`(js|javascript)/, "");
  106.            
  107.             if (code == args.splice(1).join(" ")) {
  108.                 code = args.splice(1).join(" ").replace("```", "");
  109.    
  110.                 if (code == args.splice(1).join(" ")) {
  111.                     return messag.edit({
  112.                         embed: {
  113.                             title: "Node.js Runtime Environment",
  114.                             color: 0x336699,
  115.                             description: "Failed to execute code: Invalid command syntax."
  116.                         }
  117.                     });
  118.                 }
  119.             }
  120.             code = code.substring(0, code.length - 3);
  121.  
  122.             var id = Math.random().toString(36).substr(7, 3), output = "\n";
  123.  
  124.             fs.writeFile(`${__dirname}/nodejs_environment_${id}.js`, code, err => {
  125.                 if (err) return messag.edit({
  126.                     embed: {
  127.                         title: "Node.js Runtime Environment",
  128.                         color: 0x336699,
  129.                         description: "Failed to execute code, could not write file."
  130.                     }
  131.                 });
  132.                 messag.edit({
  133.                     embed: {
  134.                         title: "Node.js Runtime Environment",
  135.                         color: 0x336699,
  136.                         description: "**ID: " + id + "**\n**Code:**```js\n" + code + "```**Output:**``` ```"
  137.                     }
  138.                 });
  139.  
  140.                 var child_process = require("child_process");
  141.                
  142.                 runtimes[id] = child_process.spawn(`node`, [`${__dirname}/nodejs_environment_${id}.js`]);
  143.                
  144.                 runtimes[id].stdout.on("data", (d) => {
  145.                     output = output + d.toString();
  146.  
  147.                     messag.edit({
  148.                         embed: {
  149.                             title: "Node.js Runtime Environment",
  150.                             color: 0x336699,
  151.                             description: "**ID: " + id + "**\n**Code:**```js\n" + code + "```**Output:**```" + output + "```"
  152.                         }
  153.                     });
  154.                 });
  155.                 runtimes[id].on("exit", (d) => {
  156.  
  157.                     message.channel.send({
  158.                         embed: {
  159.                             title: "Node.js Runtime Environment",
  160.                             color: 0x336699,
  161.                             description: `NRE \`${id}\` has closed.`
  162.                         }
  163.                     });
  164.  
  165.                     fs.unlink(`${__dirname}/nodejs_environment_${id}.js`, () => {});
  166.                 });
  167.                 runtimes[id].stderr.on("data", (d) => {
  168.                     messag.edit({
  169.                         embed: {
  170.                             title: "Node.js Runtime Environment",
  171.                             color: 0x336699,
  172.                             description: "**ID: " + id + "**\n**Code:**```js\n" + code + "```**Output:**```" + output + "```**Error:**```" + d.toString().replace(/C:\\Users\\.+(?:\\)/g, "<path>/") + "```"
  173.                         }
  174.                     });
  175.                 });
  176.             });
  177.         });
  178.     }
  179.     if (args[0] == "::close-rt") {
  180.         message.delete();
  181.  
  182.         runtimes[args[1]].stdin.pause();
  183.         runtimes[args[1]].kill();
  184.     }
  185.     if (args[0] == "::tag") {
  186.         message.delete();
  187.  
  188.         if (!args[1]) return;
  189.        
  190.         if (args[1] == "-c") {
  191.             if (args[2] && args[3]) {
  192.                 settings.tags[args[2]] = args.splice(3).join(" ");
  193.             }
  194.         } else if (args[1] == "-l") {
  195.             message.channel.send({
  196.                 embed: {
  197.                     title: "Tag",
  198.                     color: 0x336699,
  199.                     description: Object.keys(settings.tags).toString()
  200.                 }
  201.             });
  202.         } else {
  203.             message.channel.send(settings.tags[args[1]] || `Tag '${args[1]}' not found.`);
  204.         }
  205.  
  206.         fs.writeFile(`${__dirname}/settings.json`, JSON.stringify(settings), err => {});
  207.     }
  208. });
  209.  
  210. client.login("<token>");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement