Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var fs = require('fs');
- var mineflayer = require('mineflayer');
- if(process.argv.length < 4 || process.argv.length > 6) {
- console.log("Usage : node chatterbot.js <host> <port> [<name>] [<password>]");
- process.exit(1);
- }
- var bot = mineflayer.createBot({
- host: process.argv[2],
- port: parseInt(process.argv[3]),
- username: process.argv[4] ? process.argv[4] : "chatterbox",
- password: process.argv[5],
- verbose: true,
- });
- function dateFormat(date, fstr, utc) {
- utc = utc ? 'getUTC' : 'get';
- return fstr.replace (/%[YmdHMS]/g, function (m) {
- switch (m) {
- case '%Y': return date[utc + 'FullYear'] (); // no leading zeros required
- case '%m': m = 1 + date[utc + 'Month'] (); break;
- case '%d': m = date[utc + 'Date'] (); break;
- case '%H': m = date[utc + 'Hours'] (); break;
- case '%M': m = date[utc + 'Minutes'] (); break;
- case '%S': m = date[utc + 'Seconds'] (); break;
- default: return m.slice (1); // unknown code, remove %
- }
- // add leading zero if required
- return ('0' + m).slice (-2);
- });
- }
- function handleMessage(jsonMsg) {
- var msg;
- if (typeof jsonMsg.extra == "undefined") {
- // this message wasn't in the usual skyblock/mc server format. This tends to be things like whisper commands
- if ((typeof jsonMsg.text != "undefined") && (jsonMsg.text == "")) {
- return;
- }
- msg = {
- "msg": "std message, unparsed:"+JSON.stringify(jsonMsg),
- "type": "unknown",
- "date": utils.shortTime()
- }
- console.log(JSON.stringify(msg));
- } else {
- // skyblock messages, and I think all non-mc-server messages come in in this data structure
- var t = "";
- for(var x=0;x<jsonMsg.extra.length;x++) {
- if (typeof jsonMsg.extra[x] == "string") {
- t+= jsonMsg.extra[x];
- } else {
- t+= jsonMsg.extra[x].text;
- }
- }
- msg = {
- "msg": t,
- "type": "unknown",
- "date": dateFormat(new Date(), "%H:%M:%S")
- }
- // show (all) the message to the screen
- //console.log(t);
- var reg_ex = /^\[\[([^\]]+)\] ([^\s]+) -> me\] (.*)/;
- if (msg.msg.match(reg_ex)) {
- var msg_data = msg.msg.match(reg_ex);
- if (msg_data[3].startsWith("!doge")) {
- console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \"!doge\"");
- bot.chat(msg_data[2] + " loves dank memes!");
- //bot.whisper(msg_data[2], "much wow");
- } else {
- if (msg_data[3].startsWith("!time")) {
- console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " executed command \"!time\"");
- bot.whisper(msg_data[2], "The current time in Eastern Time is: " + dateFormat(new Date(), "%H:%M:%S"));
- }
- else{
- console.log("At " + dateFormat(new Date(), "%H:%M:%S") + ", " + msg_data[2] + " did an invalid command! \"" + msg_data[3] + "\"");
- //bot.whisper(msg_data[2], "Invalid command!");
- //that freaks people out ^^
- }
- }
- }
- // write all msgs to a log file
- //fs.appendFile(__dirname + '/msgs/msgs'+dateFormat(new Date(), "%H:%M:%S")+'.txt', msg.date+' - '+msg.msg+"\n", function (err) { });
- }
- bot.on('message', handleMessage);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement