maple/storage/bot/plugins/timezonediff_plugin.py
2023-03-26 15:46:52 -05:00

204 lines
9.0 KiB
Python

# -*- coding: utf-8 -*- ############################################################### SOF
###########################################################################################
from irc3.plugins.command import command
import irc3
from datetime import datetime
import timeago
from pytz import common_timezones
from pytz import timezone
import re
from tool_colors_plugin import colorform as print
###########################################################################################
###########################################################################################
@irc3.plugin
class Plugin:
#######################################################################################
#######################################################################################
def __init__(self, bot):
print('[ loaded ]')
self.bot = bot
#######################################################################################
#######################################################################################
@classmethod
def reload(cls, old):
return cls(old.bot)
#######################################################################################
#######################################################################################
def before_reload(self):
pass
#######################################################################################
#######################################################################################
def after_reload(self):
pass
#######################################################################################
#######################################################################################
@command(permission='view')
def timezonediff(self, mask, target, args):
"""timezonediff - '?timezonediff yourzone theirzone calendar_date theirzone_event_time message' or '?timezonediff yourzone theirzone theirzone_event_time message'
%%timezonediff <noise>...
"""
noise=' '.join(args['<noise>'])
if noise.split()[0].lower()=="list":
msg=', '.join(common_timezones)
msg=re.findall(r'.{1,400}(?:\s+|$)',msg)
for _ in msg: self.bot.privmsg(target,f'{_.strip()}')
return
try:
zone1=timezone(noise.split()[0])
except:
zone1=noise.split()[0]
msg=f"error: unknown timezone {zone1} in your request of {noise}, to get a list of the available timezones use this command: ?timezonediff list"
self.bot.privmsg(target,self.bot.emo(msg))
return
###################################################################################
###################################################################################
try:
zone2=timezone(noise.split()[1])
except:
try:
zone2=noise.split()[1]
except:
zone2=None
msg=f"error: unknown timezone {zone2} in your request of {noise}, to get a list of the available timezones use this command: ?timezonediff list"
self.bot.privmsg(target,self.bot.emo(msg))
return
###################################################################################
###################################################################################
FLAG_DATE=False
try:
date=noise.split()[2].replace("/","")
assert int(date)
if len(date)==6 or len(date)==8:
month=date[0:2]
day=date[2:4]
year=date[4:]
try:
assert int(month) >= 1 and int(month) <= 12
except:
msg=f"error: {month} month is not between 1 and 12. there are 12 months in a year"
self.bot.privmsg(target,self.bot.emo(msg))
return
try:
assert int(day) >= 1 and int(day) <= 31
except:
msg=f"error: {day} day is not between 1 and 31. there are at max 31 days in a month"
self.bot.privmsg(target,self.bot.emo(msg))
return
try:
assert int(year) #future assert str(int(year))[-2:] >= str(datetime.now().year)[-2:]
if len(year)==2: year=f"20{year}"
except:
msg=f"error: {year} year is not a year between 0 and {datetime.now().year}+"
self.bot.privmsg(target,self.bot.emo(msg))
return
FLAG_DATE=True
except:
pass #with no date set we presume it is implied as an event today from now into the time of then
###################################################################################
###################################################################################
if not FLAG_DATE:
try:
stime=noise.split()[2]
dtn=datetime.now()
dt=datetime.now(zone2)
year=dt.year
month=dt.month
day=dt.day
except:
msg=f"error: no date of or time of event issued in your request {noise}"
self.bot.privmsg(target,self.bot.emo(msg))
return
else:
try:
stime=noise.split()[3]
except:
msg=f"error: time of event issued in your request {noise}"
self.bot.privmsg(target,self.bot.emo(msg))
return
###################################################################################
###################################################################################
if not FLAG_DATE:
try:
smessage=' '.join(noise.split()[3:])
if not smessage: assert int('cat/0')
except:
msg=f"error: no message with your event {noise}. usage: ?timezonediff yourzone theirzone theirzone_event_time message"
self.bot.privmsg(target,self.bot.emo(msg))
return
else:
try:
smessage=' '.join(noise.split()[4:])
if not smessage: assert int('cat/0')
except:
msg=f"error: no message with your event {noise}. usage: ?timezonediff yourzone theirzone calendar_date theirzone_event_time message"
self.bot.privmsg(target,self.bot.emo(msg))
return
try:
assert int(stime.replace(":","")[0:4]) >= 0 and int(stime.replace(":","")[0:4]) <= 2400
stime=stime.replace(":","")[0:4]
hour=str(stime)[0:2]
minute=str(stime)[2:]
except:
msg="error: time of event is not military time. example 1800 or 18:00 being 6pm"
self.bot.privmsg(target,self.bot.emo(msg))
return
###################################################################################
###################################################################################
dtf=datetime(int(year),int(month),int(day),int(hour),int(minute))
dtn=datetime.now()
# zone1=timezone('us/central')
# zone2=timezone('cet')
z1=zone1.localize(dtn)
z2=zone2.localize(dtn)
delta=z1-z2
z3=zone2.localize(dtf)
tsz2a=z2.timestamp()
tsz2b=z3.timestamp()
timedistance=timeago.format(tsz2b-delta.seconds,tsz2a)
###################################################################################
###################################################################################
if str(timedistance).startswith('in'):
msg = f'i will remind in you {timedistance} in your local timezone of {zone1} about {smessage}'
self.bot.privmsg(target, msg)
message_list = self.bot.db.getlist("remind_%s" % mask.nick.lower())
new_message = {"from": self.bot.nick.lower(), "target": mask.nick.lower(), "message": msg, "time": datetime.now().isoformat(), "etime": datetime.now().timestamp(), "etrigger": tsz2b-delta.seconds }
if not message_list:
message_list = self.bot.db.setlist("remind_%s" % mask.nick.lower(), [new_message])
else:
message_list.append(new_message)
self.bot.db.setlist("remind_%s" % mask.nick.lower(), message_list)
irc_message = "TCPDIRECT/REMIND: {} < {} > reminding < {} > {}".format(new_message.get("time"),new_message.get("from"),mask.nick,new_message.get("message"))
self.bot.privmsg(target, self.bot.emo(irc_message))
else:
msg = f'sorry, this time of event is in the past now {timedistance}'
self.bot.privmsg(target, self.bot.emo(msg))
###########################################################################################
####################################################################################### EOF