gracefully handle invalid regexes

This commit is contained in:
Infinidoge 2025-01-01 01:19:15 -05:00
parent 4a27156cd0
commit cfb1b8449d
Signed by: Infinidoge
SSH key fingerprint: SHA256:GT2StvPQMMfFHyiiFJymQxfTG/z6EWLJ6NWItf5K5sA

View file

@ -4,6 +4,7 @@ from itertools import chain
import re2 as re import re2 as re
from disnake import ChannelType from disnake import ChannelType
from disnake.ext.commands import BadArgument
log = logging.getLogger("nomen.utils") log = logging.getLogger("nomen.utils")
@ -87,9 +88,14 @@ def compile_keyword(keyword, regex):
if not regex: if not regex:
keyword = re.escape(keyword) keyword = re.escape(keyword)
reg = re.compile(rf"(?i)\b{keyword}\b") try:
regex_cache[(keyword, regex)] = reg reg = re.compile(rf"(?i)\b{keyword}\b")
return reg regex_cache[(keyword, regex)] = reg
return reg
except re._re2.Error:
raise BadArgument(
"Invalid regex. Nomen only supports regexes that [re2](<https://github.com/google/re2>) can parse."
)
def contains(string, keyword, regex): def contains(string, keyword, regex):