From 6fed78025c8c2cfe76917cd3078f9e5312fb1b53 Mon Sep 17 00:00:00 2001 From: AntiTcb Date: Tue, 16 May 2017 20:02:32 -0400 Subject: [PATCH 1/6] Create DM channel if one does not exist. --- src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs index 1b599bf7e..7575309cb 100644 --- a/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs +++ b/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs @@ -66,8 +66,8 @@ namespace Discord.WebSocket internal SocketUser Clone() => MemberwiseClone() as SocketUser; //IUser - Task IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options) - => Task.FromResult(GlobalUser.DMChannel); + async Task IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options) + => await Task.FromResult(GlobalUser.DMChannel ?? await CreateDMChannelAsync(options) as IDMChannel); async Task IUser.CreateDMChannelAsync(RequestOptions options) => await CreateDMChannelAsync(options).ConfigureAwait(false); } From aeef5d08935544c10233c8741eca03ea1be67a11 Mon Sep 17 00:00:00 2001 From: AntiTcb Date: Tue, 16 May 2017 20:03:17 -0400 Subject: [PATCH 2/6] Update DM channel on entity updates. --- src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs index 0cd5f749e..3117eb14c 100644 --- a/src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs +++ b/src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Linq; using Model = Discord.API.User; using PresenceModel = Discord.API.Presence; @@ -51,6 +52,7 @@ namespace Discord.WebSocket internal void Update(ClientState state, PresenceModel model) { Presence = SocketPresence.Create(model); + DMChannel = state.DMChannels.FirstOrDefault(x => x.Recipient.Id == Id); } internal new SocketGlobalUser Clone() => MemberwiseClone() as SocketGlobalUser; From 33a91ba3dee4b1dd68ebd895b5408584a22ec398 Mon Sep 17 00:00:00 2001 From: AntiTcb Date: Tue, 16 May 2017 20:03:38 -0400 Subject: [PATCH 3/6] Remove redundant explicit interface definition. --- src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs index b92559a40..05aa132a5 100644 --- a/src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs +++ b/src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs @@ -146,11 +146,7 @@ namespace Discord.WebSocket IGuild IGuildUser.Guild => Guild; ulong IGuildUser.GuildId => Guild.Id; IReadOnlyCollection IGuildUser.RoleIds => _roleIds; - - //IUser - Task IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options) - => Task.FromResult(GlobalUser.DMChannel); - + //IVoiceState IVoiceChannel IVoiceState.VoiceChannel => VoiceChannel; } From 7db38f32bb481d790362ea6a5bc998045373113a Mon Sep 17 00:00:00 2001 From: AntiTcb Date: Tue, 16 May 2017 20:04:25 -0400 Subject: [PATCH 4/6] Attach/Remove DMChannel to SocketGlobalUser.DMChannel property --- src/Discord.Net.WebSocket/DiscordSocketClient.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Discord.Net.WebSocket/DiscordSocketClient.cs b/src/Discord.Net.WebSocket/DiscordSocketClient.cs index 4476b78c4..d42df7b55 100644 --- a/src/Discord.Net.WebSocket/DiscordSocketClient.cs +++ b/src/Discord.Net.WebSocket/DiscordSocketClient.cs @@ -1634,6 +1634,9 @@ namespace Discord.WebSocket { var channel = SocketChannel.CreatePrivate(this, state, model); state.AddChannel(channel as SocketChannel); + if (channel is SocketDMChannel dm) + dm.Recipient.GlobalUser.DMChannel = dm; + return channel; } internal ISocketPrivateChannel RemovePrivateChannel(ulong id) @@ -1641,6 +1644,9 @@ namespace Discord.WebSocket var channel = State.RemoveChannel(id) as ISocketPrivateChannel; if (channel != null) { + if (channel is SocketDMChannel dmChannel) + dmChannel.Recipient.GlobalUser.DMChannel = null; + foreach (var recipient in channel.Recipients) recipient.GlobalUser.RemoveRef(this); } From 73611d1fab6c55ca21d0115040dc2fd72dcf07c7 Mon Sep 17 00:00:00 2001 From: AntiTcb Date: Sat, 27 May 2017 14:47:12 -0400 Subject: [PATCH 5/6] Remove IUser.CreateDMChannelAsync, implicitly implement IUser.GetDMChannelAsync --- src/Discord.Net.Core/Entities/Users/IUser.cs | 4 +--- src/Discord.Net.Rest/Entities/Users/RestUser.cs | 8 +++----- src/Discord.Net.Rpc/Entities/Users/RpcUser.cs | 8 +++----- .../Entities/Users/SocketUser.cs | 12 +++++------- 4 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Users/IUser.cs b/src/Discord.Net.Core/Entities/Users/IUser.cs index 45d8862f1..249100d37 100644 --- a/src/Discord.Net.Core/Entities/Users/IUser.cs +++ b/src/Discord.Net.Core/Entities/Users/IUser.cs @@ -20,8 +20,6 @@ namespace Discord string Username { get; } /// Returns a private message channel to this user, creating one if it does not already exist. - Task GetDMChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); - /// Returns a private message channel to this user, creating one if it does not already exist. - Task CreateDMChannelAsync(RequestOptions options = null); + Task GetDMChannelAsync(RequestOptions options = null); } } diff --git a/src/Discord.Net.Rest/Entities/Users/RestUser.cs b/src/Discord.Net.Rest/Entities/Users/RestUser.cs index cded876c8..36ca242d8 100644 --- a/src/Discord.Net.Rest/Entities/Users/RestUser.cs +++ b/src/Discord.Net.Rest/Entities/Users/RestUser.cs @@ -54,7 +54,7 @@ namespace Discord.Rest Update(model); } - public Task CreateDMChannelAsync(RequestOptions options = null) + public Task GetDMChannelAsync(RequestOptions options = null) => UserHelper.CreateDMChannelAsync(this, Discord, options); public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) @@ -64,9 +64,7 @@ namespace Discord.Rest private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})"; //IUser - Task IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options) - => Task.FromResult(null); - async Task IUser.CreateDMChannelAsync(RequestOptions options) - => await CreateDMChannelAsync(options).ConfigureAwait(false); + async Task IUser.GetDMChannelAsync(RequestOptions options) + => await GetDMChannelAsync(options); } } diff --git a/src/Discord.Net.Rpc/Entities/Users/RpcUser.cs b/src/Discord.Net.Rpc/Entities/Users/RpcUser.cs index 7ed11e57d..71de1f804 100644 --- a/src/Discord.Net.Rpc/Entities/Users/RpcUser.cs +++ b/src/Discord.Net.Rpc/Entities/Users/RpcUser.cs @@ -49,7 +49,7 @@ namespace Discord.Rpc Username = model.Username.Value; } - public Task CreateDMChannelAsync(RequestOptions options = null) + public Task GetDMChannelAsync(RequestOptions options = null) => UserHelper.CreateDMChannelAsync(this, Discord, options); public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) @@ -59,9 +59,7 @@ namespace Discord.Rpc private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})"; //IUser - Task IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options) - => Task.FromResult(null); - async Task IUser.CreateDMChannelAsync(RequestOptions options) - => await CreateDMChannelAsync(options).ConfigureAwait(false); + async Task IUser.GetDMChannelAsync(RequestOptions options) + => await GetDMChannelAsync(options); } } diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs index 7575309cb..60fca73b2 100644 --- a/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs +++ b/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs @@ -53,10 +53,10 @@ namespace Discord.WebSocket hasChanges = true; } return hasChanges; - } + } - public Task CreateDMChannelAsync(RequestOptions options = null) - => UserHelper.CreateDMChannelAsync(this, Discord, options); + public async Task GetDMChannelAsync(RequestOptions options = null) + => GlobalUser.DMChannel ?? await UserHelper.CreateDMChannelAsync(this, Discord, options) as IDMChannel; public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) => CDN.GetUserAvatarUrl(Id, AvatarId, size, format); @@ -66,9 +66,7 @@ namespace Discord.WebSocket internal SocketUser Clone() => MemberwiseClone() as SocketUser; //IUser - async Task IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options) - => await Task.FromResult(GlobalUser.DMChannel ?? await CreateDMChannelAsync(options) as IDMChannel); - async Task IUser.CreateDMChannelAsync(RequestOptions options) - => await CreateDMChannelAsync(options).ConfigureAwait(false); + Task IUser.GetDMChannelAsync(RequestOptions options) + => GetDMChannelAsync(options); } } From fb57a61432d3e800c881067983fcceaf6eac2339 Mon Sep 17 00:00:00 2001 From: AntiTcb Date: Fri, 16 Jun 2017 20:43:50 -0400 Subject: [PATCH 6/6] Rename to GetOrCreateDMChannelAsync --- src/Discord.Net.Core/Entities/Users/IUser.cs | 2 +- src/Discord.Net.Rest/Entities/Users/RestUser.cs | 6 +++--- src/Discord.Net.Rpc/Entities/Users/RpcUser.cs | 6 +++--- src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs | 6 +----- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Discord.Net.Core/Entities/Users/IUser.cs b/src/Discord.Net.Core/Entities/Users/IUser.cs index 249100d37..e3f270f6f 100644 --- a/src/Discord.Net.Core/Entities/Users/IUser.cs +++ b/src/Discord.Net.Core/Entities/Users/IUser.cs @@ -20,6 +20,6 @@ namespace Discord string Username { get; } /// Returns a private message channel to this user, creating one if it does not already exist. - Task GetDMChannelAsync(RequestOptions options = null); + Task GetOrCreateDMChannelAsync(RequestOptions options = null); } } diff --git a/src/Discord.Net.Rest/Entities/Users/RestUser.cs b/src/Discord.Net.Rest/Entities/Users/RestUser.cs index 36ca242d8..d8ade3a6b 100644 --- a/src/Discord.Net.Rest/Entities/Users/RestUser.cs +++ b/src/Discord.Net.Rest/Entities/Users/RestUser.cs @@ -54,7 +54,7 @@ namespace Discord.Rest Update(model); } - public Task GetDMChannelAsync(RequestOptions options = null) + public Task GetOrCreateDMChannelAsync(RequestOptions options = null) => UserHelper.CreateDMChannelAsync(this, Discord, options); public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) @@ -64,7 +64,7 @@ namespace Discord.Rest private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})"; //IUser - async Task IUser.GetDMChannelAsync(RequestOptions options) - => await GetDMChannelAsync(options); + async Task IUser.GetOrCreateDMChannelAsync(RequestOptions options) + => await GetOrCreateDMChannelAsync(options); } } diff --git a/src/Discord.Net.Rpc/Entities/Users/RpcUser.cs b/src/Discord.Net.Rpc/Entities/Users/RpcUser.cs index 71de1f804..c6b0b2fd8 100644 --- a/src/Discord.Net.Rpc/Entities/Users/RpcUser.cs +++ b/src/Discord.Net.Rpc/Entities/Users/RpcUser.cs @@ -49,7 +49,7 @@ namespace Discord.Rpc Username = model.Username.Value; } - public Task GetDMChannelAsync(RequestOptions options = null) + public Task GetOrCreateDMChannelAsync(RequestOptions options = null) => UserHelper.CreateDMChannelAsync(this, Discord, options); public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) @@ -59,7 +59,7 @@ namespace Discord.Rpc private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})"; //IUser - async Task IUser.GetDMChannelAsync(RequestOptions options) - => await GetDMChannelAsync(options); + async Task IUser.GetOrCreateDMChannelAsync(RequestOptions options) + => await GetOrCreateDMChannelAsync(options); } } diff --git a/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs b/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs index 60fca73b2..a0c78b93f 100644 --- a/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs +++ b/src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs @@ -55,7 +55,7 @@ namespace Discord.WebSocket return hasChanges; } - public async Task GetDMChannelAsync(RequestOptions options = null) + public async Task GetOrCreateDMChannelAsync(RequestOptions options = null) => GlobalUser.DMChannel ?? await UserHelper.CreateDMChannelAsync(this, Discord, options) as IDMChannel; public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) @@ -64,9 +64,5 @@ namespace Discord.WebSocket public override string ToString() => $"{Username}#{Discriminator}"; private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})"; internal SocketUser Clone() => MemberwiseClone() as SocketUser; - - //IUser - Task IUser.GetDMChannelAsync(RequestOptions options) - => GetDMChannelAsync(options); } }