From cd408fea49e872c0b6083486d2eacfd916bdc001 Mon Sep 17 00:00:00 2001 From: Brandon Smith Date: Sat, 5 Sep 2015 16:17:52 -0300 Subject: [PATCH] Moved mentions to MessageHelper, added Escape() --- src/Discord.Net/MessageHelper.cs | 46 ++++++++++++++++++++++++++++++++ src/Discord.Net/Models/User.cs | 3 --- 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/Discord.Net/MessageHelper.cs diff --git a/src/Discord.Net/MessageHelper.cs b/src/Discord.Net/MessageHelper.cs new file mode 100644 index 000000000..06f05350f --- /dev/null +++ b/src/Discord.Net/MessageHelper.cs @@ -0,0 +1,46 @@ +using System.Text; + +namespace Discord +{ + public static class MessageHelper + { + private static char[] specialChars = new char[] {'_', '*', '\\' }; //Backslash must always be last! + + /// Removes all special formatting characters from the provided text. + public static string Escape(string text) + { + if (text.IndexOfAny(specialChars) >= 0) + { + StringBuilder builder = new StringBuilder(text); + foreach (char c in specialChars) + { + int length = builder.Length; + for (int i = 0; i < length; i++) + { + if (builder[i] == c) + { + builder.Insert(i, '\\'); + length++; + } + } + } + return builder.ToString(); + } + return text; + } + + /// Returns the string used to create a user mention. + public static string User(User user) + => $"<@{user.Id}>"; + /// Returns the string used to create a user mention. + public static string User(string userId) + => $"<@{userId}>"; + + /// Returns the string used to create a channel mention. + public static string Channel(Channel channel) + => $"<#{channel.Id}>"; + /// Returns the string used to create a channel mention. + public static string Channel(string channelId) + => $"<#{channelId}>"; + } +} diff --git a/src/Discord.Net/Models/User.cs b/src/Discord.Net/Models/User.cs index 870341130..19575d014 100644 --- a/src/Discord.Net/Models/User.cs +++ b/src/Discord.Net/Models/User.cs @@ -28,9 +28,6 @@ namespace Discord /// This field is only ever populated for the current logged in user. public bool IsVerified { get; internal set; } - /// Returns the string "<@Id>" to be used as a shortcut when including mentions in text. - public string Mention => $"<@{Id}>"; - public string PrivateChannelId { get; set; } public Channel PrivateChannel => _client.GetChannel(PrivateChannelId);