From fbe6af2fc6a8a01e94431210b6ae19215a6e688b Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 28 Mar 2019 16:24:11 +0100 Subject: [PATCH] Automatically handle flood waits when using get_chat_members --- .../client/methods/chats/get_chat_members.py | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/pyrogram/client/methods/chats/get_chat_members.py b/pyrogram/client/methods/chats/get_chat_members.py index f69674c4..726fd14b 100644 --- a/pyrogram/client/methods/chats/get_chat_members.py +++ b/pyrogram/client/methods/chats/get_chat_members.py @@ -16,12 +16,17 @@ # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . +import logging +import time from typing import Union import pyrogram from pyrogram.api import functions, types +from pyrogram.errors import FloodWait from ...ext import BaseClient +log = logging.getLogger(__name__) + class Filters: ALL = "all" @@ -116,17 +121,22 @@ class GetChatMembers(BaseClient): else: raise ValueError("Invalid filter \"{}\"".format(filter)) - return pyrogram.ChatMembers._parse( - self, - self.send( - functions.channels.GetParticipants( - channel=peer, - filter=filter, - offset=offset, - limit=limit, - hash=0 + while True: + try: + return pyrogram.ChatMembers._parse( + self, + self.send( + functions.channels.GetParticipants( + channel=peer, + filter=filter, + offset=offset, + limit=limit, + hash=0 + ) + ) ) - ) - ) + except FloodWait as e: + log.warning("Sleeping for {}s".format(e.x)) + time.sleep(e.x) else: raise ValueError("The chat_id \"{}\" belongs to a user".format(chat_id))