@@ -21,7 +21,7 @@ namespace Discord | |||||
DateTimeOffset? EditedTimestamp { get; } | DateTimeOffset? EditedTimestamp { get; } | ||||
/// <summary> Gets the id of the channel this message was sent to. </summary> | /// <summary> Gets the id of the channel this message was sent to. </summary> | ||||
ulong ChannelId { get; } | |||||
IMessageChannel Channel { get; } | |||||
/// <summary> Gets the author of this message. </summary> | /// <summary> Gets the author of this message. </summary> | ||||
IUser Author { get; } | IUser Author { get; } | ||||
/// <summary> Gets the id of the webhook used to created this message, if any. </summary> | /// <summary> Gets the id of the webhook used to created this message, if any. </summary> | ||||
@@ -0,0 +1,106 @@ | |||||
using Discord.Rest; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Diagnostics; | |||||
using System.IO; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
namespace Discord.Rest | |||||
{ | |||||
//TODO: Review this class | |||||
[DebuggerDisplay(@"{DebuggerDisplay,nq}")] | |||||
internal class RestVirtualMessageChannel : RestEntity<ulong>, IMessageChannel | |||||
{ | |||||
public string Mention => MentionUtils.MentionChannel(Id); | |||||
internal RestVirtualMessageChannel(BaseDiscordClient discord, ulong id) | |||||
: base(discord, id) | |||||
{ | |||||
} | |||||
internal static RestVirtualMessageChannel Create(BaseDiscordClient discord, ulong id) | |||||
{ | |||||
return new RestVirtualMessageChannel(discord, id); | |||||
} | |||||
public Task<RestMessage> GetMessageAsync(ulong id, RequestOptions options = null) | |||||
=> ChannelHelper.GetMessageAsync(this, Discord, id, null, options); | |||||
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null) | |||||
=> ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, null, options); | |||||
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null) | |||||
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, null, options); | |||||
public IAsyncEnumerable<IReadOnlyCollection<RestMessage>> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null) | |||||
=> ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, null, options); | |||||
public Task<IReadOnlyCollection<RestMessage>> GetPinnedMessagesAsync(RequestOptions options = null) | |||||
=> ChannelHelper.GetPinnedMessagesAsync(this, Discord, null, options); | |||||
public Task<RestUserMessage> SendMessageAsync(string text, bool isTTS, RequestOptions options = null) | |||||
=> ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, null, options); | |||||
public Task<RestUserMessage> SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options = null) | |||||
=> ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, null, options); | |||||
public Task<RestUserMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options = null) | |||||
=> ChannelHelper.SendFileAsync(this, Discord, stream, filename, text, isTTS, null, options); | |||||
public Task DeleteMessagesAsync(IEnumerable<IMessage> messages, RequestOptions options = null) | |||||
=> ChannelHelper.DeleteMessagesAsync(this, Discord, messages, options); | |||||
public Task TriggerTypingAsync(RequestOptions options = null) | |||||
=> ChannelHelper.TriggerTypingAsync(this, Discord, options); | |||||
public IDisposable EnterTypingState(RequestOptions options = null) | |||||
=> ChannelHelper.EnterTypingState(this, Discord, options); | |||||
private string DebuggerDisplay => $"({Id}, Text)"; | |||||
//IMessageChannel | |||||
async Task<IMessage> IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options) | |||||
{ | |||||
if (mode == CacheMode.AllowDownload) | |||||
return await GetMessageAsync(id, options); | |||||
else | |||||
return null; | |||||
} | |||||
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options) | |||||
{ | |||||
if (mode == CacheMode.AllowDownload) | |||||
return GetMessagesAsync(limit, options); | |||||
else | |||||
return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>(); | |||||
} | |||||
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(ulong fromMessageId, Direction dir, int limit, CacheMode mode, RequestOptions options) | |||||
{ | |||||
if (mode == CacheMode.AllowDownload) | |||||
return GetMessagesAsync(fromMessageId, dir, limit, options); | |||||
else | |||||
return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>(); | |||||
} | |||||
IAsyncEnumerable<IReadOnlyCollection<IMessage>> IMessageChannel.GetMessagesAsync(IMessage fromMessage, Direction dir, int limit, CacheMode mode, RequestOptions options) | |||||
{ | |||||
if (mode == CacheMode.AllowDownload) | |||||
return GetMessagesAsync(fromMessage, dir, limit, options); | |||||
else | |||||
return AsyncEnumerable.Empty<IReadOnlyCollection<IMessage>>(); | |||||
} | |||||
async Task<IReadOnlyCollection<IMessage>> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options) | |||||
=> await GetPinnedMessagesAsync(options); | |||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options) | |||||
=> await SendFileAsync(filePath, text, isTTS, options); | |||||
async Task<IUserMessage> IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options) | |||||
=> await SendFileAsync(stream, filename, text, isTTS, options); | |||||
async Task<IUserMessage> IMessageChannel.SendMessageAsync(string text, bool isTTS, RequestOptions options) | |||||
=> await SendMessageAsync(text, isTTS, options); | |||||
IDisposable IMessageChannel.EnterTypingState(RequestOptions options) | |||||
=> EnterTypingState(options); | |||||
//IChannel | |||||
string IChannel.Name { get { throw new NotSupportedException(); } } | |||||
IAsyncEnumerable<IReadOnlyCollection<IUser>> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options) | |||||
{ | |||||
throw new NotSupportedException(); | |||||
} | |||||
Task<IUser> IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) | |||||
{ | |||||
throw new NotSupportedException(); | |||||
} | |||||
} | |||||
} |
@@ -15,23 +15,23 @@ namespace Discord.Rest | |||||
{ | { | ||||
var args = new ModifyMessageParams(); | var args = new ModifyMessageParams(); | ||||
func(args); | func(args); | ||||
await client.ApiClient.ModifyMessageAsync(msg.ChannelId, msg.Id, args, options); | |||||
await client.ApiClient.ModifyMessageAsync(msg.Channel.Id, msg.Id, args, options); | |||||
} | } | ||||
public static async Task DeleteAsync(IMessage msg, BaseDiscordClient client, | public static async Task DeleteAsync(IMessage msg, BaseDiscordClient client, | ||||
RequestOptions options) | RequestOptions options) | ||||
{ | { | ||||
await client.ApiClient.DeleteMessageAsync(msg.ChannelId, msg.Id, options); | |||||
await client.ApiClient.DeleteMessageAsync(msg.Channel.Id, msg.Id, options); | |||||
} | } | ||||
public static async Task PinAsync(IMessage msg, BaseDiscordClient client, | public static async Task PinAsync(IMessage msg, BaseDiscordClient client, | ||||
RequestOptions options) | RequestOptions options) | ||||
{ | { | ||||
await client.ApiClient.AddPinAsync(msg.ChannelId, msg.Id, options); | |||||
await client.ApiClient.AddPinAsync(msg.Channel.Id, msg.Id, options); | |||||
} | } | ||||
public static async Task UnpinAsync(IMessage msg, BaseDiscordClient client, | public static async Task UnpinAsync(IMessage msg, BaseDiscordClient client, | ||||
RequestOptions options) | RequestOptions options) | ||||
{ | { | ||||
await client.ApiClient.RemovePinAsync(msg.ChannelId, msg.Id, options); | |||||
await client.ApiClient.RemovePinAsync(msg.Channel.Id, msg.Id, options); | |||||
} | } | ||||
public static ImmutableArray<ITag> ParseTags(string text, IMessageChannel channel, IGuild guild, ImmutableArray<IUser> userMentions) | public static ImmutableArray<ITag> ParseTags(string text, IMessageChannel channel, IGuild guild, ImmutableArray<IUser> userMentions) | ||||
@@ -12,7 +12,7 @@ namespace Discord.Rest | |||||
internal readonly IGuild _guild; | internal readonly IGuild _guild; | ||||
private long _timestampTicks; | private long _timestampTicks; | ||||
public ulong ChannelId { get; } | |||||
public IMessageChannel Channel { get; } | |||||
public RestUser Author { get; } | public RestUser Author { get; } | ||||
public string Content { get; private set; } | public string Content { get; private set; } | ||||
@@ -31,10 +31,10 @@ namespace Discord.Rest | |||||
public DateTimeOffset Timestamp => DateTimeUtils.FromTicks(_timestampTicks); | public DateTimeOffset Timestamp => DateTimeUtils.FromTicks(_timestampTicks); | ||||
internal RestMessage(BaseDiscordClient discord, ulong id, ulong channelId, RestUser author, IGuild guild) | |||||
internal RestMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, RestUser author, IGuild guild) | |||||
: base(discord, id) | : base(discord, id) | ||||
{ | { | ||||
ChannelId = channelId; | |||||
Channel = channel; | |||||
Author = author; | Author = author; | ||||
_guild = guild; | _guild = guild; | ||||
} | } | ||||
@@ -56,7 +56,7 @@ namespace Discord.Rest | |||||
public async Task UpdateAsync(RequestOptions options = null) | public async Task UpdateAsync(RequestOptions options = null) | ||||
{ | { | ||||
var model = await Discord.ApiClient.GetChannelMessageAsync(ChannelId, Id, options).ConfigureAwait(false); | |||||
var model = await Discord.ApiClient.GetChannelMessageAsync(Channel.Id, Id, options).ConfigureAwait(false); | |||||
Update(model); | Update(model); | ||||
} | } | ||||
@@ -8,13 +8,15 @@ namespace Discord.Rest | |||||
{ | { | ||||
public MessageType Type { get; private set; } | public MessageType Type { get; private set; } | ||||
internal RestSystemMessage(BaseDiscordClient discord, ulong id, ulong channelId, RestUser author, IGuild guild) | |||||
: base(discord, id, channelId, author, guild) | |||||
internal RestSystemMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, RestUser author, IGuild guild) | |||||
: base(discord, id, channel, author, guild) | |||||
{ | { | ||||
} | } | ||||
internal new static RestSystemMessage Create(BaseDiscordClient discord, IGuild guild, Model model) | internal new static RestSystemMessage Create(BaseDiscordClient discord, IGuild guild, Model model) | ||||
{ | { | ||||
var entity = new RestSystemMessage(discord, model.Id, model.ChannelId, RestUser.Create(discord, model.Author.Value), guild); | |||||
var entity = new RestSystemMessage(discord, model.Id, | |||||
RestVirtualMessageChannel.Create(discord, model.ChannelId), | |||||
RestUser.Create(discord, model.Author.Value), guild); | |||||
entity.Update(model); | entity.Update(model); | ||||
return entity; | return entity; | ||||
} | } | ||||
@@ -30,13 +30,15 @@ namespace Discord.Rest | |||||
public override IReadOnlyCollection<RestUser> MentionedUsers => MessageHelper.FilterTagsByValue<RestUser>(TagType.UserMention, _tags); | public override IReadOnlyCollection<RestUser> MentionedUsers => MessageHelper.FilterTagsByValue<RestUser>(TagType.UserMention, _tags); | ||||
public override IReadOnlyCollection<ITag> Tags => _tags; | public override IReadOnlyCollection<ITag> Tags => _tags; | ||||
internal RestUserMessage(BaseDiscordClient discord, ulong id, ulong channelId, RestUser author, IGuild guild) | |||||
: base(discord, id, channelId, author, guild) | |||||
internal RestUserMessage(BaseDiscordClient discord, ulong id, IMessageChannel channel, RestUser author, IGuild guild) | |||||
: base(discord, id, channel, author, guild) | |||||
{ | { | ||||
} | } | ||||
internal new static RestUserMessage Create(BaseDiscordClient discord, IGuild guild, Model model) | internal new static RestUserMessage Create(BaseDiscordClient discord, IGuild guild, Model model) | ||||
{ | { | ||||
var entity = new RestUserMessage(discord, model.Id, model.ChannelId, RestUser.Create(discord, model.Author.Value), guild); | |||||
var entity = new RestUserMessage(discord, model.Id, | |||||
RestVirtualMessageChannel.Create(discord, model.ChannelId), | |||||
RestUser.Create(discord, model.Author.Value), guild); | |||||
entity.Update(model); | entity.Update(model); | ||||
return entity; | return entity; | ||||
} | } | ||||
@@ -56,12 +56,12 @@ namespace Discord.WebSocket | |||||
//IMessage | //IMessage | ||||
IUser IMessage.Author => Author; | IUser IMessage.Author => Author; | ||||
IMessageChannel IMessage.Channel => Channel; | |||||
MessageType IMessage.Type => MessageType.Default; | MessageType IMessage.Type => MessageType.Default; | ||||
IReadOnlyCollection<IAttachment> IMessage.Attachments => Attachments; | IReadOnlyCollection<IAttachment> IMessage.Attachments => Attachments; | ||||
IReadOnlyCollection<IEmbed> IMessage.Embeds => Embeds; | IReadOnlyCollection<IEmbed> IMessage.Embeds => Embeds; | ||||
IReadOnlyCollection<ulong> IMessage.MentionedChannelIds => MentionedChannels.Select(x => x.Id).ToImmutableArray(); | IReadOnlyCollection<ulong> IMessage.MentionedChannelIds => MentionedChannels.Select(x => x.Id).ToImmutableArray(); | ||||
IReadOnlyCollection<ulong> IMessage.MentionedRoleIds => MentionedRoles.Select(x => x.Id).ToImmutableArray(); | IReadOnlyCollection<ulong> IMessage.MentionedRoleIds => MentionedRoles.Select(x => x.Id).ToImmutableArray(); | ||||
IReadOnlyCollection<ulong> IMessage.MentionedUserIds => MentionedUsers.Select(x => x.Id).ToImmutableArray(); | IReadOnlyCollection<ulong> IMessage.MentionedUserIds => MentionedUsers.Select(x => x.Id).ToImmutableArray(); | ||||
ulong IMessage.ChannelId => Channel.Id; | |||||
} | } | ||||
} | } |