Browse Source

Added IMessage.Emojis

tags/1.0-rc
RogueException 8 years ago
parent
commit
9e982ccd4a
10 changed files with 115 additions and 51 deletions
  1. +2
    -0
      src/Discord.Net.Core/API/CDN.cs
  2. +20
    -0
      src/Discord.Net.Core/Entities/Messages/Emoji.cs
  3. +2
    -0
      src/Discord.Net.Core/Entities/Messages/IMessage.cs
  4. +4
    -4
      src/Discord.Net.Rest/Entities/Messages/Attachment.cs
  5. +4
    -4
      src/Discord.Net.Rest/Entities/Messages/Embed.cs
  6. +20
    -0
      src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs
  7. +9
    -4
      src/Discord.Net.Rest/Entities/Messages/RestMessage.cs
  8. +15
    -12
      src/Discord.Net.Rest/Entities/Messages/RestUserMessage.cs
  9. +12
    -6
      src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs
  10. +27
    -21
      src/Discord.Net.WebSocket/Entities/Messages/SocketUserMessage.cs

+ 2
- 0
src/Discord.Net.Core/API/CDN.cs View File

@@ -12,5 +12,7 @@
=> splashId != null ? $"{DiscordConfig.CDNUrl}splashes/{guildId}/{splashId}.jpg" : null;
public static string GetChannelIconUrl(ulong channelId, string iconId)
=> iconId != null ? $"{DiscordConfig.CDNUrl}channel-icons/{channelId}/{iconId}.jpg" : null;
public static string GetEmojiUrl(ulong emojiId)
=> $"{DiscordConfig.CDNUrl}emojis/{emojiId}.png";
}
}

+ 20
- 0
src/Discord.Net.Core/Entities/Messages/Emoji.cs View File

@@ -0,0 +1,20 @@
using Discord.API;

namespace Discord
{
public struct Emoji
{
public ulong Id { get; }
public string Name { get; }
public int Index { get; }

public string Url => CDN.GetEmojiUrl(Id);

internal Emoji(ulong id, string name, int index)
{
Id = id;
Name = name;
Index = index;
}
}
}

+ 2
- 0
src/Discord.Net.Core/Entities/Messages/IMessage.cs View File

@@ -27,6 +27,8 @@ namespace Discord
IReadOnlyCollection<IAttachment> Attachments { get; }
/// <summary> Returns a collection of all embeds included in this message. </summary>
IReadOnlyCollection<IEmbed> Embeds { get; }
/// <summary> Returns a collection of all custom emoji included in this message. </summary>
IReadOnlyCollection<Emoji> Emojis { get; }
/// <summary> Returns a collection of channel ids mentioned in this message. </summary>
IReadOnlyCollection<ulong> MentionedChannelIds { get; }
/// <summary> Returns a collection of roles mentioned in this message. </summary>


src/Discord.Net.Rest/Entities/Messages/RestAttachment.cs → src/Discord.Net.Rest/Entities/Messages/Attachment.cs View File

