Skip to content

Commit 3a94ef9

Browse files
committed
EscapeForPlainText
1 parent 4370698 commit 3a94ef9

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

CommandHandlers/UserCommands.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private static string ReplaceIfMultiple(string text, char c, char replacement)
141141
return text;
142142
}
143143

144-
/// <summary>Escapes user input for output. Best when wrapped in `backticks`.</summary>
144+
/// <summary>Escapes user input for output for when it will be wrapped `backticks`. For non-backtick version, use <see cref="EscapeForPlainText(string)"/>.</summary>
145145
/// <param name="text">The user input text.</param>
146146
/// <returns>The escaped result.</returns>
147147
public static string EscapeUserInput(string text)
@@ -174,6 +174,21 @@ public static string EscapeUserInput(string text)
174174
return text;
175175
}
176176

177+
/// <summary>Escapes user input for when output will NOT be wrapped in backticks. For the backtick-compatible version, use <see cref="EscapeUserInput(string)"/>.</summary>
178+
public static string EscapeForPlainText(string text)
179+
{
180+
if (text is null)
181+
{
182+
return "(null)";
183+
}
184+
text = text.Replace("discord.gg", "discord\\.gg");
185+
if (NeedsEscapeMatcher.ContainsAnyMatch(text))
186+
{
187+
text = text.Replace("\\", "\\\\").Replace("`", "\\`").Replace("<", "\\<").Replace("@", "\\@").Replace("#", "\\#").Replace("&", "\\&").Replace("*", "\\*").Replace("~", "\\~").Replace("_", "\\_").Replace("|", "\\|").Replace(":", "\\:");
188+
}
189+
return text;
190+
}
191+
177192
/// <summary>Generates a link to a Discord message.</summary>
178193
public static string LinkToMessage(IMessage message)
179194
{

0 commit comments

Comments
 (0)