- Install latest node.js stable version (https://nodejs.org/en/)
- Install package dependencies by running each command in dependencies.sh
- Create a config file for yourself using the sample provided. DO NOT USE YOUR PASSWORD HERE.
- The correct value to use is the hashed password, instructions on how to find it are here: (https://github.com/ZombieAlex/MFCAuto/blob/master/src/main/Client.ts#L40)
- Run the bot using
env NODE_ENV=<Your config name> node bot.js
- On Windows, the command is
set NODE_ENV=<Your config name>
and thennode bot.js
- Once the startup looks correct and the bot can connect to your room, set
"live": true
in the config and restart the bot
-
-
Save CaptainMalMFC/eb062b3e4cd7f09a8386dec6fe3d7839 to your computer and use it in GitHub Desktop.
Bot Prototype
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TODO: exception handling | |
var mfc = require("MFCAuto"); | |
var events = require('events'); | |
var math = require('mathjs'); | |
var config = require('config'); | |
// Add a trim() function to all Strings | |
// From http://stackoverflow.com/questions/1418050/string-strip-for-javascript | |
if(typeof(String.prototype.trim) === "undefined") | |
{ | |
String.prototype.trim = function() | |
{ | |
return String(this).replace(/^\s+|\s+$/g, ''); | |
}; | |
} | |
var botUsername = config.get("botUsername"); | |
var botHashedPassword = config.get("botHashedPassword"); | |
var modelName = config.get("modelName"); | |
var operatorName = config.get("operatorName"); | |
var commandUserWhiteList = [modelName, operatorName]; | |
var periodicEventInterval = config.get("periodicEventInterval"); | |
var periodicResponses = config.get("periodicResponses"); | |
var live = config.get("live"); | |
var eventEmitter = new events.EventEmitter(); | |
var client = new mfc.Client(); | |
var modelId = 0; | |
var operatorId = 0; | |
var nextPeriodic = 0; | |
// EVENT PARSING FUNCTIONS | |
//Listen for chat messages and print them | |
client.on("CMESG", function(packet){ | |
if(packet.chatString !== undefined){ | |
var msgUser = packet.sMessage.nm; | |
var msgUID = packet.sMessage.uid; | |
var msgText = packet.pMessage; | |
var msg = {}; | |
msg.user = msgUser; | |
msg.uid = msgUID; | |
msg.text = msgText; | |
eventEmitter.emit("chatmsg", msg); | |
} | |
}); | |
//Listen for private messages and print them | |
client.on("PMESG", function(packet){ | |
if(packet.chatString !== undefined){ | |
var msgUser = packet.sMessage.nm; | |
var msgUID = packet.sMessage.uid; | |
var msgText = packet.pMessage; | |
var msg = {}; | |
msg.user = msgUser; | |
msg.uid = msgUID; | |
msg.text = msgText; | |
eventEmitter.emit("privmsg", msg); | |
} | |
}); | |
//Listen for tip messages and print them | |
client.on("TOKENINC", function(packet){ | |
if(packet.chatString !== undefined){ | |
var tok = packet.sMessage; | |
var msgUser = tok.u[2]; | |
var msgUID = tok.m[0]; | |
var msgText = packet.pMessage; | |
var tokens = tok.tokens; | |
var msg = {}; | |
msg.user = msgUser; | |
msg.uid = msgUID; | |
msg.text = msgText; | |
msg.tokens = tokens; | |
eventEmitter.emit("tip", msg); | |
} | |
}); | |
// EVENT HANDLERS | |
// Chat room message event handler | |
eventEmitter.on("chatmsg", function(msg) { | |
console.log("Chat Event!"); | |
console.log("--User: " + msg.user + "(" + msg.uid + ")"); | |
console.log("--Message: " + msg.text); | |
//loop through chat functions, find and execute matches | |
var response = undefined; | |
if(msg.user == operatorName && msg.text == "test"){ | |
response = "Hello, I am a Bot! Nice to meet you!"; | |
} | |
else if(commandUserWhiteList.indexOf(msg.user) != -1 && msg.text == "test"){ | |
response = "Hi " + msg.user + "! How are you today?"; | |
} | |
else if(commandUserWhiteList.indexOf(msg.user) != -1 && msg.text == "random"){ | |
response = "random number is " + Math.floor(Math.random() * 100); | |
} | |
else if(commandUserWhiteList.indexOf(msg.user) != -1 && msg.text.toLowerCase().indexOf("math") == 0){ | |
var expression = msg.text.toLowerCase().replace("math","").trim(); | |
try { | |
response = expression + " = " + math.eval(expression); | |
} catch(err) { | |
response = "I'm sorry, I don't know how to process " + expression; | |
} | |
} | |
else if(msg.user == operatorName && msg.text.includes("goodbye")){ | |
response = "Bye Everybody!"; | |
} | |
if (response !== undefined) { | |
console.log("Sending: " + response); | |
if (live) { | |
client.sendChat(modelId, response); | |
} | |
} | |
}); | |
// Private Message event handler | |
eventEmitter.on("privmsg", function(msg) { | |
console.log("Private Message Event!"); | |
console.log("--User: " + msg.user + "(" + msg.uid + ")"); | |
console.log("--Message: " + msg.text); | |
//loop through chat functions, find and execute matches | |
var response = undefined; | |
if(commandUserWhiteList.indexOf(msg.user) != -1 && msg.text.toLowerCase().indexOf("math") == 0){ | |
var expression = msg.text.toLowerCase().replace("math","").trim(); | |
try { | |
response = expression + " = " + math.eval(expression); | |
} catch(err) { | |
response = "I'm sorry, I don't know how to process " + expression; | |
} | |
} | |
if (response !== undefined) { | |
console.log("Sending: " + response); | |
client.sendPM(msg.uid, response); | |
} | |
}); | |
// Tip event handler | |
eventEmitter.on("tip", function(msg) { | |
console.log("Tip Event!"); | |
console.log("--User: " + msg.user + "(" + msg.uid + ")"); | |
console.log("--Message: " + msg.text); | |
console.log("--Amount: " + msg.tokens); | |
//loop through tip functions, find and execute matches | |
var response = undefined; | |
if (msg.tokens == "2") { | |
response = "2 tokens? Fuck yeah." | |
} | |
if (response !== undefined) { | |
console.log("Sending: " + response); | |
if (live) { | |
client.sendChat(modelId, response); | |
} | |
} | |
}); | |
// Periodic event handler | |
eventEmitter.on("periodic", function() { | |
console.log("Periodic Event!"); | |
//execute periodic functions | |
var response = undefined; | |
if (periodicResponses !== undefined && periodicResponses.length > 0) { | |
response = periodicResponses[nextPeriodic]; | |
nextPeriodic = (nextPeriodic + 1) % periodicResponses.length; | |
} | |
if (response !== undefined) { | |
console.log("Sending: " + response); | |
if (live) { | |
client.sendChat(modelId, response); | |
} | |
} | |
}); | |
// STARTUP FUNCTIONS | |
// After login, look up the UID for model and operator | |
client.on("LOGIN", function() { | |
setTimeout(function() { | |
console.log("Looking up UIDs..."); | |
client.queryUser(modelName); | |
client.queryUser(operatorName); | |
}, 500); | |
}); | |
// Join room and notify operator once we have the UIDs | |
client.on("USERNAMELOOKUP", function(packet){ | |
if (packet.sMessage.nm == modelName) { | |
modelId = packet.sMessage.uid; | |
console.log("Joining " + modelName + "'s room. (uid:" + modelId + ")"); | |
if (live) { | |
client.joinRoom(modelId); | |
} | |
setInterval(function() { | |
eventEmitter.emit("periodic"); | |
}, periodicEventInterval * 1000); | |
console.log("Joined."); | |
} else if (packet.sMessage.nm == operatorName) { | |
operatorId = packet.sMessage.uid; | |
client.sendPM(operatorId, ":robot Hello " + operatorName + "! You are the operator of " + botUsername + " in " + modelName + "'s Room!"); | |
} | |
}); | |
client.username = botUsername; | |
client.password = botHashedPassword; | |
client.connect(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm install ZombieAlex/MFCAuto | |
npm install xmlhttprequest | |
npm install mathjs | |
npm install config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"botUsername": "Bot_Username", | |
"botHashedPassword": "HashedPasswordFromCookie", | |
"modelName": "ModelName", | |
"operatorName": "MrOperator", | |
"periodicEventInterval": 60, | |
"live": false, | |
"periodicResponses": [":robot","This is a periodic message"] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example taken from https://github.com/ZombieAlex/MFCAuto and modified to be a simple bot