@@ -4,7 +4,7 @@ using Model = Discord.API.Attachment;
namespace Discord
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestAttachment : IAttachment
public class Attachment : IAttachment
{
public ulong Id { get; }
public string Filename { get; }
@@ -14,7 +14,7 @@ namespace Discord
public int? Height { get; }
public int? Width { get; }

internal RestAttachment(ulong id, string filename, string url, string proxyUrl, int size, int? height, int? width)
internal Attachment(ulong id, string filename, string url, string proxyUrl, int size, int? height, int? width)
{
Id = id;
Filename = filename;
@@ -24,9 +24,9 @@ namespace Discord
Height = height;
Width = width;
}
internal static RestAttachment Create(Model model)
internal static Attachment Create(Model model)
{
return new RestAttachment(model.Id, model.Filename, model.Url, model.ProxyUrl, model.Size,
return new Attachment(model.Id, model.Filename, model.Url, model.ProxyUrl, model.Size,
model.Height.IsSpecified ? model.Height.Value : (int?)null,
model.Width.IsSpecified ? model.Width.Value : (int?)null);
}

src/Discord.Net.Rest/Entities/Messages/RestEmbed.cs → src/Discord.Net.Rest/Entities/Messages/Embed.cs View File

@@ -4,7 +4,7 @@ using Model = Discord.API.Embed;
namespace Discord
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public class RestEmbed : IEmbed
public class Embed : IEmbed
{
public string Description { get; }
public string Url { get; }
@@ -13,7 +13,7 @@ namespace Discord
public EmbedProvider? Provider { get; }
public EmbedThumbnail? Thumbnail { get; }

internal RestEmbed(string type, string title, string description, string url, EmbedProvider? provider, EmbedThumbnail? thumbnail)
internal Embed(string type, string title, string description, string url, EmbedProvider? provider, EmbedThumbnail? thumbnail)
{
Type = type;
Title = title;
@@ -22,9 +22,9 @@ namespace Discord
Provider = provider;
Thumbnail = thumbnail;
}
internal static RestEmbed Create(Model model)
internal static Embed Create(Model model)
{
return new RestEmbed(model.Type, model.Title, model.Description, model.Url,
return new Embed(model.Type, model.Title, model.Description, model.Url,
model.Provider.IsSpecified ? EmbedProvider.Create(model.Provider.Value) : (EmbedProvider?)null,
model.Thumbnail.IsSpecified ? EmbedThumbnail.Create(model.Thumbnail.Value) : (EmbedThumbnail?)null);
}

+ 20
- 0
src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs View File

@@ -1,11 +1,18 @@
using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Discord.Rest
{
internal static class MessageHelper
{
private static readonly Regex _emojiRegex = new Regex(@"<:(.+?):(\d+?)>", RegexOptions.Compiled);

public static async Task ModifyAsync(IMessage msg, BaseDiscordClient client, Action<ModifyMessageParams> func,
RequestOptions options)
{
@@ -29,5 +36,18 @@ namespace Discord.Rest
{
await client.ApiClient.RemovePinAsync(msg.ChannelId, msg.Id, options);
}

public static ImmutableArray<Emoji> GetEmojis(string text)
{
var matches = _emojiRegex.Matches(text);
var builder = ImmutableArray.CreateBuilder<Emoji>(matches.Count);
foreach (var match in matches.OfType<Match>())
{
ulong id;
if (ulong.TryParse(match.Groups[2].Value, NumberStyles.None, CultureInfo.InvariantCulture, out id))
builder.Add(new Emoji(id, match.Groups[1].Value, match.Index));
}
return builder.ToImmutable();
}
}
}

+ 9
- 4
src/Discord.Net.Rest/Entities/Messages/RestMessage.cs View File

@@ -19,11 +19,12 @@ namespace Discord.Rest
public virtual bool IsPinned => false;
public virtual bool IsWebhook => false;
public virtual DateTimeOffset? EditedTimestamp => null;
public virtual IReadOnlyCollection<IAttachment> Attachments => ImmutableArray.Create<IAttachment>();
public virtual IReadOnlyCollection<IEmbed> Embeds => ImmutableArray.Create<IEmbed>();
public virtual IReadOnlyCollection<Attachment> Attachments => ImmutableArray.Create<Attachment>();
public virtual IReadOnlyCollection<Embed> Embeds => ImmutableArray.Create<Embed>();
public virtual IReadOnlyCollection<Emoji> Emojis => ImmutableArray.Create<Emoji>();
public virtual IReadOnlyCollection<ulong> MentionedChannelIds => ImmutableArray.Create<ulong>();
public virtual IReadOnlyCollection<IRole> MentionedRoles => ImmutableArray.Create<IRole>();
public virtual IReadOnlyCollection<IUser> MentionedUsers => ImmutableArray.Create<IUser>();
public virtual IReadOnlyCollection<RestRole> MentionedRoles => ImmutableArray.Create<RestRole>();
public virtual IReadOnlyCollection<RestUser> MentionedUsers => ImmutableArray.Create<RestUser>();

public DateTimeOffset Timestamp => DateTimeUtils.FromTicks(_timestampTicks);

@@ -57,5 +58,9 @@ namespace Discord.Rest
public override string ToString() => Content;

MessageType IMessage.Type => MessageType.Default;
IReadOnlyCollection<IAttachment> IMessage.Attachments => Attachments;
IReadOnlyCollection<IEmbed> IMessage.Embeds => Embeds;
IReadOnlyCollection<IRole> IMessage.MentionedRoles => MentionedRoles;
IReadOnlyCollection<IUser> IMessage.MentionedUsers => MentionedUsers;
}
}

+ 15
- 12
src/Discord.Net.Rest/Entities/Messages/RestUserMessage.cs View File

@@ -13,8 +13,9 @@ namespace Discord.Rest
{
private bool _isMentioningEveryone, _isTTS, _isPinned;
private long? _editedTimestampTicks;
private ImmutableArray<RestAttachment> _attachments;
private ImmutableArray<RestEmbed> _embeds;
private ImmutableArray<Attachment> _attachments;
private ImmutableArray<Embed> _embeds;
private ImmutableArray<Emoji> _emojis;
private ImmutableArray<ulong> _mentionedChannelIds;
private ImmutableArray<RestRole> _mentionedRoles;
private ImmutableArray<RestUser> _mentionedUsers;
@@ -25,11 +26,12 @@ namespace Discord.Rest
public override bool IsPinned => _isPinned;
public override bool IsWebhook => WebhookId != null;
public override DateTimeOffset? EditedTimestamp => DateTimeUtils.FromTicks(_editedTimestampTicks);
public override IReadOnlyCollection<IAttachment> Attachments => _attachments;
public override IReadOnlyCollection<IEmbed> Embeds => _embeds;
public override IReadOnlyCollection<Attachment> Attachments => _attachments;
public override IReadOnlyCollection<Embed> Embeds => _embeds;
public override IReadOnlyCollection<Emoji> Emojis => _emojis;
public override IReadOnlyCollection<ulong> MentionedChannelIds => _mentionedChannelIds;
public override IReadOnlyCollection<IRole> MentionedRoles => _mentionedRoles;
public override IReadOnlyCollection<IUser> MentionedUsers => _mentionedUsers;
public override IReadOnlyCollection<RestRole> MentionedRoles => _mentionedRoles;
public override IReadOnlyCollection<RestUser> MentionedUsers => _mentionedUsers;

internal RestUserMessage(BaseDiscordClient discord, ulong id, ulong channelId)
: base(discord, id, channelId)
@@ -62,13 +64,13 @@ namespace Discord.Rest
var value = model.Attachments.Value;
if (value.Length > 0)
{
var attachments = ImmutableArray.CreateBuilder<RestAttachment>(value.Length);
var attachments = ImmutableArray.CreateBuilder<Attachment>(value.Length);
for (int i = 0; i < value.Length; i++)
attachments.Add(RestAttachment.Create(value[i]));
attachments.Add(Attachment.Create(value[i]));
_attachments = attachments.ToImmutable();
}
else
_attachments = ImmutableArray.Create<RestAttachment>();
_attachments = ImmutableArray.Create<Attachment>();
}

if (model.Embeds.IsSpecified)
@@ -76,13 +78,13 @@ namespace Discord.Rest
var value = model.Embeds.Value;
if (value.Length > 0)
{
var embeds = ImmutableArray.CreateBuilder<RestEmbed>(value.Length);
var embeds = ImmutableArray.CreateBuilder<Embed>(value.Length);
for (int i = 0; i < value.Length; i++)
embeds.Add(RestEmbed.Create(value[i]));
embeds.Add(Embed.Create(value[i]));
_embeds = embeds.ToImmutable();
}
else
_embeds = ImmutableArray.Create<RestEmbed>();
_embeds = ImmutableArray.Create<Embed>();
}

ImmutableArray<RestUser> mentions = ImmutableArray.Create<RestUser>();
@@ -105,6 +107,7 @@ namespace Discord.Rest
_mentionedUsers = MentionUtils.GetUserMentions(text, null, mentions);
_mentionedChannelIds = MentionUtils.GetChannelMentions(text, null);
_mentionedRoles = MentionUtils.GetRoleMentions<RestRole>(text, null);
_emojis = MessageHelper.GetEmojis(text);
model.Content = text;
}
}


