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 #pragma warning disable CS1591
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Collections.Generic;


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


//Text channels //Text channels
[JsonProperty("topic")] [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> /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
public static async Task<RestTextChannel> CreateTextChannelAsync(IGuild guild, BaseDiscordClient client, 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)); if (name == null) throw new ArgumentNullException(paramName: nameof(name));


var props = new TextChannelProperties(); var props = new TextChannelProperties();
func?.Invoke(props); 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) var args = new CreateGuildChannelParams(name, ChannelType.Text)
{ {
CategoryId = props.CategoryId, CategoryId = props.CategoryId,
Topic = props.Topic, Topic = props.Topic,
IsNsfw = props.IsNsfw, 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); var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
return RestTextChannel.Create(client, guild, model); return RestTextChannel.Create(client, guild, model);
} }
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
public static async Task<RestVoiceChannel> CreateVoiceChannelAsync(IGuild guild, BaseDiscordClient client, 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)); if (name == null) throw new ArgumentNullException(paramName: nameof(name));


var props = new VoiceChannelProperties(); var props = new VoiceChannelProperties();
func?.Invoke(props); 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) var args = new CreateGuildChannelParams(name, ChannelType.Voice)
{ {
CategoryId = props.CategoryId, CategoryId = props.CategoryId,
Bitrate = props.Bitrate, Bitrate = props.Bitrate,
UserLimit = props.UserLimit, 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); var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
return RestVoiceChannel.Create(client, guild, model); return RestVoiceChannel.Create(client, guild, model);
} }
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
public static async Task<RestCategoryChannel> CreateCategoryChannelAsync(IGuild guild, BaseDiscordClient client, 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)); if (name == null) throw new ArgumentNullException(paramName: nameof(name));


var props = new GuildChannelProperties(); var props = new GuildChannelProperties();
func?.Invoke(props); 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) 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); 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 /// A task that represents the asynchronous creation operation. The task result contains the newly created
/// text channel. /// text channel.
/// </returns> /// </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> /// <summary>
/// Creates a voice channel with the provided name. /// Creates a voice channel with the provided name.
/// </summary> /// </summary>
@@ -462,8 +462,8 @@ namespace Discord.Rest
/// <returns> /// <returns>
/// The created voice channel. /// The created voice channel.
/// </returns> /// </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> /// <summary>
/// Creates a category channel with the provided name. /// Creates a category channel with the provided name.
/// </summary> /// </summary>
@@ -474,8 +474,8 @@ namespace Discord.Rest
/// <returns> /// <returns>
/// The created category channel. /// The created category channel.
/// </returns> /// </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> /// <summary>
/// Gets a collection of all the voice regions this guild can access. /// 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 /// A task that represents the asynchronous creation operation. The task result contains the newly created
/// text channel. /// text channel.
/// </returns> /// </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> /// <summary>
/// Creates a new voice channel in this guild. /// Creates a new voice channel in this guild.
/// </summary> /// </summary>
@@ -594,8 +594,8 @@ namespace Discord.WebSocket
/// A task that represents the asynchronous creation operation. The task result contains the newly created /// A task that represents the asynchronous creation operation. The task result contains the newly created
/// voice channel. /// voice channel.
/// </returns> /// </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> /// <summary>
/// Creates a new channel category in this guild. /// Creates a new channel category in this guild.
/// </summary> /// </summary>
@@ -607,8 +607,8 @@ namespace Discord.WebSocket
/// A task that represents the asynchronous creation operation. The task result contains the newly created /// A task that represents the asynchronous creation operation. The task result contains the newly created
/// category channel. /// category channel.
/// </returns> /// </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) internal SocketGuildChannel AddChannel(ClientState state, ChannelModel model)
{ {


Loading…
Cancel
Save