Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  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 reg_ex = /^\[\[([^\]]+)\] ([^\s]+) -> me\] (.*)/;
  67. if (msg.msg.match(reg_ex)) {
  68. var msg_data = msg.msg.match(reg_ex);
  69. if (msg_data[3].startsWith("!doge")) {
  70. console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \"!doge\"");
  71. bot.chat(msg_data[2] + " loves dank memes!");
  72. //bot.whisper(msg_data[2], "much wow");
  73. } else {
  74. if (msg_data[3].startsWith("!time")) {
  75. console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \"!time\"");
  76. bot.whisper(msg_data[2], "The current time in Eastern Time is: " + dateFormat(new Date(), "%H:%M:%S"));
  77. }
  78. else{
  79. console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " did an invalid command! \"" + msg_data[3] + "\"");
  80. //bot.whisper(msg_data[2], "Invalid command!");
  81. //that freaks people out ^^
  82. }
  83. }
  84. }
  85.  
  86. // write all msgs to a log file
  87. //fs.appendFile(__dirname + '/msgs/msgs'+dateFormat(new Date(), "%H:%M:%S")+'.txt', msg.date+' - '+msg.msg+"\n", function (err) { });
  88.  
  89.  
  90. bot.on('message', handleMessage);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement