Renamed Message.Text to RawText and added lazy-loaded Text with mention conversion.
This commit is contained in:
@@ -6,6 +6,7 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -16,6 +17,8 @@ namespace Discord
|
||||
{
|
||||
private DiscordWebSocket _webSocket;
|
||||
private ManualResetEventSlim _isStopping;
|
||||
private readonly Regex _userRegex, _channelRegex;
|
||||
private readonly MatchEvaluator _userRegexEvaluator, _channelRegexEvaluator;
|
||||
|
||||
/// <summary> Returns the User object for the current logged in user. </summary>
|
||||
public User User { get; private set; }
|
||||
@@ -61,6 +64,27 @@ namespace Discord
|
||||
{
|
||||
_isStopping = new ManualResetEventSlim(false);
|
||||
|
||||
_userRegex = new Regex(@"<@\d+?>", RegexOptions.Compiled);
|
||||
_channelRegex = new Regex(@"<#\d+?>", RegexOptions.Compiled);
|
||||
_userRegexEvaluator = new MatchEvaluator(e =>
|
||||
{
|
||||
string id = e.Value.Substring(2, e.Value.Length - 3);
|
||||
var user = _users[id];
|
||||
if (user != null)
|
||||
return '@' + user.Name;
|
||||
else //User not found
|
||||
return e.Value;
|
||||
});
|
||||
_channelRegexEvaluator = new MatchEvaluator(e =>
|
||||
{
|
||||
string id = e.Value.Substring(2, e.Value.Length - 3);
|
||||
var channel = _channels[id];
|
||||
if (channel != null)
|
||||
return channel.Name;
|
||||
else //Channel not found
|
||||
return e.Value;
|
||||
});
|
||||
|
||||
_servers = new AsyncCache<Server, API.Models.ServerReference>(
|
||||
(key, parentKey) => new Server(key, this),
|
||||
(server, model) =>
|
||||
@@ -148,10 +172,10 @@ namespace Discord
|
||||
message.IsTTS = extendedModel.IsTextToSpeech;
|
||||
message.MentionIds = extendedModel.Mentions?.Select(x => x.Id)?.ToArray() ?? new string[0];
|
||||
message.IsMentioningMe = message.MentionIds.Contains(UserId);
|
||||
message.RawText = extendedModel.Content;
|
||||
message.Timestamp = extendedModel.Timestamp;
|
||||
if (extendedModel.Author != null)
|
||||
message.UserId = extendedModel.Author.Id;
|
||||
message.Timestamp = extendedModel.Timestamp;
|
||||
message.Text = extendedModel.Content;
|
||||
}
|
||||
},
|
||||
message => { }
|
||||
@@ -1060,6 +1084,12 @@ namespace Discord
|
||||
{
|
||||
if (!_isReady)
|
||||
throw new InvalidOperationException("The client is not currently connected to Discord");
|
||||
}
|
||||
internal string CleanMessageText(string text)
|
||||
{
|
||||
text = _userRegex.Replace(text, _userRegexEvaluator);
|
||||
text = _channelRegex.Replace(text, _channelRegexEvaluator);
|
||||
return text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -2,11 +2,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Discord
|
||||
{
|
||||
public sealed class Message
|
||||
{
|
||||
|
||||
public struct Attachment
|
||||
{
|
||||
public string Id;
|
||||
@@ -17,7 +20,8 @@ namespace Discord
|
||||
}
|
||||
|
||||
private readonly DiscordClient _client;
|
||||
|
||||
private string _cleanText;
|
||||
|
||||
/// <summary> Returns the unique identifier for this message. </summary>
|
||||
public string Id { get; }
|
||||
|
||||
@@ -28,8 +32,11 @@ namespace Discord
|
||||
public bool IsMentioningEveryone { get; internal set; }
|
||||
/// <summary> Returns true if the message was sent as text-to-speech by someone with permissions to do so. </summary>
|
||||
public bool IsTTS { get; internal set; }
|
||||
/// <summary> Returns the content of this message. </summary>
|
||||
public string Text { get; internal set; }
|
||||
/// <summary> Returns the raw content of this message as it was received from the server.. </summary>
|
||||
public string RawText { get; internal set; }
|
||||
/// <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>
|
||||
public string Text => _cleanText != null ? _cleanText : (_cleanText = _client.CleanMessageText(RawText));
|
||||
/// <summary> Returns the timestamp of this message. </summary>
|
||||
public DateTime Timestamp { get; internal set; }
|
||||
/// <summary> Returns the attachments included in this message. </summary>
|
||||
@@ -68,5 +75,5 @@ namespace Discord
|
||||
{
|
||||
return User.ToString() + ": " + Text;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,8 +36,9 @@
|
||||
"System.Linq": "4.0.0",
|
||||
"System.Net.Requests": "4.0.10",
|
||||
"System.Net.WebSockets.Client": "4.0.0-beta-23123",
|
||||
"System.Runtime": "4.0.20"
|
||||
"System.Runtime": "4.0.20",
|
||||
"System.Text.RegularExpressions": "4.0.10"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user