Merge pull request #656 from AntiTcb/fix/GetDMChannelAsync

Remove IUser.CreateDMChannelAsync / Fix SocketGlobalUser.DMChannel
This commit is contained in:
Christopher F
2017-06-16 21:22:56 -04:00
committed by GitHub
7 changed files with 19 additions and 27 deletions

View File

@@ -20,8 +20,6 @@ namespace Discord
string Username { get; }
/// <summary> Returns a private message channel to this user, creating one if it does not already exist. </summary>
Task<IDMChannel> GetDMChannelAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
/// <summary> Returns a private message channel to this user, creating one if it does not already exist. </summary>
Task<IDMChannel> CreateDMChannelAsync(RequestOptions options = null);
Task<IDMChannel> GetOrCreateDMChannelAsync(RequestOptions options = null);
}
}

View File

@@ -54,7 +54,7 @@ namespace Discord.Rest
Update(model);
}
public Task<RestDMChannel> CreateDMChannelAsync(RequestOptions options = null)
public Task<RestDMChannel> GetOrCreateDMChannelAsync(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<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IDMChannel>(null);
async Task<IDMChannel> IUser.CreateDMChannelAsync(RequestOptions options)
=> await CreateDMChannelAsync(options).ConfigureAwait(false);
async Task<IDMChannel> IUser.GetOrCreateDMChannelAsync(RequestOptions options)
=> await GetOrCreateDMChannelAsync(options);
}
}

View File

@@ -49,7 +49,7 @@ namespace Discord.Rpc
Username = model.Username.Value;
}
public Task<RestDMChannel> CreateDMChannelAsync(RequestOptions options = null)
public Task<RestDMChannel> GetOrCreateDMChannelAsync(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<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IDMChannel>(null);
async Task<IDMChannel> IUser.CreateDMChannelAsync(RequestOptions options)
=> await CreateDMChannelAsync(options).ConfigureAwait(false);
async Task<IDMChannel> IUser.GetOrCreateDMChannelAsync(RequestOptions options)
=> await GetOrCreateDMChannelAsync(options);
}
}

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -146,11 +146,7 @@ namespace Discord.WebSocket
IGuild IGuildUser.Guild => Guild;
ulong IGuildUser.GuildId => Guild.Id;
IReadOnlyCollection<ulong> IGuildUser.RoleIds => _roleIds;
//IUser
Task<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IDMChannel>(GlobalUser.DMChannel);
//IVoiceState
IVoiceChannel IVoiceState.VoiceChannel => VoiceChannel;
}

View File

@@ -53,10 +53,10 @@ namespace Discord.WebSocket
hasChanges = true;
}
return hasChanges;
}
}
public Task<RestDMChannel> CreateDMChannelAsync(RequestOptions options = null)
=> UserHelper.CreateDMChannelAsync(this, Discord, options);
public async Task<IDMChannel> GetOrCreateDMChannelAsync(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);
@@ -64,11 +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<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IDMChannel>(GlobalUser.DMChannel);
async Task<IDMChannel> IUser.CreateDMChannelAsync(RequestOptions options)
=> await CreateDMChannelAsync(options).ConfigureAwait(false);
}
}