This repository was archived by the owner on Apr 6, 2020. It is now read-only.
forked from CloudBotIRC/Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkarma.py
More file actions
158 lines (130 loc) · 4.99 KB
/
Copy pathkarma.py
File metadata and controls
158 lines (130 loc) · 4.99 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from cloudbot import hook
from cloudbot.util import timeformat, formatting, botvars
import time
import re
CAN_DOWNVOTE = True
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),
Column('total_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):
query = karma_table.update().values(
up_karma=karma_table.c.up_karma + 1,
total_karma=karma_table.c.total_karma + 1
).where(karma_table.c.nick_vote == nick_vote.lower())
db.execute(query)
db.commit()
def down(db, nick_vote):
query = karma_table.update().values(
down_karma=karma_table.c.down_karma + 1,
total_karma=karma_table.c.total_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():
return "You can't vote on yourself!"
return
if len(nick_vote) < 1 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) == '++':
db.execute("""INSERT or IGNORE INTO karma(
nick_vote,
up_karma,
down_karma,
total_karma) values(:nick,0,0,0)""", {'nick': nick_vote.lower()})
db.commit()
up(db, nick_vote)
return "Gave {} 1 karma!".format(nick_vote)
if match.group(2) == '--' and CAN_DOWNVOTE:
db.execute("""INSERT or IGNORE INTO karma(
nick_vote,
up_karma,
down_karma,
total_karma) values(:nick,0,0,0)""", {'nick': nick_vote.lower()})
db.commit()
down(db, nick_vote)
return "Took away 1 karma from {}.".format(nick_vote)
else:
return
else:
return "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> | list -- returns karma stats for <nick>. list returns top 5 karma."""
if not chan.startswith('#'):
return
nick_vote = text
if nick_vote != "list":
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)
else:
query = db.execute(
select([karma_table]).order_by(karma_table.c.total_karma.desc()).limit(5)
).fetchall()
if not query:
return "No karma registered yet."
else:
output = "Five users with most karma:"
for q in query:
total_karma = str(q['total_karma'])
nick_vote = str(q['nick_vote'])
output += "\n" + nick_vote + " has " + total_karma + " karma."
return output