From 6e76b45713ae95cf6fdfb96b57ed7095f6b6cc59 Mon Sep 17 00:00:00 2001
From: Still Hsu <341464@gmail.com>
Date: Tue, 11 Sep 2018 01:48:55 +0800
Subject: [PATCH] Initial implementation
---
.../Entities/Channels/INestedChannel.cs | 5 +++++
src/Discord.Net.Rest/API/Common/Overwrite.cs | 10 +++++++++-
.../API/Rest/ModifyGuildChannelParams.cs | 4 +++-
.../Entities/Channels/ChannelHelper.cs | 14 ++++++++++++++
.../Entities/Channels/RestTextChannel.cs | 2 ++
.../Entities/Channels/RestVoiceChannel.cs | 2 ++
.../Entities/Channels/SocketTextChannel.cs | 2 ++
.../Entities/Channels/SocketVoiceChannel.cs | 2 ++
8 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/src/Discord.Net.Core/Entities/Channels/INestedChannel.cs b/src/Discord.Net.Core/Entities/Channels/INestedChannel.cs
index c8d2bcaaf..8a9d74a8f 100644
--- a/src/Discord.Net.Core/Entities/Channels/INestedChannel.cs
+++ b/src/Discord.Net.Core/Entities/Channels/INestedChannel.cs
@@ -12,5 +12,10 @@ namespace Discord
ulong? CategoryId { get; }
/// Gets the parent channel (category) of this channel, if it is set. If unset, returns null.
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..bf5e85fef 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
@@ -13,5 +13,13 @@ namespace Discord.API
public ulong Deny { get; set; }
[JsonProperty("allow"), Int53]
public ulong Allow { get; set; }
+
+ public Overwrite(ulong targetId, PermissionTarget targetType, ulong allowValue, ulong denyValue)
+ {
+ TargetId = targetId;
+ TargetType = targetType;
+ Allow = allowValue;
+ Deny = denyValue;
+ }
}
}
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 50cb352a7..5b108577b 100644
--- a/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
@@ -326,6 +326,20 @@ 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) return;
+
+ var apiArgs = new ModifyGuildChannelParams
+ {
+ Overwrites = category.PermissionOverwrites
+ .Select(overwrite => new API.Overwrite(overwrite.TargetId, overwrite.TargetType,
+ overwrite.Permissions.AllowValue, 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 7f57c96ff..f41344203 100644
--- a/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
@@ -92,6 +92,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 13f3b5efa..d2bd3ace2 100644
--- a/src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs
+++ b/src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs
@@ -41,6 +41,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 e41c2ba94..d2ee5fba0 100644
--- a/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs
+++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs
@@ -19,6 +19,8 @@ namespace Discord.WebSocket
public ulong? CategoryId { get; private set; }
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;
public bool IsNsfw => _nsfw;
diff --git a/src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs b/src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs
index 8e6e09a9f..164a93017 100644
--- a/src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs
+++ b/src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs
@@ -18,6 +18,8 @@ namespace Discord.WebSocket
public ulong? CategoryId { get; private set; }
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
=> Guild.Users.Where(x => x.VoiceChannel?.Id == Id).ToImmutableArray();