Last active
August 29, 2015 13:56
-
-
Save donpdonp/8815732 to your computer and use it in GitHub Desktop.
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
function(payload){ | |
var key = 'tips' | |
if(payload.type == "emessage"){ | |
var keyword = /^\s*(\S+)\+\+\s*$/ | |
var match = keyword.exec(payload.message) | |
if(match){ | |
tip(payload.target, match[1]) | |
} | |
} | |
function tip(channel, username){ | |
var tip = get_tip(channel, username) | |
tip.magnitude += 1 | |
bot.say("#zrobo",tip.username+" now has "+tip.magnitude+" in tips.") | |
tip.last_tipped_at = new Date() | |
save_tip(tip) | |
} | |
function save_tip(tip){ | |
var tips = get_tips() | |
tips[tip.channel+":"+tip.username] = tip | |
save_tips(tips) | |
} | |
function get_tip(channel, username){ | |
var tips = get_tips() | |
var tip = tips[channel+":"+username] | |
if(tip) { | |
return tip | |
} else { | |
var new_tip = {username: username, last_tipped_at: null, | |
channel: channel, magnitude: 0} | |
return new_tip | |
} | |
} | |
function get_tips(){ | |
var tips = db.get(key) | |
if(tips) { return JSON.parse(tips) } else { return {} } | |
} | |
function save_tips(tips){ | |
db.set(key, JSON.stringify(tips)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment