From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [101.128.125.177]) by gnuweeb.org (Postfix) with ESMTPSA id 91EC3804FD; Sat, 1 Oct 2022 13:04:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1664629490; bh=FG7dXlErfD0Xq3nfDmdW9qGrdp5T2c7Bf2jfY7uOJgI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QO4wDpquO3kV/DsY7oEeZQUTeo9sodM8Uu/QdJi7we7eC5xmu9kLZN9dEaVweXjNE fxFZFm4WV2S/LbqC2Zy/GPqC8f5MvucqpnKK9G7DvD5zeev/LOGa4qirMj/H8BADSZ GDhpScNQdogbBYL8BYfOKSdQ6raVMxY+alEgV3jjSA4KiOv5x72YTgqwPWhI3W/Jqe 0rIJI3yJlcQe8CNnhJOkJrAf66qj8uTQwEo7VHdE/5dUtgxogBklo2Kj99EmdEf/tD mGSZEHMe8oU5mhLUanhbaijL9f3MEY1OMx/Ltx7Bv2dzKxiaVormzPNMYDXcgy4FSf QNK4my02X60Nw== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v1 14/26] discord: Add filters.wait_on_limit() decorator Date: Sat, 1 Oct 2022 20:03:42 +0700 Message-Id: <20221001130355.784-15-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20221001130355.784-1-kiizuha@gnuweeb.org> References: <20221001130355.784-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Add wait_on_limit() decorator in filters.py to prevent the rate limit hit for sending a message. Signed-off-by: Muhammad Rizki --- daemon/dscord/gnuweeb/filters.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 daemon/dscord/gnuweeb/filters.py diff --git a/daemon/dscord/gnuweeb/filters.py b/daemon/dscord/gnuweeb/filters.py new file mode 100644 index 0000000..19f9445 --- /dev/null +++ b/daemon/dscord/gnuweeb/filters.py @@ -0,0 +1,27 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Copyright (C) 2022 Muhammad Rizki +# + +import discord +import asyncio +from typing import Any +from functools import wraps + + +def wait_on_limit(func): + @wraps(func) + async def callback(*args: Any) -> Any: + while True: + try: + return await func(*args) + except discord.errors.RateLimited as e: + _flood_exceptions(e) + print("[wait_on_limit]: Woken up from flood wait...") + return callback + + +async def _flood_exceptions(e: "discord.errors.RateLimited"): + wait = e.retry_after + print(f"[wait_on_limit]: Sleeping for {wait} seconds due to Telegram limit") + await asyncio.sleep(wait) -- Muhammad Rizki