From 9c2246b9b250f0d5cd92027a8d0c521b7e7bd914 Mon Sep 17 00:00:00 2001 From: Twentysix Date: Fri, 23 Sep 2016 09:41:29 +0200 Subject: [PATCH] [Utils] General escape function Allows to escape formatting and mass mentions --- cogs/utils/chat_formatting.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/cogs/utils/chat_formatting.py b/cogs/utils/chat_formatting.py index 64cabf611..c94c401d3 100644 --- a/cogs/utils/chat_formatting.py +++ b/cogs/utils/chat_formatting.py @@ -58,12 +58,16 @@ def strikethrough(text): def underline(text): return "__{}__".format(text) +def escape(text, *, mass_mentions=False, formatting=False): + if mass_mentions: + text = text.replace("@everyone", "@\u200beveryone") + text = text.replace("@here", "@\u200bhere") + if formatting: + text = (text.replace("`", "\\`") + .replace("*", "\\*") + .replace("_", "\\_") + .replace("~", "\\~")) + return text def escape_mass_mentions(text): - words = { - "@everyone": "@\u200beveryone", - "@here": "@\u200bhere" - } - for k, v in words.items(): - text = text.replace(k, v) - return text + return escape(text, mass_mentions=True)