diff --git a/Discord.Net.Tests/Tests.cs b/Discord.Net.Tests/Tests.cs index 2b32c57c4..d055aa76d 100644 --- a/Discord.Net.Tests/Tests.cs +++ b/Discord.Net.Tests/Tests.cs @@ -29,7 +29,7 @@ namespace Discord.Net.Tests Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray()); Task.WaitAll(_bot2.Servers.Select(x => _bot2.LeaveServer(x)).ToArray()); - _testServer = _bot1.CreateServer("Discord.Net Testbed", Region.US_East).Result; + _testServer = _bot1.CreateServer("Discord.Net Testbed", Regions.US_East).Result; _testServerChannel = _testServer.DefaultChannel; Invite invite = _bot1.CreateInvite(_testServer, 60, 1, false, false).Result; _bot2.AcceptInvite(invite).Wait(); @@ -48,17 +48,22 @@ namespace Discord.Net.Tests } [TestMethod] - public void TestCreateRoom() + public void TestCreateTextRoom() + => TestCreateRoom(ChannelTypes.Text); + [TestMethod] + public void TestCreateVoiceRoom() + => TestCreateRoom(ChannelTypes.Voice); + private void TestCreateRoom(string type) { - Channel channel; + Channel channel = null; string name = $"test_{_random.Next()}"; AssertEvent( "ChannelCreated event never received", - () => channel = _bot1.CreateChannel(_testServerChannel, name), + () => channel = _bot1.CreateChannel(_testServer, name, type).Result, x => _bot2.ChannelCreated += x, x => _bot2.ChannelCreated -= x, (s, e) => e.Channel.Name == name); - + AssertEvent( "ChannelDestroyed event never received", () => _bot1.DestroyChannel(channel), diff --git a/Discord.Net/API/DiscordAPI.cs b/Discord.Net/API/DiscordAPI.cs index 82177fbc6..cbdd040c9 100644 --- a/Discord.Net/API/DiscordAPI.cs +++ b/Discord.Net/API/DiscordAPI.cs @@ -24,9 +24,7 @@ namespace Discord.API return response; } public static Task Logout(HttpOptions options) - { - return Http.Post(Endpoints.AuthLogout, options); - } + => Http.Post(Endpoints.AuthLogout, options); //Servers public static Task CreateServer(string name, string region, HttpOptions options) @@ -35,29 +33,26 @@ namespace Discord.API return Http.Post(Endpoints.Servers, request, options); } public static Task LeaveServer(string id, HttpOptions options) - { - return Http.Delete(Endpoints.Server(id), options); - } + => Http.Delete(Endpoints.Server(id), options); //Channels - public static Task GetMessages(string channelId, HttpOptions options) + public static Task CreateChannel(string serverId, string name, string channelType, HttpOptions options) { - return Http.Get(Endpoints.ChannelMessages(channelId, 50), options); + var request = new APIRequests.CreateChannel { Name = name, Type = channelType }; + return Http.Post(Endpoints.ServerChannels(serverId), request, options); } + public static Task DestroyChannel(string channelId, HttpOptions options) + => Http.Delete(Endpoints.Channel(channelId), options); + public static Task GetMessages(string channelId, HttpOptions options) + => Http.Get(Endpoints.ChannelMessages(channelId, 50), options); //Members public static Task Kick(string serverId, string memberId, HttpOptions options) - { - return Http.Delete(Endpoints.ServerMember(serverId, memberId), options); - } + => Http.Delete(Endpoints.ServerMember(serverId, memberId), options); public static Task Ban(string serverId, string memberId, HttpOptions options) - { - return Http.Put(Endpoints.ServerBan(serverId, memberId), options); - } + => Http.Put(Endpoints.ServerBan(serverId, memberId), options); public static Task Unban(string serverId, string memberId, HttpOptions options) - { - return Http.Delete(Endpoints.ServerBan(serverId, memberId), options); - } + => Http.Delete(Endpoints.ServerBan(serverId, memberId), options); //Invites public static Task CreateInvite(string channelId, int maxAge, int maxUses, bool isTemporary, bool hasXkcdPass, HttpOptions options) @@ -66,17 +61,11 @@ namespace Discord.API return Http.Post(Endpoints.ChannelInvites(channelId), request, options); } public static Task GetInvite(string id, HttpOptions options) - { - return Http.Get(Endpoints.Invite(id), options); - } + => Http.Get(Endpoints.Invite(id), options); public static Task AcceptInvite(string id, HttpOptions options) - { - return Http.Post(Endpoints.Invite(id), options); - } + => Http.Post(Endpoints.Invite(id), options); public static Task DeleteInvite(string id, HttpOptions options) - { - return Http.Delete(Endpoints.Invite(id), options); - } + => Http.Delete(Endpoints.Invite(id), options); //Chat public static Task SendMessage(string channelId, string message, string[] mentions, HttpOptions options) @@ -85,19 +74,13 @@ namespace Discord.API return Http.Post(Endpoints.ChannelMessages(channelId), request, options); } public static Task SendIsTyping(string channelId, HttpOptions options) - { - return Http.Post(Endpoints.ChannelTyping(channelId), options); - } + => Http.Post(Endpoints.ChannelTyping(channelId), options); //Voice public static Task GetVoiceRegions(HttpOptions options) - { - return Http.Get(Endpoints.VoiceRegions, options); - } + => Http.Get(Endpoints.VoiceRegions, options); public static Task GetVoiceIce(HttpOptions options) - { - return Http.Get(Endpoints.VoiceIce, options); - } + => Http.Get(Endpoints.VoiceIce, options); public static Task Mute(string serverId, string memberId, HttpOptions options) { var request = new APIRequests.SetMemberMute { Mute = true }; diff --git a/Discord.Net/API/Endpoints.cs b/Discord.Net/API/Endpoints.cs index 9f1780935..fefc5b295 100644 --- a/Discord.Net/API/Endpoints.cs +++ b/Discord.Net/API/Endpoints.cs @@ -19,6 +19,7 @@ // /api/guilds public static readonly string Servers = $"{BaseApi}/guilds"; public static string Server(string id) => $"{Servers}/{id}"; + public static string ServerChannels(string serverId) => $"{Servers}/{serverId}/channels"; public static string ServerMember(string serverId, string userId) => $"{Servers}/{serverId}/members/{userId}"; public static string ServerBan(string serverId, string userId) => $"{Servers}/{serverId}/bans/{userId}"; diff --git a/Discord.Net/API/Models/APIResponses.cs b/Discord.Net/API/Models/APIResponses.cs index 047466202..b3e8e7d1d 100644 --- a/Discord.Net/API/Models/APIResponses.cs +++ b/Discord.Net/API/Models/APIResponses.cs @@ -24,6 +24,9 @@ namespace Discord.API.Models public class CreateServer : ServerInfo { } public class DeleteServer : ServerInfo { } + public class CreateChannel : ChannelInfo { } + public class DestroyChannel : ChannelInfo { } + public class CreateInvite : GetInvite { [JsonProperty(PropertyName = "max_age")] diff --git a/Discord.Net/API/Models/ApiRequests.cs b/Discord.Net/API/Models/ApiRequests.cs index 8f61f5796..0989c5520 100644 --- a/Discord.Net/API/Models/ApiRequests.cs +++ b/Discord.Net/API/Models/ApiRequests.cs @@ -31,6 +31,14 @@ namespace Discord.API.Models public string Region; } + public class CreateChannel + { + [JsonProperty(PropertyName = "name")] + public string Name; + [JsonProperty(PropertyName = "type")] + public string Type; + } + public class CreateInvite { [JsonProperty(PropertyName = "max_age")] diff --git a/Discord.Net/ChannelTypes.cs b/Discord.Net/ChannelTypes.cs new file mode 100644 index 000000000..149646ae7 --- /dev/null +++ b/Discord.Net/ChannelTypes.cs @@ -0,0 +1,8 @@ +namespace Discord +{ + public static class ChannelTypes + { + public const string Text = "text"; + public const string Voice = "voice"; + } +} diff --git a/Discord.Net/Discord.Net.csproj b/Discord.Net/Discord.Net.csproj index 5968f7cb4..25cd10184 100644 --- a/Discord.Net/Discord.Net.csproj +++ b/Discord.Net/Discord.Net.csproj @@ -50,6 +50,7 @@ + @@ -60,7 +61,7 @@ - + diff --git a/Discord.Net/DiscordClient.cs b/Discord.Net/DiscordClient.cs index 860460257..32c8b400d 100644 --- a/Discord.Net/DiscordClient.cs +++ b/Discord.Net/DiscordClient.cs @@ -73,7 +73,7 @@ namespace Discord foreach (var channel in extendedModel.Channels) { _channels.Update(channel.Id, model.Id, channel); - if (channel.Type == "text") + if (channel.Type == ChannelTypes.Text) { try { @@ -437,6 +437,24 @@ namespace Discord return _servers.Remove(id); } + //Channels + public Task CreateChannel(Server server, string name, string region) + => CreateChannel(server.Id, name, region); + public async Task CreateChannel(string serverId, string name, string region) + { + CheckReady(); + var response = await DiscordAPI.CreateChannel(serverId, name, region, _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); + } + //Bans public Task Ban(Server server, User user) => Ban(server.Id, user.Id); diff --git a/Discord.Net/Region.cs b/Discord.Net/Regions.cs similarity index 91% rename from Discord.Net/Region.cs rename to Discord.Net/Regions.cs index 89a6e76d0..087b2c552 100644 --- a/Discord.Net/Region.cs +++ b/Discord.Net/Regions.cs @@ -1,6 +1,6 @@ namespace Discord { - public static class Region + public static class Regions { public const string US_West = "us-west"; public const string US_East = "us-east";