From 763e84a7756b6c26472e86183cfa757e09b53214 Mon Sep 17 00:00:00 2001 From: skinmaker1345 Date: Tue, 17 Sep 2024 23:15:27 +0900 Subject: [PATCH 1/3] fix: handle error raised during guild count update --- koreanbots/http.py | 8 ++++++-- koreanbots/integrations/dico.py | 10 +++++++--- koreanbots/integrations/discord.py | 10 +++++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/koreanbots/http.py b/koreanbots/http.py index 0c9dfbc..084fff3 100644 --- a/koreanbots/http.py +++ b/koreanbots/http.py @@ -113,8 +113,12 @@ async def request( async with self.session.request( method, KOREANBOTS_URL + endpoint, **kwargs ) as response: - remain_limit = response.headers["x-ratelimit-remaining"] - if int(remain_limit) == 0 or response.status == 429: + remain_limit = response.headers.get("x-ratelimit-remaining") + if ( + remain_limit is not None + and int(remain_limit) == 0 + or response.status == 429 + ): reset_limit_timestamp = int(response.headers["x-ratelimit-reset"]) reset_limit = datetime.fromtimestamp(reset_limit_timestamp) retry_after = reset_limit - datetime.now() diff --git a/koreanbots/integrations/dico.py b/koreanbots/integrations/dico.py index 53e1821..ba3778f 100644 --- a/koreanbots/integrations/dico.py +++ b/koreanbots/integrations/dico.py @@ -93,7 +93,11 @@ async def tasks_send_guildcount(self) -> None: if self.include_shard_count: if self.client.shard_count: kwargs.update({"shards": self.client.shard_count}) - log.info("Send") - await self.guildcount(int(self.client.application_id), **kwargs) - log.info("Complete i will sleep") + log.info("Initiating guild count update...") + try: + await self.guildcount(int(self.client.application_id), **kwargs) + except Exception as e: + log.error("Guild count update failed due to an error.") + else: + log.info("Guild count updated successfully. Waiting 30 minutes for the next update.") await sleep(1800) diff --git a/koreanbots/integrations/discord.py b/koreanbots/integrations/discord.py index 406c254..0ee3716 100644 --- a/koreanbots/integrations/discord.py +++ b/koreanbots/integrations/discord.py @@ -117,7 +117,11 @@ async def tasks_send_guildcount(self) -> None: if self.include_shard_count: if self.client.shard_count: kwargs.update({"shards": self.client.shard_count}) - log.info("Send") - await self.guildcount(self.client.user.id, **kwargs) - log.info("Complete i will sleep") + log.info("Initiating guild count update...") + try: + await self.guildcount(int(self.client.user.id), **kwargs) + except Exception as e: + log.error("Guild count update failed due to an error.") + else: + log.info("Guild count updated successfully. Waiting 30 minutes for the next update.") await sleep(1800) From 64b70c5b6643a8a82fff406c6e5086e1ece3b9f9 Mon Sep 17 00:00:00 2001 From: skinmaker1345 Date: Tue, 17 Sep 2024 23:16:58 +0900 Subject: [PATCH 2/3] chore: reformat code --- koreanbots/integrations/dico.py | 6 ++++-- koreanbots/integrations/discord.py | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/koreanbots/integrations/dico.py b/koreanbots/integrations/dico.py index ba3778f..1484c06 100644 --- a/koreanbots/integrations/dico.py +++ b/koreanbots/integrations/dico.py @@ -96,8 +96,10 @@ async def tasks_send_guildcount(self) -> None: log.info("Initiating guild count update...") try: await self.guildcount(int(self.client.application_id), **kwargs) - except Exception as e: + except: log.error("Guild count update failed due to an error.") else: - log.info("Guild count updated successfully. Waiting 30 minutes for the next update.") + log.info( + "Guild count updated successfully. Waiting 30 minutes for the next update." + ) await sleep(1800) diff --git a/koreanbots/integrations/discord.py b/koreanbots/integrations/discord.py index 0ee3716..63e8887 100644 --- a/koreanbots/integrations/discord.py +++ b/koreanbots/integrations/discord.py @@ -120,8 +120,10 @@ async def tasks_send_guildcount(self) -> None: log.info("Initiating guild count update...") try: await self.guildcount(int(self.client.user.id), **kwargs) - except Exception as e: + except: log.error("Guild count update failed due to an error.") else: - log.info("Guild count updated successfully. Waiting 30 minutes for the next update.") + log.info( + "Guild count updated successfully. Waiting 30 minutes for the next update." + ) await sleep(1800) From 28c216841a0150ad0176983acf7d174b01cb38e5 Mon Sep 17 00:00:00 2001 From: skinmaker1345 Date: Tue, 17 Sep 2024 23:43:40 +0900 Subject: [PATCH 3/3] feat: attach exception on error --- koreanbots/integrations/dico.py | 2 +- koreanbots/integrations/discord.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/koreanbots/integrations/dico.py b/koreanbots/integrations/dico.py index 1484c06..d354901 100644 --- a/koreanbots/integrations/dico.py +++ b/koreanbots/integrations/dico.py @@ -97,7 +97,7 @@ async def tasks_send_guildcount(self) -> None: try: await self.guildcount(int(self.client.application_id), **kwargs) except: - log.error("Guild count update failed due to an error.") + log.exception("Guild count update failed due to an error.") else: log.info( "Guild count updated successfully. Waiting 30 minutes for the next update." diff --git a/koreanbots/integrations/discord.py b/koreanbots/integrations/discord.py index 63e8887..18da3fd 100644 --- a/koreanbots/integrations/discord.py +++ b/koreanbots/integrations/discord.py @@ -121,7 +121,7 @@ async def tasks_send_guildcount(self) -> None: try: await self.guildcount(int(self.client.user.id), **kwargs) except: - log.error("Guild count update failed due to an error.") + log.exception("Guild count update failed due to an error.") else: log.info( "Guild count updated successfully. Waiting 30 minutes for the next update."