+ 12
- 6
src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Model = Discord.API.Message;

namespace Discord.WebSocket
@@ -19,11 +20,12 @@ namespace Discord.WebSocket
public virtual bool IsWebhook => false;
public virtual DateTimeOffset? EditedTimestamp => null;

public virtual IReadOnlyCollection<IAttachment> Attachments => ImmutableArray.Create<IAttachment>();
public virtual IReadOnlyCollection<IEmbed> Embeds => ImmutableArray.Create<IEmbed>();
public virtual IReadOnlyCollection<ulong> MentionedChannelIds => ImmutableArray.Create<ulong>();
public virtual IReadOnlyCollection<IRole> MentionedRoles => ImmutableArray.Create<IRole>();
public virtual IReadOnlyCollection<IUser> MentionedUsers => ImmutableArray.Create<IUser>();
public virtual IReadOnlyCollection<Attachment> Attachments => ImmutableArray.Create<Attachment>();
public virtual IReadOnlyCollection<Embed> Embeds => ImmutableArray.Create<Embed>();
public virtual IReadOnlyCollection<Emoji> Emojis => ImmutableArray.Create<Emoji>();
public virtual IReadOnlyCollection<SocketGuildChannel> MentionedChannels => ImmutableArray.Create<SocketGuildChannel>();
public virtual IReadOnlyCollection<SocketRole> MentionedRoles => ImmutableArray.Create<SocketRole>();
public virtual IReadOnlyCollection<SocketUser> MentionedUsers => ImmutableArray.Create<SocketUser>();

