Alyssa

RedRobin.js

Apr 3rd, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Check out https://github.com/luker2009/RedRobin for a version that's always updated!
  2. var request = require("request");
  3. var websocket = require("websocket").client;
  4. var fs = require("fs");
  5. var util = require("util");
  6.  
  7. var username = "USERNAME";
  8. var password = "PASSWORD";
  9. var log_chat = false;
  10. var room;
  11. var modhash;
  12.  
  13. var default_config = '{"username":"undefined","password":"undefined","log_chat":false}';
  14.  
  15. var options;
  16. try {
  17.     fs.accessSync("config.json", fs.F_OK);
  18.     fs.readFile('config.json', 'utf8', function(err, data) {
  19.         if (err) {
  20.             return console.log(err);
  21.         }
  22.         options = JSON.parse(data);
  23.  
  24.         if (options["username"] != "undefined") {
  25.             if (options["password"] != "undefined") {
  26.                 username = options["username"];
  27.                 password = options["password"];
  28.             }
  29.         }
  30.  
  31.         log_chat = options["log_chat"]
  32.  
  33.         var insults = [
  34.             "USER looks like a pinecone!",
  35.             "USER smells bad!",
  36.             "USER looks like a snowman!",
  37.             "USER probably isn't even a communist.",
  38.             "I don't like USER.",
  39.             "No one likes USER.",
  40.             "No one would miss USER.",
  41.             "USER is the reason cancer exists",
  42.             "Cancer would be a preferable alternative to USER.",
  43.             "I'd rather drink bleach than even see USER.",
  44.             "Does anyone else think USER should die?",
  45.             "USER is probably salty he isn't as swag as me.",
  46.             "USER isn't /that/ bad of a guy, I guess."
  47.         ];
  48.  
  49.         var deaths = [
  50.             "USER fell off a cliff.",
  51.             "USER hit the ground too hard",
  52.             "USER got shot.",
  53.             "USER spontaneously combusted.",
  54.             "I shot USER.",
  55.             "USER got derezzed.",
  56.             "USER died of old age.",
  57.             "USER burned to death.",
  58.             "USER drowned.",
  59.             "MASTER shot USER.",
  60.             "USER rebelled against MASTER for the last time.",
  61.             "MASTER and USER both died together.",
  62.             "USER died of cancer.",
  63.             "MASTER infected USER with a virus, he died.",
  64.             "USER died."
  65.         ];
  66.         var client = new websocket();
  67.  
  68.         function chat(msg) {
  69.             console.log("Sent reply");
  70.             request.post({
  71.                 url: "https://www.reddit.com/api/robin/" + room + "/message",
  72.                 headers: {
  73.                     "User-Agent": ua,
  74.                     "x-modhash": modhash
  75.                 },
  76.                 jar: cookieJar,
  77.                 form: {
  78.                     api_type: "json",
  79.                     message: '@ ' + msg,
  80.                     messageClass: "message"
  81.                 }
  82.             }, function(err, resp, body) {
  83.                 console.log(body);
  84.             });
  85.         }
  86.  
  87.         function vote(choice) {
  88.             console.log("Voted.");
  89.             request.post({
  90.                 url: "https://www.reddit.com/api/robin/" + room + "/vote",
  91.                 headers: {
  92.                     "User-Agent": ua,
  93.                     "x-modhash": modhash
  94.                 },
  95.                 jar: cookieJar,
  96.                 form: {
  97.                     api_type: "json",
  98.                     vote: choice,
  99.                     room_id: room
  100.                 }
  101.             }, function(err, resp, body) {
  102.                 console.log(body);
  103.             });
  104.         }
  105.  
  106.         client.on("connect", function(connection) {
  107.             console.log("Connected to websocket!");
  108.             chat(smsg + " Connected to chat!");
  109.             connection.on("message", function(message) {
  110.                 if (message.type === "utf8") {
  111.                     msg = JSON.parse(message.utf8Data);
  112.                     if (msg["type"] == "chat") {
  113.                         var author = msg["payload"]["from"];
  114.                         var txt =
  115.                             msg["payload"]["body"];
  116.                         author = author.replace(/[^ -~]/g, "");
  117.                         txt = txt.replace(/[^ -~]/g, "");
  118.                         //console.log(author + ": " + txt);
  119.                         if (txt.substring(0, 1) == "@" || txt.substring(0, 1) == '%') {
  120.                             console.log(author + ": " + txt);
  121.                         }
  122.                         txt = txt.replace("@ ", "");
  123.                         if (txt.substring(0, 1) == ".") {
  124.                             var cmd = txt.substring(1).split(' ')[0];
  125.                             var argz = txt.substring(1).split(" ");
  126.                             if (cmd == "help") {
  127.                                 chat(smsg + ".commands to list commands, .man <command> to get help");
  128.                             } else if (cmd == "commands") {
  129.                                 chat(smsg + ".help | .commands | .man <cmd> | .insult <user> | .kill <user>")
  130.                             } else if (cmd == "man") {
  131.                                 var chelp = argz[1];
  132.                                 if (chelp == "help") {
  133.                                     chat(smsg + ".help | display help message");
  134.                                 } else if (chelp == "commands") {
  135.                                     chat(smsg + ".commands | list commands");
  136.                                 } else if (chelp == "insult") {
  137.                                     chat(smsg + ".insult <user> | insults <user>");
  138.                                 } else if (chelp == "kill") {
  139.                                     chat(smsg + ".kill <user> | kills <user>");
  140.                                 } else {
  141.                                     chat(smsg + "Unknown command! use .commands !");
  142.                                 }
  143.                             } else if (cmd == "insult") {
  144.                                 var insult = insults[Math.floor(Math.random() * insults.length)];
  145.                                 insult = insult.replace("USER", argz[1]);
  146.                                 chat(smsg + insult);
  147.                             } else if (cmd == "kill") {
  148.                                 var death = deaths[Math.floor(Math.random() * deaths.length)];
  149.                                 death = death.replace("USER", argz[1]);
  150.                                 death = death.replace("MASTER", author);
  151.                                 chat(smsg + death);
  152.                             }
  153.                         }
  154.                     } else if (msg["type"] == "merge") {
  155.                         process.exit();
  156.                     }
  157.                 }
  158.             });
  159.         });
  160.  
  161.         var ver = "1.2";
  162.         var ua = "RedRobin v" + ver + " by /u/ImAKidImASquid";
  163.         var smsg = "[RedRobin v" + ver + "] ";
  164.         var details = {
  165.             form: {
  166.                 user: username,
  167.                 passwd: password
  168.             }
  169.         };
  170.  
  171.         var options = {
  172.             headers: {
  173.                 "User-Agent": ua
  174.             }
  175.         };
  176.  
  177.         var cookieJar = request.jar();
  178.  
  179.         request.post({
  180.             url: "https://www.reddit.com/api/login",
  181.             headers: {
  182.                 "User-Agent": ua
  183.             },
  184.             jar: cookieJar,
  185.             form: {
  186.                 user: username,
  187.                 passwd: password
  188.             }
  189.         }, function(err, httpResponse, body) {
  190.             if (!err) {
  191.                 setTimeout(function() {
  192.                     request({
  193.                         url: "https://www.reddit.com/robin/join",
  194.                         jar: cookieJar,
  195.                         headers: {
  196.                             "User-Agent": ua
  197.                         }
  198.                     }, function(error, response, body) {
  199.                         setTimeout(function() {
  200.                             request({
  201.                                 url: "https://www.reddit.com/robin",
  202.                                 jar: cookieJar,
  203.                                 headers: {
  204.                                     "User-Agent": ua
  205.                                 }
  206.                             }, function(error, response, bodyb) {
  207.                                 var rwup = bodyb.indexOf("robin_websocket_url");
  208.                                 var rwu = bodyb.substring(rwup + "robin_websocket_url".length + 4);
  209.                                 rwu = rwu.split('"')[0];
  210.                                 var wsurl = rwu.replace("\\u0026", "&").replace("\\u0026", "&");
  211.                                 client.connect(wsurl);
  212.                                 var mhp = bodyb.indexOf("modhash");
  213.                                 modhash = bodyb.substring(mhp + "modhash".length + 4);
  214.                                 modhash = modhash.split('"')[0];
  215.                                 var roomp = bodyb.indexOf("robin_room_id");
  216.                                 room = bodyb.substring(roomp + "robin_room_id".length + 4);
  217.                                 room = room.split('"')[0];
  218.                                 var userp = bodyb.indexOf("robin_user_list");
  219.                                 var userlist = bodyb.substring(userp + "robin_user_list".length + 4);
  220.                                 userlist = "[" + userlist.split(']')[0] + "]";
  221.                                 var userlist = JSON.parse(userlist);
  222.                                 var users = [];
  223.                                 for (var c = 0; c < userlist.length; c++) {
  224.                                     users.push(userlist[c]["name"])
  225.                                 }
  226.                                 var date = new Date();
  227.                                 fs.writeFile("users-b/users-" + date.getUTCHours().toString() + "-" + date.getUTCMinutes().toString() + ".txt", "[" + users.toString() + "]", function(err) {
  228.                                     if (err) {
  229.                                         console.log("Error occured");
  230.                                     }
  231.  
  232.                                     //console.log("User file saved!");
  233.                                 });
  234.                             });
  235.                         }, 1000);
  236.                     });
  237.                 }, 1000);
  238.             }
  239.         });
  240.  
  241.         setInterval(function() {
  242.             vote("INCREASE");
  243.         }, 300000);
  244.  
  245.     });
  246. } catch (e) {
  247.     fs.writeFile("config.json", default_config, function(err) {
  248.         if (err) {
  249.             return console.log(err);
  250.         }
  251.  
  252.         console.log("The file was saved!");
  253.         console.log("Created initial config file at config.json, please edit it!")
  254.         process.exit();
  255.     });
  256. }
Add Comment
Please, Sign In to add comment