pyircbot/pyircbot/modules/Inventory.py

104 lines
3.7 KiB
Python
Raw Normal View History

2014-10-08 00:47:10 -07:00
#!/usr/bin/env python
"""
.. module:: Inventory
2015-11-01 18:03:11 -08:00
:synopsis: Lets the bot hold random items
2014-10-08 00:47:10 -07:00
.. moduleauthor:: Dave Pedu <dave@davepedu.com>
"""
2017-11-27 18:58:20 -08:00
from pyircbot.modulebase import ModuleBase, command
from pyircbot.modules.ModInfo import info
2014-10-08 00:47:10 -07:00
from datetime import datetime
2017-01-01 14:59:01 -08:00
2014-10-08 00:47:10 -07:00
class Inventory(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("Inventory: Could not find a valid sqlite service provider")
else:
self.log.info("Inventory: Selecting sqlite service provider: %s" % serviceProviders[0])
self.db = serviceProviders[0].opendb("inventory.db")
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
if not self.db.tableExists("inventory"):
self.log.info("Inventory: Creating table: inventory")
c = self.db.query("""CREATE TABLE IF NOT EXISTS `inventory` (
`id` INTEGER PRIMARY KEY,
`date` INTEGER,
`donor` varchar(64),
`item` varchar(64)
) ;""")
c.close()
2017-01-01 14:59:01 -08:00
2017-11-27 18:58:20 -08:00
@info("have <item> give the bot an item", cmds=["have"])
@command("have")
def checkInv(self, msg, cmd):
if len(cmd.args) < 1:
2015-11-01 18:03:11 -08:00
return
2017-11-27 18:58:20 -08:00
adjective = None
newItem = cmd.args_str
if cmd.args[0] in self.config["adjectives"]:
newItem = cmd.args_str[len(cmd.args[0]):].strip()
adjective = cmd.args[0]
2017-01-01 14:59:01 -08:00
2017-11-27 18:58:20 -08:00
if self.has_item(newItem):
self.bot.act_PRIVMSG(msg.args[0], self.config["dupe_msg"] % {"item": newItem})
return
2017-01-01 14:59:01 -08:00
2017-11-27 18:58:20 -08:00
dropped = self.add_item(msg.prefix.nick, newItem)
if len(dropped) > 0:
self.bot.act_PRIVMSG(msg.args[0], self.config["swap_msg"] %
{"adjective": (adjective + " ") if adjective else "",
"recv_item": newItem, "drop_item": ", ".join(dropped)})
else:
self.bot.act_PRIVMSG(msg.args[0], self.config["recv_msg"] %
{"item": newItem, "adjective": "these " if newItem[-1:] == "s" else "this "})
2017-01-01 14:59:01 -08:00
2017-11-27 18:58:20 -08:00
@info("inventory show the bot's inventory", cmds=["inventory", "inv"])
@command("inventory", "inv")
def cmd_inv(self, msg, cmd):
inv = self.getinventory()
if len(inv) == 0:
self.bot.act_PRIVMSG(msg.args[0], self.config["inv_msg"] % {"itemlist": "nothing!"})
else:
self.bot.act_PRIVMSG(msg.args[0], self.config["inv_msg"] % {"itemlist": ", ".join(inv)})
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def has_item(self, itemName):
2017-01-01 14:59:01 -08:00
c = self.db.query("SELECT COUNT(*) as `num` FROM `inventory` WHERE `item`=? COLLATE NOCASE", (itemName,))
2015-11-01 18:03:11 -08:00
row = c.fetchone()
c.close()
2017-01-01 14:59:01 -08:00
return row["num"] > 0
2015-11-01 18:03:11 -08:00
def add_item(self, donor, itemName):
dropped = []
2017-12-03 23:18:33 -08:00
c = self.db.query("SELECT * FROM `inventory` ORDER BY RANDOM() LIMIT %s,1000000" % str(int(self.config["limit"]) - 1))
2015-11-01 18:03:11 -08:00
while True:
row = c.fetchone()
2017-01-01 14:59:01 -08:00
if row is None:
2015-11-01 18:03:11 -08:00
break
dropped.append(row["item"])
self.db.query("DELETE FROM `inventory` WHERE `id`=?", (row["id"],)).close()
c.close()
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
self.db.query("INSERT INTO `inventory` (`date`, `donor`, `item`) VALUES (?, ?, ?)",
2017-01-01 14:59:01 -08:00
(int(datetime.now().timestamp()), donor, itemName)).close()
2015-11-01 18:03:11 -08:00
return dropped
2017-01-01 14:59:01 -08:00
2015-11-01 18:03:11 -08:00
def getinventory(self):
inv = []
c = self.db.query("SELECT * FROM `inventory`")
while True:
row = c.fetchone()
2017-01-01 14:59:01 -08:00
if row is None:
2015-11-01 18:03:11 -08:00
break
inv.append(row["item"])
c.close()
return inv
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