2014-10-07 23:27:58 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
.. module:: RandQuote
|
2015-11-01 18:03:11 -08:00
|
|
|
:synopsis: Log a configurable number of messages and pull up random ones on command
|
2014-10-07 23:27:58 -07:00
|
|
|
|
|
|
|
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2017-11-27 18:58:20 -08:00
|
|
|
from pyircbot.modulebase import ModuleBase, hook, command
|
2014-10-07 23:27:58 -07:00
|
|
|
from datetime import datetime
|
2017-11-27 18:58:20 -08:00
|
|
|
from pyircbot.modules.ModInfo import info
|
2014-10-07 23:27:58 -07:00
|
|
|
|
2017-01-01 14:59:01 -08:00
|
|
|
|
2014-10-07 23:27:58 -07:00
|
|
|
class RandQuote(ModuleBase):
|
2015-11-01 18:03:11 -08:00
|
|
|
def __init__(self, bot, moduleName):
|
2017-01-01 14:59:01 -08:00
|
|
|
ModuleBase.__init__(self, bot, moduleName)
|
2015-11-01 18:03:11 -08:00
|
|
|
self.db = None
|
|
|
|
serviceProviders = self.bot.getmodulesbyservice("sqlite")
|
2017-01-01 14:59:01 -08:00
|
|
|
if not serviceProviders:
|
2015-11-01 18:03:11 -08:00
|
|
|
self.log.error("RandQuote: Could not find a valid sqlite service provider")
|
|
|
|
else:
|
|
|
|
self.log.info("RandQuote: Selecting sqlite service provider: %s" % serviceProviders[0])
|
|
|
|
self.db = serviceProviders[0].opendb("randquote.db")
|
2017-01-01 14:59:01 -08:00
|
|
|
|
2015-11-01 18:03:11 -08:00
|
|
|
if not self.db.tableExists("chat"):
|
|
|
|
self.log.info("RandQuote: Creating table: chat")
|
|
|
|
c = self.db.query("""CREATE TABLE IF NOT EXISTS `chat` (
|
|
|
|
`id` INTEGER PRIMARY KEY,
|
|
|
|
`date` INTEGER,
|
|
|
|
`sender` varchar(64),
|
|
|
|
`message` varchar(2048)
|
|
|
|
) ;""")
|
|
|
|
c.close()
|
2017-01-01 14:59:01 -08:00
|
|
|
|
2017-11-27 18:58:20 -08:00
|
|
|
@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()
|
|
|
|
c.close()
|
|
|
|
if row:
|
|
|
|
self.bot.act_PRIVMSG(msg.args[0], "<%s> %s" % (row["sender"], row["message"],))
|
2017-01-01 14:59:01 -08:00
|
|
|
|
2017-11-27 18:58:20 -08:00
|
|
|
@hook("PRIVMSG")
|
|
|
|
def logquote(self, msg, cmd):
|
2015-11-01 18:03:11 -08:00
|
|
|
self.db.query("INSERT INTO `chat` (`date`, `sender`, `message`) VALUES (?, ?, ?)",
|
2017-11-27 18:58:20 -08:00
|
|
|
(int(datetime.now().timestamp()), msg.prefix.nick, msg.trailing)).close()
|
2017-01-01 14:59:01 -08:00
|
|
|
# Trim quotes
|
2015-11-01 18:03:11 -08:00
|
|
|
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:
|
|
|
|
break
|
|
|
|
self.db.query("DELETE FROM `chat` WHERE id=?", (row["id"],)).close()
|
|
|
|
c.close()
|
2017-01-01 14:59:01 -08:00
|
|
|
|
2015-11-01 18:03:11 -08:00
|
|
|
def ondisable(self):
|
|
|
|
self.db.close()
|
2017-01-01 14:59:01 -08:00
|
|
|
|