diff --git a/src/Discord.Net/Models/Message.cs b/src/Discord.Net/Models/Message.cs index bd5792c68..b0c993413 100644 --- a/src/Discord.Net/Models/Message.cs +++ b/src/Discord.Net/Models/Message.cs @@ -23,17 +23,32 @@ namespace Discord { private readonly static Action _cloner = DynamicIL.CreateCopyMethod(); - private static readonly Regex _userRegex = new Regex(@"<@[0-9]+>"); - private static readonly Regex _channelRegex = new Regex(@"<#[0-9]+>"); - private static readonly Regex _roleRegex = new Regex(@"<@&[0-9]+>"); + private static readonly Regex _userRegex = new Regex(@"<@[0-9]+>", RegexOptions.Compiled); + private static readonly Regex _userNicknameRegex = new Regex(@"<@![0-9]+>", RegexOptions.Compiled); + private static readonly Regex _channelRegex = new Regex(@"<#[0-9]+>", RegexOptions.Compiled); + private static readonly Regex _roleRegex = new Regex(@"<@&[0-9]+>", RegexOptions.Compiled); private static readonly Attachment[] _initialAttachments = new Attachment[0]; private static readonly Embed[] _initialEmbeds = new Embed[0]; internal static string CleanUserMentions(Channel channel, string text, List users = null) { + ulong id; + text = _userNicknameRegex.Replace(text, new MatchEvaluator(e => + { + if (e.Value.Substring(3, e.Value.Length - 4).TryToId(out id)) + { + var user = channel.GetUserFast(id); + if (user != null) + { + if (users != null) + users.Add(user); + return '@' + user.Nickname; + } + } + return e.Value; //User not found or parse failed + })); return _userRegex.Replace(text, new MatchEvaluator(e => { - ulong id; if (e.Value.Substring(2, e.Value.Length - 3).TryToId(out id)) { var user = channel.GetUserFast(id); @@ -43,7 +58,7 @@ namespace Discord users.Add(user); return '@' + user.Name; } - } + } return e.Value; //User not found or parse failed })); } diff --git a/src/Discord.Net/Models/User.cs b/src/Discord.Net/Models/User.cs index ca2273786..3a2af6a12 100644 --- a/src/Discord.Net/Models/User.cs +++ b/src/Discord.Net/Models/User.cs @@ -83,6 +83,8 @@ namespace Discord public Channel PrivateChannel => Client.GetPrivateChannel(Id); /// Returns the string used to mention this user. public string Mention => $"<@{Id}>"; + /// Returns the string used to mention this user by nickname. + public string NicknameMention => $"<@!{Id}>"; /// Returns true if this user has marked themselves as muted. public bool IsSelfMuted => (_voiceState & VoiceState.SelfMuted) != 0; /// Returns true if this user has marked themselves as deafened.