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") 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( 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):