diff options
author | 2021-10-17 15:19:35 +0530 | |
---|---|---|
committer | 2021-10-18 06:34:10 +0530 | |
commit | e84ced79ade1a28a7e24c307d16126dc80a17a7b (patch) | |
tree | 344fd62a0f3c34271b51e32c19040dfd3da7fa20 | |
parent | Merge remote-tracking branch 'upstream/main' into feature/enhance-incidents (diff) |
Refactor shorten_text utility function
-rw-r--r-- | bot/exts/moderation/incidents.py | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/bot/exts/moderation/incidents.py b/bot/exts/moderation/incidents.py index 5c5efdb15..b62ba0629 100644 --- a/bot/exts/moderation/incidents.py +++ b/bot/exts/moderation/incidents.py @@ -142,21 +142,23 @@ def has_signals(message: discord.Message) -> bool: def shorten_text(text: str) -> str: """Truncate the text if there are over 3 lines or 300 characters, or if it is a single word.""" original_length = len(text) - lines = text.split("\n") + # Truncate text to a maximum of 300 characters + if len(text) > 300: + text = text[:300] + # Limit to a maximum of three lines - if len(lines) > 3: - text = "\n".join(line for line in lines[:3]) + text = "\n".join(line for line in text.split("\n", maxsplit=3)[:3]) # If it is a single word, then truncate it to 50 characters - if text.count(" ") < 1: + if text.find(" ") == -1: text = text[:50] - # Truncate text to a maximum of 300 characters - elif len(text) > 300: - text = text[:300] + + # Remove extra whitespaces from the `text` + text = text.strip() # Add placeholder if the text was shortened if len(text) < original_length: - text += "..." + text = f"{text}..." return text |