Skip to content

Instantly share code, notes, and snippets.

@anarcie
Last active June 9, 2017 20:07
Show Gist options
  • Save anarcie/b4b98d0d9ee7db88f9eaa2b998444a45 to your computer and use it in GitHub Desktop.
Save anarcie/b4b98d0d9ee7db88f9eaa2b998444a45 to your computer and use it in GitHub Desktop.
Duck Bot
var DuckBot = function(words, users){
//Settings
this.needles = words;
this.hayStacks = users;
this.rateLimit = 30000; // milliseconds
this.debug = true;
//Properties
this.rateLock = false;
this.write = kiwi.components.ControlInput();
this.listener = false;
this.channel = 'edmonton';
//Proper Scope to functions.
var _this = this;
//Setters
this.setNeedles = function(needles){this.needles = needles};
this.setHayStacks = function(haystacks){this.hayStacks = needles};
this.setRateLimit = function(rateLimit){this.rateLimit = rateLimit};
this.setPingComp = function(pingComp){this.pingComp = pingComp};
this.setChannel = function(channel){this.channel = channel};
this.setDebug = function(debug){this.debug = debug}
//Getters
this.getNeedles = function(){return this.needles};
this.getHayStacks = function(){return this.hayStacks};
this.getRateLimit = function(){return this.rateLimit};
this.getPingComp = function(){return this.pingComp};
this.getChannel = function(){return this.channel};
this.getDebug = function(){return this.debug};
//Methods
this.setRateLock = function(){
setTimeout(function(){ _this.rateLock = false; }, _this.rateLimit);
_this.rateLock = true;
};
this.Debug = function(event){
var Nick = this.parseNick(event);
var Mess = this.parseMessage(event);
var Tmpr = this.parseTmpr(event);
console.log('')
console.log('---------------------------------')
console.log('Nick: '+ this.parseNick(event));
console.log('Message: '+ this.parseMessage(event));
console.log('Nick Check: ' + (_this.hayStacks.indexOf(this.parseNick(event)) > -1).toString());
var finds = 0;
_this.needles.forEach(function(needle) {
if (Mess.indexOf(needle) > -1 && Tmpr.length == 0){
console.log('Message contains: ' + needle)
finds++;
}
});
if(finds == 0){ console.log('Message contains no matching strings'); };
console.log('Tamper Check: ' + this.parseTmpr(event).length)
console.log('Rate Locked: ' + _this.rateLock.toString());
console.log('')
}
this.parseNick = function(event){
return $(event.target).children('.nick').text().toLowerCase().replace('@','').trim();
}
this.parseMessage = function(event){
return $(event.target).children('.text').text().toLowerCase().trim();
}
this.parseTmpr = function(event){
return $(event.target).children('.text').children('.inline-nick');
}
this.ProcessMessage = function(event){
if(_this.debug) { _this.Debug(event); };
var Nick = this.parseNick(event);
var Mess = this.parseMessage(event);
var Tmpr = this.parseTmpr(event);
//Is the Right User
if (_this.hayStacks.indexOf(Nick) > -1 ){
//Has the Right Message
_this.needles.forEach(function(needle) {
if (Mess.indexOf(needle) > -1 && Tmpr.length == 0){
//Not Rate Locked
if(!_this.rateLock){
setTimeout(function(){
_this.write.run('/msg #' + _this.channel + ' .Bang a Rang');
}, 1000);
_this.setRateLock();
}
}
});
}
}
this.Start = function() {
_this.listener = $('.messages').on('DOMNodeInserted', 'div', function(e){_this.ProcessMessage(e);} );
}
this.Stop = function() {
_this.listener = $('.messages').off('DOMNodeInserted', 'div' );
}
}
//Params:
// Watch Words, in lowerCase
// Watch Users, in lowerCase
var Bot = new DuckBot(['quack', 'flap'], ['gonzobot']);
Bot.Start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment