From f059c0c603d61c4c1b007270919fea8257b82e65 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 11 Aug 2024 11:31:21 +1000 Subject: [PATCH] Move markdown conversion methods to Utils class --- MCGalaxy/MCGalaxy_.csproj | 1 + MCGalaxy/Modules/Relay/Discord/DiscordBot.cs | 58 +++------------ .../Modules/Relay/Discord/DiscordUtils.cs | 70 +++++++++++++++++++ 3 files changed, 81 insertions(+), 48 deletions(-) create mode 100644 MCGalaxy/Modules/Relay/Discord/DiscordUtils.cs diff --git a/MCGalaxy/MCGalaxy_.csproj b/MCGalaxy/MCGalaxy_.csproj index d455a71bc..cc3c5db54 100644 --- a/MCGalaxy/MCGalaxy_.csproj +++ b/MCGalaxy/MCGalaxy_.csproj @@ -601,6 +601,7 @@ + diff --git a/MCGalaxy/Modules/Relay/Discord/DiscordBot.cs b/MCGalaxy/Modules/Relay/Discord/DiscordBot.cs index 484536885..efd1ed505 100644 --- a/MCGalaxy/Modules/Relay/Discord/DiscordBot.cs +++ b/MCGalaxy/Modules/Relay/Discord/DiscordBot.cs @@ -169,7 +169,7 @@ void LoadReplacements() { ChatTokens.LoadTokens(lines, (phrase, replacement) => { filter_triggers.Add(phrase); - filter_replacements.Add(MarkdownToSpecial(replacement)); + filter_replacements.Add(DiscordUtils.MarkdownToSpecial(replacement)); }); } @@ -465,19 +465,8 @@ protected override void DoSendMessage(string channel, string message) { protected string ConvertMessage(string message) { message = ConvertMessageCommon(message); message = Colors.StripUsed(message); - message = EscapeMarkdown(message); - message = SpecialToMarkdown(message); - return message; - } - - static readonly string[] markdown_special = { @"\", @"*", @"_", @"~", @"`", @"|", @"-", @"#" }; - static readonly string[] markdown_escaped = { @"\\", @"\*", @"\_", @"\~", @"\`", @"\|", @"\-", @"\#" }; - static string EscapeMarkdown(string message) { - // don't let user use bold/italic etc markdown - for (int i = 0; i < markdown_special.Length; i++) - { - message = message.Replace(markdown_special[i], markdown_escaped[i]); - } + message = DiscordUtils.EscapeMarkdown(message); + message = DiscordUtils.SpecialToMarkdown(message); return message; } @@ -546,7 +535,7 @@ static string FormatNick(Player p, Player pl) { return string.Format(format, p.FormatNick(pl), // level name must not have _ escaped as the level name is in a code block - // otherwise the escaped "\_" actually shows as "\_" instead of "_" - pl.level.name.Replace('_', UNDERSCORE), + pl.level.name.Replace('_', DiscordUtils.UNDERSCORE), flags); } @@ -568,38 +557,11 @@ void AddGameStatus(ChannelSendEmbed embed) { } - // these characters are chosen specifically to lie within the unspecified unicode range, - // as those characters are "application defined" (EDCX = Escaped Discord Character #X) - // https://en.wikipedia.org/wiki/Private_Use_Areas - const char UNDERSCORE = '\uEDC1'; // _ - const char TILDE = '\uEDC2'; // ~ - const char STAR = '\uEDC3'; // * - const char GRAVE = '\uEDC4'; // ` - const char BAR = '\uEDC5'; // | - - public const string UNDERLINE = "\uEDC1\uEDC1"; // __ - public const string BOLD = "\uEDC3\uEDC3"; // ** - public const string ITALIC = "\uEDC1"; // _ - public const string CODE = "\uEDC4"; // ` - public const string SPOILER = "\uEDC5\uEDC5"; // || - public const string STRIKETHROUGH = "\uEDC2\uEDC2"; // ~~ - - static string MarkdownToSpecial(string input) { - return input - .Replace('_', UNDERSCORE) - .Replace('~', TILDE) - .Replace('*', STAR) - .Replace('`', GRAVE) - .Replace('|', BAR); - } - - static string SpecialToMarkdown(string input) { - return input - .Replace(UNDERSCORE, '_') - .Replace(TILDE, '~') - .Replace(STAR, '*') - .Replace(GRAVE, '`') - .Replace(BAR, '|'); - } + public const string UNDERLINE = DiscordUtils.UNDERLINE; + public const string BOLD = DiscordUtils.BOLD; + public const string ITALIC = DiscordUtils.ITALIC; + public const string CODE = DiscordUtils.CODE; + public const string SPOILER = DiscordUtils.SPOILER; + public const string STRIKETHROUGH = DiscordUtils.STRIKETHROUGH; } } diff --git a/MCGalaxy/Modules/Relay/Discord/DiscordUtils.cs b/MCGalaxy/Modules/Relay/Discord/DiscordUtils.cs new file mode 100644 index 000000000..5c281f9ab --- /dev/null +++ b/MCGalaxy/Modules/Relay/Discord/DiscordUtils.cs @@ -0,0 +1,70 @@ +/* + Copyright 2015 MCGalaxy + + Dual-licensed under the Educational Community License, Version 2.0 and + the GNU General Public License, Version 3 (the "Licenses"); you may + not use this file except in compliance with the Licenses. You may + obtain a copy of the Licenses at + + https://opensource.org/license/ecl-2-0/ + https://www.gnu.org/licenses/gpl-3.0.html + + Unless required by applicable law or agreed to in writing, + software distributed under the Licenses are distributed on an "AS IS" + BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + or implied. See the Licenses for the specific language governing + permissions and limitations under the Licenses. + */ +using System; + +namespace MCGalaxy.Modules.Relay.Discord +{ + public static class DiscordUtils + { + static readonly string[] markdown_special = { @"\", @"*", @"_", @"~", @"`", @"|", @"-", @"#" }; + static readonly string[] markdown_escaped = { @"\\", @"\*", @"\_", @"\~", @"\`", @"\|", @"\-", @"\#" }; + public static string EscapeMarkdown(string message) { + // don't let user use bold/italic etc markdown + for (int i = 0; i < markdown_special.Length; i++) + { + message = message.Replace(markdown_special[i], markdown_escaped[i]); + } + return message; + } + + + // these characters are chosen specifically to lie within the unspecified unicode range, + // as those characters are "application defined" (EDCX = Escaped Discord Character #X) + // https://en.wikipedia.org/wiki/Private_Use_Areas + public const char UNDERSCORE = '\uEDC1'; // _ + public const char TILDE = '\uEDC2'; // ~ + public const char STAR = '\uEDC3'; // * + public const char GRAVE = '\uEDC4'; // ` + public const char BAR = '\uEDC5'; // | + + public const string UNDERLINE = "\uEDC1\uEDC1"; // __ + public const string BOLD = "\uEDC3\uEDC3"; // ** + public const string ITALIC = "\uEDC1"; // _ + public const string CODE = "\uEDC4"; // ` + public const string SPOILER = "\uEDC5\uEDC5"; // || + public const string STRIKETHROUGH = "\uEDC2\uEDC2"; // ~~ + + public static string MarkdownToSpecial(string input) { + return input + .Replace('_', UNDERSCORE) + .Replace('~', TILDE) + .Replace('*', STAR) + .Replace('`', GRAVE) + .Replace('|', BAR); + } + + public static string SpecialToMarkdown(string input) { + return input + .Replace(UNDERSCORE, '_') + .Replace(TILDE, '~') + .Replace(STAR, '*') + .Replace(GRAVE, '`') + .Replace(BAR, '|'); + } + } +}