Browse Source

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

* Initial implementation

* Adjust according to comments

See: 6e76b45713 (diff-58466c35787d448266d026692e467baa)
tags/2.0
Still Hsu Christopher F 6 years ago
parent
commit
5ea1fb374e
8 changed files with 34 additions and 2 deletions
  1. +5
    -0
      src/Discord.Net.Core/Entities/Channels/INestedChannel.cs
  2. +1
    -1
      src/Discord.Net.Rest/API/Common/Overwrite.cs
  3. +3
    -1
      src/Discord.Net.Rest/API/Rest/ModifyGuildChannelParams.cs
  4. +17
    -0
      src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
  5. +2
    -0
      src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs
  6. +2
    -0
      src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs
  7. +2
    -0
      src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs
  8. +2
    -0
      src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs

+ 5
- 0
src/Discord.Net.Core/Entities/Channels/INestedChannel.cs View File

@@ -25,5 +25,10 @@ namespace Discord
/// representing the parent of this channel; <c>null</c> if none is set.
/// </returns>
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);
}
}

+ 1
- 1
src/Discord.Net.Rest/API/Common/Overwrite.cs View File

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

namespace Discord.API


+ 3
- 1
src/Discord.Net.Rest/API/Rest/ModifyGuildChannelParams.cs View File

@@ -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<int> Position { get; set; }
[JsonProperty("parent_id")]
public Optional<ulong?> CategoryId { get; set; }
[JsonProperty("permission_overwrites")]
public Optional<Overwrite[]> Overwrites { get; set; }
}
}

+ 17
- 0
src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs View File

@@ -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)


+ 2
- 0
src/Discord.Net.Rest/Entities/Channels/RestTextChannel.cs View File

@@ -201,6 +201,8 @@ namespace Discord.Rest
/// </returns>
public Task<ICategoryChannel> 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)";



+ 2
- 0
src/Discord.Net.Rest/Entities/Channels/RestVoiceChannel.cs View File

@@ -57,6 +57,8 @@ namespace Discord.Rest
/// </returns>
public Task<ICategoryChannel> 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)";



+ 2
- 0
src/Discord.Net.WebSocket/Entities/Channels/SocketTextChannel.cs View File

@@ -31,6 +31,8 @@ namespace Discord.WebSocket
/// </returns>
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;
/// <inheritdoc />


+ 2
- 0
src/Discord.Net.WebSocket/Entities/Channels/SocketVoiceChannel.cs View File

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

/// <inheritdoc />
public override IReadOnlyCollection<SocketGuildUser> Users


Loading…
Cancel
Save