From f28821b0f8878f3687a4e0edf6c9ee4cd3138fd1 Mon Sep 17 00:00:00 2001 From: David Slutsky Date: Sat, 1 Feb 2020 01:44:59 +0200 Subject: [PATCH] Added the option to set overwrites on channel creation --- .../Rest/CreateChannelPermissionsParams.cs | 29 +++++++++++++++++++ .../API/Rest/CreateGuildChannelParams.cs | 3 ++ .../Entities/Guilds/GuildHelper.cs | 21 ++++++++++---- .../Entities/Guilds/RestGuild.cs | 12 ++++---- .../Entities/Guilds/SocketGuild.cs | 12 ++++---- 5 files changed, 59 insertions(+), 18 deletions(-) create mode 100644 src/Discord.Net.Rest/API/Rest/CreateChannelPermissionsParams.cs diff --git a/src/Discord.Net.Rest/API/Rest/CreateChannelPermissionsParams.cs b/src/Discord.Net.Rest/API/Rest/CreateChannelPermissionsParams.cs new file mode 100644 index 000000000..43fbe08c1 --- /dev/null +++ b/src/Discord.Net.Rest/API/Rest/CreateChannelPermissionsParams.cs @@ -0,0 +1,29 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Discord.API.Rest +{ + class CreateChannelPermissionsParams + { + [JsonProperty("id")] + public ulong Id { get; set; } + [JsonProperty("type")] + public string Type { get; } + [JsonProperty("allow")] + public ulong Allow { get; } + [JsonProperty("deny")] + public ulong Deny { get; } + + public CreateChannelPermissionsParams(ulong id, string type, ulong allow, ulong deny) + { + Id = id; + Type = type; + Allow = allow; + Deny = deny; + } + } +} diff --git a/src/Discord.Net.Rest/API/Rest/CreateGuildChannelParams.cs b/src/Discord.Net.Rest/API/Rest/CreateGuildChannelParams.cs index a102bd38d..6ed37a76e 100644 --- a/src/Discord.Net.Rest/API/Rest/CreateGuildChannelParams.cs +++ b/src/Discord.Net.Rest/API/Rest/CreateGuildChannelParams.cs @@ -1,5 +1,6 @@ #pragma warning disable CS1591 using Newtonsoft.Json; +using System.Collections.Generic; namespace Discord.API.Rest { @@ -14,6 +15,8 @@ namespace Discord.API.Rest public Optional CategoryId { get; set; } [JsonProperty("position")] public Optional Position { get; set; } + [JsonProperty("permission_overwrite")] + public Optional> PermissionOverwrite {get;set;} //Text channels [JsonProperty("topic")] diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs index 790b1e5c3..613f10de5 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs @@ -164,54 +164,63 @@ namespace Discord.Rest } /// is null. public static async Task CreateTextChannelAsync(IGuild guild, BaseDiscordClient client, - string name, RequestOptions options, Action func = null) + string name, RequestOptions options, Action func = null, IEnumerable overwrites = null) { if (name == null) throw new ArgumentNullException(paramName: nameof(name)); var props = new TextChannelProperties(); func?.Invoke(props); + var perms = overwrites?.Select(perm => new CreateChannelPermissionsParams(perm.TargetId, perm.TargetType == PermissionTarget.Role ? "role" : "member", perm.Permissions.AllowValue, perm.Permissions.DenyValue)); + var args = new CreateGuildChannelParams(name, ChannelType.Text) { CategoryId = props.CategoryId, Topic = props.Topic, IsNsfw = props.IsNsfw, - Position = props.Position + Position = props.Position, + PermissionOverwrite = new Optional>(perms) }; var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false); return RestTextChannel.Create(client, guild, model); } /// is null. public static async Task CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client, - string name, RequestOptions options, Action func = null) + string name, RequestOptions options, Action func = null, IEnumerable overwrites = null) { if (name == null) throw new ArgumentNullException(paramName: nameof(name)); var props = new VoiceChannelProperties(); func?.Invoke(props); + var perms = overwrites?.Select(perm => new CreateChannelPermissionsParams(perm.TargetId, perm.TargetType == PermissionTarget.Role ? "role" : "member", perm.Permissions.AllowValue, perm.Permissions.DenyValue)); + var args = new CreateGuildChannelParams(name, ChannelType.Voice) { CategoryId = props.CategoryId, Bitrate = props.Bitrate, UserLimit = props.UserLimit, - Position = props.Position + Position = props.Position, + PermissionOverwrite = new Optional>(perms) }; var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false); return RestVoiceChannel.Create(client, guild, model); } /// is null. public static async Task CreateCategoryChannelAsync(IGuild guild, BaseDiscordClient client, - string name, RequestOptions options, Action func = null) + string name, RequestOptions options, Action func = null, IEnumerable overwrites = null) { if (name == null) throw new ArgumentNullException(paramName: nameof(name)); var props = new GuildChannelProperties(); func?.Invoke(props); + var perms = overwrites?.Select(perm => new CreateChannelPermissionsParams(perm.TargetId, perm.TargetType == PermissionTarget.Role ? "role" : "member", perm.Permissions.AllowValue, perm.Permissions.DenyValue)); + var args = new CreateGuildChannelParams(name, ChannelType.Category) { - Position = props.Position + Position = props.Position, + PermissionOverwrite = new Optional>(perms) }; var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false); diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs index 900f5045e..3cd3bdca9 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs @@ -450,8 +450,8 @@ namespace Discord.Rest /// A task that represents the asynchronous creation operation. The task result contains the newly created /// text channel. /// - public Task CreateTextChannelAsync(string name, Action func = null, RequestOptions options = null) - => GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func); + public Task CreateTextChannelAsync(string name, Action func = null, RequestOptions options = null, IEnumerable overwritePermissions = null) + => GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func, overwritePermissions); /// /// Creates a voice channel with the provided name. /// @@ -462,8 +462,8 @@ namespace Discord.Rest /// /// The created voice channel. /// - public Task CreateVoiceChannelAsync(string name, Action func = null, RequestOptions options = null) - => GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options, func); + public Task CreateVoiceChannelAsync(string name, Action func = null, RequestOptions options = null, IEnumerable overwritePermissions = null) + => GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options, func, overwritePermissions); /// /// Creates a category channel with the provided name. /// @@ -474,8 +474,8 @@ namespace Discord.Rest /// /// The created category channel. /// - public Task CreateCategoryChannelAsync(string name, Action func = null, RequestOptions options = null) - => GuildHelper.CreateCategoryChannelAsync(this, Discord, name, options, func); + public Task CreateCategoryChannelAsync(string name, Action func = null, RequestOptions options = null, IEnumerable overwritePermissions = null) + => GuildHelper.CreateCategoryChannelAsync(this, Discord, name, options, func, overwritePermissions); /// /// Gets a collection of all the voice regions this guild can access. diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index da9a316eb..20b46349e 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -581,8 +581,8 @@ namespace Discord.WebSocket /// A task that represents the asynchronous creation operation. The task result contains the newly created /// text channel. /// - public Task CreateTextChannelAsync(string name, Action func = null, RequestOptions options = null) - => GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func); + public Task CreateTextChannelAsync(string name, Action func = null, RequestOptions options = null, IEnumerable overwritePermissions = null) + => GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func, overwritePermissions); /// /// Creates a new voice channel in this guild. /// @@ -594,8 +594,8 @@ namespace Discord.WebSocket /// A task that represents the asynchronous creation operation. The task result contains the newly created /// voice channel. /// - public Task CreateVoiceChannelAsync(string name, Action func = null, RequestOptions options = null) - => GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options, func); + public Task CreateVoiceChannelAsync(string name, Action func = null, RequestOptions options = null, IEnumerable overwritePermissions = null) + => GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options, func, overwritePermissions); /// /// Creates a new channel category in this guild. /// @@ -607,8 +607,8 @@ namespace Discord.WebSocket /// A task that represents the asynchronous creation operation. The task result contains the newly created /// category channel. /// - public Task CreateCategoryChannelAsync(string name, Action func = null, RequestOptions options = null) - => GuildHelper.CreateCategoryChannelAsync(this, Discord, name, options, func); + public Task CreateCategoryChannelAsync(string name, Action func = null, RequestOptions options = null, IEnumerable overwritePermissions = null) + => GuildHelper.CreateCategoryChannelAsync(this, Discord, name, options, func, overwritePermissions); internal SocketGuildChannel AddChannel(ClientState state, ChannelModel model) {