Browse Source

Initial implementation

pull/1159/head
Still Hsu 7 years ago
parent
commit
6e76b45713
No known key found for this signature in database GPG Key ID: 8601A145FDA95209
8 changed files with 39 additions and 2 deletions
  1. +5
    -0
      src/Discord.Net.Core/Entities/Channels/INestedChannel.cs
  2. +9
    -1
      src/Discord.Net.Rest/API/Common/Overwrite.cs
  3. +3
    -1
      src/Discord.Net.Rest/API/Rest/ModifyGuildChannelParams.cs
  4. +14
    -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

@@ -12,5 +12,10 @@ namespace Discord
ulong? CategoryId { get; }
/// <summary> Gets the parent channel (category) of this channel, if it is set. If unset, returns null.</summary>
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);
}
}

+ 9
- 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
@@ -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;
}
}
}

+ 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; }
}
}

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

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


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

@@ -92,6 +92,8 @@ namespace Discord.Rest

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

@@ -41,6 +41,8 @@ namespace Discord.Rest

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

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


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

@@ -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<SocketGuildUser> Users
=> Guild.Users.Where(x => x.VoiceChannel?.Id == Id).ToImmutableArray();


Loading…
Cancel
Save