Add SyncPermissionsAsync to Sync Child Channels with its Parent (#1159)

* Initial implementation

* Adjust according to comments

See: 6e76b45713 (diff-58466c35787d448266d026692e467baa)
This commit is contained in:
Still Hsu
2018-10-20 05:21:37 +08:00
committed by Christopher F
parent d30d12246d
commit 5ea1fb374e
8 changed files with 34 additions and 2 deletions

View File

@@ -25,5 +25,10 @@ namespace Discord
/// representing the parent of this channel; <c>null</c> if none is set. /// representing the parent of this channel; <c>null</c> if none is set.
/// </returns> /// </returns>
Task<ICategoryChannel> GetCategoryAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null); Task<ICategoryChannel> GetCategoryAsync(CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null);
/// <summary>
/// Syncs the permissions of this nested channel with its parent's.
/// </summary>
Task SyncPermissionsAsync(RequestOptions options = null);
} }
} }

View File

@@ -1,4 +1,4 @@
#pragma warning disable CS1591 #pragma warning disable CS1591
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Discord.API namespace Discord.API

View File

@@ -1,4 +1,4 @@
#pragma warning disable CS1591 #pragma warning disable CS1591
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Discord.API.Rest namespace Discord.API.Rest
@@ -12,5 +12,7 @@ namespace Discord.API.Rest
public Optional<int> Position { get; set; } public Optional<int> Position { get; set; }
[JsonProperty("parent_id")] [JsonProperty("parent_id")]
public Optional<ulong?> CategoryId { get; set; } public Optional<ulong?> CategoryId { get; set; }
[JsonProperty("permission_overwrites")]
public Optional<Overwrite[]> Overwrites { get; set; }
} }
} }

View File

@@ -348,6 +348,23 @@ namespace Discord.Rest
var model = await client.ApiClient.GetChannelAsync(channel.CategoryId.Value, options).ConfigureAwait(false); var model = await client.ApiClient.GetChannelAsync(channel.CategoryId.Value, options).ConfigureAwait(false);
return RestCategoryChannel.Create(client, model) as ICategoryChannel; 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 //Helpers
private static IUser GetAuthor(BaseDiscordClient client, IGuild guild, UserModel model, ulong? webhookId) private static IUser GetAuthor(BaseDiscordClient client, IGuild guild, UserModel model, ulong? webhookId)

View File

@@ -201,6 +201,8 @@ namespace Discord.Rest
/// </returns> /// </returns>
public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null) public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null)
=> ChannelHelper.GetCategoryAsync(this, Discord, options); => ChannelHelper.GetCategoryAsync(this, Discord, options);
public Task SyncPermissionsAsync(RequestOptions options = null)
=> ChannelHelper.SyncPermissionsAsync(this, Discord, options);
private string DebuggerDisplay => $"{Name} ({Id}, Text)"; private string DebuggerDisplay => $"{Name} ({Id}, Text)";

View File

@@ -57,6 +57,8 @@ namespace Discord.Rest
/// </returns> /// </returns>
public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null) public Task<ICategoryChannel> GetCategoryAsync(RequestOptions options = null)
=> ChannelHelper.GetCategoryAsync(this, Discord, options); => ChannelHelper.GetCategoryAsync(this, Discord, options);
public Task SyncPermissionsAsync(RequestOptions options = null)
=> ChannelHelper.SyncPermissionsAsync(this, Discord, options);
private string DebuggerDisplay => $"{Name} ({Id}, Voice)"; private string DebuggerDisplay => $"{Name} ({Id}, Voice)";

View File

@@ -31,6 +31,8 @@ namespace Discord.WebSocket
/// </returns> /// </returns>
public ICategoryChannel Category public ICategoryChannel Category
=> CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null; => CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null;
public Task SyncPermissionsAsync(RequestOptions options = null)
=> ChannelHelper.SyncPermissionsAsync(this, Discord, options);
private bool _nsfw; private bool _nsfw;
/// <inheritdoc /> /// <inheritdoc />

View File

@@ -30,6 +30,8 @@ namespace Discord.WebSocket
/// </returns> /// </returns>
public ICategoryChannel Category public ICategoryChannel Category
=> CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null; => CategoryId.HasValue ? Guild.GetChannel(CategoryId.Value) as ICategoryChannel : null;
public Task SyncPermissionsAsync(RequestOptions options = null)
=> ChannelHelper.SyncPermissionsAsync(this, Discord, options);
/// <inheritdoc /> /// <inheritdoc />
public override IReadOnlyCollection<SocketGuildUser> Users public override IReadOnlyCollection<SocketGuildUser> Users