diff options
Diffstat (limited to '')
| -rw-r--r-- | bot/cogs/filtering.py | 35 | 
1 files changed, 24 insertions, 11 deletions
| diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 1d1d74e74..772c5dbb2 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -154,11 +154,11 @@ class Filtering(Cog):                      # Does the filter only need the message content or the full message?                      if _filter["content_only"]: -                        triggered = await _filter["function"](msg.content) +                        match = await _filter["function"](msg.content)                      else: -                        triggered = await _filter["function"](msg) +                        match = await _filter["function"](msg) -                    if triggered: +                    if match:                          # If this is a filter (not a watchlist), we should delete the message.                          if _filter["type"] == "filter":                              try: @@ -184,12 +184,23 @@ class Filtering(Cog):                          else:                              channel_str = f"in {msg.channel.mention}" +                        # Word and match stats for watch_words and watch_tokens +                        if filter_name in ("watch_words", "watch_tokens"): +                            surroundings = match.string[max(match.start() - 10, 0): match.end() + 10] +                            message_content = ( +                                f"**Match:** '{match[0]}'\n" +                                f"**Location:** '...{surroundings}...'\n" +                                f"\n**Original Message:**\n{msg.content}" +                            ) +                        else:  # Use content of discord Message +                            message_content = msg.content +                          message = (                              f"The {filter_name} {_filter['type']} was triggered "                              f"by **{msg.author}** "                              f"(`{msg.author.id}`) {channel_str} with [the "                              f"following message]({msg.jump_url}):\n\n" -                            f"{msg.content}" +                            f"{message_content}"                          )                          log.debug(message) @@ -199,7 +210,7 @@ class Filtering(Cog):                          if filter_name == "filter_invites":                              additional_embeds = [] -                            for invite, data in triggered.items(): +                            for invite, data in match.items():                                  embed = discord.Embed(description=(                                      f"**Members:**\n{data['members']}\n"                                      f"**Active:**\n{data['active']}" @@ -230,31 +241,33 @@ class Filtering(Cog):                          break  # We don't want multiple filters to trigger      @staticmethod -    async def _has_watchlist_words(text: str) -> bool: +    async def _has_watchlist_words(text: str) -> Union[bool, re.Match]:          """          Returns True if the text contains one of the regular expressions from the word_watchlist in our filter config.          Only matches words with boundaries before and after the expression.          """          for regex_pattern in WORD_WATCHLIST_PATTERNS: -            if regex_pattern.search(text): -                return True +            match = regex_pattern.search(text) +            if match: +                return match  # match objects always have a boolean value of True          return False      @staticmethod -    async def _has_watchlist_tokens(text: str) -> bool: +    async def _has_watchlist_tokens(text: str) -> Union[bool, re.Match]:          """          Returns True if the text contains one of the regular expressions from the token_watchlist in our filter config.          This will match the expression even if it does not have boundaries before and after.          """          for regex_pattern in TOKEN_WATCHLIST_PATTERNS: -            if regex_pattern.search(text): +            match = regex_pattern.search(text) +            if match:                  # Make sure it's not a URL                  if not URL_RE.search(text): -                    return True +                    return match  # match objects always have a boolean value of True          return False | 
