-
Notifications
You must be signed in to change notification settings - Fork 2
/
karma.py
135 lines (111 loc) · 4.07 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from cloudbot import hook
from cloudbot.util import timeformat, formatting, botvars
import time
import re
CAN_DOWNVOTE = False
from sqlalchemy import Table, Column, Integer, Float, String, PrimaryKeyConstraint
from sqlalchemy import select
karma_table = Table(
'karma',
botvars.metadata,
Column('nick_vote', String),
Column('up_karma', Integer, default=0),
Column('down_karma', Integer, default=0),
PrimaryKeyConstraint('nick_vote')
)
voter_table = Table(
'karma_voters',
botvars.metadata,
Column('voter', String),
Column('votee', String),
Column('timestamp', Float),
PrimaryKeyConstraint('voter', 'votee')
)
def up(db, nick_vote):
db.execute("""INSERT or IGNORE INTO karma(
nick_vote,
up_karma,
down_karma,
total_karma) values(:nick,0,0,0)""", {'nick': nick_vote.lower()})
query = karma_table.update().values(
up_karma=karma_table.c.up_karma + 1
).where(karma_table.c.nick_vote == nick_vote.lower())
db.execute(query)
db.commit()
def down(db, nick_vote):
db.execute("""INSERT or IGNORE INTO karma(
nick_vote,
up_karma,
down_karma,
total_karma) values(:nick,0,0,0)""", {'nick': nick_vote.lower()})
query = karma_table.update().values(
down_karma=karma_table.c.down_karma + 1
).where(karma_table.c.nick_vote == nick_vote.lower())
db.execute(query)
db.commit()
def allowed(db, nick, nick_vote):
time_restriction = 3600.0
db.execute(voter_table.delete().where((time.time() - voter_table.c.timestamp) >= time_restriction))
db.commit()
check = db.execute(
select([voter_table.c.timestamp])
.where(voter_table.c.voter == nick.lower())
.where(voter_table.c.votee == nick_vote.lower())
).scalar()
if check:
print(check)
if time.time() - check >= time_restriction:
db.execute("""INSERT OR REPLACE INTO karma_voters(
voter,
votee,
timestamp) values(:voter, :votee, :timestamp)""",
{'voter': nick.lower(), 'votee': nick_vote.lower(), 'timestamp': time.time()})
db.commit()
return True, 0
else:
return False, timeformat.time_until(check, now=time.time() - time_restriction)
else:
db.execute("""INSERT OR REPLACE INTO karma_voters(
voter,
votee,
timestamp) values(:voter, :votee, :timestamp)""",
{'voter': nick.lower(), 'votee': nick_vote.lower(), 'timestamp': time.time()})
db.commit()
return True, 0
karma_re = re.compile('^([a-z0-9_\-\[\]\\^{}|`]+)(\+\+|\-\-)$', re.I)
@hook.regex(karma_re)
def karma_add(match, nick, db, notice):
nick_vote = match.group(1).strip().replace("+", "")
if nick.lower() == nick_vote.lower():
notice("You can't vote on yourself!")
return
if len(nick_vote) < 3 or " " in nick_vote:
return # ignore anything below 3 chars in length or with spaces
vote_allowed, when = allowed(db, nick, nick_vote)
if vote_allowed:
if match.group(2) == '++':
up(db, nick_vote)
notice("Gave {} 1 karma!".format(nick_vote))
if match.group(2) == '--' and CAN_DOWNVOTE:
down(db, nick_vote)
notice("Took away 1 karma from {}.".format(nick_vote))
else:
return
else:
notice("You are trying to vote too often. You can vote on this user again in {}!".format(when))
@hook.command('karma', 'k')
def karma(text, chan, db):
"""k/karma <nick> -- returns karma stats for <nick>"""
if not chan.startswith('#'):
return
nick_vote = text
query = db.execute(
select([karma_table])
.where(karma_table.c.nick_vote == nick_vote.lower())
).fetchall()
if not query:
return "That user has no karma."
else:
query = query[0]
karma_text = formatting.pluralize(query['up_karma'] - query['down_karma'], 'karma point')
return "{} has {}.".format(nick_vote, karma_text)