From 2c7d049704396d2a64d05f60fe536fd85d2aaeaf Mon Sep 17 00:00:00 2001 From: Brandon Smith Date: Fri, 21 Aug 2015 20:18:18 -0300 Subject: [PATCH] Added GetPMChannel --- src/Discord.Net/DiscordClient.cs | 30 +++++++++++++++++++++++++++--- src/Discord.Net/User.cs | 3 +++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/Discord.Net/DiscordClient.cs b/src/Discord.Net/DiscordClient.cs index c410a3278..f703ada3d 100644 --- a/src/Discord.Net/DiscordClient.cs +++ b/src/Discord.Net/DiscordClient.cs @@ -106,10 +106,23 @@ namespace Discord { var extendedModel = model as ChannelInfo; channel.PermissionOverwrites = extendedModel.PermissionOverwrites; - channel.RecipientId = extendedModel.Recipient?.Id; + if (extendedModel.IsPrivate) + { + var user = _users.Update(extendedModel.Recipient.Id, extendedModel.Recipient); + channel.RecipientId = user.Id; + user.PrivateChannelId = channel.Id; + } } }, - channel => { }); + channel => + { + if (channel.IsPrivate) + { + var user = channel.Recipient; + if (user.PrivateChannelId == channel.Id) + user.PrivateChannelId = null; + } + }); _messages = new AsyncCache( (key, parentKey) => new Message(key, parentKey, this), (message, model) => @@ -451,7 +464,7 @@ namespace Discord ) .FirstOrDefault(); } - /// Returns all users with the specified name. + /// Returns all users with the specified name across all servers. /// Name formats supported: Name and @Name. Search is case-insensitive. /*public IEnumerable FindUsers(string name) { @@ -506,6 +519,17 @@ namespace Discord /// Returns the channel with the specified id, or null if none was found. public Channel GetChannel(string id) => _channels[id]; + /// Returns a private channel with the provided user. + public Task GetPMChannel(string userId, bool createIfNotExists = false) + => GetPMChannel(_users[userId], createIfNotExists); + /// Returns a private channel with the provided user. + public async Task GetPMChannel(User user, bool createIfNotExists = false) + { + var channel = user.PrivateChannel; + if (channel == null && createIfNotExists) + await CreatePMChannel(user); + return channel; + } /// Returns all channels with the specified server and name. /// Name formats supported: Name and #Name. Search is case-insensitive. public IEnumerable FindChannels(Server server, string name) diff --git a/src/Discord.Net/User.cs b/src/Discord.Net/User.cs index c9b5e484e..eee366cab 100644 --- a/src/Discord.Net/User.cs +++ b/src/Discord.Net/User.cs @@ -35,6 +35,9 @@ namespace Discord /// Returns the string "<@Id>" to be used as a shortcut when including mentions in text. public string Mention { get { return $"<@{Id}>"; } } + public string PrivateChannelId { get; internal set; } + public Channel PrivateChannel => _client.GetChannel(PrivateChannelId); + //TODO: Add voice /// Returns the time this user last sent a message. /// Is not currently affected by voice activity