Cleaned up MentionUtils

This commit is contained in:
RogueException
2016-07-13 13:43:47 -03:00
parent efc87ef06d
commit dc6ee3510a
2 changed files with 17 additions and 14 deletions

View File

@@ -114,7 +114,7 @@ namespace Discord
if (guildChannel != null) if (guildChannel != null)
{ {
MentionedUsers = MentionUtils.GetUserMentions(text, Channel.IsAttached ? Channel : null, MentionedUsers); MentionedUsers = MentionUtils.GetUserMentions(text, Channel, MentionedUsers);
MentionedChannelIds = MentionUtils.GetChannelMentions(text, guildChannel.Guild); MentionedChannelIds = MentionUtils.GetChannelMentions(text, guildChannel.Guild);
MentionedRoles = MentionUtils.GetRoleMentions(text, guildChannel.Guild); MentionedRoles = MentionUtils.GetRoleMentions(text, guildChannel.Guild);
} }
@@ -173,7 +173,6 @@ namespace Discord
text = MentionUtils.ResolveUserMentions(text, Channel, MentionedUsers, userMode); text = MentionUtils.ResolveUserMentions(text, Channel, MentionedUsers, userMode);
if (guild != null) if (guild != null)
{ {
if (guild.IsAttached) //It's too expensive to do a channel lookup in REST mode
text = MentionUtils.ResolveChannelMentions(text, guild); text = MentionUtils.ResolveChannelMentions(text, guild);
text = MentionUtils.ResolveRoleMentions(text, guild, MentionedRoles); text = MentionUtils.ResolveRoleMentions(text, guild, MentionedRoles);
} }

View File

@@ -99,7 +99,7 @@ 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 != null) 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)
{ {
@@ -194,6 +194,8 @@ namespace Discord
})); }));
} }
internal static string ResolveChannelMentions(string text, IGuild guild) internal static string ResolveChannelMentions(string text, IGuild guild)
{
if (guild.IsAttached) //It's too expensive to do a channel lookup in REST mode
{ {
return _channelRegex.Replace(text, new MatchEvaluator(e => return _channelRegex.Replace(text, new MatchEvaluator(e =>
{ {
@@ -208,6 +210,8 @@ namespace Discord
return e.Value; return e.Value;
})); }));
} }
return text;
}
internal static string ResolveRoleMentions(string text, IGuild guild, IReadOnlyCollection<IRole> mentions) internal static string ResolveRoleMentions(string text, IGuild guild, IReadOnlyCollection<IRole> mentions)
{ {
return _roleRegex.Replace(text, new MatchEvaluator(e => return _roleRegex.Replace(text, new MatchEvaluator(e =>