Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var discord = require("discord.js");
  2. var client = new discord.Client();
  3.  
  4. var fs = require("fs"),
  5.     time = 10000,
  6.     settings = {}
  7.  
  8. var runtimes = {}, prefix = "#";
  9.  
  10. fs.readFile(__dirname + "/settings.json", (error, data) => {
  11.     if (error) return fs.writeFile(__dirname + "/settings.json", "{\"tags\":{}}", e => {});
  12.  
  13.     settings = JSON.parse(data);
  14. });
  15.  
  16. client.on("ready", pingTrash);
  17. function pingTrash() {
  18.     client.channels.get("234257773079035914").send("Ok").then(m => m.delete());
  19.     console.log("Pinged #trash");
  20. }
  21. setInterval(pingTrash, 61000);
  22.  
  23.  
  24. client.on("message", message => {
  25.     if (message.author.id != 263617003904958465) return;
  26.  
  27.     var args = message.content.split(" ");
  28.  
  29.      /* ///////////////// NODE.JS EXECUTION ///////////////// */
  30.     if (args[0] == prefix + "eval") {
  31.         message.delete();
  32.        
  33.         message.channel.send({
  34.             embed: {
  35.                 title: "Executing Javascript",
  36.                 color: 0x336699,
  37.                 description: "Executing Javascript. Please wait."
  38.             }
  39.         }).then(messag => {
  40.             var arg2 = args.splice(1).join(" ");
  41.  
  42.             var code = arg2.replace(/\`\`\`(js|javascript)/, "");
  43.            
  44.             if (code == arg2) {
  45.                 code = arg2.replace("```", "");
  46.    
  47.                 if (code == args.splice(1).join(" ")) {
  48.                     return messag.edit({
  49.                         embed: {
  50.                             title: "Node.js Runtime Environment",
  51.                             color: 0x336699,
  52.                             description: "Failed to execute code: Invalid command syntax."
  53.                         }
  54.                     });
  55.                 }
  56.             }
  57.             code = code.substring(0, code.length - 3);
  58.  
  59.             var id = Math.random().toString(36).substr(7, 3), output = "\n";
  60.  
  61.             fs.writeFile(`${__dirname}/nodejs_execute_${id}.js`, code, err => {
  62.                 if (err) return messag.edit({
  63.                     embed: {
  64.                         title: "Executing Javascript",
  65.                         color: 0x336699,
  66.                         description: "Failed to execute code, could not write file."
  67.                     }
  68.                 });
  69.                 messag.edit({
  70.                     embed: {
  71.                         title: "Executing Javascript",
  72.                         color: 0x336699,
  73.                         description: "**Code:**```js\n" + code.substr(0, 500) + "```**Output:**``` ```"
  74.                     }
  75.                 });
  76.  
  77.                 var child_process = require("child_process");
  78.                
  79.                 var _ = child_process.exec(`node "${__dirname}/nodejs_execute_${id}.js"`, (error, stdout, stdin) => {
  80.                     if (!error) messag.edit({
  81.                         embed: {
  82.                             title: "Executing Javascript",
  83.                             color: 0x336699,
  84.                             description: "**Code:**```js\n" + code + "```**Output:**```" + stdout.toString() + "```"
  85.                         }
  86.                     }); else messag.edit({
  87.                         embed: {
  88.                             title: "Executing Javascript",
  89.                             color: 0x336699,
  90.                             description: "**Code:**```js\n" + code + "```**Error:**```" + error.toString().replace(/C:\\Users\\.+(?:\\)/g, "<path>/") + "```"
  91.                         }
  92.                     });
  93.                     fs.unlink(`${__dirname}/nodejs_execute_${id}.js`, () => {});
  94.                 });
  95.             });
  96.         });
  97.     }
  98.  
  99.     /* ///////////////// NODE.JS ENVIRONMENT ///////////////// */
  100.     if (args[0] == prefix + "nodejs-runtime" || args[0] == prefix + "?nre") {
  101.         message.delete();
  102.  
  103.         message.channel.send({
  104.             embed: {
  105.                 title: "Node.js Runtime Environment",
  106.                 color: 0x336699,
  107.                 description: "Preparing Node.js Runtime Environment, please stand by."
  108.             }
  109.         }).then(messag => {
  110.             var arg2 = args.splice(1).join(" ");
  111.  
  112.             var code = arg2.replace(/\`\`\`(js|javascript)/, "");
  113.            
  114.             if (code == arg2) {
  115.                 code = arg2.replace("```", "");
  116.    
  117.                 if (code == args.splice(1).join(" ")) {
  118.                     return messag.edit({
  119.                         embed: {
  120.                             title: "Node.js Runtime Environment",
  121.                             color: 0x336699,
  122.                             description: "Failed to execute code: Invalid command syntax."
  123.                         }
  124.                     });
  125.                 }
  126.             }
  127.             code = code.substring(0, code.length - 3);
  128.  
  129.             var id = Math.random().toString(36).substr(7, 3), output = "\n";
  130.  
  131.             fs.writeFile(`${__dirname}/nodejs_environment_${id}.js`, code, err => {
  132.                 if (err) return messag.edit({
  133.                     embed: {
  134.                         title: "Node.js Runtime Environment",
  135.                         color: 0x336699,
  136.                         description: "Failed to execute code, could not write file."
  137.                     }
  138.                 });
  139.                 messag.edit({
  140.                     embed: {
  141.                         title: "Node.js Runtime Environment",
  142.                         color: 0x336699,
  143.                         description: "**ID: " + id + "**\n**Code:**```js\n" + code.substr(0, 500) + "```**Output:**``` ```"
  144.                     }
  145.                 });
  146.  
  147.                 var child_process = require("child_process");
  148.                
  149.                 runtimes[id] = child_process.spawn(`node`, [`${__dirname}/nodejs_environment_${id}.js`]);
  150.                
  151.                 setTimeout(function () {
  152.                     if (!runtimes[id]) return;
  153.                     runtimes[id].stdin.pause();
  154.                     runtimes[id].kill();
  155.                 }, 300000);
  156.                
  157.                 runtimes[id].stdout.on("data", (d) => {
  158.                     output = output + d.toString();
  159.  
  160.                     messag.edit({
  161.                         embed: {
  162.                             title: "Node.js Runtime Environment",
  163.                             color: 0x336699,
  164.                             description: "**ID: " + id + "**\n**Code:**```js\n" + code.substr(0, 500) + "```**Output:**```" + output.substr(output.length - 500) + "```"
  165.                         }
  166.                     });
  167.                 });
  168.                 runtimes[id].on("exit", (d) => {
  169.  
  170.                     message.channel.send({
  171.                         embed: {
  172.                             title: "Node.js Runtime Environment",
  173.                             color: 0x336699,
  174.                             description: `NRE \`${id}\` has closed.`
  175.                         }
  176.                     });
  177.    
  178.                     delete runtimes[id];
  179.                    
  180.                     fs.unlink(`${__dirname}/nodejs_environment_${id}.js`, () => {});
  181.                 });
  182.                 runtimes[id].stderr.on("data", (d) => {
  183.                     messag.edit({
  184.                         embed: {
  185.                             title: "Node.js Runtime Environment",
  186.                             color: 0x336699,
  187.                             description: "**ID: " + id + "**\n**Code:**```js\n" + code.substr(0, 500) + "```**Output:**```" + output.substr(output.length - 500) + "```**Error:**```" + d.toString().replace(/C:\\Users\\.+(?:\\)/g, "<path>/") + "```"
  188.                         }
  189.                     });
  190.                 });
  191.             });
  192.         });
  193.     }
  194.     if (args[0] == prefix + "close-rt") {
  195.         message.delete();
  196.  
  197.         runtimes[args[1]].stdin.pause();
  198.         runtimes[args[1]].kill();
  199.     }
  200.     if (args[0] == prefix + "tag") {
  201.         message.delete();
  202.  
  203.         if (!args[1]) return;
  204.        
  205.         if (args[1] == "-c") {
  206.             if (args[2] && args[3]) {
  207.                 settings.tags[args[2].toLowerCase()] = args.splice(3).join(" ");
  208.             }
  209.         } else if (args[1] == "-l") {
  210.             message.channel.send({
  211.                 embed: {
  212.                     title: "Tag",
  213.                     color: 0x336699,
  214.                     description: Object.keys(settings.tags).toString()
  215.                 }
  216.             });
  217.         } else if (args[1] == "-d") {
  218.             if (args[2]) {
  219.                 delete settings.tags[args[2].toLowerCase()];
  220.             }
  221.         } else {
  222.             var tag = settings.tags[args[1].toLowerCase()];
  223.            
  224.             if (!tag) message.channel.send(`Tag '${args[1]}' not found.`);
  225.            
  226.             while (tag.substr(0, 2) == "a:") {
  227.                 tag = settings.tags[tag.substr(2)].toLowerCase();
  228.                 console.log(tag);
  229.             }
  230.            
  231.             message.channel.send(tag || `Tag '${args[1]}' not found.`);
  232.         }
  233.  
  234.         fs.writeFile(`${__dirname}/settings.json`, JSON.stringify(settings), err => {});
  235.     }
  236.     for (let i = 0;i < args.length; i++){
  237.         let a = args[i].toLowerCase();
  238.        
  239.         if (a.match(/\?.+/g) && a.match(/\?.+/g)[0] == a) {
  240.             let v = a.substr(1);
  241.            
  242.             if (settings.tags[v]) {
  243.                 var tag = settings.tags[v];
  244.            
  245.                 if (!tag) return;
  246.                
  247.                 while (tag.substr(0, 2) == "a:") {
  248.                     tag = settings.tags[tag.substr(2)].toLowerCase();
  249.                     if (!tag) return;
  250.                    
  251.                     console.log(tag);
  252.                 }
  253.                
  254.                 args[i] = tag;
  255.            
  256.                 message.edit(args.join(" "));
  257.             }
  258.         }
  259.     }
  260.    
  261. });
  262.  
  263. client.login("MjYzNjE3MDAzOTA0OTU4NDY1.DFtH7A.44t7aYtRQPvRdgWPu4HTEZHJNIw");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement