]> jfr.im git - erebus.git/blobdiff - modules/trivia.py
add nitterize module
[erebus.git] / modules / trivia.py
index 30ec6047060483c5f14bb4840b3a18eb27832452..b716a597672834719e26051a0aa5a2473a6daa38 100644 (file)
@@ -271,7 +271,7 @@ class TriviaState(object):
 
        def endPointVote(self):
                self.getchan().msg("Voting has ended!")
-               votelist = sorted(self.voteamounts.items(), key=lambda item: item[1]) #sort into list of tuples: [(option, number_of_votes), ...]
+               votelist = sorted(self.voteamounts.items(), key=lambda item: (item[1], -item[0])) #sort into list of tuples: [(option, number_of_votes), ...]
                for i in range(len(votelist)-1):
                        item = votelist[i]
                        self.getchan().msg("%s place: %s (%s votes)" % (len(votelist)-i, item[0], item[1]))
@@ -729,7 +729,7 @@ def findq(bot, user, chan, realtarget, *args):
                questions = state.questions
 
        pattern = re.escape(' '.join(args))
-       bot.msg(user, _findq(questions, pattern))
+       return _findq(questions, pattern)
 
 @lib.hook(glevel=1, needchan=False)
 @lib.help("[@<category>] <regex>", "finds a question (qid) given a regex")
@@ -742,17 +742,31 @@ def findqre(bot, user, chan, realtarget, *args):
        else:
                questions = state.questions
        pattern = ' '.join(args)
-       bot.msg(user, _findq(questions, pattern))
+       return _findq(questions, pattern)
 
-def _findq(questions, pattern):
+@lib.hook(glevel=1, needchan=False)
+@lib.help("[@<category>] <phrase>", "finds a question (qid) given a (partial) question or answer")
+@lib.argsGE(1)
+def findqa(bot, user, chan, realtarget, *args):
+       args = list(args)
+       if args[0].startswith("@"):
+               cat = args.pop(0)[1:].lower()
+               questions = state.db['questions'][cat]
+       else:
+               questions = state.questions
+       pattern = ' '.join(args)
+       return _findq(questions, pattern, True)
+
+def _findq(questions, pattern, check_answers=False):
        searcher = re.compile(pattern, re.IGNORECASE)
-       matches = [str(i) for i in range(len(questions)) if searcher.search(questions[i][0]) is not None]
+       matches = [i for i in range(len(questions)) if searcher.search(questions[i][0]) is not None or (check_answers and searcher.search(questions[i][1]) is not None)]
        if len(matches) > 25:
                return "Too many matches! (>25)"
        elif len(matches) > 1:
-               return "Multiple matches: %s" % (', '.join(matches))
+               return "Multiple matches: %s" % (', '.join(str(x) for x in matches))
        elif len(matches) == 1:
-               return "One match: %s" % (matches[0])
+               i = matches[0]
+               return "One match: %s %s*%s" % (i, questions[i][0], questions[i][1])
        else:
                return "No match."