Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,7 @@ def main(conn, out):
input.inp = m

dispatch(input, "regex", func, args)

# TIMERS
for func, args in bot.plugs['timer']:
dispatch(input, "timer", func, args)
30 changes: 30 additions & 0 deletions plugins/util/hook.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
import re
import time


def _hook_add(func, add, name=''):
Expand Down Expand Up @@ -91,6 +92,35 @@ def annotate(func):
return func
return annotate

class Timer():
def __init__(self,func,cycle):
self.func = func
self.func_name = func.func_name
self.__name__ = func.__name__
self.func_code = func.func_code
self._args = ['bot']
self.cycle = cycle

def __call__(self,inp,**kwargs):
while True:
self.func(inp,**kwargs)
time.sleep(self.cycle)

def timer(timer, **kwargs):
args = kwargs

def timer_wrapper(func):
func = Timer(func,timer)
args['name'] = func.func_name
args['timer'] = timer
singlethread(func)
_hook_add(func, ['timer', (func, args)], 'timer')
return func

if isinstance(timer,type(0)):
return timer_wrapper
else:
raise ValueError("timer decorators require a time to cyclically run.")

def regex(regex, flags=0, **kwargs):
args = kwargs
Expand Down