cosmicbot/commands/water.py

30 lines
1.1 KiB
Python
Raw Normal View History

2021-06-15 01:23:25 +00:00
import plugin, os.path, json, time
2021-06-15 01:18:58 +00:00
2021-07-15 21:52:28 +00:00
def water(user,by="cosmicbot"):
2021-06-15 01:21:03 +00:00
try:
with open(os.path.expanduser("~{}/.botany/visitors.json".format(user))) as f:
visitors = json.load(f)
2021-07-15 21:52:28 +00:00
visitors.append(dict(timestamp=int(time.time()),user=by))
2021-06-15 01:21:03 +00:00
with open(os.path.expanduser("~{}/.botany/visitors.json".format(user)),"w") as f:
json.dump(visitors,f,indent=2)
with open(os.path.expanduser("~{}/.botany/{}_plant_data.json".format(user,user))) as f:
return json.load(f)['description'] # Success
2021-06-15 01:21:03 +00:00
except:
return False # Failure
2021-06-15 01:18:58 +00:00
@plugin.command("water")
def cmd_water(bot,channel,nick,user=None):
if user is not None:
2021-07-15 21:52:28 +00:00
if plant := water(user,nick):
2021-07-06 12:13:40 +00:00
bot.say(channel,f"Watered _{user}_'s {plant}!")
2021-06-15 01:18:58 +00:00
else:
2021-07-06 12:13:40 +00:00
bot.say(channel,f"Unable to water _{user}_'s plant; are you sure they have one?")
2021-06-15 01:18:58 +00:00
else:
if plant := water(nick):
2021-07-06 12:13:40 +00:00
bot.say(channel,f"Watered _{nick}_'s {plant}!")
2021-06-15 01:18:58 +00:00
return
if plant := water(bot.event.source.user):
2021-07-06 12:13:40 +00:00
bot.say(channel,f"Watered _{bot.event.source.user}_'s {plant}!")
2021-06-15 01:18:58 +00:00
return
bot.say(channel,"I couldn't figure out which plant was yours. Give me your username as an argument.")