minerbot2/plugins/autowater.py

76 lines
2.4 KiB
Python

from bot import IRCLine
BOT=None
def say(target,message):
if not BOT: return
BOT.socket.send(IRCLine("PRIVMSG",target,":"+message))
import plugin, time, os, json
from dictdata import DictData
watered = DictData("autowater.json")
def water(user):
try:
with open(os.path.expanduser("~{}/.botany/visitors.json".format(user))) as f:
visitors = json.load(f)
visitors.append(dict(timestamp=int(time.time()),user="minerbot"))
with open(os.path.expanduser("~{}/.botany/visitors.json".format(user)),"w") as f:
json.dump(visitors,f,indent=2)
except:
# user doesn't exist, remove
watered[user]=(2**64)-1
@plugin.group("autowater","<add/remove>")
def autowater(bot,channel,nick,*args):
if args[0] not in "add remove".split():
say(channel,"{}: Usage: {}autowater <add/remove>".format(nick,BOT.prefix))
return True
return False
@autowater.command("help","")
def autowater_help(bot,channel,nick,*args):
say(channel,"{}: Usage: {}autowater <add/remove>".format(nick,BOT.prefix))
@autowater.command("add","")
def autowater_add(bot,channel,userhost,*args):
user = userhost.user.strip("~")
nick = userhost.nick
if user in watered:
say(channel,"{}: I'm already watching your plant. Did you mean to remove yourself? (!autowater remove)".format(nick))
else:
say(channel,"{}: I'll watch your plant for you!".format(nick))
watered[user]=0 # force a plant watering on the next message
@autowater.command("remove","")
def autowater_remove(bot,channel,userhost,*args):
user = userhost.user.strip("~")
nick = userhost.nick
if user not in watered:
say(channel,"{}: I'm not watching your plant. Did you mean to add yourself? (!autowater add)".format(nick))
else:
say(channel,"{}: Welcome back! I'll stop watching your plant since you're here.".format(nick))
del watered.value[user]
watered.save(watered.filename)
BOTANY_PLANT_WATER_TRIGGER = (1 * (24 * 3600)) # 5 days for a botany plant to die, so water every day
def autowater_listen(event):
for user in watered.value.keys():
if (int(time.time())-watered[user])>=BOTANY_PLANT_WATER_TRIGGER:
print("watering user {}".format(user))
water(user)
watered[user]=int(time.time())
def on_cmd_autowater(event):
try:
autowater(BOT,event.target if event.target.startswith("#") else event.hostmask.nick,event.hostmask,*event.parts)
except:
pass
def register(bot):
global BOT
BOT=bot
bot.event_manager.on("command_autowater",on_cmd_autowater)
bot.event_manager.on("privmsg",autowater_listen)