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,19 +99,19 @@ 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;
//Verify this user was actually mentioned
foreach (var userMention in mentionedUsers)
{
if (userMention.Id == id)
{
if (channel.IsAttached) //Waiting this sync is safe because it's using a cache if (channel.IsAttached) //Waiting this sync is safe because it's using a cache
user = channel.GetUserAsync(id).GetAwaiter().GetResult() as IUser; user = channel.GetUserAsync(id).GetAwaiter().GetResult() as IUser;
if (user == null) if (user == null) //User not found, fallback to basic mention info
{ user = userMention;
foreach (var fallbackUser in fallbackUsers)
{
if (fallbackUser.Id == id)
{
user = fallbackUser;
break; break;
} }
} }
}
if (user != null) if (user != null)
builder.Add(user); builder.Add(user);