From 5ea1fb374e1fdb65d4410a5744434fbc01b39543 Mon Sep 17 00:00:00 2001
From: Still Hsu <341464@gmail.com>
Date: Sat, 20 Oct 2018 05:21:37 +0800
Subject: [PATCH] Add SyncPermissionsAsync to Sync Child Channels with its
Parent (#1159)
* Initial implementation
* Adjust according to comments
See: https://github.com/RogueException/Discord.Net/pull/1159/files/6e76b45713ae95cf6fdfb96b57ed7095f6b6cc59#diff-58466c35787d448266d026692e467baa
---
.../Entities/Channels/INestedChannel.cs | 5 +++++
src/Discord.Net.Rest/API/Common/Overwrite.cs | 2 +-
.../API/Rest/ModifyGuildChannelParams.cs | 4 +++-
.../Entities/Channels/ChannelHelper.cs | 17 +++++++++++++++++
.../Entities/Channels/RestTextChannel.cs | 2 ++
.../Entities/Channels/RestVoiceChannel.cs | 2 ++
.../Entities/Channels/SocketTextChannel.cs | 2 ++
.../Entities/Channels/SocketVoiceChannel.cs | 2 ++
8 files changed, 34 insertions(+), 2 deletions(-)
diff --git a/src/Discord.Net.Core/Entities/Channels/INestedChannel.cs b/src/Discord.Net.Core/Entities/Channels/INestedChannel.cs
index 22182a4ca..6719f91d4 100644
--- a/src/Discord.Net.Core/Entities/Channels/INestedChannel.cs
+++ b/src/Discord.Net.Core/Entities/Channels/INestedChannel.cs
@@ -25,5 +25,10 @@ namespace Discord
/// representing the parent of this channel; null if none is set.
///
Task GetCategoryAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
+
+ ///
+ /// Syncs the permissions of this nested channel with its parent's.
+ ///
+ Task SyncPermissionsAsync(RequestOptions options = null);
}
}
diff --git a/src/Discord.Net.Rest/API/Common/Overwrite.cs b/src/Discord.Net.Rest/API/Common/Overwrite.cs
index 1ba836127..1f3548a1c 100644
--- a/src/Discord.Net.Rest/API/Common/Overwrite.cs
+++ b/src/Discord.Net.Rest/API/Common/Overwrite.cs
@@ -1,4 +1,4 @@
-#pragma warning disable CS1591
+#pragma warning disable CS1591
using Newtonsoft.Json;
namespace Discord.API
diff --git a/src/Discord.Net.Rest/API/Rest/ModifyGuildChannelParams.cs b/src/Discord.Net.Rest/API/Rest/ModifyGuildChannelParams.cs
index 120eeb3a8..e5e8a4632 100644
--- a/src/Discord.Net.Rest/API/Rest/ModifyGuildChannelParams.cs
+++ b/src/Discord.Net.Rest/API/Rest/ModifyGuildChannelParams.cs
@@ -1,4 +1,4 @@
-#pragma warning disable CS1591
+#pragma warning disable CS1591
using Newtonsoft.Json;
namespace Discord.API.Rest
@@ -12,5 +12,7 @@ namespace Discord.API.Rest
public Optional Position { get; set; }
[JsonProperty("parent_id")]
public Optional CategoryId { get; set; }
+ [JsonProperty("permission_overwrites")]
+ public Optional Overwrites { get; set; }
}
}
diff --git a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
index 716f3beaf..65b4869b8 100644
--- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
@@ -348,6 +348,23 @@ namespace Discord.Rest
var model = await client.ApiClient.GetChannelAsync(channel.CategoryId.Value, options).ConfigureAwait(false);
return RestCategoryChannel.Create(client, model) as ICategoryChannel;
}
+ public static async Task SyncPermissionsAsync(INestedChannel channel, BaseDiscordClient client, RequestOptions options)
+ {
+ var category = await GetCategoryAsync(channel, client, options).ConfigureAwait(false);
+ if (category == null) throw new InvalidOperationException("This channel does not have a parent channel.");
+
+ var apiArgs = new ModifyGuildChannelParams
+ {
+ Overwrites = category.PermissionOverwrites
+ .Select(overwrite => new API.Overwrite{
+ TargetId = overwrite.TargetId,
+ TargetType = overwrite.TargetType,
+ Allow = overwrite.Permissions.AllowValue,
+ Deny = overwrite.Permissions.DenyValue
+ }).ToArray()
+ };
+ await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false);
+ }
//Helpers
private static IUser GetAuthor(BaseDiscordClient client, IGuild guild, UserModel model, ulong? webhookId)
diff --git a/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
index 4ccd35a3a..beeb8de19 100644
--- a/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
@@ -201,6 +201,8 @@ namespace Discord.Rest
///
public Task GetCategoryAsync(RequestOptions options = null)
=> ChannelHelper.GetCategoryAsync(this, Discord, options);
+ public Task SyncPermissionsAsync(RequestOptions options = null)
+ => ChannelHelper.SyncPermissionsAsync(this, Discord, options);
private string DebuggerDisplay => $"{Name} ({Id}, Text)";
diff --git a/src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs b/src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs
index 7f0295c18..1a2c5ceae 100644
--- a/src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs
@@ -57,6 +57,8 @@ namespace Discord.Rest
///
public Task GetCategoryAsync(RequestOptions options = null)
=> ChannelHelper.GetCategoryAsync(this, Discord, options);
+ public Task SyncPermissionsAsync(RequestOptions options = null)
+ => ChannelHelper.SyncPermissionsAsync(this, Discord, options);
private string DebuggerDisplay => $"{Name} ({Id}, Voice)";
diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs
index 8faf1c6eb..acd868020 100644
--- a/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs
+++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs
@@ -31,6 +31,8 @@ namespace Discord.WebSocket
///
public ICategoryChannel Category
=> CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null;
+ public Task SyncPermissionsAsync(RequestOptions options = null)
+ => ChannelHelper.SyncPermissionsAsync(this, Discord, options);
private bool _nsfw;
///
diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs
index 07977e5e0..dd71416db 100644
--- a/src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs
+++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs
@@ -30,6 +30,8 @@ namespace Discord.WebSocket
///
public ICategoryChannel Category
=> CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null;
+ public Task SyncPermissionsAsync(RequestOptions options = null)
+ => ChannelHelper.SyncPermissionsAsync(this, Discord, options);
///
public override IReadOnlyCollection Users