@@ -9,28 +9,27 @@ using Model = Discord.API.Message;
namespace Discord.Rest
namespace Discord.Rest
{
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
internal class Message : SnowflakeEntity, IMessage
internal abstract class Message : SnowflakeEntity, IMessage
{
{
private bool _isMentioningEveryone;
private long _timestampTicks;
private long _timestampTicks;
private long? _editedTimestampTicks;
public MessageType Type { get; }
public IMessageChannel Channel { get; }
public IMessageChannel Channel { get; }
public IUser Author { get; }
public IUser Author { get; }
public bool IsTTS { get; private set; }
public string Content { get; private set; }
public string Content { get; private set; }
public bool IsPinned { get; private set; }
public IReadOnlyCollection<IAttachment> Attachments { get; private set; }
public IReadOnlyCollection<IEmbed> Embeds { get; private set; }
public IReadOnlyCollection<ulong> MentionedChannelIds { get; private set; }
public IReadOnlyCollection<IRole> MentionedRoles { get; private set; }
public IReadOnlyCollection<IUser> MentionedUsers { get; private set; }
public override DiscordRestClient Discord => (Channel as Entity<ulong>).Discord;
public override DiscordRestClient Discord => (Channel as Entity<ulong>).Discord;
public DateTimeOffset? EditedTimestamp => DateTimeUtils.FromTicks(_editedTimestampTicks);
public virtual bool IsTTS => false;
public virtual bool IsPinned => 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 DateTimeOffset Timestamp => DateTimeUtils.FromTicks(_timestampTicks);
public DateTimeOffset Timestamp => DateTimeUtils.FromTicks(_timestampTicks);
public Message(IMessageChannel channel, IUser author, Model model)
public Message(IMessageChannel channel, IUser author, Model model)
@@ -38,86 +37,21 @@ namespace Discord.Rest
{
{
Channel = channel;
Channel = channel;
Author = author;
Author = author;
Type = model.Type;
MentionedUsers = ImmutableArray.Create<IUser>();
MentionedChannelIds = ImmutableArray.Create<ulong>();
MentionedRoles = ImmutableArray.Create<IRole>();
Update(model, UpdateSource.Creation);
Update(model, UpdateSource.Creation);
}
}
public void Update(Model model, UpdateSource source)
public virtual void Update(Model model, UpdateSource source)
{
{
if (source == UpdateSource.Rest && IsAttached) return;
if (source == UpdateSource.Rest && IsAttached) return;
var guildChannel = Channel as GuildChannel;
var guildChannel = Channel as GuildChannel;
var guild = guildChannel?.Guild;
var guild = guildChannel?.Guild;
if (model.IsTextToSpeech.IsSpecified)
IsTTS = model.IsTextToSpeech.Value;
if (model.Pinned.IsSpecified)
IsPinned = model.Pinned.Value;
if (model.Timestamp.IsSpecified)
if (model.Timestamp.IsSpecified)
_timestampTicks = model.Timestamp.Value.UtcTicks;
_timestampTicks = model.Timestamp.Value.UtcTicks;
if (model.EditedTimestamp.IsSpecified)
_editedTimestampTicks = model.EditedTimestamp.Value?.UtcTicks;
if (model.MentionEveryone.IsSpecified)
_isMentioningEveryone = model.MentionEveryone.Value;
if (model.Attachments.IsSpecified)
{
var value = model.Attachments.Value;
if (value.Length > 0)
{
var attachments = new Attachment[value.Length];
for (int i = 0; i < attachments.Length; i++)
attachments[i] = new Attachment(value[i]);
Attachments = ImmutableArray.Create(attachments);
}
else
Attachments = ImmutableArray.Create<Attachment>();
}
if (model.Embeds.IsSpecified)
{
var value = model.Embeds.Value;
if (value.Length > 0)
{
var embeds = new Embed[value.Length];
for (int i = 0; i < embeds.Length; i++)
embeds[i] = new Embed(value[i]);
Embeds = ImmutableArray.Create(embeds);
}
else
Embeds = ImmutableArray.Create<Embed>();
}
if (model.Mentions.IsSpecified)
{
var value = model.Mentions.Value;
if (value.Length > 0)
{
var mentions = new User[value.Length];
for (int i = 0; i < value.Length; i++)
mentions[i] = new User(value[i]);
MentionedUsers = ImmutableArray.Create(mentions);
}
else
MentionedUsers = ImmutableArray.Create<IUser>();
}
if (model.Content.IsSpecified)
if (model.Content.IsSpecified)
{
var text = model.Content.Value;
if (guildChannel != null)
{
MentionedUsers = MentionUtils.GetUserMentions(text, Channel, MentionedUsers);
MentionedChannelIds = MentionUtils.GetChannelMentions(text, guildChannel.Guild);
MentionedRoles = MentionUtils.GetRoleMentions(text, guildChannel.Guild);
}
Content = text;
}
Content = model.Content.Value;
}
}
public async Task UpdateAsync()
public async Task UpdateAsync()
@@ -159,23 +93,6 @@ namespace Discord.Rest
{
{
await Discord.ApiClient.RemovePinAsync(Channel.Id, Id).ConfigureAwait(false);
await Discord.ApiClient.RemovePinAsync(Channel.Id, Id).ConfigureAwait(false);
}
}
public string Resolve(int startIndex, int length, UserMentionHandling userHandling, ChannelMentionHandling channelHandling,
RoleMentionHandling roleHandling, EveryoneMentionHandling everyoneHandling)
=> Resolve(Content.Substring(startIndex, length), userHandling, channelHandling, roleHandling, everyoneHandling);
public string Resolve(UserMentionHandling userHandling, ChannelMentionHandling channelHandling,
RoleMentionHandling roleHandling, EveryoneMentionHandling everyoneHandling)
=> Resolve(Content, userHandling, channelHandling, roleHandling, everyoneHandling);
private string Resolve(string text, UserMentionHandling userHandling, ChannelMentionHandling channelHandling,
RoleMentionHandling roleHandling, EveryoneMentionHandling everyoneHandling)
{
text = MentionUtils.ResolveUserMentions(text, Channel, MentionedUsers, userHandling);
text = MentionUtils.ResolveChannelMentions(text, (Channel as IGuildChannel)?.Guild, channelHandling);
text = MentionUtils.ResolveRoleMentions(text, MentionedRoles, roleHandling);
text = MentionUtils.ResolveEveryoneMentions(text, everyoneHandling);
return text;
}
public override string ToString() => Content;
public override string ToString() => Content;
private string DebuggerDisplay => $"{Author}: {Content}{(Attachments.Count > 0 ? $" [{Attachments.Count} Attachments]" : "")}";
private string DebuggerDisplay => $"{Author}: {Content}{(Attachments.Count > 0 ? $" [{Attachments.Count} Attachments]" : "")}";