Skip to content

Commit 791b60e

Browse files
authored
Fix accidentally weighting grabs by nick (CloudBotIRC#210)
* Fix accidentally weighting grabs by nick * Fix adding the wrong value as a grab * Refactor grabrandom logic * Fix order of nick and grab in grabrandom
1 parent a093fb8 commit 791b60e

1 file changed

Lines changed: 23 additions & 17 deletions

File tree

plugins/grab.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,26 +152,32 @@ def lastgrab(text, chan, message):
152152
def grabrandom(text, chan, message):
153153
"""[nick] - grabs a random quote from the grab database"""
154154
with cache_lock:
155-
if text:
156-
tokens = text.split(' ')
157-
if len(tokens) > 1:
158-
name = random.choice(tokens)
159-
else:
160-
name = tokens[0]
161-
else:
162-
try:
163-
name = random.choice(list(grab_cache[chan].keys()))
164-
except KeyError:
165-
return "I couldn't find any grabs in {}.".format(chan)
166155
try:
167-
grab = random.choice(grab_cache[chan][name.lower()])
156+
chan_grabs = grab_cache[chan]
168157
except KeyError:
169-
return "it appears {} has never been grabbed in {}".format(name, chan)
158+
return "I couldn't find any grabs in {}.".format(chan)
170159

171-
if grab:
172-
message(format_grab(name, grab), chan)
173-
else:
174-
return "Hmmm try grabbing a quote first."
160+
matching_quotes = []
161+
162+
if text:
163+
for nick in text.split():
164+
try:
165+
quotes = chan_grabs[nick.lower()]
166+
except LookupError:
167+
pass
168+
else:
169+
matching_quotes.extend((nick, quote) for quote in quotes)
170+
else:
171+
matching_quotes.extend(
172+
(name, quote) for name, quotes in chan_grabs.items() for quote in quotes
173+
)
174+
175+
if not matching_quotes:
176+
return "I couldn't find any grabs in {}.".format(chan)
177+
178+
name, quote_text = random.choice(matching_quotes)
179+
180+
message(format_grab(name, quote_text))
175181

176182

177183
@hook.command("grabsearch", "grabs", autohelp=False)

0 commit comments

Comments
 (0)