Created
December 3, 2010 03:38
-
-
Save justinabrahms/726553 to your computer and use it in GitHub Desktop.
bot to handle votes for pycon selection.
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
// pycon talk selection irc bot. | |
var irc = require('../../node-irc/lib/irc.js'); | |
var sys = require('sys'); | |
var imb0t = function () { | |
return { | |
pattern_list: [], | |
client: undefined, | |
position: -1, // starts at -1 so we can increment it on the first ,next to make it 0, which properly indexes | |
connectClient: function (server, nick, chan_list) { | |
pattern_list = this.pattern_list; | |
this.nick = nick; | |
var client = new irc.Client(server, nick, {channels: chan_list}); | |
this.client = client; | |
this.client.addListener( | |
'message', | |
function (from, to, message) { | |
for(var i = 0; i < pattern_list.length; i++) { | |
var matches = message.match(pattern_list[i].rxp); | |
if (matches) { | |
pattern_list[i].func(client, from, to, message, matches); | |
} | |
} | |
}); | |
}, | |
register: function (_rgxp, _func) { | |
this.pattern_list.push({rxp:_rgxp, func:_func, 'bot':this}); | |
}, | |
karma: {}, | |
talks: [ | |
{'number': 70, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 189,'yay': [],'nay': [],'abstain': []}, | |
{'number': 36, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 42, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 94, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 76, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 132,'yay': [],'nay': [],'abstain': []}, | |
{'number': 59, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 190,'yay': [],'nay': [],'abstain': []}, | |
{'number': 155,'yay': [],'nay': [],'abstain': []}, | |
{'number': 240,'yay': [],'nay': [],'abstain': []}, | |
{'number': 232,'yay': [],'nay': [],'abstain': []}, | |
{'number': 105,'yay': [],'nay': [],'abstain': []}, | |
{'number': 20, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 210,'yay': [],'nay': [],'abstain': []}, | |
{'number': 253,'yay': [],'nay': [],'abstain': []}, | |
{'number': 45, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 201,'yay': [],'nay': [],'abstain': []}, | |
{'number': 103,'yay': [],'nay': [],'abstain': []}, | |
{'number': 86, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 73, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 212,'yay': [],'nay': [],'abstain': []}, | |
{'number': 48, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 40, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 104,'yay': [],'nay': [],'abstain': []}, | |
{'number': 140,'yay': [],'nay': [],'abstain': []}, | |
{'number': 67, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 93, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 130,'yay': [],'nay': [],'abstain': []}, | |
{'number': 134,'yay': [],'nay': [],'abstain': []}, | |
{'number': 125,'yay': [],'nay': [],'abstain': []}, | |
{'number': 182,'yay': [],'nay': [],'abstain': []}, | |
{'number': 206,'yay': [],'nay': [],'abstain': []}, | |
{'number': 89, 'yay': [],'nay': [],'abstain': []}, | |
{'number': 174,'yay': [],'nay': [],'abstain': []}, | |
{'number': 137,'yay': [],'nay': [],'abstain': []}, | |
{'number': 75, 'yay': [],'nay': [],'abstain': []} | |
], | |
getCurrentTalk: function () { return this.talks[this.position]; }, | |
vote: function (who, way) { | |
nonways = { // probably could so something clever here, but it's late. | |
'nay': ['yay', 'abstain'], | |
'yay': ['nay', 'abstain'], | |
'abstain': ['yay', 'nay'] | |
} | |
if (this.counting) { | |
current = this.talks[this.position]; | |
if (current[way].indexOf(who) == -1) { // hasn't already voted this | |
for (var i = 0; i < nonways[way].length; i++) { | |
if ((_index = current[nonways[way][i]].indexOf(who)) !== -1) { | |
current[nonways[way][i]].splice(_index, 1); // remove from yays | |
} | |
} | |
current[way].push(who); | |
} | |
} | |
} | |
} | |
}; | |
exports.imbot = imb0t; | |
if (module.parent === undefined) { | |
var bot = new imb0t; | |
bot.connectClient('irc.freenode.net', 'pyc0n', ['#pycon-pc']); | |
bot.register(/(.*)/, function no_private_msg(client, from, to, msg, matches) { | |
if (to == this.nick) { | |
client.say(from, "I don't speak privately."); | |
} | |
}); | |
bot.register(/(.*)/, function echo(client, from, to, msg, matches) { | |
console.log(from + " => " + to + ": " + msg); | |
}); | |
bot.register(/,listening/, function am_listening(client, from, to, msg, matches) { | |
console.log(client.chans[to].users[from]) | |
}) | |
bot.register(/,next/, function pycon_thing(client, from, to, msg, matches) { | |
if (client.chans[to].users[from] !== '@') | |
return; | |
bot.position += 1; | |
if (bot.counting) { bot.counting = false; } | |
var talk_num = bot.talks[bot.position]['number']; | |
var has_next = false; | |
if (bot.talks.length > bot.position+1) { // +1 is to account for 0 offset | |
var next_talk = bot.talks[bot.position+1]['number']; | |
client.say(to, "==== Talk #"+talk_num+" is on deck. #" + next_talk + " is next in queue."); | |
} else { | |
client.say(to, "==== Talk #"+talk_num+" is on deck. THIS IS THE LAST ONE!"); | |
} | |
client.say(to, "==== Talk #"+talk_num+" http://us.pycon.org/2011/review/"+talk_num+"/ is currently beign discussed."); | |
client.say(to, "If you are (a/the) champion for this talk, or willing to champion the talk, please type a succinct argument for inclusion of this talk. (2 Minutes). State when you are done."); | |
}); | |
bot.register(/,debate/, function debate(client, from, to, msg, matches) { | |
if (client.chans[to].users[from] !== '@') | |
return; | |
client.say(to, "==== General Debate (3 minutes) for Talk: #"+bot.talks[bot.position]['number']); | |
}); | |
bot.register(/nay.*/i, function nay_vote_count(client, from, to, msg, matches) { | |
this.bot.vote(from, 'nay'); | |
}); | |
bot.register(/abstain.*/i, function abstain_vote_count(client, from, to, msg, matches) { | |
this.bot.vote(from, 'abstain'); | |
}); | |
bot.register(/yay.*/i, function yay_vote_count(client, from, to, msg, matches) { | |
this.bot.vote(from, 'yay'); | |
}); | |
bot.register(/,report/, function report(client, from, to, msg, matches) { | |
if (client.chans[to].users[from] !== '@') | |
return; | |
if (bot.counting) { | |
client.say(to, "==== Vote Tally for talk #"+bot.talks[bot.position]['number']+": "+bot.getCurrentTalk()['yay'].length + " yay votes, " + bot.getCurrentTalk()['nay'].length + " nay votes and " + bot.getCurrentTalk()['abstain'].length + " abstainers."); | |
bot.counting = false; | |
} | |
}); | |
bot.register(/,vote/, function vote(client, from, to, msg, matches) { | |
if (client.chans[to].users[from] !== '@') | |
return; | |
if (!bot.counting) { | |
client.say(to, "==== Voting Time! - yay/nay votes for Talk #"+bot.talks[bot.position]['number']); | |
bot.counting = true; | |
} | |
}); | |
bot.register(/,final-report/, function final_report(client, from, to, msg, matches) { | |
if (client.chans[to].users[from] !== '@') | |
return; | |
for (var i = 0; i < bot.talks.length; i++) { | |
client.say(to, "#"+bot.talks[i]['number']+": "+bot.talks[i]['yay'].length+" for, "+bot.talks[i]['nay'].length+" against, "+bot.talks[i]['abstain'].length+" abstainers"); | |
} | |
}); | |
bot.register(/,state-dump/, function dump(client, from, to, msg, matches) { | |
if (from !== 'justinlilly') | |
return; | |
console.log(bot); | |
}); | |
bot.register(/,pester/, function pester(client, from, to, msg, matches) { | |
if (client.chans[to].users[from] !== '@') | |
return; | |
non_votes = []; | |
current = bot.talks[bot.position]; | |
for (var user in bot.client.chans[to].users) { | |
if (user === this.nick) | |
continue; | |
if (current['yay'].indexOf(user) == -1) { | |
if(current['nay'].indexOf(user) == -1) { | |
if (current['abstain'].indexOf(user) == -1) { | |
non_votes.push(user); | |
} | |
} | |
} | |
} | |
if (non_votes.length) { | |
client.say(to, "Didn't vote: " + non_votes.join(', ')); | |
} | |
}); | |
bot.register(/,rules/, function rules(client, from, to, msg, matches) { | |
client.say(to, "Meeting rules can be found here: http://goo.gl/2E9US"); | |
}); | |
bot.register(/\s(.*)\+\+/, function karma(client, from, to, msg, matches) { | |
if (from == matches[1]) { | |
client.say(to, from + ": don't be a dick. Karma reset."); | |
this.bot.karma[from] = 0; | |
return; | |
} | |
if (this.bot.karma.indexOf(matches[1]) === -1) | |
this.bot.karma[matches[1]] = 1; | |
else | |
this.bot.karma[matches[1]] += 1; | |
}); | |
bot.register(/,karma(.*)/, function karma_list(client, from, to, msg, matches) { | |
console.log(this.bot.karma); | |
console.log(matches); | |
var person = matches[1].trim(); | |
console.log('person: '+person); | |
if (person === "") | |
var user = from; | |
else | |
var user = matches[1]; | |
client.say(to, from + " has " + this.bot.karma[from] + " karma."); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment