m4pl1mp/plugins/remind_plugin.py
2022-02-01 22:44:42 -06:00

107 lines
5.8 KiB
Python

# -*- coding: utf-8 -*-
from irc3.plugins.command import command
from irc3.plugins.cron import cron
import irc3
from datetime import datetime
from time import time
import re
REMIND_RE = re.compile('maple.remind\s(.+?)\s(.+)')
###########################################################################################
###########################################################################################
@irc3.plugin
class Plugin:
#######################################################################################
#######################################################################################
def __init__(self, bot):
self.bot = bot
#######################################################################################
#######################################################################################
def __triggered(i):
time_now = int(str(time()).split('.')[0])
if i <= time_now:
print('triggered')
else:
print('not triggered')
#######################################################################################
#######################################################################################
@irc3.event(irc3.rfc.PRIVMSG)
def on_privmsg_search_for_remind(self, mask=None, target=None, data=None, **kw):
match_list = REMIND_RE.findall(data)
if len(match_list) > 0:
stime = ""
trigger = ""
bhours = False
bminutes = False
tell_nick, message = match_list[0]
if tell_nick == 'me': tell_nick = mask.nick
if not message.split()[0].lower() == 'in':
irc_message = "TCPDIRECT/REMIND: reminding {} the format is maple remind ircnick in 2h15m to pet cat, or maple remind me in 1h to pet cat.".format(mask.nick)
self.bot.privmsg(target, self.bot.emo(irc_message))
return
else:
message = message[3:]
trigger = message.split()[0]
message = message[len(trigger)+1:]
message_list = self.bot.db.getlist("remind_%s" % tell_nick.lower())
ihours = 0
iminutes = 0
xpos = 0
ypos = 0
itrigger = 0
epoch_time = int(str(time()).split('.')[0])
stime = trigger
try:
if not stime.lower().find('h') == -1:
xpos = stime.lower().find('h')+1
hours = int(stime[:stime.lower().find('h')])
ihours = hours * (60*60)
if not stime.lower().find('m') == -1:
ypos = stime.lower().find('m')
minutes = int(stime[xpos:ypos])
if minutes < 1:
irc_message = "TCPDIRECT/REMIND: reminding {} to quit wasting my time. format is maple remind ircnick in 2h15m to pet cat, or maple remind me in 1h to pet cat.".format(mask.nick)
self.bot.privmsg(target, self.bot.emo(irc_message))
return
iminutes = minutes * 60
except:
irc_message = "TCPDIRECT/REMIND: reminding {} to go and fuck themself err i mean, format is maple remind ircnick in 2h15m to pet cat, or maple remind me in 1h to pet cat.".format(mask.nick)
self.bot.privmsg(target, self.bot.emo(irc_message))
return
epoch_trigger = epoch_time + ihours + iminutes
if epoch_trigger <= epoch_time:
irc_message = "TCPDIRECT/REMIND: reminding {} to quit wasting my time. format is maple remind ircnick in 2h15m to pet cat, or maple remind me in 1h to pet cat.".format(mask.nick)
self.bot.privmsg(target, self.bot.emo(irc_message))
return
new_message = {"from": mask.nick.lower(), "target": target, "message": message, "time": datetime.now().isoformat(), "etime": epoch_time, "etrigger": epoch_trigger }
if not message_list:
message_list = self.bot.db.setlist("remind_%s" % tell_nick.lower(), [new_message])
else:
message_list.append(new_message)
self.bot.db.setlist("remind_%s" % tell_nick.lower(), message_list)
irc_message = "TCPDIRECT/REMIND: {} < {} > reminding < {} > {}".format(new_message.get("time"),new_message.get("from"),tell_nick,new_message.get("message"))
self.bot.privmsg(target, self.bot.emo(irc_message))
#######################################################################################
#######################################################################################
###########################################################################################
###########################################################################################
@cron('* * * * *')
def _reminding(bot):
nicks = ','.join(bot.db.context.nicks.keys()).split(',')
for _ in nicks:
if bot.db.getlist('remind_{}'.format(_)):
_keys = []
for __ in bot.db.getlist('remind_{}'.format(_)):
etime = __['etime']
etrigger = __['etrigger']
_from = __['from']
_target = __['target']
_message = __['message']
etime_now = int(str(time()).split('.')[0])
if etime_now >= etrigger:
msg = "TCPDIRECT/REMIND: < {} > reminded < {} > - {}".format(_from,_,_message)
bot.privmsg(_target,msg)
else:
_keys.append(__)
bot.db.setlist("remind_{}".format(_),_keys)
###########################################################################################
###########################################################################################