minerbot-phoenix/plugins/autowater.py

57 lines
2.0 KiB
Python

import plugin, time, os, json
from dictdata import DictData
watered = DictData("autowater.json")
def water(user):
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)
def userForNick(f):
def __nicktouser(bot,channel,nick,*args):
return f(bot,channel,bot.event.source,*args)
return __nicktouser
@plugin.group("autowater","<add/remove>")
def autowater(bot,channel,nick,*args):
if args[0] not in "add remove".split():
bot.say(channel,"{}: Usage: {}autowater <add/remove>".format(nick,bot.prefix))
return True
return False
@autowater.command("add")
@userForNick
def autowater_add(bot,channel,userhost,*args):
user = userhost.user.strip("~")
nick = userhost.nick
if user in watered:
bot.say(channel,"{}: I'm already watching your plant. Did you mean to remove yourself? (!autowater remove)".format(nick))
else:
bot.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")
@userForNick
def autowater_remove(bot,channel,userhost,*args):
user = userhost.user.strip("~")
nick = userhost.nick
if user not in watered:
bot.say(channel,"{}: I'm not watching your plant. Did you mean to add yourself? (!autowater add)".format(nick))
else:
bot.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
@plugin.listener("autowater")
def autowater_listen(bot,*args):
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())