diff --git a/plugins/QuoteGrabs.py b/plugins/QuoteGrabs.py index 0c2225de9..81e4c39cf 100644 --- a/plugins/QuoteGrabs.py +++ b/plugins/QuoteGrabs.py @@ -209,6 +209,33 @@ class QuoteGrabs(plugins.ChannelDBHandler, l.append(item_str) irc.reply(msg, utils.commaAndify(l)) + def randomquote(self, irc, msg, args): + """[] + + Returns a randomly grabbed quote, optionally choosing only from those + quotes grabbed for . + """ + channel = privmsgs.getChannel(msg, args) + nick = privmsgs.getArgs(args, required=0, optional=1) + db = self.getDb(channel) + cursor = db.cursor() + if nick: + cursor.execute("""SELECT quote FROM quotegrabs + WHERE nick LIKE %s ORDER BY random() LIMIT 1""", + nick) + else: + cursor.execute("""SELECT quote FROM quotegrabs + ORDER BY random() LIMIT 1""") + if cursor.rowcount == 0: + if nick: + irc.error(msg, 'Couldn\'t get a random quote for that nick.') + else: + irc.error(msg, 'Couldn\'t get a random quote. Are there any' + 'grabbed quotes in the database?') + return + quote = cursor.fetchone()[0] + irc.reply(msg, quote) + def get(self, irc, msg, args): """ diff --git a/test/test_QuoteGrabs.py b/test/test_QuoteGrabs.py index 3b56a2553..eb2e6498a 100644 --- a/test/test_QuoteGrabs.py +++ b/test/test_QuoteGrabs.py @@ -91,6 +91,17 @@ if sqlite: self.assertNotError('quote FoO') self.assertNotError('quote Foo') + def testRandomquote(self): + testPrefix = 'foo!bar@baz' + self.assertError('randomquote') + self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'test', + prefix=testPrefix)) + self.assertError('randomquote') # still none in the db + self.assertNotError('grab foo') + self.assertResponse('randomquote', ' test') + self.assertResponse('randomquote foo', ' test') + self.assertResponse('randomquote FOO', ' test') + def testGet(self): testPrefix= 'foo!bar@baz' self.assertError('quotegrabs get asdf')