Browse Source

Merge pull request #656 from AntiTcb/fix/GetDMChannelAsync

Remove IUser.CreateDMChannelAsync / Fix SocketGlobalUser.DMChannel
tags/1.0
Christopher F GitHub 8 years ago
parent
commit
1942637380
7 changed files with 19 additions and 27 deletions
  1. +1
    -3
      src/Discord.Net.Core/Entities/Users/IUser.cs
  2. +3
    -5
      src/Discord.Net.Rest/Entities/Users/RestUser.cs
  3. +3
    -5
      src/Discord.Net.Rpc/Entities/Users/RpcUser.cs
  4. +6
    -0
      src/Discord.Net.WebSocket/DiscordSocketClient.cs
  5. +2
    -0
      src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs
  6. +1
    -5
      src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs
  7. +3
    -9
      src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs

+ 1
- 3
src/Discord.Net.Core/Entities/Users/IUser.cs View File

@@ -20,8 +20,6 @@ namespace Discord
string Username { get; } string Username { get; }


/// <summary> Returns a private message channel to this user, creating one if it does not already exist. </summary> /// <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);
} }
} }

+ 3
- 5
src/Discord.Net.Rest/Entities/Users/RestUser.cs View File

@@ -54,7 +54,7 @@ namespace Discord.Rest
Update(model); Update(model);
} }


public Task<RestDMChannel> CreateDMChannelAsync(RequestOptions options = null)
public Task<RestDMChannel> GetOrCreateDMChannelAsync(RequestOptions options = null)
=> UserHelper.CreateDMChannelAsync(this, Discord, options); => UserHelper.CreateDMChannelAsync(this, Discord, options);


public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) 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" : "")})"; private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})";


//IUser //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);
} }
} }

+ 3
- 5
src/Discord.Net.Rpc/Entities/Users/RpcUser.cs View File

@@ -49,7 +49,7 @@ namespace Discord.Rpc
Username = model.Username.Value; Username = model.Username.Value;
} }


public Task<RestDMChannel> CreateDMChannelAsync(RequestOptions options = null)
public Task<RestDMChannel> GetOrCreateDMChannelAsync(RequestOptions options = null)
=> UserHelper.CreateDMChannelAsync(this, Discord, options); => UserHelper.CreateDMChannelAsync(this, Discord, options);


public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128) 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" : "")})"; private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})";


//IUser //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);
} }
} }

+ 6
- 0
src/Discord.Net.WebSocket/DiscordSocketClient.cs View File

@@ -1634,6 +1634,9 @@ namespace Discord.WebSocket
{ {
var channel = SocketChannel.CreatePrivate(this, state, model); var channel = SocketChannel.CreatePrivate(this, state, model);
state.AddChannel(channel as SocketChannel); state.AddChannel(channel as SocketChannel);
if (channel is SocketDMChannel dm)
dm.Recipient.GlobalUser.DMChannel = dm;

return channel; return channel;
} }
internal ISocketPrivateChannel RemovePrivateChannel(ulong id) internal ISocketPrivateChannel RemovePrivateChannel(ulong id)
@@ -1641,6 +1644,9 @@ namespace Discord.WebSocket
var channel = State.RemoveChannel(id) as ISocketPrivateChannel; var channel = State.RemoveChannel(id) as ISocketPrivateChannel;
if (channel != null) if (channel != null)
{ {
if (channel is SocketDMChannel dmChannel)
dmChannel.Recipient.GlobalUser.DMChannel = null;

foreach (var recipient in channel.Recipients) foreach (var recipient in channel.Recipients)
recipient.GlobalUser.RemoveRef(this); recipient.GlobalUser.RemoveRef(this);
} }


+ 2
- 0
src/Discord.Net.WebSocket/Entities/Users/SocketGlobalUser.cs View File

@@ -1,4 +1,5 @@
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using Model = Discord.API.User; using Model = Discord.API.User;
using PresenceModel = Discord.API.Presence; using PresenceModel = Discord.API.Presence;


@@ -51,6 +52,7 @@ namespace Discord.WebSocket
internal void Update(ClientState state, PresenceModel model) internal void Update(ClientState state, PresenceModel model)
{ {
Presence = SocketPresence.Create(model); Presence = SocketPresence.Create(model);
DMChannel = state.DMChannels.FirstOrDefault(x => x.Recipient.Id == Id);
} }
internal new SocketGlobalUser Clone() => MemberwiseClone() as SocketGlobalUser; internal new SocketGlobalUser Clone() => MemberwiseClone() as SocketGlobalUser;


+ 1
- 5
src/Discord.Net.WebSocket/Entities/Users/SocketGuildUser.cs View File

@@ -146,11 +146,7 @@ namespace Discord.WebSocket
IGuild IGuildUser.Guild => Guild; IGuild IGuildUser.Guild => Guild;
ulong IGuildUser.GuildId => Guild.Id; ulong IGuildUser.GuildId => Guild.Id;
IReadOnlyCollection<ulong> IGuildUser.RoleIds => _roleIds; IReadOnlyCollection<ulong> IGuildUser.RoleIds => _roleIds;

//IUser
Task<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IDMChannel>(GlobalUser.DMChannel);

//IVoiceState //IVoiceState
IVoiceChannel IVoiceState.VoiceChannel => VoiceChannel; IVoiceChannel IVoiceState.VoiceChannel => VoiceChannel;
} }


+ 3
- 9
src/Discord.Net.WebSocket/Entities/Users/SocketUser.cs View File

@@ -53,10 +53,10 @@ namespace Discord.WebSocket
hasChanges = true; hasChanges = true;
} }
return hasChanges; 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) public string GetAvatarUrl(ImageFormat format = ImageFormat.Auto, ushort size = 128)
=> CDN.GetUserAvatarUrl(Id, AvatarId, size, format); => CDN.GetUserAvatarUrl(Id, AvatarId, size, format);
@@ -64,11 +64,5 @@ namespace Discord.WebSocket
public override string ToString() => $"{Username}#{Discriminator}"; public override string ToString() => $"{Username}#{Discriminator}";
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})"; private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})";
internal SocketUser Clone() => MemberwiseClone() as SocketUser; 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);
} }
} }

Loading…
Cancel
Save