Skip to content

Instantly share code, notes, and snippets.

@CaptainMalMFC
Last active August 24, 2016 03:14
Show Gist options
  • Save CaptainMalMFC/eb062b3e4cd7f09a8386dec6fe3d7839 to your computer and use it in GitHub Desktop.
Save CaptainMalMFC/eb062b3e4cd7f09a8386dec6fe3d7839 to your computer and use it in GitHub Desktop.
Bot Prototype

MFC Chat Bot!! :D

Install

  1. Install latest node.js stable version (https://nodejs.org/en/)
  2. Install package dependencies by running each command in dependencies.sh
  3. Create a config file for yourself using the sample provided. DO NOT USE YOUR PASSWORD HERE.
  4. 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)
  5. Run the bot using env NODE_ENV=<Your config name> node bot.js
  6. On Windows, the command is set NODE_ENV=<Your config name> and then node bot.js
  7. Once the startup looks correct and the bot can connect to your room, set "live": true in the config and restart the bot
// 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();
npm install ZombieAlex/MFCAuto
npm install xmlhttprequest
npm install mathjs
npm install config
{
"botUsername": "Bot_Username",
"botHashedPassword": "HashedPasswordFromCookie",
"modelName": "ModelName",
"operatorName": "MrOperator",
"periodicEventInterval": 60,
"live": false,
"periodicResponses": [":robot","This is a periodic message"]
}
@CaptainMalMFC
Copy link
Author

Example taken from https://github.com/ZombieAlex/MFCAuto and modified to be a simple bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment