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#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#646 for an
example traceback.
  • Loading branch information
quachtridat committed Oct 20, 2024
1 parent 861ba9a commit c55ca92
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cogs/wordfilter/wordfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ 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}")
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 c55ca92

Please sign in to comment.