@@ -160,6 +160,9 @@ | |||||
<Compile Include="..\Discord.Net\DiscordSimpleClientConfig.cs"> | <Compile Include="..\Discord.Net\DiscordSimpleClientConfig.cs"> | ||||
<Link>DiscordSimpleClientConfig.cs</Link> | <Link>DiscordSimpleClientConfig.cs</Link> | ||||
</Compile> | </Compile> | ||||
<Compile Include="..\Discord.Net\Enums\AvatarImageType.cs"> | |||||
<Link>Enums\AvatarImageType.cs</Link> | |||||
</Compile> | |||||
<Compile Include="..\Discord.Net\Enums\ChannelTypes.cs"> | <Compile Include="..\Discord.Net\Enums\ChannelTypes.cs"> | ||||
<Link>Enums\ChannelTypes.cs</Link> | <Link>Enums\ChannelTypes.cs</Link> | ||||
</Compile> | </Compile> | ||||
@@ -184,8 +187,8 @@ | |||||
<Compile Include="..\Discord.Net\Helpers\Mention.cs"> | <Compile Include="..\Discord.Net\Helpers\Mention.cs"> | ||||
<Link>Helpers\Mention.cs</Link> | <Link>Helpers\Mention.cs</Link> | ||||
</Compile> | </Compile> | ||||
<Compile Include="..\Discord.Net\Helpers\MessageCleaner.cs"> | |||||
<Link>Helpers\MessageCleaner.cs</Link> | |||||
<Compile Include="..\Discord.Net\Helpers\MentionHelper.cs"> | |||||
<Link>Helpers\MentionHelper.cs</Link> | |||||
</Compile> | </Compile> | ||||
<Compile Include="..\Discord.Net\Helpers\Shared\CollectionHelper.cs"> | <Compile Include="..\Discord.Net\Helpers\Shared\CollectionHelper.cs"> | ||||
<Link>Helpers\Shared\CollectionHelper.cs</Link> | <Link>Helpers\Shared\CollectionHelper.cs</Link> | ||||
@@ -2,11 +2,9 @@ | |||||
{ | { | ||||
public sealed class Messages : AsyncCollection<Message> | public sealed class Messages : AsyncCollection<Message> | ||||
{ | { | ||||
private readonly MessageCleaner _msgCleaner; | |||||
internal Messages(DiscordClient client, object writerLock) | internal Messages(DiscordClient client, object writerLock) | ||||
: base(client, writerLock) | : base(client, writerLock) | ||||
{ | { | ||||
_msgCleaner = new MessageCleaner(client); | |||||
} | } | ||||
internal Message GetOrAdd(string id, string channelId, string userId) => GetOrAdd(id, () => new Message(_client, id, channelId, userId)); | internal Message GetOrAdd(string id, string channelId, string userId) => GetOrAdd(id, () => new Message(_client, id, channelId, userId)); | ||||
@@ -29,7 +27,5 @@ | |||||
} | } | ||||
internal Message this[string id] => Get(id); | internal Message this[string id] => Get(id); | ||||
internal string CleanText(string text) => _msgCleaner.Clean(text); | |||||
} | } | ||||
} | } |
@@ -0,0 +1,48 @@ | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Text.RegularExpressions; | |||||
namespace Discord | |||||
{ | |||||
internal static class MentionHelper | |||||
{ | |||||
private static readonly Regex _userRegex, _channelRegex; | |||||
static MentionHelper() | |||||
{ | |||||
_userRegex = new Regex(@"<@(\d+?)>", RegexOptions.Compiled); | |||||
_channelRegex = new Regex(@"<#(\d+?)>", RegexOptions.Compiled); | |||||
} | |||||
public static string ConvertToNames(DiscordClient client, string text) | |||||
{ | |||||
text = _userRegex.Replace(text, new MatchEvaluator(e => | |||||
{ | |||||
string id = e.Value.Substring(2, e.Value.Length - 3); | |||||
var user = client.Users[id]; | |||||
if (user != null) | |||||
return '@' + user.Name; | |||||
else //User not found | |||||
return e.Value; | |||||
})); | |||||
text = _channelRegex.Replace(text, new MatchEvaluator(e => | |||||
{ | |||||
string id = e.Value.Substring(2, e.Value.Length - 3); | |||||
var channel = client.Channels[id]; | |||||
if (channel != null) | |||||
return channel.Name; | |||||
else //Channel not found | |||||
return e.Value; | |||||
})); | |||||
return text; | |||||
} | |||||
public static IEnumerable<string> GetUserIds(string text) | |||||
{ | |||||
return _userRegex.Matches(text) | |||||
.OfType<Match>() | |||||
.Select(x => x.Groups[1].Value) | |||||
.Where(x => x != null); | |||||
} | |||||
} | |||||
} |
@@ -1,43 +0,0 @@ | |||||
using System.Text.RegularExpressions; | |||||
namespace Discord | |||||
{ | |||||
//TODO: Better name please? | |||||
internal class MessageCleaner | |||||
{ | |||||
private readonly Regex _userRegex, _channelRegex; | |||||
private readonly MatchEvaluator _userRegexEvaluator, _channelRegexEvaluator; | |||||
public MessageCleaner(DiscordClient client) | |||||
{ | |||||
_userRegex = new Regex(@"<@\d+?>", RegexOptions.Compiled); | |||||
_userRegexEvaluator = new MatchEvaluator(e => | |||||
{ | |||||
string id = e.Value.Substring(2, e.Value.Length - 3); | |||||
var user = client.Users[id]; | |||||
if (user != null) | |||||
return '@' + user.Name; | |||||
else //User not found | |||||
return e.Value; | |||||
}); | |||||
_channelRegex = new Regex(@"<#\d+?>", RegexOptions.Compiled); | |||||
_channelRegexEvaluator = new MatchEvaluator(e => | |||||
{ | |||||
string id = e.Value.Substring(2, e.Value.Length - 3); | |||||
var channel = client.Channels[id]; | |||||
if (channel != null) | |||||
return channel.Name; | |||||
else //Channel not found | |||||
return e.Value; | |||||
}); | |||||
} | |||||
public string Clean(string text) | |||||
{ | |||||
text = _userRegex.Replace(text, _userRegexEvaluator); | |||||
text = _channelRegex.Replace(text, _channelRegexEvaluator); | |||||
return text; | |||||
} | |||||
} | |||||
} |
@@ -114,7 +114,7 @@ namespace Discord | |||||
public string RawText { get; private set; } | public string RawText { get; private set; } | ||||
/// <summary> Returns the content of this message with any special references such as mentions converted. </summary> | /// <summary> Returns the content of this message with any special references such as mentions converted. </summary> | ||||
/// <remarks> This value is lazy loaded and only processed on first request. Each subsequent request will pull from cache. </remarks> | /// <remarks> This value is lazy loaded and only processed on first request. Each subsequent request will pull from cache. </remarks> | ||||
public string Text => _cleanText != null ? _cleanText : (_cleanText = _client.Messages.CleanText(RawText)); | |||||
public string Text => _cleanText != null ? _cleanText : (_cleanText = MentionHelper.ConvertToNames(_client, RawText)); | |||||
/// <summary> Returns the timestamp for when this message was sent. </summary> | /// <summary> Returns the timestamp for when this message was sent. </summary> | ||||
public DateTime Timestamp { get; private set; } | public DateTime Timestamp { get; private set; } | ||||
/// <summary> Returns the timestamp for when this message was last edited. </summary> | /// <summary> Returns the timestamp for when this message was last edited. </summary> | ||||