diff options
| author | 2020-05-06 11:22:28 -0700 | |
|---|---|---|
| committer | 2020-06-13 11:21:05 -0700 | |
| commit | edc6c9a39c7681a72fca7ba053f5161f46eadfb9 (patch) | |
| tree | 29ab3c302e3f64f1917f87b8639ca93fb66d6691 | |
| parent | Code block: exclude code blocks 3 lines or shorter (diff) | |
Code block: add function to check if REPL code exists
The `repl_stripping` function was re-purposed. The plan going forward
is to not show the user's code in the output so actual stripping is no
longer necessary.
| -rw-r--r-- | bot/cogs/codeblock/cog.py | 27 |
1 files changed, 10 insertions, 17 deletions
diff --git a/bot/cogs/codeblock/cog.py b/bot/cogs/codeblock/cog.py index 64f9a4cbc..25791801e 100644 --- a/bot/cogs/codeblock/cog.py +++ b/bot/cogs/codeblock/cog.py @@ -260,25 +260,18 @@ class CodeBlockCog(Cog, name="Code Block"): msg = f"{first_line}\n{unindent(code, 4)}" return msg - def repl_stripping(self, msg: str) -> Tuple[str, bool]: - """ - Strip msg in order to extract Python code out of REPL output. + @staticmethod + def is_repl_code(content: str, threshold: int = 3) -> bool: + """Return True if `content` has at least `threshold` number of Python REPL-like lines.""" + repl_lines = 0 + for line in content.splitlines(): + if line.startswith(">>> ") or line.startswith("... "): + repl_lines += 1 - Tries to strip out REPL Python code out of msg and returns the stripped msg. + if repl_lines == threshold: + return True - Returns True for the boolean if REPL code was found in the input msg. - """ - final = "" - for line in msg.splitlines(keepends=True): - if line.startswith(">>>") or line.startswith("..."): - final += line[4:] - log.trace(f"Formatted: \n\n{msg}\n\n to \n\n{final}\n\n") - if not final: - log.trace(f"Found no REPL code in \n\n{msg}\n\n") - return msg, False - else: - log.trace(f"Found REPL code in \n\n{msg}\n\n") - return final.rstrip(), True + return False @staticmethod def has_bad_ticks(message: discord.Message) -> bool: |