Skip to content

Instantly share code, notes, and snippets.

@navarr
Last active December 12, 2015 10:29
Show Gist options
  • Save navarr/4759956 to your computer and use it in GitHub Desktop.
Save navarr/4759956 to your computer and use it in GitHub Desktop.
/**
* EnjinBot
*
* EnjinBot is a unique JavaScript object that allows for the instantiation
* and manipulation of a bot specifically for the Enjin chat system.
* This is not very useful, as the Enjin chat system has a very slow update
* time, and a very awkward update pattern. It is very likely for messages
* to get lost in the Enjin system, and even more likely to have response times
* upwards of 10 seconds or more due to Enjin's lack of update frequency.
*
* If you can't tell, I think Enjin is very poorly written for its wide popularity.
*
* This script is written for GOOGLE CHROME, but should work in any browser with
* localStorage support.
*
* @author Navarr T. Barnier <[email protected]>
*/
function EnjinBot()
{
// NAMESPACE - IMPORTANT FOR DATA PERSISTENCE
this.namespace = "bot_";
// RANK SYSTEM
this.rankMap = {};
this.rankValue = function (rank) {
return this.rankMap[rank.toLowerCase()];
};
this.rankCompare = function (rankA, rankB) {
rankA = this.rankMap[rankA.toLowerCase()];
rankB = this.rankMap[rankB.toLowerCase()];
if (rankA > rankB) return 1;
else if (rankA < rankB) return -1;
else return 0;
};
this.addRank = function (rank, val) {
this.rankMap[rank.toLowerCase()] = val;
};
/* COMMAND HOOK SYSTEM
*
* Command Hooks are utilized as such:
* bot.addCommandHook(token,callback)
* Token may not contain spaces. A command is triggered by the first word
* a user enters into chat. It is recommended to prefix commands with a '!' character
*
* Command hooks will NOT be preserved persistently, as they are always code.
*
* Callback should be a function, the following parameters will be passed to it (in order)
*
* user The user that triggered the command
* rank The rank of the user that triggered the command
* line The entire message sent (you may want to tokenize this by doing line.split(" ")
*/
this.commandHooks = {};
this.addCommandHook = function (token, callback) {
token = token.toLowerCase();
if(!this.commandHooks[token]) this.commandHooks[token] = [];
this.commandHooks[token].push(callback);
};
this.hasCommand = function (token) {
return !!this.commandHooks[token.toLowerCase()];
};
this.triggerCommandHook = function (token, user, rank, line) {
token = token.toLowerCase();
for (var i = 0,l = this.commandHooks[token].length;i < l;++i) {
this.commandHooks[token][i](user, rank, line);
};
};
// SEND MESSAGE SYSTEM
this.sendMessage = function(message) {
var e = $('#chatform .chat-input');
var t = e.val();
e.val(msg);
$('#chatform input[type=submit]').click();
e.val(t);
// Clear Variables?
e = t = null;
};
// BAN SYSTEM
this.updateBanList = function () {
localStorage[this.namespace + "banlist"] = JSON.stringify(this.banList);
};
this.getBanList = function () {
return this.banList;
};
this.addBan = function (user,userRank,by,byRank,reason) {
if (this.isBanned(user)) return true;
if (this.rankCompare(byRank,userRank) >= 0) {
this.banList[user.toLowerCase()] = {
by: by,
byRank: byRank,
reason: reason,
timestamp: new Date()
};
this.updateBanList();
return true;
}
};
this.isBanned = function (user) {
var banList = this.getBanList();
if (banList[user.toLowerCase()]) {
return true;
}
return false;
};
if (!localStorage[this.namespace + "banlist_instantiated"]) {
// If the banlist isn't setup properly, initiate a new one
localStorage[this.namespace+"banlist_instantiated"] = true;
this.banList = {};
this.updateBanList();
} else {
this.banList = JSON.parse(localStorage[this.namespace + "banlist"]);
};
};
// requires EnjinBot.js
// MineZ Bot
MineZBot = new EnjinBot();
// Rank definitions
MineZBot.addRank("Dev",1100);
MineZBot.addRank("JrDev",1090);
MineZBot.addRank("Staff",1080);
MineZBot.addRank("GFX",1070);
MineZBot.addRank("Bot",1060);
MineZBot.addRank("O",900);
MineZBot.addRank("E",800);
MineZBot.addRank("P",700);
MineZBot.addRank("",0);
// Commands
MineZBot.addCommandHook("!test", function(user, rank, line) {
console.log(user + " triggered " + line);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment