from telebot.apihelper import ApiTelegramException
from time import sleep


async def antiflood(function, *args, **kwargs):
    try:
        return await function(*args, **kwargs)
    except ApiTelegramException as ex:
        if ex.error_code == 429:
            sleep(ex.result_json['parameters']['retry_after'])
            return await function(*args, **kwargs)
        raise ex
