gracefully handle invalid regexes
This commit is contained in:
parent
4a27156cd0
commit
cfb1b8449d
1 changed files with 9 additions and 3 deletions
|
@ -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)
|
||||
|
||||
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](<https://github.com/google/re2>) can parse."
|
||||
)
|
||||
|
||||
|
||||
def contains(string, keyword, regex):
|
||||
|
|
Loading…
Reference in a new issue