From 66d8eab986ce8f2b0179ed594b211dbefc62c4e6 Mon Sep 17 00:00:00 2001 From: Brandon Smith Date: Sun, 30 Aug 2015 16:46:52 -0300 Subject: [PATCH] Changed GetPMChannel to GetOrCreatePMChannel, added SendPrivateMessage, hid User.PrivateChannel --- src/Discord.Net/DiscordClient.API.cs | 18 ++++++++++-- src/Discord.Net/DiscordClient.Cache.cs | 38 +++++++++++++------------- src/Discord.Net/User.cs | 4 +-- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/Discord.Net/DiscordClient.API.cs b/src/Discord.Net/DiscordClient.API.cs index 0d142788f..11b4765a9 100644 --- a/src/Discord.Net/DiscordClient.API.cs +++ b/src/Discord.Net/DiscordClient.API.cs @@ -57,9 +57,6 @@ namespace Discord return _channels.Update(response.Id, serverId, response); } - /// Creates a new private channel with the provided user. - public Task CreatePMChannel(User user) - => CreatePMChannel(user?.Id); /// Creates a new private channel with the provided user. public async Task CreatePMChannel(string userId) { @@ -278,6 +275,21 @@ namespace Discord } return result; } + /// Sends a private message to the provided channel. + public async Task SendPrivateMessage(User user, string text) + => await SendMessage(await GetOrCreatePMChannel(user), text, new string[0]); + /// Sends a private message to the provided channel. + public async Task SendPrivateMessage(string userId, string text) + => await SendMessage(await GetOrCreatePMChannel(userId), text, new string[0]); + /*/// Sends a private message to the provided user, mentioning certain users. + /// While not required, it is recommended to include a mention reference in the text (see User.Mention). + public async Task SendPrivateMessage(User user, string text, string[] mentions) + => SendMessage(await GetOrCreatePMChannel(user), text, mentions); + /// Sends a private message to the provided user, mentioning certain users. + /// While not required, it is recommended to include a mention reference in the text (see User.Mention). + public async Task SendPrivateMessage(string userId, string text, string[] mentions) + => SendMessage(await GetOrCreatePMChannel(userId), text, mentions);*/ + /// Edits a message the provided message. public Task EditMessage(Message message, string text) diff --git a/src/Discord.Net/DiscordClient.Cache.cs b/src/Discord.Net/DiscordClient.Cache.cs index 1f087b208..c17273930 100644 --- a/src/Discord.Net/DiscordClient.Cache.cs +++ b/src/Discord.Net/DiscordClient.Cache.cs @@ -286,7 +286,8 @@ namespace Discord } /// Returns the user with the specified id, or null if none was found. - public User GetUser(string id) => _users[id]; + public User GetUser(string id) + => _users[id]; /// Returns the user with the specified name and discriminator, or null if none was found. /// Name formats supported: Name and @Name. Search is case-insensitive. public User GetUser(string name, string discriminator) @@ -369,7 +370,8 @@ namespace Discord } /// Returns the server with the specified id, or null if none was found. - public Server GetServer(string id) => _servers[id]; + public Server GetServer(string id) + => _servers[id]; /// Returns all servers with the specified name. /// Search is case-insensitive. public IEnumerable FindServers(string name) @@ -382,25 +384,21 @@ 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) + /// Returns the private channel with the provided user, creating one if it does not currently exist. + public Task GetOrCreatePMChannel(string userId) + => GetOrCreatePMChannel(_users[userId]); + /// Returns the private channel with the provided user, creating one if it does not currently exist. + public async Task GetOrCreatePMChannel(User user) { - if (user == null) - { - if (createIfNotExists) - throw new ArgumentNullException(nameof(user)); - else - return null; - } + CheckReady(); + if (user == null) throw new ArgumentNullException(nameof(user)); var channel = user.PrivateChannel; - if (channel == null && createIfNotExists) - await CreatePMChannel(user); - return channel; + if (channel != null) + return channel; + return await CreatePMChannel(user?.Id); } + /// 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) @@ -426,7 +424,8 @@ namespace Discord } /// Returns the role with the specified id, or null if none was found. - public Role GetRole(string id) => _roles[id]; + public Role GetRole(string id) + => _roles[id]; /// Returns all roles with the specified server and name. /// Name formats supported: Name and @Name. Search is case-insensitive. public IEnumerable FindRoles(Server server, string name) @@ -452,6 +451,7 @@ namespace Discord } /// Returns the message with the specified id, or null if none was found. - public Message GetMessage(string id) => _messages[id]; + public Message GetMessage(string id) + => _messages[id]; } } diff --git a/src/Discord.Net/User.cs b/src/Discord.Net/User.cs index 49b2e7d6d..4d48440d1 100644 --- a/src/Discord.Net/User.cs +++ b/src/Discord.Net/User.cs @@ -31,8 +31,8 @@ namespace Discord /// Returns the string "<@Id>" to be used as a shortcut when including mentions in text. public string Mention => $"<@{Id}>"; - public string PrivateChannelId { get; internal set; } - public Channel PrivateChannel => _client.GetChannel(PrivateChannelId); + internal string PrivateChannelId { get; internal set; } + internal Channel PrivateChannel => _client.GetChannel(PrivateChannelId); //TODO: Add voice /// Returns the time this user last sent a message.