Advertisement
DayDun

Untitled

Jan 6th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. const Discord = require("discord.js");
  4. const WebSocket = require("ws");
  5. const Canvas = require("canvas");
  6.  
  7. const Token = "";
  8. const ServerId = "350296414720491530";
  9. const adminPass = "";
  10.  
  11. var gatewayChannel,
  12.     archiveChannel;
  13.  
  14. let bot = new Discord.Client();
  15.  
  16.  
  17.  
  18. let chunkRequest = {};
  19. let chunkChannel;
  20. let chunkSize = [0, 0];
  21. let chunkStart = [0, 0];
  22.  
  23. // Connect to owop
  24. let botId = 0;
  25. let owopBot = new WebSocket("ws://ourworldofpixels.com:443", {
  26.   origin: "http://ourworldofpixels.com/"
  27. });
  28. owopBot.on("open", function() {
  29.   console.log("owop open!");
  30. });
  31. owopBot.on("message", function(data) {
  32.   if (typeof data == "string") {
  33.     if (!data.startsWith("[D]") && !data.startsWith("DEV") && !data.startsWith("Nickname") && !data.startsWith("User: ")) {
  34.       if (gatewayChannel) {
  35.         gatewayChannel.send(data);
  36.       }
  37.     }
  38.   } else {
  39.     switch(data.readUInt8(0)) {
  40.       case 0: // Get id
  41.         botId = data.readUInt32LE(1);
  42.         console.log("owop ready!");
  43.         owopBot.send("/adminlogin " + adminPass + String.fromCharCode(10));
  44.         sendMove();
  45.         setInterval(sendMove, 600000);
  46.         break;
  47.       case 1: // Get all cursors, tile updates, disconnects
  48.        
  49.         break;
  50.       case 2: // Get chunk
  51.         let x = data.readInt32LE(1);
  52.         let y = data.readInt32LE(5);
  53.         if ([x, y] in chunkRequest) {
  54.           let image = new Canvas(16, 16);
  55.           let imageData = image.getContext("2d").createImageData(16, 16);
  56.           for (let i=0, n=0; i<1024; i+=4, n++) {
  57.             imageData.data[i] = data.readUInt8(9 + n * 3);
  58.             imageData.data[i + 1] = data.readUInt8(10 + n * 3);
  59.             imageData.data[i + 2] = data.readUInt8(11 + n * 3);
  60.             imageData.data[i + 3] = 255;
  61.           }
  62.           image.getContext("2d").putImageData(imageData, 0, 0);
  63.           chunkRequest[[x, y]] = {
  64.             done: true,
  65.             x: x - chunkStart[0],
  66.             y: y - chunkStart[1],
  67.             image: image
  68.           };
  69.          
  70.           let allDone = true;
  71.           for (let i in chunkRequest) {
  72.             if (!chunkRequest[i].done) {
  73.               allDone = false;
  74.               break;
  75.             }
  76.           }
  77.           if (allDone) {
  78.             let canvas = new Canvas(chunkSize[0] * 16, chunkSize[1] * 16);
  79.             let ctx = canvas.getContext("2d");
  80.             for (let i in chunkRequest) {
  81.               ctx.drawImage(chunkRequest[i].image, chunkRequest[i].x * 16, chunkRequest[i].y * 16);
  82.             }
  83.             chunkChannel.send("", new Discord.Attachment(canvas.toBuffer(), "region.png"));
  84.             chunkChannel.stopTyping();
  85.             chunkRequest = {};
  86.           }
  87.         }
  88.         break;
  89.       case 3: // Teleport
  90.        
  91.         break;
  92.       case 4: // Get rank
  93.         var rank = data.readUInt8(1);
  94.         break;
  95.       case 5: // Captcha
  96.         switch(data.readUInt8(1)) {
  97.           case 0:
  98.             owopBot.send("CaptchALETMEINPLS" + adminPass);
  99.             break;
  100.           case 3:
  101.             owopBot.send(new Buffer([109, 97, 105, 110, 57, 5]));
  102.             break;
  103.         }
  104.         break;
  105.     }
  106.   }
  107. });
  108. owopBot.on("close", function() {
  109.   console.log("owop close!!!");
  110. });
  111. function sendMove() {
  112.   owopBot.send(new Buffer([127, 255, 255, 255,   127, 255, 255, 255,   0, 0, 0, 0]));
  113. }
  114.  
  115.  
  116. let roles = {
  117.   "350303014944505866": { // Admin
  118.     commands: ["help", "view", "eval"]
  119.   },
  120.   "350296414720491530": { // @everyone
  121.     commands: ["help", "view"]
  122.   }
  123. };
  124.  
  125. let commands = {
  126.   "help": {
  127.     description: "Guess what it does",
  128.     usage: "b!help (<command>)",
  129.     use: function(args, message) {
  130.       if (args.length == 1 && args[0].toLowerCase() in commands) {
  131.         message.channel.send("**" + args[0].toLowerCase() + " usage:** `" + commands[args[0].toLowerCase()].usage + "`");
  132.       } else {
  133.         var output = "**Command List**\n```markdown";
  134.         for (var i in commands) {
  135.           output += "\n[ " + i + " ](" + commands[i].description + ")";
  136.         }
  137.         output += "```";
  138.         message.channel.send(output);
  139.       }
  140.       return true;
  141.     }
  142.   },
  143.   "view": {
  144.     description: "View a region of chunks on OWOP",
  145.     usage: "b!view <chunkX> <chunkY> (<width> <height>)",
  146.     use: function(args, message) {
  147.       if (chunkRequest.length) {
  148.         message.channel.send(":x:  **Chunk render already in progress! Please wait**");
  149.         return true;
  150.       } else if (args.length == 2 || args.length == 4) {
  151.         let x = parseInt(args[0]);
  152.         let y = parseInt(args[1]);
  153.         let w = parseInt(args[2]) || 1;
  154.         let h = parseInt(args[3]) || 1;
  155.        
  156.         if (isNaN(x) || isNaN(y)) {
  157.           return false;
  158.         } else if (x > 0xFFFFF || x < ~0xFFFFF || y > 0xFFFFF || y < ~0xFFFFF || x + w > 0xFFFFF || x + w < ~ 0xFFFFF || y + h > 0xFFFFF || y + h < ~0xFFFFF) {
  159.           message.channel.send(":x:  **Chunks out of range!**");
  160.           return false;
  161.         } else if (!message.member.roles.has("350303014944505866") && w * h > 576) {
  162.           message.channel.send(":x:  **Area too large! Max 576 chunks**");
  163.           return false;
  164.         }
  165.        
  166.         message.channel.startTyping();
  167.        
  168.         setTimeout(function() {
  169.           chunkChannel = message.channel;
  170.           chunkStart = [x, y];
  171.           chunkSize = [w, h];
  172.  
  173.           for (let xx=x; xx<x+w; xx++) {
  174.             for (let yy=y; yy<y+h; yy++) {
  175.               chunkRequest[[xx, yy]] = {
  176.                 done: false
  177.               };
  178.               var buffer = new Buffer(8);
  179.               buffer.writeInt32LE(xx, 0);
  180.               buffer.writeInt32LE(yy, 4);
  181.               owopBot.send(buffer);
  182.             }
  183.           }
  184.         }, 100);
  185.         return true;
  186.       } else {
  187.         return false;
  188.       }
  189.       return false;
  190.     }
  191.   },
  192.   "eval": {
  193.     description: "Runs a snippet of javascript code on the server",
  194.     usage: "b!eval <javascipt>",
  195.     use: function(args, message) {
  196.       if (args.length === 0) {
  197.         return false;
  198.       } else {
  199.         var result;
  200.         try {
  201.           result = eval(args.join(" "));
  202.          
  203.         } catch(e) {
  204.           message.channel.send(e.toString());
  205.           return true;
  206.         }
  207.         if (typeof result == "undefined") {
  208.           message.channel.send("`undefined`");
  209.         } else if (typeof result == "number") {
  210.           message.channel.send("`" + result + "`");
  211.         } else if (typeof result == "string") {
  212.           message.channel.send("`\"" + result + "\"`");
  213.         } else if (Array.isArray(result)) {
  214.           message.channel.send("`" + JSON.stringify(result) + "`");
  215.         } else {
  216.           message.channel.send("`" + result.toString() + "`");
  217.         }
  218.         return true;
  219.       }
  220.     }.bind(this)
  221.   }
  222. };
  223.  
  224.  
  225. bot.on("ready", function() {
  226.   console.log("Discord Ready!");
  227.   gatewayChannel = bot.channels.get("398541666442674187");
  228.   archiveChannel = bot.channels.get("399099019063721995");
  229. });
  230.  
  231. bot.on("message", function(message) {
  232.   if (!message.author.bot) {
  233.     if (message.content.startsWith("b!")) {
  234.       var content = message.content.slice(2).split(" ");
  235.       let command = content[0].toLowerCase();
  236.       if (command in commands) {
  237.         let canUse = false;
  238.         var rolesArray = message.member.roles.array();
  239.         for (var i=0; i<rolesArray.length; i++) {
  240.           if ((rolesArray[i].id in roles) && roles[rolesArray[i].id].commands.includes(command)) {
  241.             canUse = true;
  242.             break;
  243.           }
  244.         }
  245.         /*  if ((msg.member.roles[i] in roles) && roles[msg.member.roles[i]].commands.indexOf(command) != -1) {
  246.             canUse = true;
  247.             break;
  248.           }
  249.         }*/
  250.  
  251.         if (canUse) {
  252.           let result = commands[command].use(content.slice(1), message);
  253.           if (!result) {
  254.             message.channel.send("**:x:  " + command + " usage:** `" + commands[command].usage + "`");
  255.           }
  256.         } else {
  257.           message.channel.send(":x:  You don't have permission to use this command!");
  258.         }
  259.       }
  260.     } else if (message.channel.id == "398541666442674187") {
  261.       if (owopBot.readyState == WebSocket.OPEN) {
  262.         owopBot.send("/nick [D] " + message.member.displayName + String.fromCharCode(10));
  263.         owopBot.send("​" + message.content + String.fromCharCode(10));
  264.       }
  265.     }
  266.   }
  267. });
  268. bot.on("messageDelete", function(message) {
  269.   bot.channels.find("name", "deleted-messages").send("<@" + message.author.id + ">\n```" + message.content.replace(/`/g, "`​") + "```\nDeleted from <#" + message.channel.id + ">");
  270.   //bot.createMessage("398548887314628608", "`" + msg.id + "` Deleted from <#" + msg.channel.id + ">, content wasn't cached!");
  271. });
  272.  
  273. bot.login(Token);
  274.  
  275. // Archive
  276. function archive() {
  277.   if (!archiveChannel || chunkRequest.length) {
  278.     console.log("ARCHIVE IDLE!");
  279.     setTimeout(archive, 1000);
  280.     return;
  281.   }
  282.  
  283.   archiveChannel.startTyping();
  284.  
  285.   setTimeout(function() {
  286.     chunkChannel = archiveChannel;
  287.     chunkStart = [-50, -50];
  288.     chunkSize = [100, 100];
  289.  
  290.     for (let x=-50; x<50; x++) {
  291.       for (let y=-50; y<50; y++) {
  292.         chunkRequest[[x, y]] = {
  293.           done: false
  294.         };
  295.         var buffer = new Buffer(8);
  296.         buffer.writeInt32LE(x, 0);
  297.         buffer.writeInt32LE(y, 4);
  298.         owopBot.send(buffer);
  299.       }
  300.     }
  301.   }, 100);
  302. }
  303. setInterval(archive, 1000 * 60 * 60);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement