Awesome IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.2 KiB

#!/usr/bin/env python
.. module:: RandQuote
:synopsis: Log a configurable number of messages and pull up random ones on command
.. moduleauthor:: Dave Pedu <>
from pyircbot.modulebase import ModuleBase, hook, command
from datetime import datetime
from pyircbot.modules.ModInfo import info
class RandQuote(ModuleBase):
def __init__(self, bot, moduleName):
ModuleBase.__init__(self, bot, moduleName)
self.db = None
serviceProviders ="sqlite")
if not serviceProviders:
self.log.error("RandQuote: Could not find a valid sqlite service provider")
else:"RandQuote: Selecting sqlite service provider: %s" % serviceProviders[0])
self.db = serviceProviders[0].opendb("randquote.db")
if not self.db.tableExists("chat"):"RandQuote: Creating table: chat")
c = self.db.query("""CREATE TABLE IF NOT EXISTS `chat` (
`date` INTEGER,
`sender` varchar(64),
`message` varchar(2048)
) ;""")
@info("randquote print a random quote", cmds=["randquote", "randomquote", "rq"])
@command("randquote", "randomquote", "rq")
def fetchquotes(self, msg, cmd):
c = self.db.query("SELECT * FROM `chat` ORDER BY RANDOM() LIMIT 1;")
row = c.fetchone()
if row:[0], "<%s> %s" % (row["sender"], row["message"],))
def logquote(self, msg, cmd):
self.db.query("INSERT INTO `chat` (`date`, `sender`, `message`) VALUES (?, ?, ?)",
(int(, msg.prefix.nick, msg.trailing)).close()
# Trim quotes
c = self.db.query("SELECT * FROM `chat` ORDER BY `date` DESC LIMIT %s, 1000000;" % self.config["limit"])
while True:
row = c.fetchone()
if not row:
self.db.query("DELETE FROM `chat` WHERE id=?", (row["id"],)).close()
def ondisable(self):