Skip to content

Commit

Permalink
[WordFilter] Try compiling RegEx before adding
Browse files Browse the repository at this point in the history
This commit resolves SFUAnime/Ren#646 by compiling RegEx before adding
a RegEx string into WordFilter's books. With this commit, at word filter
insertion time (`[p]wordfilter regex add <word>`), `re.compile` will be
invoked, and if there is a `re.error`, a message will be sent to where
the command has been invoked to notify the user executing the command
about the invalid RegEx pattern that was input. Without this commit, the
cog will crash when it tries to substitute/match/search invalid RegEx
patterns during handling of an `on_message` event. See SFUAnime/Ren#646
for an example traceback.
  • Loading branch information
quachtridat authored and Injabie3 committed Feb 13, 2025
1 parent 5639f01 commit abd2e75
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions wordfilter/wordfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ async def addFilter(self, ctx, word: str):
filters = await self.config.guild(ctx.guild).get_attr(KEY_FILTERS)()

if word not in filters:
try:
re.compile(word, flags=re.IGNORECASE)
except re.error as e:
await ctx.send(
f"`Word Filter:` The word `{word}` is invalid: {e}."
"Please make sure the input is a valid RegEx before adding."
)
return

filters.append(word)
await self.config.guild(ctx.guild).get_attr(KEY_FILTERS).set(filters)
await ctx.send(
Expand Down

0 comments on commit abd2e75

Please # to comment.