Skip to content

Commit af0f89f

Browse files
committed
Tweak a few things in karma
1 parent 9edf390 commit af0f89f

1 file changed

Lines changed: 32 additions & 18 deletions

File tree

plugins/karma.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1+
import time
2+
import re
3+
4+
from sqlalchemy import Table, Column, Integer, String, PrimaryKeyConstraint
5+
from sqlalchemy import select
6+
17
from cloudbot import hook
28
from cloudbot.util import timeformat, formatting, database
39

4-
import time
5-
import re
610

711
CAN_DOWNVOTE = False
12+
TIME_LIMIT = 300.0
813

9-
from sqlalchemy import Table, Column, Integer, String, PrimaryKeyConstraint
10-
from sqlalchemy import select
1114

1215
karma_table = Table(
1316
'karma',
1417
database.metadata,
1518
Column('nick_vote', String(25)),
1619
Column('up_karma', Integer, default=0),
1720
Column('down_karma', Integer, default=0),
21+
Column('total_karma', Integer, default=0), # used for querying
1822
PrimaryKeyConstraint('nick_vote')
1923
)
2024

@@ -29,7 +33,8 @@ def up(db, nick_vote):
2933
down_karma,
3034
total_karma) values(:nick,0,0,0)""", {'nick': nick_vote.lower()})
3135
query = karma_table.update().values(
32-
up_karma=karma_table.c.up_karma + 1
36+
up_karma=karma_table.c.up_karma + 1,
37+
total_karma=karma_table.c.total_karma + 1
3338
).where(karma_table.c.nick_vote == nick_vote.lower())
3439
db.execute(query)
3540
db.commit()
@@ -43,7 +48,8 @@ def down(db, nick_vote):
4348
down_karma,
4449
total_karma) values(:nick,0,0,0)""", {'nick': nick_vote.lower()})
4550
query = karma_table.update().values(
46-
down_karma=karma_table.c.down_karma + 1
51+
down_karma=karma_table.c.down_karma + 1,
52+
total_karma=karma_table.c.total_karma - 1
4753
).where(karma_table.c.nick_vote == nick_vote.lower())
4854
db.execute(query)
4955
db.commit()
@@ -52,20 +58,20 @@ def down(db, nick_vote):
5258
def allowed(uid):
5359
""" checks if a user is allowed to vote, and keeps track of voters """
5460
global voters
55-
time_restriction = 300.0
5661

5762
# clear expired voters
5863
for _uid, _timestamp in voters.items():
59-
if (time.time() - _timestamp) >= time_restriction:
64+
if (time.time() - _timestamp) >= TIME_LIMIT:
6065
del voters[_uid]
6166

6267
if uid in voters:
6368
last_voted = voters[uid]
64-
return False, timeformat.time_until(last_voted, now=time.time() - time_restriction)
69+
return False, timeformat.time_until(last_voted, now=time.time() - TIME_LIMIT)
6570
else:
6671
voters[uid] = time.time()
6772
return True, 0
6873

74+
6975
karma_re = re.compile('^([a-z0-9_\-\[\]\\^{}|`]{3,})(\+\+|\-\-)$', re.I)
7076

7177

@@ -93,22 +99,30 @@ def karma_add(match, nick, conn, db, notice):
9399

94100

95101
@hook.command('karma', 'k')
96-
def karma(text, chan, db):
102+
def karma(text, db):
97103
"""k/karma <nick> -- returns karma stats for <nick>"""
104+
query = db.execute(
105+
select([karma_table])
106+
.where(karma_table.c.nick_vote == text.lower())
107+
).fetchone()
98108

99-
if not chan.startswith('#'):
100-
return
109+
if not query:
110+
return "That user has no karma :("
111+
else:
112+
karma_text = formatting.pluralize(query['up_karma'] - query['down_karma'], '\x02karma')
113+
return "{} has \x02{}\x02 karma!".format(text, query['up_karma'] - query['down_karma'])
101114

102-
nick_vote = text
103115

116+
@hook.command('loved')
117+
def loved(text, db):
118+
"""loved -- Shows the users with the most karma!"""
104119
query = db.execute(
105120
select([karma_table])
106-
.where(karma_table.c.nick_vote == nick_vote.lower())
121+
.order_by(karma_table.c.total_karma.desc())
122+
.limit(5)
107123
).fetchall()
108124

109125
if not query:
110-
return "That user has no karma."
126+
return "??"
111127
else:
112-
query = query[0]
113-
karma_text = formatting.pluralize(query['up_karma'] - query['down_karma'], 'karma point')
114-
return "{} has {}.".format(nick_vote, karma_text)
128+
return query

0 commit comments

Comments
 (0)