Browse Source

Added the option to set overwrites on channel creation

pull/1444/head
David Slutsky 5 years ago
parent
commit
f28821b0f8
5 changed files with 59 additions and 18 deletions
  1. +29
    -0
      src/Discord.Net.Rest/API/Rest/CreateChannelPermissionsParams.cs
  2. +3
    -0
      src/Discord.Net.Rest/API/Rest/CreateGuildChannelParams.cs
  3. +15
    -6
      src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
  4. +6
    -6
      src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
  5. +6
    -6
      src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs

+ 29
- 0
src/Discord.Net.Rest/API/Rest/CreateChannelPermissionsParams.cs View File

@@ -0,0 +1,29 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord.API.Rest
{
class CreateChannelPermissionsParams
{
[JsonProperty("id")]
public ulong Id { get; set; }
[JsonProperty("type")]
public string Type { get; }
[JsonProperty("allow")]
public ulong Allow { get; }
[JsonProperty("deny")]
public ulong Deny { get; }

public CreateChannelPermissionsParams(ulong id, string type, ulong allow, ulong deny)
{
Id = id;
Type = type;
Allow = allow;
Deny = deny;
}
}
}

+ 3
- 0
src/Discord.Net.Rest/API/Rest/CreateGuildChannelParams.cs View File

@@ -1,5 +1,6 @@
#pragma warning disable CS1591
using Newtonsoft.Json;
using System.Collections.Generic;

namespace Discord.API.Rest
{
@@ -14,6 +15,8 @@ namespace Discord.API.Rest
public Optional<ulong?> CategoryId { get; set; }
[JsonProperty("position")]
public Optional<int> Position { get; set; }
[JsonProperty("permission_overwrite")]
public Optional<IEnumerable<CreateChannelPermissionsParams>> PermissionOverwrite {get;set;}

//Text channels
[JsonProperty("topic")]


+ 15
- 6
src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs View File

@@ -164,54 +164,63 @@ namespace Discord.Rest
}
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
public static async Task<RestTextChannel> CreateTextChannelAsync(IGuild guild, BaseDiscordClient client,
string name, RequestOptions options, Action<TextChannelProperties> func = null)
string name, RequestOptions options, Action<TextChannelProperties> func = null, IEnumerable<Overwrite> overwrites = null)
{
if (name == null) throw new ArgumentNullException(paramName: nameof(name));

var props = new TextChannelProperties();
func?.Invoke(props);

var perms = overwrites?.Select(perm => new CreateChannelPermissionsParams(perm.TargetId, perm.TargetType == PermissionTarget.Role ? "role" : "member", perm.Permissions.AllowValue, perm.Permissions.DenyValue));

var args = new CreateGuildChannelParams(name, ChannelType.Text)
{
CategoryId = props.CategoryId,
Topic = props.Topic,
IsNsfw = props.IsNsfw,
Position = props.Position
Position = props.Position,
PermissionOverwrite = new Optional<IEnumerable<CreateChannelPermissionsParams>>(perms)
};
var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
return RestTextChannel.Create(client, guild, model);
}
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
public static async Task<RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client,
string name, RequestOptions options, Action<VoiceChannelProperties> func = null)
string name, RequestOptions options, Action<VoiceChannelProperties> func = null, IEnumerable<Overwrite> overwrites = null)
{
if (name == null) throw new ArgumentNullException(paramName: nameof(name));

var props = new VoiceChannelProperties();
func?.Invoke(props);

var perms = overwrites?.Select(perm => new CreateChannelPermissionsParams(perm.TargetId, perm.TargetType == PermissionTarget.Role ? "role" : "member", perm.Permissions.AllowValue, perm.Permissions.DenyValue));

var args = new CreateGuildChannelParams(name, ChannelType.Voice)
{
CategoryId = props.CategoryId,
Bitrate = props.Bitrate,
UserLimit = props.UserLimit,
Position = props.Position
Position = props.Position,
PermissionOverwrite = new Optional<IEnumerable<CreateChannelPermissionsParams>>(perms)
};
var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
return RestVoiceChannel.Create(client, guild, model);
}
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
public static async Task<RestCategoryChannel> CreateCategoryChannelAsync(IGuild guild, BaseDiscordClient client,
string name, RequestOptions options, Action<GuildChannelProperties> func = null)
string name, RequestOptions options, Action<GuildChannelProperties> func = null, IEnumerable<Overwrite> overwrites = null)
{
if (name == null) throw new ArgumentNullException(paramName: nameof(name));

var props = new GuildChannelProperties();
func?.Invoke(props);

var perms = overwrites?.Select(perm => new CreateChannelPermissionsParams(perm.TargetId, perm.TargetType == PermissionTarget.Role ? "role" : "member", perm.Permissions.AllowValue, perm.Permissions.DenyValue));

var args = new CreateGuildChannelParams(name, ChannelType.Category)
{
Position = props.Position
Position = props.Position,
PermissionOverwrite = new Optional<IEnumerable<CreateChannelPermissionsParams>>(perms)
};

var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);


