-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkarma.py
89 lines (71 loc) · 2.65 KB
/
karma.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from errbot import BotPlugin, botcmd, re_botcmd
KARMA_INC_REGEX = '^([a-zA-Z0-9_]+)((:?\+)+)$'
KARMA_DEC_REGEX = '^([a-zA-Z0-9_]+)((:?\-)+)$'
class Karma(BotPlugin):
def activate(self):
super(Karma, self).activate()
try:
if type(self['karma']) is not dict:
self['karma'] = {}
except KeyError:
self['karma'] = {}
def update_karma(self, msg, match, increment):
name = match.group(1)
update = match.group(2)
nick = msg.frm.nick
karmas = self['karma']
if name == nick:
return 'Not in this universe, maggot!'
else:
if name not in karmas:
karmas[name] = 0
if increment:
karmas[name] += len(update)//2
else:
karmas[name] -= len(update)//2
self['karma'] = karmas
@re_botcmd(pattern=KARMA_INC_REGEX, prefixed=False)
def karma_inc(self, msg, match):
"""Increment user's karma (e.g. user++)"""
return self.update_karma(msg, match, True)
@re_botcmd(pattern=KARMA_DEC_REGEX, prefixed=False)
def karma_dec(self, msg, match):
"""Decrement user's karma (e.g. user--)"""
return self.update_karma(msg, match, False)
@botcmd
def karma(self, msg, args):
"""Get karma either of specific user or command caller"""
karmas = self['karma']
if not len(args):
user = msg.frm.nick
else:
user = args
if user not in karmas:
karmas[user] = 0
self['karma'] = karmas
return "{0}'s karma level is: {1}".format(user, karmas[user])
def number_suffix(self, num):
suffixes = {1: 'st', 2: 'nd', 3: 'rd'}
i = num if (num < 20) else (num % 10)
return suffixes.get(i, 'th')
@botcmd
def top_karma(self, msg, args):
"""Get n people with most karma points. If argument not
specified, get 5 people with most karma points."""
karmas = self['karma']
output = ''
karmees = sorted([(value, key) for (key, value) in karmas.items()],
reverse=True)
n = 5
if len(args) > 0:
# Takes top 'n' or less if len(karmees) < n
try:
n = int(args)
except ValueError:
return "Argument must be a number!"
karmees = karmees[:n]
for pos, (k, v) in enumerate(karmees, start=1):
output += '{0}{1} {2} with {3}\n'.format(pos,
self.number_suffix(pos),
v, k)
return output