public DateTimeOffset Timestamp => DateTimeUtils.FromTicks(_timestampTicks);

@@ -55,7 +57,11 @@ namespace Discord.WebSocket
//IMessage
IUser IMessage.Author => Author;
MessageType IMessage.Type => MessageType.Default;

IReadOnlyCollection<IAttachment> IMessage.Attachments => Attachments;
IReadOnlyCollection<IEmbed> IMessage.Embeds => Embeds;
IReadOnlyCollection<ulong> IMessage.MentionedChannelIds => MentionedChannels.Select(x => x.Id).ToImmutableArray();
IReadOnlyCollection<IRole> IMessage.MentionedRoles => MentionedRoles;
IReadOnlyCollection<IUser> IMessage.MentionedUsers => MentionedUsers;
ulong IMessage.ChannelId => Channel.Id;
}
}

+ 27
- 21
src/Discord.Net.WebSocket/Entities/Messages/SocketUserMessage.cs View File

@@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Model = Discord.API.Message;

@@ -14,11 +15,12 @@ namespace Discord.WebSocket
{
private bool _isMentioningEveryone, _isTTS, _isPinned;
private long? _editedTimestampTicks;
private ImmutableArray<RestAttachment> _attachments;
private ImmutableArray<RestEmbed> _embeds;
private ImmutableArray<ulong> _mentionedChannelIds;
private ImmutableArray<RestRole> _mentionedRoles;
private ImmutableArray<RestUser> _mentionedUsers;
private ImmutableArray<Attachment> _attachments;
private ImmutableArray<Embed> _embeds;
private ImmutableArray<Emoji> _emojis;
private ImmutableArray<SocketGuildChannel> _mentionedChannels;
private ImmutableArray<SocketRole> _mentionedRoles;
private ImmutableArray<SocketUser> _mentionedUsers;

public ulong? WebhookId { get; private set; }

@@ -27,11 +29,12 @@ namespace Discord.WebSocket
public override bool IsWebhook => WebhookId != null;
public override DateTimeOffset? EditedTimestamp => DateTimeUtils.FromTicks(_editedTimestampTicks);

public override IReadOnlyCollection<IAttachment> Attachments => _attachments;
public override IReadOnlyCollection<IEmbed> Embeds => _embeds;
public override IReadOnlyCollection<ulong> MentionedChannelIds => _mentionedChannelIds;
public override IReadOnlyCollection<IRole> MentionedRoles => _mentionedRoles;
public override IReadOnlyCollection<IUser> MentionedUsers => _mentionedUsers;
public override IReadOnlyCollection<Attachment> Attachments => _attachments;
public override IReadOnlyCollection<Embed> Embeds => _embeds;
public override IReadOnlyCollection<Emoji> Emojis => _emojis;
public override IReadOnlyCollection<SocketGuildChannel> MentionedChannels => _mentionedChannels;
public override IReadOnlyCollection<SocketRole> MentionedRoles => _mentionedRoles;
public override IReadOnlyCollection<SocketUser> MentionedUsers => _mentionedUsers;

internal SocketUserMessage(DiscordSocketClient discord, ulong id, ISocketMessageChannel channel, SocketUser author)
: base(discord, id, channel, author)
@@ -64,13 +67,13 @@ namespace Discord.WebSocket
var value = model.Attachments.Value;
if (value.Length > 0)
{
var attachments = ImmutableArray.CreateBuilder<RestAttachment>(value.Length);
var attachments = ImmutableArray.CreateBuilder<Attachment>(value.Length);
for (int i = 0; i < value.Length; i++)
attachments.Add(RestAttachment.Create(value[i]));
attachments.Add(Attachment.Create(value[i]));
_attachments = attachments.ToImmutable();
}
else
_attachments = ImmutableArray.Create<RestAttachment>();
_attachments = ImmutableArray.Create<Attachment>();
}

