Browse Source

Renamed Message.Text to RawText and added lazy-loaded Text with mention conversion.

tags/docs-0.9
Brandon Smith 10 years ago
parent
commit
d34f3f9db1
3 changed files with 46 additions and 8 deletions
  1. +32
    -2
      src/Discord.Net/DiscordClient.cs
  2. +11
    -4
      src/Discord.Net/Message.cs
  3. +3
    -2
      src/Discord.Net/project.json

+ 32
- 2
src/Discord.Net/DiscordClient.cs View File

@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;


@@ -16,6 +17,8 @@ namespace Discord
{ {
private DiscordWebSocket _webSocket; private DiscordWebSocket _webSocket;
private ManualResetEventSlim _isStopping; 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> /// <summary> Returns the User object for the current logged in user. </summary>
public User User { get; private set; } public User User { get; private set; }
@@ -61,6 +64,27 @@ namespace Discord
{ {
_isStopping = new ManualResetEventSlim(false); _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>( _servers = new AsyncCache<Server, API.Models.ServerReference>(
(key, parentKey) => new Server(key, this), (key, parentKey) => new Server(key, this),
(server, model) => (server, model) =>
@@ -148,10 +172,10 @@ namespace Discord
message.IsTTS = extendedModel.IsTextToSpeech; message.IsTTS = extendedModel.IsTextToSpeech;
message.MentionIds = extendedModel.Mentions?.Select(x => x.Id)?.ToArray() ?? new string[0]; message.MentionIds = extendedModel.Mentions?.Select(x => x.Id)?.ToArray() ?? new string[0];
message.IsMentioningMe = message.MentionIds.Contains(UserId); message.IsMentioningMe = message.MentionIds.Contains(UserId);
message.RawText = extendedModel.Content;
message.Timestamp = extendedModel.Timestamp;
if (extendedModel.Author != null) if (extendedModel.Author != null)
message.UserId = extendedModel.Author.Id; message.UserId = extendedModel.Author.Id;
message.Timestamp = extendedModel.Timestamp;
message.Text = extendedModel.Content;
} }
}, },
message => { } message => { }
@@ -1060,6 +1084,12 @@ namespace Discord
{ {
if (!_isReady) if (!_isReady)
throw new InvalidOperationException("The client is not currently connected to Discord"); 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> /// <summary>


+ 11
- 4
src/Discord.Net/Message.cs View File

@@ -2,11 +2,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace Discord namespace Discord
{ {
public sealed class Message public sealed class Message
{ {

public struct Attachment public struct Attachment
{ {
public string Id; public string Id;
@@ -17,7 +20,8 @@ namespace Discord
} }


private readonly DiscordClient _client; private readonly DiscordClient _client;

private string _cleanText;
/// <summary> Returns the unique identifier for this message. </summary> /// <summary> Returns the unique identifier for this message. </summary>
public string Id { get; } public string Id { get; }


@@ -28,8 +32,11 @@ namespace Discord
public bool IsMentioningEveryone { get; internal set; } 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> /// <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; } 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> /// <summary> Returns the timestamp of this message. </summary>
public DateTime Timestamp { get; internal set; } public DateTime Timestamp { get; internal set; }
/// <summary> Returns the attachments included in this message. </summary> /// <summary> Returns the attachments included in this message. </summary>
@@ -68,5 +75,5 @@ namespace Discord
{ {
return User.ToString() + ": " + Text; return User.ToString() + ": " + Text;
} }
}
}
} }

+ 3
- 2
src/Discord.Net/project.json View File

@@ -36,8 +36,9 @@
"System.Linq": "4.0.0", "System.Linq": "4.0.0",
"System.Net.Requests": "4.0.10", "System.Net.Requests": "4.0.10",
"System.Net.WebSockets.Client": "4.0.0-beta-23123", "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"
} }
} }
} }
}
}

Loading…
Cancel
Save