From 28d69300b86d256f8177422207847ddb41b6c31f Mon Sep 17 00:00:00 2001 From: Brandon Smith Date: Thu, 13 Aug 2015 12:22:09 -0300 Subject: [PATCH] Added CreatePMChannel --- Discord.Net.Tests/Tests.cs | 2 +- Discord.Net/API/DiscordAPI.cs | 5 ++++ Discord.Net/API/Endpoints.cs | 20 +++++++------ Discord.Net/API/Models/ApiRequests.cs | 5 ++++ Discord.Net/DiscordClient.cs | 41 ++++++++++++++++++++------- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/Discord.Net.Tests/Tests.cs b/Discord.Net.Tests/Tests.cs index 6581fa0be..ef01833cb 100644 --- a/Discord.Net.Tests/Tests.cs +++ b/Discord.Net.Tests/Tests.cs @@ -31,7 +31,7 @@ namespace Discord.Net.Tests Task.WaitAll(_bot2.Servers.Select(x => _bot2.LeaveServer(x)).ToArray()); //Create new server and invite other bot to it - _testServer = _bot1.CreateServer("Discord.Net Testbed", Regions.US_East).Result; + _testServer = _bot1.CreateServer("Discord.Net Testing", Regions.US_East).Result; _testServerChannel = _testServer.DefaultChannel; Invite invite = _bot1.CreateInvite(_testServer, 60, 1, false, false).Result; _bot2.AcceptInvite(invite).Wait(); diff --git a/Discord.Net/API/DiscordAPI.cs b/Discord.Net/API/DiscordAPI.cs index cbdd040c9..9ea171692 100644 --- a/Discord.Net/API/DiscordAPI.cs +++ b/Discord.Net/API/DiscordAPI.cs @@ -41,6 +41,11 @@ namespace Discord.API var request = new APIRequests.CreateChannel { Name = name, Type = channelType }; return Http.Post(Endpoints.ServerChannels(serverId), request, options); } + public static Task CreatePMChannel(string myId, string recipientId, HttpOptions options) + { + var request = new APIRequests.CreatePMChannel { RecipientId = recipientId }; + return Http.Post(Endpoints.UserChannels(myId), request, options); + } public static Task DestroyChannel(string channelId, HttpOptions options) => Http.Delete(Endpoints.Channel(channelId), options); public static Task GetMessages(string channelId, HttpOptions options) diff --git a/Discord.Net/API/Endpoints.cs b/Discord.Net/API/Endpoints.cs index fefc5b295..292d0cc9e 100644 --- a/Discord.Net/API/Endpoints.cs +++ b/Discord.Net/API/Endpoints.cs @@ -16,6 +16,14 @@ public static readonly string AuthLogin = $"{Auth}/login"; public static readonly string AuthLogout = $"{Auth}/logout"; + // /api/channels + public static readonly string Channels = $"{BaseApi}/channels"; + public static string Channel(string id) => $"{Channels}/{id}"; + public static string ChannelTyping(string id) => $"{Channels}/{id}/typing"; + public static string ChannelMessages(string id) => $"{Channels}/{id}/messages"; + public static string ChannelMessages(string id, int limit) => $"{Channels}/{id}/messages?limit={limit}"; + public static string ChannelInvites(string id) => $"{Channels}/{id}/invites"; + // /api/guilds public static readonly string Servers = $"{BaseApi}/guilds"; public static string Server(string id) => $"{Servers}/{id}"; @@ -27,15 +35,11 @@ public static readonly string Invites = $"{BaseApi}/invite"; public static string Invite(string id) => $"{Invites}/{id}"; - // /api/channels - public static readonly string Channels = $"{BaseApi}/channels"; - public static string Channel(string id) => $"{Channels}/{id}"; - public static string ChannelTyping(string id) => $"{Channels}/{id}/typing"; - public static string ChannelMessages(string id) => $"{Channels}/{id}/messages"; - public static string ChannelMessages(string id, int limit) => $"{Channels}/{id}/messages?limit={limit}"; - public static string ChannelInvites(string id) => $"{Channels}/{id}/invites"; + // /api/users + public static readonly string Users = $"{BaseApi}/users"; + public static string UserChannels(string id) => $"{Users}/{id}/channels"; - // /api/voice + // /api/voice public static readonly string Voice = $"{BaseApi}/voice"; public static readonly string VoiceRegions = $"{Voice}/regions"; public static readonly string VoiceIce = $"{Voice}/ice"; diff --git a/Discord.Net/API/Models/ApiRequests.cs b/Discord.Net/API/Models/ApiRequests.cs index 0989c5520..26ed03459 100644 --- a/Discord.Net/API/Models/ApiRequests.cs +++ b/Discord.Net/API/Models/ApiRequests.cs @@ -38,6 +38,11 @@ namespace Discord.API.Models [JsonProperty(PropertyName = "type")] public string Type; } + public class CreatePMChannel + { + [JsonProperty(PropertyName = "recipient_id")] + public string RecipientId; + } public class CreateInvite { diff --git a/Discord.Net/DiscordClient.cs b/Discord.Net/DiscordClient.cs index 74c2c4d07..55eb1819d 100644 --- a/Discord.Net/DiscordClient.cs +++ b/Discord.Net/DiscordClient.cs @@ -444,16 +444,15 @@ namespace Discord } public Task LeaveServer(Server server) => LeaveServer(server.Id); - public async Task LeaveServer(string id) + public async Task LeaveServer(string serverId) { CheckReady(); try { - await DiscordAPI.LeaveServer(id, _httpOptions); + await DiscordAPI.LeaveServer(serverId, _httpOptions); } - //Happens if the room was destroyed as we try to leave it catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) {} - return _servers.Remove(id); + return _servers.Remove(serverId); } //Channels @@ -465,13 +464,25 @@ namespace Discord var response = await DiscordAPI.CreateChannel(serverId, name, region, _httpOptions); return _channels.Update(response.Id, response); } + public Task CreatePMChannel(User user, string name, string region) + => CreateChannel(user.Id, name, region); + public async Task CreatePMChannel(string recipientId, string name, string region) + { + CheckReady(); + var response = await DiscordAPI.CreatePMChannel(UserId, recipientId, _httpOptions); + return _channels.Update(response.Id, response); + } public Task DestroyChannel(Channel channel) => DestroyChannel(channel.Id); public async Task DestroyChannel(string channelId) { CheckReady(); - var response = await DiscordAPI.DestroyChannel(channelId, _httpOptions); - return _channels.Remove(response.Id); + try + { + var response = await DiscordAPI.DestroyChannel(channelId, _httpOptions); + } + catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) { } + return _channels.Remove(channelId); } //Bans @@ -492,10 +503,14 @@ namespace Discord => Unban(server.Id, userId); public Task Unban(string server, User user) => Unban(server, user.Id); - public Task Unban(string serverId, string userId) + public async Task Unban(string serverId, string userId) { CheckReady(); - return DiscordAPI.Unban(serverId, userId, _httpOptions); + try + { + await DiscordAPI.Unban(serverId, userId, _httpOptions); + } + catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) { } } @@ -553,9 +568,13 @@ namespace Discord public async Task DeleteInvite(string id) { CheckReady(); - //Check if this is a human-readable link and get its ID - var response = await DiscordAPI.GetInvite(id, _httpOptions); - await DiscordAPI.DeleteInvite(response.Code, _httpOptions); + try + { + //Check if this is a human-readable link and get its ID + var response = await DiscordAPI.GetInvite(id, _httpOptions); + await DiscordAPI.DeleteInvite(response.Code, _httpOptions); + } + catch (WebException ex) when ((ex.Response as HttpWebResponse)?.StatusCode == HttpStatusCode.NotFound) { } } //Chat