Last active
January 22, 2016 19:59
-
-
Save jamiew/323a4d38a941fca91fe3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Description: | |
# It's all about the points. jamie++ | |
# | |
# Commands: | |
# @username++ - Add a karma point for said user | |
# hubot leaderboard - Show karma points for all users. Also understands "scoreboard" | |
# | |
module.exports = (robot) -> | |
robot.brain.data.karma ?= {} | |
# listen for @username++ or @username-- (and now also .username++/--) | |
robot.hear /(@)?[a-z0-9]*\s?(?:[\+]{2}|[\-]{2})/i, (msg) -> | |
# store @username(++|--), convert to string and normalize | |
user = msg.match[0].toLowerCase() | |
# store @username w/o increment | |
username = user.replace(/\s?(?:[\+]{2}|[\-]{2})/, '') | |
# strip @ symbol | |
username = username.replace(/^@/,'') | |
# store discretely for each month | |
date = new Date() | |
date_key = date.getFullYear() + '-' + date.getMonth() | |
username_key = username + '_' + date_key | |
# store increment value | |
increment = parseInt(user[user.length-1] + 1) | |
robot.brain.data.karma[username_key] ?= 0 | |
score = robot.brain.data.karma[username_key] += increment | |
# don't let losers vote for themselves | |
if username == msg.message.user.mention_name || username == msg.message.user.name | |
response = "Only losers vote for themselves" | |
else | |
# month_name = date.toLocaleString('en-us', { month: "long" }) | |
month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] | |
#month_name = month_names[d.getMonth()] | |
month_name = month_names[date.getMonth()] | |
response = "I've updated #{username} to #{score} for #{month_name}!" | |
msg.send response | |
# leaderboard, output as /code so we don't notify everyone everytime | |
robot.respond /(leaderboard|scoreboard)/i, (msg) -> | |
list = '' | |
counter = 0 | |
for user of robot.brain.data.karma | |
list_item = '' | |
sanitized_user = user.replace(/^@/,'') | |
if counter < Object.keys(robot.brain.data.karma).length - 1 | |
list_item = "#{sanitized_user}: #{robot.brain.data.karma[user]}\n" | |
else | |
list_item = "#{sanitized_user}: #{robot.brain.data.karma[user]}" | |
counter++ | |
list = list + list_item | |
msg.send "Karma:\n#{list}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment