diff options
author | 2019-10-25 09:14:54 -0400 | |
---|---|---|
committer | 2019-10-25 09:14:54 -0400 | |
commit | fa343dc8ddfcb73ba458349b9229f9581451af37 (patch) | |
tree | 190628d6b35972073a014e8ab4264e1402e7991e | |
parent | Merge branch 'master' into moderator-channel-check (diff) | |
parent | Merge pull request #550 from python-discord/###-filtering-devtest (diff) |
Merge branch 'master' into moderator-channel-check
-rw-r--r-- | bot/cogs/filtering.py | 41 | ||||
-rw-r--r-- | bot/cogs/snekbox.py | 13 |
2 files changed, 37 insertions, 17 deletions
diff --git a/bot/cogs/filtering.py b/bot/cogs/filtering.py index 1d1d74e74..be9b95bc7 100644 --- a/bot/cogs/filtering.py +++ b/bot/cogs/filtering.py @@ -9,7 +9,7 @@ from discord.ext.commands import Bot, Cog from bot.cogs.moderation import ModLog from bot.constants import ( - Channels, Colours, DEBUG_MODE, + Channels, Colours, Filter, Icons, URLs ) @@ -136,10 +136,6 @@ class Filtering(Cog): and not msg.author.bot # Author not a bot ) - # If we're running the bot locally, ignore role whitelist and only listen to #dev-test - if DEBUG_MODE: - filter_message = not msg.author.bot and msg.channel.id == Channels.devtest - # If none of the above, we can start filtering. if filter_message: for filter_name, _filter in self.filters.items(): @@ -154,11 +150,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 +180,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 +206,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 +237,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 diff --git a/bot/cogs/snekbox.py b/bot/cogs/snekbox.py index c0390cb1e..362968bd0 100644 --- a/bot/cogs/snekbox.py +++ b/bot/cogs/snekbox.py @@ -115,6 +115,16 @@ class Snekbox(Cog): return msg, error + @staticmethod + def get_status_emoji(results: dict) -> str: + """Return an emoji corresponding to the status code or lack of output in result.""" + if not results["stdout"].strip(): # No output + return ":warning:" + elif results["returncode"] == 0: # No error + return ":white_check_mark:" + else: # Exception + return ":x:" + async def format_output(self, output: str) -> Tuple[str, Optional[str]]: """ Format the output and return a tuple of the formatted output and a URL to the full output. @@ -201,7 +211,8 @@ class Snekbox(Cog): else: output, paste_link = await self.format_output(results["stdout"]) - msg = f"{ctx.author.mention} {msg}.\n\n```py\n{output}\n```" + icon = self.get_status_emoji(results) + msg = f"{ctx.author.mention} {icon} {msg}.\n\n```py\n{output}\n```" if paste_link: msg = f"{msg}\nFull output: {paste_link}" |