From dc6dbbb3fa3dccc837a39acc8f5371c5c80d2d1d Mon Sep 17 00:00:00 2001 From: shtlrs Date: Thu, 1 Feb 2024 12:39:23 +0100 Subject: make error_handling a package This also moves the handle_forbidden_from_block util to the package's ctor to ensure backwards compat --- pydis_core/utils/error_handling.py | 35 ----------------------------- pydis_core/utils/error_handling/__init__.py | 35 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 35 deletions(-) delete mode 100644 pydis_core/utils/error_handling.py create mode 100644 pydis_core/utils/error_handling/__init__.py (limited to 'pydis_core') diff --git a/pydis_core/utils/error_handling.py b/pydis_core/utils/error_handling.py deleted file mode 100644 index 51d23ca0..00000000 --- a/pydis_core/utils/error_handling.py +++ /dev/null @@ -1,35 +0,0 @@ -from discord import Forbidden, Message - -from pydis_core.utils import logging - -log = logging.get_logger(__name__) - - -async def handle_forbidden_from_block(error: Forbidden, message: Message | None = None) -> None: - """ - Handles ``discord.Forbidden`` 90001 errors, or re-raises if ``error`` isn't a 90001 error. - - Args: - error: The raised ``discord.Forbidden`` to check. - message: The message to reply to and include in logs, if error is 90001 and message is provided. - """ - if error.code != 90001: - # The error ISN'T caused by the bot attempting to add a reaction - # to a message whose author has blocked the bot, so re-raise it - raise error - - if not message: - log.info("Failed to add reaction(s) to a message since the message author has blocked the bot") - return - - if message: - log.info( - "Failed to add reaction(s) to message %d-%d since the message author (%d) has blocked the bot", - message.channel.id, - message.id, - message.author.id, - ) - await message.channel.send( - f":x: {message.author.mention} failed to add reaction(s) to your message as you've blocked me.", - delete_after=30 - ) diff --git a/pydis_core/utils/error_handling/__init__.py b/pydis_core/utils/error_handling/__init__.py new file mode 100644 index 00000000..51d23ca0 --- /dev/null +++ b/pydis_core/utils/error_handling/__init__.py @@ -0,0 +1,35 @@ +from discord import Forbidden, Message + +from pydis_core.utils import logging + +log = logging.get_logger(__name__) + + +async def handle_forbidden_from_block(error: Forbidden, message: Message | None = None) -> None: + """ + Handles ``discord.Forbidden`` 90001 errors, or re-raises if ``error`` isn't a 90001 error. + + Args: + error: The raised ``discord.Forbidden`` to check. + message: The message to reply to and include in logs, if error is 90001 and message is provided. + """ + if error.code != 90001: + # The error ISN'T caused by the bot attempting to add a reaction + # to a message whose author has blocked the bot, so re-raise it + raise error + + if not message: + log.info("Failed to add reaction(s) to a message since the message author has blocked the bot") + return + + if message: + log.info( + "Failed to add reaction(s) to message %d-%d since the message author (%d) has blocked the bot", + message.channel.id, + message.id, + message.author.id, + ) + await message.channel.send( + f":x: {message.author.mention} failed to add reaction(s) to your message as you've blocked me.", + delete_after=30 + ) -- cgit v1.2.3