From f5bf412f087649ed10d7d80031870790e74bfa7a Mon Sep 17 00:00:00 2001 From: PatriotRossii Date: Wed, 18 Jan 2023 15:52:39 +0300 Subject: [PATCH] Implement schedule decorator --- kutana/plugin.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/kutana/plugin.py b/kutana/plugin.py index e129ccb..daa6f62 100644 --- a/kutana/plugin.py +++ b/kutana/plugin.py @@ -1,4 +1,5 @@ import re +import asyncio import inspect import warnings import functools @@ -432,3 +433,23 @@ async def wrapper(upd, ctx, *args, **kwargs): return await func(upd, ctx, *args, **kwargs) return wrapper return decorator + + def schedule(self, delay): + """ + Decorator for registering coroutine to be called + every `delay` seconds + + NOTE: + Coroutine must not take any arguments + """ + + def decorator(func): + async def wrapper(): + while True: + await func() + await asyncio.sleep(delay) + + @self.on_start() + async def _(): + asyncio.create_task(wrapper()) + return decorator