File size: 991 Bytes
4051191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import asyncio
from typing import Callable, Awaitable, Any
from loguru import logger

import pyrogram.errors


# retries an async awaitable as long as it raises FloodWait, and waits for err.x time
def retry_on_flood(function: Callable[[Any], Awaitable]):
    async def wrapper(*args, **kwargs):
        while True:
            try:
                return await function(*args, **kwargs)
            except pyrogram.errors.FloodWait as err:
                logger.warning(f'FloodWait, waiting {err.x} seconds: {err.MESSAGE}')
                await asyncio.sleep(err.x)
                continue
            except pyrogram.errors.RPCError as err:
                if err.MESSAGE == 'FloodWait':
                    logger.warning(f'FloodWait, waiting {err.x} seconds: {err.MESSAGE}')
                    await asyncio.sleep(err.x)
                    continue
                else:
                    raise err
            except Exception as err:
                raise err
    return wrapper