README
Karma lets your team upvote and downvote everything. This way you can keep track of what really matters — internet points for everything.
Give things Karma by saying word++ and remove it by saying word–. With Pattern support, you don’t have to mention Abbot at all!
Add these two Regular Expression Patterns after installing the skill:
Upvotes:
(\S+[^+:\s])[: ]*++(\s|$)
Downvotes:
(\S+[^-:\s])[: ]*–(\s|$)
What will they think of next?
Usage
Karma works with a Pattern. Give something points by saying term++
, and remove them with term--
.
This was inspired by Hubot’s Karma script: https://github.com/github/hubot-scripts/blob/master/src/scripts/karma.coffee
Code
karmas = bot.brain.get(‘karma’)
if karmas is None:
karmas = {}
increment_responses = [
“+1!”, “gained a level!”, “is on the rise!”, “leveled up!”, “is going up. Nice!”
]
decrement_responses = [
“took a hit! Ouch.”, “took a dive.”, “lost a life.”, “lost a level.”, “got worse. dang.”
]
def upsert_term(term, score):
“””Upsert a term’s score. Create one if it doesn’t exist”””
item = karmas.get(term)
if item is None:
karmas[term] = score
else:
karmas[term] = karmas[term] + score
bot.brain.write(‘karma’, karmas)
def the_best():
“””Report the top 5 items”””
if len(karmas) == 0:
bot.reply(“There isn’t anything to report yet…”)
return
items = sorted(karmas.items(), key=lambda x: x[1], reverse=True)
output = “”
counter = 0
for item in items:
output = output + “\n * {}: {} point(s)”.format(item[0], item[1])
counter = counter + 1
if counter == 5: break
bot.reply(“Simply the best: ” + output)
def the_worst():
“””Report the worst 5 items”””
if len(karmas) == 0:
bot.reply(“There isn’t anything to report yet…”)
return
items = sorted(karmas.items(), key=lambda x: x[1])
output = “”
for item in items:
output = output + “\n * {}: {} point(s)”.format(item[0], item[1])
counter = counter + 1
if counter == 5: break
bot.reply(“Simply the WORST: ” + output)
if bot.is_pattern_match:
pos = re.match(r'(\S+[^+:\s])[: ]*\+\+(\s|$)’, bot.arguments)
if pos:
term = pos.groups()[0]
upsert_term(term, 1)
bot.reply(term + ” ” + random.choice(increment_responses))
neg = re.match(r'(\S+[^+:\s])[: ]*\-\-(\s|$)’,bot.arguments)
if neg:
term = neg.groups()[0]
upsert_term(term, -1)
bot.reply(term + ” ” + random.choice(decrement_responses))
elif bot.arguments == “best”:
the_best()
elif bot.arguments == “worst”:
the_worst()
elif bot.is_pattern_match:
bot.reply(bot.arguments)
elif bot.arguments == “clear”:
bot.brain.write(“karma”, None)
bot.reply(“All karma has been reset. It’s a fresh start!”)
elif len(bot.arguments) == 0:
the_best()
else:
bot.reply(“I don’t know what you meant by that. Are you sure that’s a karma command?”)