diff --git a/src/Discord.Net/Format.cs b/src/Discord.Net/Format.cs index 8b1d06bf8..d31f0ad0a 100644 --- a/src/Discord.Net/Format.cs +++ b/src/Discord.Net/Format.cs @@ -2,6 +2,9 @@ { public static class Format { + // Characters which need escaping + private static string[] SensitiveCharacters = { "*", "_", "~", "`", "\\" }; + /// Returns a markdown-formatted string with bold formatting. public static string Bold(string text) => $"**{text}**"; /// Returns a markdown-formatted string with italics formatting. @@ -19,5 +22,15 @@ else return $"`{text}`"; } + + /// Sanitizes the string, safely escaping any Markdown sequences. + public static string Sanitize(string text) + { + foreach (string unsafeChar in SensitiveCharacters) + { + text = text.Replace(unsafeChar, $"\\{unsafeChar}"); + } + return text; + } } }