diff --git a/src/Discord.Net.Core/Entities/Messages/IMessage.cs b/src/Discord.Net.Core/Entities/Messages/IMessage.cs index 6f00c9ba0..9b15d1b07 100644 --- a/src/Discord.Net.Core/Entities/Messages/IMessage.cs +++ b/src/Discord.Net.Core/Entities/Messages/IMessage.cs @@ -21,7 +21,7 @@ namespace Discord DateTimeOffset? EditedTimestamp { get; } /// Gets the id of the channel this message was sent to. - ulong ChannelId { get; } + IMessageChannel Channel { get; } /// Gets the author of this message. IUser Author { get; } /// Gets the id of the webhook used to created this message, if any. diff --git a/src/Discord.Net.Rest/Entities/Channels/RestVirtualMessageChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestVirtualMessageChannel.cs new file mode 100644 index 000000000..75f6e836f --- /dev/null +++ b/src/Discord.Net.Rest/Entities/Channels/RestVirtualMessageChannel.cs @@ -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, 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 GetMessageAsync(ulong id, RequestOptions options = null) + => ChannelHelper.GetMessageAsync(this, Discord, id, null, options); + public IAsyncEnumerable> GetMessagesAsync(int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null) + => ChannelHelper.GetMessagesAsync(this, Discord, null, Direction.Before, limit, null, options); + public IAsyncEnumerable> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null) + => ChannelHelper.GetMessagesAsync(this, Discord, fromMessageId, dir, limit, null, options); + public IAsyncEnumerable> GetMessagesAsync(IMessage fromMessage, Direction dir, int limit = DiscordConfig.MaxMessagesPerBatch, RequestOptions options = null) + => ChannelHelper.GetMessagesAsync(this, Discord, fromMessage.Id, dir, limit, null, options); + public Task> GetPinnedMessagesAsync(RequestOptions options = null) + => ChannelHelper.GetPinnedMessagesAsync(this, Discord, null, options); + + public Task SendMessageAsync(string text, bool isTTS, RequestOptions options = null) + => ChannelHelper.SendMessageAsync(this, Discord, text, isTTS, null, options); + public Task SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options = null) + => ChannelHelper.SendFileAsync(this, Discord, filePath, text, isTTS, null, options); + public Task 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 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 IMessageChannel.GetMessageAsync(ulong id, CacheMode mode, RequestOptions options) + { + if (mode == CacheMode.AllowDownload) + return await GetMessageAsync(id, options); + else + return null; + } + IAsyncEnumerable> IMessageChannel.GetMessagesAsync(int limit, CacheMode mode, RequestOptions options) + { + if (mode == CacheMode.AllowDownload) + return GetMessagesAsync(limit, options); + else + return AsyncEnumerable.Empty>(); + } + IAsyncEnumerable> 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>(); + } + IAsyncEnumerable> 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>(); + } + async Task> IMessageChannel.GetPinnedMessagesAsync(RequestOptions options) + => await GetPinnedMessagesAsync(options); + + async Task IMessageChannel.SendFileAsync(string filePath, string text, bool isTTS, RequestOptions options) + => await SendFileAsync(filePath, text, isTTS, options); + async Task IMessageChannel.SendFileAsync(Stream stream, string filename, string text, bool isTTS, RequestOptions options) + => await SendFileAsync(stream, filename, text, isTTS, options); + async Task 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> IChannel.GetUsersAsync(CacheMode mode, RequestOptions options) + { + throw new NotSupportedException(); + } + Task IChannel.GetUserAsync(ulong id, CacheMode mode, RequestOptions options) + { + throw new NotSupportedException(); + } + } +} diff --git a/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs b/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs index c62f59071..a203b8e49 100644 --- a/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs +++ b/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs @@ -15,23 +15,23 @@ namespace Discord.Rest { var args = new ModifyMessageParams(); 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, 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, 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, RequestOptions options) { - await client.ApiClient.RemovePinAsync(msg.ChannelId, msg.Id, options); + await client.ApiClient.RemovePinAsync(msg.Channel.Id, msg.Id, options); } public static ImmutableArray ParseTags(string text, IMessageChannel channel, IGuild guild, ImmutableArray userMentions) diff --git a/src/Discord.Net.Rest/Entities/Messages/RestMessage.cs b/src/Discord.Net.Rest/Entities/Messages/RestMessage.cs index 41bb6c7f1..ff54dad98 100644 --- a/src/Discord.Net.Rest/Entities/Messages/RestMessage.cs +++ b/src/Discord.Net.Rest/Entities/Messages/RestMessage.cs @@ -12,7 +12,7 @@ namespace Discord.Rest internal readonly IGuild _guild; private long _timestampTicks; - public ulong ChannelId { get; } + public IMessageChannel Channel { get; } public RestUser Author { get; } public string Content { get; private set; } @@ -31,10 +31,10 @@ namespace Discord.Rest 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) { - ChannelId = channelId; + Channel = channel; Author = author; _guild = guild; } @@ -56,7 +56,7 @@ namespace Discord.Rest 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); } diff --git a/src/Discord.Net.Rest/Entities/Messages/RestSystemMessage.cs b/src/Discord.Net.Rest/Entities/Messages/RestSystemMessage.cs index 58aac4129..0725ab603 100644 --- a/src/Discord.Net.Rest/Entities/Messages/RestSystemMessage.cs +++ b/src/Discord.Net.Rest/Entities/Messages/RestSystemMessage.cs @@ -8,13 +8,15 @@ namespace Discord.Rest { 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) { - 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); return entity; } diff --git a/src/Discord.Net.Rest/Entities/Messages/RestUserMessage.cs b/src/Discord.Net.Rest/Entities/Messages/RestUserMessage.cs index 2d096434a..655929a61 100644 --- a/src/Discord.Net.Rest/Entities/Messages/RestUserMessage.cs +++ b/src/Discord.Net.Rest/Entities/Messages/RestUserMessage.cs @@ -30,13 +30,15 @@ namespace Discord.Rest public override IReadOnlyCollection MentionedUsers => MessageHelper.FilterTagsByValue(TagType.UserMention, _tags); public override IReadOnlyCollection 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) { - 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); return entity; } diff --git a/src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs b/src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs index a3a4cb465..709f36fa0 100644 --- a/src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs +++ b/src/Discord.Net.WebSocket/Entities/Messages/SocketMessage.cs @@ -56,12 +56,12 @@ namespace Discord.WebSocket //IMessage IUser IMessage.Author => Author; + IMessageChannel IMessage.Channel => Channel; MessageType IMessage.Type => MessageType.Default; IReadOnlyCollection IMessage.Attachments => Attachments; IReadOnlyCollection IMessage.Embeds => Embeds; IReadOnlyCollection IMessage.MentionedChannelIds => MentionedChannels.Select(x => x.Id).ToImmutableArray(); IReadOnlyCollection IMessage.MentionedRoleIds => MentionedRoles.Select(x => x.Id).ToImmutableArray(); IReadOnlyCollection IMessage.MentionedUserIds => MentionedUsers.Select(x => x.Id).ToImmutableArray(); - ulong IMessage.ChannelId => Channel.Id; } }