This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrep.py
139 lines (111 loc) Β· 4.68 KB
/
rep.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
136
137
138
139
from sopel import module
from sopel.tools import Identifier
import time
import re
TIMEOUT = 36000
@module.rule('^(</?3)\s+([a-zA-Z0-9\[\]\\`_\^\{\|\}-]{1,32})\s*$')
@module.intent('ACTION')
@module.require_chanmsg("π you may only modify someone's social credits in a channel.")
def heart_cmd(bot, trigger):
luv_h8(bot, trigger, trigger.group(2), 'down' if '/' in trigger.group(1) else 'up')
@module.rule('.*?(?:([a-zA-Z0-9\[\]\\`_\^\{\|\}-]{1,32})(\+{2}|-{2})).*?')
@module.require_chanmsg("You may only modify someone's social credits in a channel.")
def karma_cmd(bot, trigger):
if re.match('^({prefix})({cmds})'.format(prefix=bot.config.core.prefix, cmds='|'.join(luv_h8_cmd.commands)),
trigger.group(0)):
return # avoid processing commands if people try to be tricky
for (nick, act) in re.findall('(?:([a-zA-Z0-9\[\]\\`_\^\{\|\}-]{1,32})(\+{2}|-{2}))', trigger.raw):
if luv_h8(bot, trigger, nick, 'up' if act == '++' else 'down', warn_nonexistent=False):
break
@module.commands('up', 'down')
@module.example(".up moop")
@module.example(".down Akundo69")
@module.require_chanmsg("You may only modify someone's social credits in a channel.")
def luv_h8_cmd(bot, trigger):
if not trigger.group(3):
bot.reply("π no user specified.")
return
target = Identifier(trigger.group(3))
luv_h8(bot, trigger, target, trigger.group(1))
def luv_h8(bot, trigger, target, which, warn_nonexistent=True):
target = verified_nick(bot, target, trigger.sender)
which = which.lower() # issue #18
pfx = change = selfreply = None # keep PyCharm & other linters happy
if not target:
if warn_nonexistent:
bot.reply("π you can only %s someone who is here." % which)
return False
if rep_too_soon(bot, trigger.nick):
return False
if which == 'up':
selfreply = "π no narcissism allowed!"
pfx, change = 'in', 1
if which == 'down':
selfreply = "π go elsewhere if you hate yourself!"
pfx, change = 'de', -1
if not (pfx and change and selfreply): # safeguard against leaving something in the above mass-None assignment
bot.say("π Logic error! Please report this to %s." % bot.config.core.owner)
return
if is_self(bot, trigger.nick, target):
bot.reply(selfreply)
return False
rep = mod_rep(bot, trigger.nick, target, change)
bot.say("π %s has %screased %s's social credits to %d" % (trigger.nick, pfx, target, rep))
return True
@module.commands('rep','credits')
@module.example(".rep Phixion")
def show_rep(bot, trigger):
target = trigger.group(3) or trigger.nick
rep = get_rep(bot, target)
if rep is None:
bot.say("π %s has no social credits yet." % target)
return
bot.say("π %s's current social credits: %d." % (target, rep))
# helpers
def get_rep(bot, target):
return bot.db.get_nick_value(Identifier(target), 'rep_score')
def set_rep(bot, caller, target, newrep):
bot.db.set_nick_value(Identifier(target), 'rep_score', newrep)
bot.db.set_nick_value(Identifier(caller), 'rep_used', time.time())
def mod_rep(bot, caller, target, change):
rep = get_rep(bot, target) or 0
rep += change
set_rep(bot, caller, target, rep)
return rep
def get_rep_used(bot, nick):
return bot.db.get_nick_value(Identifier(nick), 'rep_used') or 0
def set_rep_used(bot, nick):
bot.db.set_nick_value(Identifier(nick), 'rep_used', time.time())
def rep_used_since(bot, nick):
now = time.time()
last = get_rep_used(bot, nick)
return abs(last - now)
def rep_too_soon(bot, nick):
since = rep_used_since(bot, nick)
if since < TIMEOUT:
bot.notice("π you must wait %d more seconds before changing someone's social credits again." % (TIMEOUT - since), nick)
return True
else:
return False
def is_self(bot, nick, target):
nick = Identifier(nick)
target = Identifier(target)
if nick == target:
return True # shortcut to catch common goofballs
try:
nick_id = bot.db.get_nick_id(nick, False)
target_id = bot.db.get_nick_id(target, False)
except ValueError:
return False # if either nick doesn't have an ID, they can't be in a group
return nick_id == target_id
def verified_nick(bot, nick, channel):
nick = re.search('([a-zA-Z0-9\[\]\\`_\^\{\|\}-]{1,32})', nick).group(1)
if not nick:
return None
nick = Identifier(nick)
if nick.lower() not in bot.privileges[channel.lower()]:
if nick.endswith('--'):
if Identifier(nick[:-2]).lower() in bot.privileges[channel.lower()]:
return Identifier(nick[:-2])
return None
return nick