Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var fs = require('fs');
  2. var mineflayer = require('mineflayer');
  3.  
  4. if(process.argv.length < 4 || process.argv.length > 6) {
  5.   console.log("Usage : node chatterbot.js <host> <port> [<name>] [<password>]");
  6.   process.exit(1);
  7. }
  8.  
  9. var bot = mineflayer.createBot({
  10.   host: process.argv[2],
  11.   port: parseInt(process.argv[3]),
  12.   username: process.argv[4] ? process.argv[4] : "chatterbox",
  13.   password: process.argv[5],
  14.   verbose: true,
  15. });
  16.  
  17. function dateFormat(date, fstr, utc) {
  18.         utc = utc ? 'getUTC' : 'get';
  19.         return fstr.replace (/%[YmdHMS]/g, function (m) {
  20.             switch (m) {
  21.             case '%Y': return date[utc + 'FullYear'] (); // no leading zeros required
  22.             case '%m': m = 1 + date[utc + 'Month'] (); break;
  23.             case '%d': m = date[utc + 'Date'] (); break;
  24.             case '%H': m = date[utc + 'Hours'] (); break;
  25.             case '%M': m = date[utc + 'Minutes'] (); break;
  26.             case '%S': m = date[utc + 'Seconds'] (); break;
  27.             default: return m.slice (1); // unknown code, remove %
  28.             }
  29.             // add leading zero if required
  30.             return ('0' + m).slice (-2);
  31.         });
  32.     }
  33.  
  34.   function handleMessage(jsonMsg) {
  35.       var msg;
  36.    
  37.       if (typeof jsonMsg.extra == "undefined") {
  38.         // this message wasn't in the usual skyblock/mc server format. This tends to be things like whisper commands
  39.         if ((typeof jsonMsg.text != "undefined") && (jsonMsg.text == "")) {
  40.           return;
  41.         }
  42.         msg = {
  43.            "msg": "std message, unparsed:"+JSON.stringify(jsonMsg),
  44.            "type": "unknown",
  45.            "date": utils.shortTime()
  46.         };
  47.         console.log(JSON.stringify(msg));
  48.       } else {
  49.         // skyblock messages, and I think all non-mc-server messages come in in this data structure        
  50.         var t = "";
  51.         for(var x=0;x<jsonMsg.extra.length;x++) {
  52.         if (typeof jsonMsg.extra[x] == "string") {
  53.           t+= jsonMsg.extra[x];
  54.         } else {
  55.           t+= jsonMsg.extra[x].text;
  56.         }
  57.       }
  58.         msg = {
  59.            "msg": t,
  60.            "type": "unknown",
  61.            "date": dateFormat(new Date(), "%H:%M:%S")
  62.         };
  63.        // show (all) the message to the screen
  64.        //console.log(t);
  65.        
  66.        var regex_rank = /^\[\[([^\]]+)\] ([^\s]+) -> me\] (.*)/;
  67.          var regex_greenie = /^\[([^\s]+) -> me\] (.*)/;
  68.       if (msg.msg.match(regex_rank)) {
  69.         var msg_data = msg.msg.match(regex_rank);
  70.         if (msg_data[3] == "!doge") {
  71.         console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \""+ msg_data[3] + "\"");
  72.           bot.chat(msg_data[2] + " loves dank memes!");
  73.           //bot.whisper(msg_data[2], "much wow");
  74.         } else {
  75.             if (msg_data[3] == "!time") {
  76.             console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \""+ msg_data[3] + "\"");
  77.                 bot.whisper(msg_data[2], "The current time in Eastern Time is: " + dateFormat(new Date(), "%H:%M:%S"));
  78.             }
  79.             else{
  80.                 if (msg_data[3] == "!( ͡° ͜ʖ ͡°)") {
  81.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \""+ msg_data[3] + "\"");
  82.                 bot.chat(msg_data[2] + ", ( ͡° ͜ʖ ͡°)");
  83.                 }
  84.         else{
  85.                     if (msg_data[3] == "!song") {
  86.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \""+ msg_data[3] + "\"");
  87.                 bot.whisper(msg_data[2], "http://bit.ly/IqT6zt"); // rick rollllllllllll!
  88.                 }
  89.         else{
  90.                     if (msg_data[3] == "!afk" && msg_data[2] == "skizz") {
  91.                     console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \""+ msg_data[3] + "\"");
  92.                     bot.chat("AFK");
  93.                 }
  94.         else{
  95.                     if (msg_data[3] == "!grinders") {
  96.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \""+ msg_data[3] + "\"");
  97.                 bot.whisper(msg_data[2], "Good public grinders: Sonnung, GeoSkye, RedCarsen, Chroniclez, and Dinjer.");
  98.                 }
  99.         else{
  100.                     if (msg_data[3] == "!help") {
  101.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \""+ msg_data[3] + "\"");
  102.                 bot.whisper(msg_data[2], "You can do: !doge, !time, !( ͡° ͜ʖ ͡°), !song, !grinders, !shops, !fanboy, and !help");
  103.                 }
  104.         else{
  105.                     if (msg_data[3] == "!brb" && msg_data[2] == "skizz") {
  106.                     console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \""+ msg_data[3] + "\"");
  107.                     bot.chat("BRB, restarting!");
  108.                 }
  109.         else{
  110.                     if (msg_data[3] == "!shops") {
  111.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \""+ msg_data[3] + "\"");
  112.                 bot.whisper(msg_data[2], "Some shops: Sonnung, GeoSkye, Dinjer, SakuraWolf, xphstos37, CF69, and landry12345");
  113.                 }
  114.         else{
  115.           if (msg_data[3] == "!fanboy") {
  116.         console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \""+ msg_data[3] + "\"");
  117.                 bot.chat("OMG " + msg_data[2] + " im ur biggest fan! will you sign my face?");
  118.         }
  119.         else{
  120.             console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " did an invalid command! \"" + msg_data[3] + "\"");
  121.             bot.whisper(msg_data[2], "Invalid command! Do !help to see a list of commands.");
  122.             //that freaks people out ^^
  123.                 }
  124.             }
  125.             }
  126.             }
  127.             }
  128.             }
  129.             }
  130.     }
  131.             }
  132.       }
  133.       }
  134.       else if (msg.msg.match(regex_greenie)){
  135.           var msg_data_greenie = msg.msg.match(regex_greenie);
  136.         if (msg_data_greenie[2] == "!doge") {
  137.         console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  138.           bot.chat(msg_data_greenie[1] + " loves dank memes!");
  139.           //bot.whisper(msg_data[1], "much wow");
  140.         }
  141.         else {
  142.             if (msg_data_greenie[2] == "!time") {
  143.             console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  144.                 bot.whisper(msg_data_greenie[1], "The current time in Eastern Time is: " + dateFormat(new Date(), "%H:%M:%S"));
  145.             }
  146.             else{
  147.                 if (msg_data_greenie[2] == "!( ͡° ͜ʖ ͡°)") {
  148.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  149.                 bot.chat(msg_data_greenie[1] + ", ( ͡° ͜ʖ ͡°)");
  150.                 }
  151.         else{
  152.                     if (msg_data_greenie[2] == "!song") {
  153.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  154.                 bot.whisper(msg_data_greenie[1], "http://bit.ly/IqT6zt"); // rick rollllllllllll!
  155.                 }
  156.         else{
  157.                     if (msg_data_greenie[2] == "!grinders") {
  158.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  159.                 bot.whisper(msg_data_greenie[1], "Good public grinders: Sonnung, GeoSkye, RedCarsen, Chroniclez, and Dinjer.");
  160.                 }
  161.         else{
  162.                     if (msg_data_greenie[2] == "!help") {
  163.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  164.                 bot.whisper(msg_data_greenie[1], "You can do: !doge, !time, !( ͡° ͜ʖ ͡°), !song, !grinders, !shops, !fanboy, and !help");
  165.                 }
  166.         else{
  167.                     if (msg_data_greenie[2] == "!shops") {
  168.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  169.                 bot.whisper(msg_data_greenie[1], "Some shops: Sonnung, GeoSkye, Dinjer, SakuraWolf, xphstos37, CF69, and landry12345");
  170.                 }
  171.  
  172.         else{
  173.           if (msg_data_greenie[2] == "!fanboy") {
  174.         console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  175.                 bot.chat("OMG " + msg_data_greenie[1] + " im ur biggest fan! will you sign my face?");
  176.         }
  177.         else{
  178.             console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " did an invalid command! \"" + msg_data_greenie[2] + "\"");
  179.             bot.whisper(msg_data_greenie[1], "Invalid command! Do !help to see a list of commands.");
  180.             //that freaks people out ^^
  181.             }
  182.             }
  183.             }
  184.             }
  185.             }
  186.     }
  187.             }
  188.       }
  189.       }
  190.       // write all msgs to a log file
  191.       //fs.appendFile(__dirname + '/msgs/msgs'+dateFormat(new Date(), "%H:%M:%S")+'.txt', msg.date+' - '+msg.msg+"\n", function (err) { });
  192.   }
  193. }
  194.  
  195. bot.on('message', handleMessage);
  196. bot.on('kicked', function(reason) {
  197.   console.log("I got kicked for " + reason);
  198. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement