From f4e550bc1db9725577459f9a46d02da038e39c32 Mon Sep 17 00:00:00 2001 From: Rohan Date: Wed, 2 Dec 2020 21:54:14 +0530 Subject: Add sub_clyde utility function. --- bot/utils/messages.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 bot/utils/messages.py (limited to 'bot/utils/messages.py') diff --git a/bot/utils/messages.py b/bot/utils/messages.py new file mode 100644 index 00000000..a6c035f9 --- /dev/null +++ b/bot/utils/messages.py @@ -0,0 +1,19 @@ +import re +from typing import Optional + + +def sub_clyde(username: Optional[str]) -> Optional[str]: + """ + Replace "e"/"E" in any "clyde" in `username` with a Cyrillic "е"/"E" and return the new string. + + Discord disallows "clyde" anywhere in the username for webhooks. It will return a 400. + Return None only if `username` is None. + """ + def replace_e(match: re.Match) -> str: + char = "е" if match[2] == "e" else "Е" + return match[1] + char + + if username: + return re.sub(r"(clyd)(e)", replace_e, username, flags=re.I) + else: + return username # Empty string or None -- cgit v1.2.3