if (model.Embeds.IsSpecified)
@@ -78,24 +81,24 @@ namespace Discord.WebSocket
var value = model.Embeds.Value;
if (value.Length > 0)
{
var embeds = ImmutableArray.CreateBuilder<RestEmbed>(value.Length);
var embeds = ImmutableArray.CreateBuilder<Embed>(value.Length);
for (int i = 0; i < value.Length; i++)
embeds.Add(RestEmbed.Create(value[i]));
embeds.Add(Embed.Create(value[i]));
_embeds = embeds.ToImmutable();
}
else
_embeds = ImmutableArray.Create<RestEmbed>();
_embeds = ImmutableArray.Create<Embed>();
}

ImmutableArray<RestUser> mentions = ImmutableArray.Create<RestUser>();
ImmutableArray<SocketUser> mentions = ImmutableArray.Create<SocketUser>();
if (model.Mentions.IsSpecified)
{
var value = model.Mentions.Value;
if (value.Length > 0)
{
var newMentions = ImmutableArray.CreateBuilder<RestUser>(value.Length);
var newMentions = ImmutableArray.CreateBuilder<SocketUser>(value.Length);
for (int i = 0; i < value.Length; i++)
newMentions.Add(RestUser.Create(Discord, value[i]));
newMentions.Add(SocketSimpleUser.Create(Discord, Discord.State, value[i]));
mentions = newMentions.ToImmutable();
}
}
@@ -106,8 +109,11 @@ namespace Discord.WebSocket
var guild = (Channel as SocketGuildChannel)?.Guild;

_mentionedUsers = MentionUtils.GetUserMentions(text, Channel, mentions);
_mentionedChannelIds = MentionUtils.GetChannelMentions(text, guild);
_mentionedRoles = MentionUtils.GetRoleMentions<RestRole>(text, guild);
_mentionedChannels = MentionUtils.GetChannelMentions(text, guild)
.Select(x => guild?.GetChannel(x))
.Where(x => x != null).ToImmutableArray();
_mentionedRoles = MentionUtils.GetRoleMentions<SocketRole>(text, guild);
_emojis = MessageHelper.GetEmojis(text);
model.Content = text;
}
}


Loading…
Cancel
Save