+ 6
- 6
src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs View File

@@ -450,8 +450,8 @@ namespace Discord.Rest
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// text channel.
/// </returns>
public Task<RestTextChannel> CreateTextChannelAsync(string name, Action<TextChannelProperties> func = null, RequestOptions options = null)
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func);
public Task<RestTextChannel> CreateTextChannelAsync(string name, Action<TextChannelProperties> func = null, RequestOptions options = null, IEnumerable<Overwrite> overwritePermissions = null)
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func, overwritePermissions);
/// <summary>
/// Creates a voice channel with the provided name.
/// </summary>
@@ -462,8 +462,8 @@ namespace Discord.Rest
/// <returns>
/// The created voice channel.
/// </returns>
public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, Action<VoiceChannelProperties> func = null, RequestOptions options = null)
=> GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options, func);
public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, Action<VoiceChannelProperties> func = null, RequestOptions options = null, IEnumerable<Overwrite> overwritePermissions = null)
=> GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options, func, overwritePermissions);
/// <summary>
/// Creates a category channel with the provided name.
/// </summary>
@@ -474,8 +474,8 @@ namespace Discord.Rest
/// <returns>
/// The created category channel.
/// </returns>
public Task<RestCategoryChannel> CreateCategoryChannelAsync(string name, Action<GuildChannelProperties> func = null, RequestOptions options = null)
=> GuildHelper.CreateCategoryChannelAsync(this, Discord, name, options, func);
public Task<RestCategoryChannel> CreateCategoryChannelAsync(string name, Action<GuildChannelProperties> func = null, RequestOptions options = null, IEnumerable<Overwrite> overwritePermissions = null)
=> GuildHelper.CreateCategoryChannelAsync(this, Discord, name, options, func, overwritePermissions);

/// <summary>
/// Gets a collection of all the voice regions this guild can access.


+ 6
- 6
src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs View File

@@ -581,8 +581,8 @@ namespace Discord.WebSocket
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// text channel.
/// </returns>
public Task<RestTextChannel> CreateTextChannelAsync(string name, Action<TextChannelProperties> func = null, RequestOptions options = null)
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func);
public Task<RestTextChannel> CreateTextChannelAsync(string name, Action<TextChannelProperties> func = null, RequestOptions options = null, IEnumerable<Overwrite> overwritePermissions = null)
=> GuildHelper.CreateTextChannelAsync(this, Discord, name, options, func, overwritePermissions);
/// <summary>
/// Creates a new voice channel in this guild.
/// </summary>
@@ -594,8 +594,8 @@ namespace Discord.WebSocket
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// voice channel.
/// </returns>
public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, Action<VoiceChannelProperties> func = null, RequestOptions options = null)
=> GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options, func);
public Task<RestVoiceChannel> CreateVoiceChannelAsync(string name, Action<VoiceChannelProperties> func = null, RequestOptions options = null, IEnumerable<Overwrite> overwritePermissions = null)
=> GuildHelper.CreateVoiceChannelAsync(this, Discord, name, options, func, overwritePermissions);
/// <summary>
/// Creates a new channel category in this guild.
/// </summary>
@@ -607,8 +607,8 @@ namespace Discord.WebSocket
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// category channel.
/// </returns>
public Task<RestCategoryChannel> CreateCategoryChannelAsync(string name, Action<GuildChannelProperties> func = null, RequestOptions options = null)
=> GuildHelper.CreateCategoryChannelAsync(this, Discord, name, options, func);
public Task<RestCategoryChannel> CreateCategoryChannelAsync(string name, Action<GuildChannelProperties> func = null, RequestOptions options = null, IEnumerable<Overwrite> overwritePermissions = null)
=> GuildHelper.CreateCategoryChannelAsync(this, Discord, name, options, func, overwritePermissions);

internal SocketGuildChannel AddChannel(ClientState state, ChannelModel model)
{


Loading…
Cancel
Save