aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar mbaruh <[email protected]>2021-09-11 20:15:21 +0300
committerGravatar mbaruh <[email protected]>2021-09-11 20:15:21 +0300
commitf4658f8468cbe055e67d795cc6aa4b171c8c0b0f (patch)
tree7c29ccefc6feaa84f1b27bb8f3006c275dd1d26a
parentFix incorrect cache usage (diff)
Handle Regex converter errors
Handle cases where there are no enclosing backticks, and where the regex pattern is invalid.
Diffstat (limited to '')
-rw-r--r--bot/exts/moderation/clean.py21
1 files changed, 13 insertions, 8 deletions
diff --git a/bot/exts/moderation/clean.py b/bot/exts/moderation/clean.py
index f12550ab6..af79d5a35 100644
--- a/bot/exts/moderation/clean.py
+++ b/bot/exts/moderation/clean.py
@@ -44,16 +44,21 @@ class CleanChannels(Converter):
class Regex(Converter):
- """A converter that takes a string in the form `.+` and returns the contents of the inline code."""
+ """A converter that takes a string in the form `.+` and returns the contents of the inline code compiled."""
- async def convert(self, ctx: Context, argument: str) -> str:
- """Strips the backticks from the string."""
- return re.fullmatch(r"`(.+?)`", argument).group(1)
+ async def convert(self, ctx: Context, argument: str) -> re.Pattern:
+ """Strips the backticks from the string and compiles it to a regex pattern."""
+ if not (match := re.fullmatch(r"`(.+?)`", argument)):
+ raise BadArgument("Regex pattern missing wrapping backticks")
+ try:
+ return re.compile(match.group(1), re.IGNORECASE + re.DOTALL)
+ except re.error as e:
+ raise BadArgument(f"Regex error: {e.msg}")
if TYPE_CHECKING:
CleanChannels = Union[Literal["*"], list[TextChannel]] # noqa: F811
- Regex = str # noqa: F811
+ Regex = re.Pattern # noqa: F811
class Clean(Cog):
@@ -120,7 +125,7 @@ class Clean(Cog):
def _build_predicate(
bots_only: bool = False,
users: list[User] = None,
- regex: Optional[str] = None,
+ regex: Optional[re.Pattern] = None,
first_limit: Optional[datetime] = None,
second_limit: Optional[datetime] = None,
) -> Predicate:
@@ -151,7 +156,7 @@ class Clean(Cog):
content = "\n".join(attr for attr in content if attr)
# Now let's see if there's a regex match
- return bool(re.search(regex, content, re.IGNORECASE + re.DOTALL))
+ return bool(regex.search(content))
def predicate_range(message: Message) -> bool:
"""Check if the message age is between the two limits."""
@@ -346,7 +351,7 @@ class Clean(Cog):
channels: CleanChannels,
bots_only: bool = False,
users: list[User] = None,
- regex: Optional[str] = None,
+ regex: Optional[re.Pattern] = None,
first_limit: Optional[CleanLimit] = None,
second_limit: Optional[CleanLimit] = None,
use_cache: Optional[bool] = True