Use mentions array to validate detected mentions

This commit is contained in:
RogueException
2016-07-16 10:27:52 -03:00
parent 7eb06a6111
commit edb4941e04

View File

@@ -89,7 +89,7 @@ namespace Discord
return false; return false;
} }
internal static ImmutableArray<IUser> GetUserMentions(string text, IMessageChannel channel, IReadOnlyCollection<IUser> fallbackUsers) internal static ImmutableArray<IUser> GetUserMentions(string text, IMessageChannel channel, IReadOnlyCollection<IUser> mentionedUsers)
{ {
var matches = _userRegex.Matches(text); var matches = _userRegex.Matches(text);
var builder = ImmutableArray.CreateBuilder<IUser>(matches.Count); var builder = ImmutableArray.CreateBuilder<IUser>(matches.Count);
@@ -99,17 +99,17 @@ namespace Discord
if (ulong.TryParse(match.Groups[1].Value, NumberStyles.None, CultureInfo.InvariantCulture, out id)) if (ulong.TryParse(match.Groups[1].Value, NumberStyles.None, CultureInfo.InvariantCulture, out id))
{ {
IUser user = null; IUser user = null;
if (channel.IsAttached) //Waiting this sync is safe because it's using a cache
user = channel.GetUserAsync(id).GetAwaiter().GetResult() as IUser; //Verify this user was actually mentioned
if (user == null) foreach (var userMention in mentionedUsers)
{ {
foreach (var fallbackUser in fallbackUsers) if (userMention.Id == id)
{ {
if (fallbackUser.Id == id) if (channel.IsAttached) //Waiting this sync is safe because it's using a cache
{ user = channel.GetUserAsync(id).GetAwaiter().GetResult() as IUser;
user = fallbackUser; if (user == null) //User not found, fallback to basic mention info
break; user = userMention;
} break;
} }
} }