Browse Source

Added Message.MentionedChannels

tags/docs-0.9
RogueException 9 years ago
parent
commit
44ebf50d81
3 changed files with 42 additions and 7 deletions
  1. +14
    -3
      src/Discord.Net/DiscordClient.Messages.cs
  2. +7
    -0
      src/Discord.Net/Helpers/Mention.cs
  3. +21
    -4
      src/Discord.Net/Models/Message.cs

+ 14
- 3
src/Discord.Net/DiscordClient.Messages.cs View File

@@ -125,6 +125,7 @@ namespace Discord
var userIds = !channel.IsPrivate ? Mention.GetUserIds(text).Distinct() : new string[0];
if (Config.UseMessageQueue)
{
var channelIds = !channel.IsPrivate ? Mention.GetChannelIds(text).Distinct() : new string[0];
var nonce = GenerateNonce();
msg = _messages.GetOrAdd("nonce_" + nonce, channel.Id, _userId);
var currentUser = msg.User;
@@ -136,9 +137,19 @@ namespace Discord
ChannelId = channel.Id,
IsTextToSpeech = isTextToSpeech
});
msg.Mentions = userIds.Select(x => _users[x, channel.Server.Id]).Where(x => x != null).ToArray();
msg.IsQueued = true;
msg.Nonce = nonce;
msg.IsQueued = true;

//IsPrivate check is already done earlier
msg.MentionedUsers = userIds
.Select(x => _users[x, channel.Server.Id])
.Where(x => x != null)
.ToArray();
msg.MentionedChannels = channelIds
.Select(x => _channels[x])
.Where(x => x != null && x.Server == channel.Server)
.ToArray();

_pendingMessages.Enqueue(msg);
}
else
@@ -258,7 +269,7 @@ namespace Discord
SendMessageResponse response = null;
try
{
response = await _api.SendMessage(msg.Channel.Id, msg.RawText, msg.Mentions.Select(x => x.Id), msg.Nonce, msg.IsTTS).ConfigureAwait(false);
response = await _api.SendMessage(msg.Channel.Id, msg.RawText, msg.MentionedUsers.Select(x => x.Id), msg.Nonce, msg.IsTTS).ConfigureAwait(false);
}
catch (WebException) { break; }
catch (HttpException) { hasFailed = true; }


+ 7
- 0
src/Discord.Net/Helpers/Mention.cs View File

@@ -49,5 +49,12 @@ namespace Discord
.Select(x => x.Groups[1].Value)
.Where(x => x != null);
}
internal static IEnumerable<string> GetChannelIds(string text)
{
return _channelRegex.Matches(text)
.OfType<Match>()
.Select(x => x.Groups[1].Value)
.Where(x => x != null);
}
}
}

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

@@ -125,8 +125,12 @@ namespace Discord
/// <summary> Returns a collection of all users mentioned in this message. </summary>
[JsonIgnore]
public IEnumerable<User> Mentions { get; internal set; }
public IEnumerable<User> MentionedUsers { get; internal set; }

/// <summary> Returns a collection of all channels mentioned in this message. </summary>
[JsonIgnore]
public IEnumerable<Channel> MentionedChannels { get; internal set; }

/// <summary> Returns the server containing the channel this message was sent to. </summary>
[JsonIgnore]
public Server Server => _channel.Value.Server;
@@ -208,13 +212,26 @@ namespace Discord
EditedTimestamp = model.EditedTimestamp;
if (model.Mentions != null)
{
Mentions = model.Mentions.Select(x => _client.Users[x.Id, Channel.Server?.Id]).ToArray();
IsMentioningMe = model.Mentions.Any(x => x.Id == _client.CurrentUserId);
MentionedUsers = model.Mentions
.Select(x => _client.Users[x.Id, Channel.Server?.Id])
.ToArray();
IsMentioningMe = model.Mentions
.Any(x => x.Id == _client.CurrentUserId);
}
if (model.Content != null)
{
RawText = model.Content;
_cleanText = null;
if (!Channel.IsPrivate)
{
MentionedChannels = Mention.GetChannelIds(model.Content)
.Select(x => _client.Channels[x])
.Where(x => x.Server == Channel.Server)
.ToArray();
}
else
MentionedChannels = new Channel[0];
}
}



Loading…
Cancel
Save