def timer_hook(ctx, pline, userdata): caller = pline.prefix.nick args = pline.trailing.split(None, 2) usage = '/notice {} Invalid syntax: +timer <[ digits "h" ]' \ '[ digits "m" ][ digits "s" ]> <message>'.format(caller) if len(args) < 3: ctx.command(usage) return _, time_string, message = args time_seconds = to_seconds(time_string) if not time_seconds: ctx.command(usage) return if time_seconds > 2147483: ctx.command('/notice {} Too large, maximum is 2147483 seconds or {}' .format(caller, seconds_to_string(2147483))) return _userdata = dict( ctx=ctx, caller=caller, message=message, time_seconds=time_seconds ) _userdata['hook'] = hook_timer(time_seconds, cmd_timer_callback, _userdata) _userdata['tid'] = add_timer(time_seconds, _userdata) dt = datetime.now() + timedelta(seconds=time_seconds) ctx.command('/notice {} timer set to {} seconds ({}, Timer Id: {}, around: {})' .format(caller, time_seconds, seconds_to_string(time_seconds), _userdata['tid'], dt.strftime('%Y-%m-%d %H:%M:%S')))
def timer_hook(ctx, pline, userdata): caller = pline.prefix.nick usage = ('/notice {} Invalid syntax: +timer <[ digits "d" ][ digits "h" ]' '[ digits "m" ][ digits "s" ]> [<message>] || ' '+timer "<timestamp>" [<message>]' .format(caller)) _, _, arg_string = pline.trailing.partition(" ") # allow first argument to be quoted # while everything following becomes "the message" pattern = re.compile(r'''^(?P<q>["']?)(?P<time_string>[^"']+?)(?P=q)(\s+(?P<message>.*))?$''') match = pattern.match(arg_string.strip()) if match is None: ctx.command(usage) return time_string = match.group('time_string').strip() message = match.group('message') if message is None or not message.strip(): message = '' message = message.strip() # interpret timestamp time_seconds = to_seconds(time_string) if not time_seconds: timestamp = parse_timestamp(time_string) if not timestamp: ctx.command(usage) return delta = timestamp - datetime.now() time_seconds = int(delta.seconds + 86400*delta.days) if time_seconds < 0: ctx.command('/notice Timestamp is in past') return elif time_seconds > MAX_TIMER_LENGTH: ctx.command('/notice {} Too large, maximum is {} seconds or {}' .format(caller, MAX_TIMER_LENGTH, seconds_to_string(MAX_TIMER_LENGTH))) return _userdata = dict( ctx=ctx, caller=caller, message=message, time_seconds=time_seconds ) _userdata['hook'] = hook_timer(time_seconds, cmd_timer_callback, _userdata) _userdata['tid'] = add_timer(time_seconds, _userdata) dt = datetime.now() + timedelta(seconds=time_seconds) ctx.command('/notice {} timer set to {} seconds ({}, Timer Id: {}, around: {})' .format(caller, time_seconds, seconds_to_string(time_seconds), _userdata['tid'], dt.strftime('%Y-%m-%d %H:%M:%S')))
def cmd_timer_callback(userdata): """Callback for +timer command.""" remove_timer(userdata['tid'], False) delay = '' if '_when' in userdata: int_delay = abs(int(time.time() - userdata['_when'])) if int_delay > 5: delay = ' with ~{} seconds delay because of script reloading'.format(int_delay) if userdata['message']: output_message = '/say {}, I remind you of: {} ({} ago{})'.format( userdata['caller'], userdata['message'], seconds_to_string(userdata['time_seconds']), delay) else: output_message = '/say {}, I am supposed to remind you of something. ({} ago{})'.format( userdata['caller'], seconds_to_string(userdata['time_seconds']), delay) userdata['ctx'].command(output_message)
def timer_hook(ctx, pline, userdata): caller = pline.prefix.nick args = pline.trailing.split(None, 2) usage = '/notice {} Invalid syntax: +timer <[ digits "h" ]' \ '[ digits "m" ][ digits "s" ]> <message>'.format(caller) if len(args) < 3: ctx.command(usage) return _, time_string, message = args time_seconds = to_seconds(time_string) if not time_seconds: ctx.command(usage) return if time_seconds > 2147483: ctx.command( '/notice {} Too large, maximum is 2147483 seconds or {}'.format( caller, seconds_to_string(2147483))) return _userdata = dict(ctx=ctx, caller=caller, message=message, time_seconds=time_seconds) hook = hook_timer(time_seconds, cmd_timer_callback, _userdata) _userdata['hook'] = hook tid = add_timer(time_seconds, _userdata) _userdata['tid'] = tid dt = datetime.now() + timedelta(seconds=time_seconds) ctx.command( '/notice {} timer set to {} seconds ({}, Timer Id: {}, around: {})'. format(caller, time_seconds, seconds_to_string(time_seconds), tid, dt.strftime('%Y-%m-%d %H:%M:%S')))
def cmd_timer_callback(userdata): """Callback for +timer command.""" remove_timer(userdata['tid'], False) userdata['ctx'].command('/say {}, I remind you of: {} ({} ago)'.format( userdata['caller'], userdata['message'], seconds_to_string(userdata['time_seconds'])))