Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
116
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, 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.             console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " did an invalid command! \"" + msg_data[3] + "\"");
  116.             bot.whisper(msg_data[2], "Invalid command! Do !help to see a list of commands.");
  117.             //that freaks people out ^^
  118.                 }
  119.             }
  120.             }
  121.             }
  122.             }
  123.             }
  124.             }
  125.             }
  126.       }
  127.       }
  128.       else if (msg.msg.match(regex_greenie)){
  129.           var msg_data_greenie = msg.msg.match(regex_greenie);
  130.         if (msg_data_greenie[2] == "!doge") {
  131.         console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  132.           bot.chat(msg_data_greenie[1] + " loves dank memes!");
  133.           //bot.whisper(msg_data[1], "much wow");
  134.         }
  135.         else {
  136.             if (msg_data_greenie[2] == "!time") {
  137.             console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  138.                 bot.whisper(msg_data_greenie[1], "The current time in Eastern Time is: " + dateFormat(new Date(), "%H:%M:%S"));
  139.             }
  140.             else{
  141.                 if (msg_data_greenie[2] == "!( ͡° ͜ʖ ͡°)") {
  142.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  143.                 bot.chat(msg_data_greenie[1] + ", ( ͡° ͜ʖ ͡°)");
  144.                 }
  145.         else{
  146.                     if (msg_data_greenie[2] == "!song") {
  147.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  148.                 bot.whisper(msg_data_greenie[1], "http://bit.ly/IqT6zt"); // rick rollllllllllll!
  149.                 }
  150.         else{
  151.                     if (msg_data_greenie[2] == "!grinders") {
  152.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  153.                 bot.whisper(msg_data_greenie[1], "Good public grinders: Sonnung, GeoSkye, RedCarsen, Chroniclez, and Dinjer.");
  154.                 }
  155.         else{
  156.                     if (msg_data_greenie[2] == "!help") {
  157.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  158.                 bot.whisper(msg_data_greenie[1], "You can do: !doge, !time, !( ͡° ͜ʖ ͡°), !song, !grinders, !shops, and !help");
  159.                 }
  160.         else{
  161.                     if (msg_data_greenie[2] == "!shops") {
  162.                 console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " executed command \""+ msg_data_greenie[2] + "\"");
  163.                 bot.whisper(msg_data_greenie[1], "Some shops: Sonnung, GeoSkye, Dinjer, SakuraWolf, xphstos37, CF69, and landry12345");
  164.                 }
  165.         else{
  166.             console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data_greenie[1] + " did an invalid command! \"" + msg_data_greenie[2] + "\"");
  167.             bot.whisper(msg_data_greenie[1], "Invalid command! Do !help to see a list of commands.");
  168.             //that freaks people out ^^
  169.             }
  170.             }
  171.             }
  172.             }
  173.             }
  174.             }
  175.       }
  176.       }
  177.       // write all msgs to a log file
  178.       //fs.appendFile(__dirname + '/msgs/msgs'+dateFormat(new Date(), "%H:%M:%S")+'.txt', msg.date+' - '+msg.msg+"\n", function (err) { });
  179.   }
  180. }
  181.  
  182. bot.on('message', handleMessage);
  183. bot.on('kicked', function(reason) {
  184.   console.log("I got kicked for " + reason);
  185. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement