From 0cf5493c618b7d0e08bd970707e19d0879bd3637 Mon Sep 17 00:00:00 2001 From: RogueException Date: Tue, 7 Feb 2017 21:10:30 -0400 Subject: [PATCH] Fixed crash, added DM/Group channel helpers --- src/Discord.Net.Core/IDiscordClient.cs | 2 ++ src/Discord.Net.Rest/BaseDiscordClient.cs | 4 ++++ src/Discord.Net.Rest/ClientHelper.cs | 18 +++++++++++++++-- src/Discord.Net.Rest/DiscordRestClient.cs | 20 ++++++++++++++++++- .../DiscordSocketClient.cs | 8 ++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Discord.Net.Core/IDiscordClient.cs b/src/Discord.Net.Core/IDiscordClient.cs index a26f27dc0..b9a08d32d 100644 --- a/src/Discord.Net.Core/IDiscordClient.cs +++ b/src/Discord.Net.Core/IDiscordClient.cs @@ -17,6 +17,8 @@ namespace Discord Task GetChannelAsync(ulong id, CacheMode mode = CacheMode.AllowDownload); Task> GetPrivateChannelsAsync(CacheMode mode = CacheMode.AllowDownload); + Task> GetDMChannelsAsync(CacheMode mode = CacheMode.AllowDownload); + Task> GetGroupChannelsAsync(CacheMode mode = CacheMode.AllowDownload); Task> GetConnectionsAsync(); diff --git a/src/Discord.Net.Rest/BaseDiscordClient.cs b/src/Discord.Net.Rest/BaseDiscordClient.cs index 592931c86..8948c87dc 100644 --- a/src/Discord.Net.Rest/BaseDiscordClient.cs +++ b/src/Discord.Net.Rest/BaseDiscordClient.cs @@ -134,6 +134,10 @@ namespace Discord.Rest => Task.FromResult(null); Task> IDiscordClient.GetPrivateChannelsAsync(CacheMode mode) => Task.FromResult>(ImmutableArray.Create()); + Task> IDiscordClient.GetDMChannelsAsync(CacheMode mode) + => Task.FromResult>(ImmutableArray.Create()); + Task> IDiscordClient.GetGroupChannelsAsync(CacheMode mode) + => Task.FromResult>(ImmutableArray.Create()); Task> IDiscordClient.GetConnectionsAsync() => Task.FromResult>(ImmutableArray.Create()); diff --git a/src/Discord.Net.Rest/ClientHelper.cs b/src/Discord.Net.Rest/ClientHelper.cs index e7ef55033..456362be6 100644 --- a/src/Discord.Net.Rest/ClientHelper.cs +++ b/src/Discord.Net.Rest/ClientHelper.cs @@ -24,10 +24,24 @@ namespace Discord.Rest return RestChannel.Create(client, model); return null; } - public static async Task> GetPrivateChannelsAsync(BaseDiscordClient client) + public static async Task> GetPrivateChannelsAsync(BaseDiscordClient client) { var models = await client.ApiClient.GetMyPrivateChannelsAsync().ConfigureAwait(false); - return models.Select(x => RestDMChannel.Create(client, x)).ToImmutableArray(); + return models.Select(x => RestChannel.CreatePrivate(client, x)).ToImmutableArray(); + } + public static async Task> GetDMChannelsAsync(BaseDiscordClient client) + { + var models = await client.ApiClient.GetMyPrivateChannelsAsync().ConfigureAwait(false); + return models + .Where(x => x.Type == ChannelType.DM) + .Select(x => RestDMChannel.Create(client, x)).ToImmutableArray(); + } + public static async Task> GetGroupChannelsAsync(BaseDiscordClient client) + { + var models = await client.ApiClient.GetMyPrivateChannelsAsync().ConfigureAwait(false); + return models + .Where(x => x.Type == ChannelType.Group) + .Select(x => RestGroupChannel.Create(client, x)).ToImmutableArray(); } public static async Task> GetConnectionsAsync(BaseDiscordClient client) diff --git a/src/Discord.Net.Rest/DiscordRestClient.cs b/src/Discord.Net.Rest/DiscordRestClient.cs index f5f2cb8b0..0727576bf 100644 --- a/src/Discord.Net.Rest/DiscordRestClient.cs +++ b/src/Discord.Net.Rest/DiscordRestClient.cs @@ -39,8 +39,12 @@ namespace Discord.Rest public Task GetChannelAsync(ulong id) => ClientHelper.GetChannelAsync(this, id); /// - public Task> GetPrivateChannelsAsync() + public Task> GetPrivateChannelsAsync() => ClientHelper.GetPrivateChannelsAsync(this); + public Task> GetDMChannelsAsync() + => ClientHelper.GetDMChannelsAsync(this); + public Task> GetGroupChannelsAsync() + => ClientHelper.GetGroupChannelsAsync(this); /// public Task> GetConnectionsAsync() @@ -98,6 +102,20 @@ namespace Discord.Rest else return ImmutableArray.Create(); } + async Task> IDiscordClient.GetDMChannelsAsync(CacheMode mode) + { + if (mode == CacheMode.AllowDownload) + return await GetDMChannelsAsync().ConfigureAwait(false); + else + return ImmutableArray.Create(); + } + async Task> IDiscordClient.GetGroupChannelsAsync(CacheMode mode) + { + if (mode == CacheMode.AllowDownload) + return await GetGroupChannelsAsync().ConfigureAwait(false); + else + return ImmutableArray.Create(); + } async Task> IDiscordClient.GetConnectionsAsync() => await GetConnectionsAsync().ConfigureAwait(false); diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 0641672d2..6a23d3e04 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -68,6 +68,10 @@ namespace Discord.WebSocket public new SocketSelfUser CurrentUser { get { return base.CurrentUser as SocketSelfUser; } private set { base.CurrentUser = value; } } public IReadOnlyCollection Guilds => State.Guilds; public IReadOnlyCollection PrivateChannels => State.PrivateChannels; + public IReadOnlyCollection DMChannels + => State.PrivateChannels.Select(x => x as SocketDMChannel).Where(x => x != null).ToImmutableArray(); + public IReadOnlyCollection GroupChannels + => State.PrivateChannels.Select(x => x as SocketGroupChannel).Where(x => x != null).ToImmutableArray(); public IReadOnlyCollection VoiceRegions => _voiceRegions.ToReadOnlyCollection(); /// Creates a new REST/WebSocket discord client. @@ -1803,6 +1807,10 @@ namespace Discord.WebSocket => Task.FromResult(GetChannel(id)); Task> IDiscordClient.GetPrivateChannelsAsync(CacheMode mode) => Task.FromResult>(PrivateChannels); + Task> IDiscordClient.GetDMChannelsAsync(CacheMode mode) + => Task.FromResult>(DMChannels); + Task> IDiscordClient.GetGroupChannelsAsync(CacheMode mode) + => Task.FromResult>(GroupChannels); async Task> IDiscordClient.GetConnectionsAsync() => await GetConnectionsAsync().ConfigureAwait(false);