From cb227109f48197143e023edf71761a37add16fc7 Mon Sep 17 00:00:00 2001 From: Infinidoge Date: Wed, 1 Jan 2025 01:18:40 -0500 Subject: [PATCH 1/3] gracefully handle not being able to message users --- nomen/notifications.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/nomen/notifications.py b/nomen/notifications.py index f1a0bf3..b69a4f3 100644 --- a/nomen/notifications.py +++ b/nomen/notifications.py @@ -5,6 +5,7 @@ from typing import Union from disnake import Embed, Member, Thread, User from disnake.abc import GuildChannel +from disnake.errors import Forbidden from disnake.ext.commands import Cog, group, guild_only from .utils import can_view, confirm, test_keyword @@ -64,10 +65,16 @@ async def handle_notification(db_updates, ctx, message, keyword, user_id, use_em icon_url=message.author.display_avatar, ) - await member.send(header, embed=embed) + try: + await member.send(header, embed=embed) + except Forbidden: + log.warning("Cannot send messages to this user") else: log.debug("Sending plain message") - await member.send("\n".join((header, indent(message.content, "> ", lambda line: True).strip(), footer))) + try: + await member.send("\n".join((header, indent(message.content, "> ", lambda line: True).strip(), footer))) + except Forbidden: + log.warning("Cannot send messages to this user") log.debug("Sent") From 4a27156cd063dbf15d9a82db15b4acb7bdf9d5ba Mon Sep 17 00:00:00 2001 From: Infinidoge Date: Wed, 1 Jan 2025 01:19:00 -0500 Subject: [PATCH 2/3] prevent repeatedly opting out and in --- nomen/settings.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/nomen/settings.py b/nomen/settings.py index b86434c..ee6f7da 100644 --- a/nomen/settings.py +++ b/nomen/settings.py @@ -148,6 +148,10 @@ You may want to `{ctx.clean_prefix}nomen export` first""" You will not trigger anyone else's notifications, and you will not receive any notifications """ + if await ctx.bot.get_setting(ctx.author.id, "disabled"): + await ctx.send("You are already opted-out") + return + log.debug(f"Opting-out: {ctx.author} ({ctx.author.id})") await ctx.send( @@ -163,6 +167,10 @@ You may want to `{ctx.clean_prefix}nomen export` first""" Opt-in to Nomen processing your messages """ + if not await ctx.bot.get_setting(ctx.author.id, "disabled"): + await ctx.send("You are already opted-in") + return + log.debug(f"Opting-in: {ctx.author} ({ctx.author.id})") await ctx.send( From cfb1b8449d9c19fe24153d1539b10e0e05b85cbc Mon Sep 17 00:00:00 2001 From: Infinidoge Date: Wed, 1 Jan 2025 01:19:15 -0500 Subject: [PATCH 3/3] gracefully handle invalid regexes --- nomen/utils.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/nomen/utils.py b/nomen/utils.py index 417a9c8..29a77ef 100644 --- a/nomen/utils.py +++ b/nomen/utils.py @@ -4,6 +4,7 @@ from itertools import chain import re2 as re from disnake import ChannelType +from disnake.ext.commands import BadArgument log = logging.getLogger("nomen.utils") @@ -87,9 +88,14 @@ def compile_keyword(keyword, regex): if not regex: keyword = re.escape(keyword) - reg = re.compile(rf"(?i)\b{keyword}\b") - regex_cache[(keyword, regex)] = reg - return reg + try: + reg = re.compile(rf"(?i)\b{keyword}\b") + regex_cache[(keyword, regex)] = reg + return reg + except re._re2.Error: + raise BadArgument( + "Invalid regex. Nomen only supports regexes that [re2]() can parse." + ) def contains(string, keyword, regex):