From e65bc3f0b6cd14c3c8a1c40775f6da3d0413c95d Mon Sep 17 00:00:00 2001 From: Toby Jones Date: Mon, 18 Mar 2024 14:50:24 +0000 Subject: [PATCH] bug(message_rate_limiter): Fix exception handling on message rate limiter when sending alert message to affected user --- harmony_cogs/message_rate_limiter.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/harmony_cogs/message_rate_limiter.py b/harmony_cogs/message_rate_limiter.py index 347f91e..575091f 100644 --- a/harmony_cogs/message_rate_limiter.py +++ b/harmony_cogs/message_rate_limiter.py @@ -75,17 +75,24 @@ async def on_message(self, message: discord.Message) -> typing.NoReturn: guild_channel = message.guild.get_channel(message.channel.id) - await message.author.send(embed=harmony_ui.message_rate_limiter.create_message_limited_embed( - author_name=message.author.name, - guild_name=message.guild.name, - guild_channel_name=guild_channel.name, - guild_channel_url=guild_channel.jump_url, - original_message_timestamp=rate_limiter_data.message_timestamp, - rate_limit_seconds=channel_limit.rate_limit_seconds, - deleted_message_content=message.clean_content - )) - await message.delete() + + try: + await message.author.send( + embed=harmony_ui.message_rate_limiter.create_message_limited_embed( + author_name=message.author.name, + guild_name=message.guild.name, + guild_channel_name=guild_channel.name, + guild_channel_url=guild_channel.jump_url, + original_message_timestamp=rate_limiter_data.message_timestamp, + rate_limit_seconds=channel_limit.rate_limit_seconds, + deleted_message_content=message.clean_content() + ) + ) + except discord.errors.DiscordException as e: + logger.warning(f"Failed to PM user {message.author.name} with message rate-limit, " + f"got exception: {str(e)}") + else: # Delete their existing data and move on. rate_limiter_data.delete()