Browse Source

Add missing channel create/modify properties

pull/1595/head
Paulo 5 years ago
parent
commit
dc3456c2a2
4 changed files with 106 additions and 7 deletions
  1. +16
    -0
      src/Discord.Net.Core/Entities/Channels/GuildChannelProperties.cs
  2. +4
    -0
      src/Discord.Net.Rest/API/Rest/CreateGuildChannelParams.cs
  3. +22
    -1
      src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs
  4. +64
    -6
      src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs

+ 16
- 0
src/Discord.Net.Core/Entities/Channels/GuildChannelProperties.cs View File

@@ -1,3 +1,5 @@
using System.Collections.Generic;

namespace Discord namespace Discord
{ {
/// <summary> /// <summary>
@@ -30,5 +32,19 @@ namespace Discord
/// is set. /// is set.
/// </remarks> /// </remarks>
public Optional<ulong?> CategoryId { get; set; } public Optional<ulong?> CategoryId { get; set; }
/// <summary>
/// Gets or sets the user permission overwrites for this channel.
/// </summary>
/// <remarks>
/// The <see cref="ulong"/> key needs to be a user snowflake identifier.
/// </remarks>
public Optional<IEnumerable<KeyValuePair<ulong, OverwritePermissions>>> UserOverwrites { get; set; }
/// <summary>
/// Gets or sets the role permission overwrites for this channel.
/// </summary>
/// <remarks>
/// The <see cref="ulong"/> key needs to be a role snowflake identifier.
/// </remarks>
public Optional<IEnumerable<KeyValuePair<ulong, OverwritePermissions>>> RoleOverwrites { get; set; }
} }
} }

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

@@ -14,12 +14,16 @@ 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_overwrites")]
public Optional<Overwrite[]> Overwrites { get; set; }


//Text channels //Text channels
[JsonProperty("topic")] [JsonProperty("topic")]
public Optional<string> Topic { get; set; } public Optional<string> Topic { get; set; }
[JsonProperty("nsfw")] [JsonProperty("nsfw")]
public Optional<bool> IsNsfw { get; set; } public Optional<bool> IsNsfw { get; set; }
[JsonProperty("rate_limit_per_user")]
public Optional<int> SlowModeInterval { get; set; }


//Voice channels //Voice channels
[JsonProperty("bitrate")] [JsonProperty("bitrate")]


+ 22
- 1
src/Discord.Net.Rest/Entities/Channels/ChannelHelper.cs View File

@@ -38,6 +38,25 @@ namespace Discord.Rest
{ {
var args = new TextChannelProperties(); var args = new TextChannelProperties();
func(args); func(args);

var overwrites = new List<API.Overwrite>();
if (args.UserOverwrites.IsSpecified)
overwrites.AddRange(args.UserOverwrites.Value.Select(x => new API.Overwrite()
{
TargetId = x.Key,
TargetType = PermissionTarget.User,
Allow = x.Value.AllowValue,
Deny = x.Value.DenyValue,
}));
if (args.RoleOverwrites.IsSpecified)
overwrites.AddRange(args.RoleOverwrites.Value.Select(x => new API.Overwrite()
{
TargetId = x.Key,
TargetType = PermissionTarget.Role,
Allow = x.Value.AllowValue,
Deny = x.Value.DenyValue,
}));

var apiArgs = new API.Rest.ModifyTextChannelParams var apiArgs = new API.Rest.ModifyTextChannelParams
{ {
Name = args.Name, Name = args.Name,
@@ -46,6 +65,7 @@ namespace Discord.Rest
Topic = args.Topic, Topic = args.Topic,
IsNsfw = args.IsNsfw, IsNsfw = args.IsNsfw,
SlowModeInterval = args.SlowModeInterval, SlowModeInterval = args.SlowModeInterval,
Overwrites = overwrites.Count != 0 ? Optional.Create(overwrites.ToArray()) : Optional.Create<API.Overwrite[]>(),
}; };
return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false); return await client.ApiClient.ModifyGuildChannelAsync(channel.Id, apiArgs, options).ConfigureAwait(false);
} }
@@ -413,7 +433,8 @@ namespace Discord.Rest
var apiArgs = new ModifyGuildChannelParams var apiArgs = new ModifyGuildChannelParams
{ {
Overwrites = category.PermissionOverwrites Overwrites = category.PermissionOverwrites
.Select(overwrite => new API.Overwrite{
.Select(overwrite => new API.Overwrite
{
TargetId = overwrite.TargetId, TargetId = overwrite.TargetId,
TargetType = overwrite.TargetType, TargetType = overwrite.TargetType,
Allow = overwrite.Permissions.AllowValue, Allow = overwrite.Permissions.AllowValue,


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

@@ -171,12 +171,32 @@ namespace Discord.Rest
var props = new TextChannelProperties(); var props = new TextChannelProperties();
func?.Invoke(props); func?.Invoke(props);


var overwrites = new List<API.Overwrite>();
if (props.UserOverwrites.IsSpecified)
overwrites.AddRange(props.UserOverwrites.Value.Select(x => new API.Overwrite()
{
TargetId = x.Key,
TargetType = PermissionTarget.User,
Allow = x.Value.AllowValue,
Deny = x.Value.DenyValue,
}));
if (props.RoleOverwrites.IsSpecified)
overwrites.AddRange(props.RoleOverwrites.Value.Select(x => new API.Overwrite()
{
TargetId = x.Key,
TargetType = PermissionTarget.Role,
Allow = x.Value.AllowValue,
Deny = x.Value.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,
SlowModeInterval = props.SlowModeInterval,
Overwrites = overwrites.Count != 0 ? Optional.Create(overwrites.ToArray()) : Optional.Create<API.Overwrite[]>(),
}; };
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);
@@ -190,12 +210,31 @@ namespace Discord.Rest
var props = new VoiceChannelProperties(); var props = new VoiceChannelProperties();
func?.Invoke(props); func?.Invoke(props);


var overwrites = new List<API.Overwrite>();
if (props.UserOverwrites.IsSpecified)
overwrites.AddRange(props.UserOverwrites.Value.Select(x => new API.Overwrite()
{
TargetId = x.Key,
TargetType = PermissionTarget.User,
Allow = x.Value.AllowValue,
Deny = x.Value.DenyValue,
}));
if (props.RoleOverwrites.IsSpecified)
overwrites.AddRange(props.RoleOverwrites.Value.Select(x => new API.Overwrite()
{
TargetId = x.Key,
TargetType = PermissionTarget.Role,
Allow = x.Value.AllowValue,
Deny = x.Value.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,
Overwrites = overwrites.Count != 0 ? Optional.Create(overwrites.ToArray()) : Optional.Create<API.Overwrite[]>(),
}; };
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);
@@ -209,9 +248,28 @@ namespace Discord.Rest
var props = new GuildChannelProperties(); var props = new GuildChannelProperties();
func?.Invoke(props); func?.Invoke(props);


var overwrites = new List<API.Overwrite>();
if (props.UserOverwrites.IsSpecified)
overwrites.AddRange(props.UserOverwrites.Value.Select(x => new API.Overwrite()
{
TargetId = x.Key,
TargetType = PermissionTarget.User,
Allow = x.Value.AllowValue,
Deny = x.Value.DenyValue,
}));
if (props.RoleOverwrites.IsSpecified)
overwrites.AddRange(props.RoleOverwrites.Value.Select(x => new API.Overwrite()
{
TargetId = x.Key,
TargetType = PermissionTarget.Role,
Allow = x.Value.AllowValue,
Deny = x.Value.DenyValue,
}));

var args = new CreateGuildChannelParams(name, ChannelType.Category) var args = new CreateGuildChannelParams(name, ChannelType.Category)
{ {
Position = props.Position
Position = props.Position,
Overwrites = overwrites.Count != 0 ? Optional.Create(overwrites.ToArray()) : Optional.Create<API.Overwrite[]>(),
}; };


var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false); var model = await client.ApiClient.CreateGuildChannelAsync(guild.Id, args, options).ConfigureAwait(false);
@@ -451,7 +509,7 @@ namespace Discord.Rest
var emote = await client.ApiClient.GetGuildEmoteAsync(guild.Id, id, options).ConfigureAwait(false); var emote = await client.ApiClient.GetGuildEmoteAsync(guild.Id, id, options).ConfigureAwait(false);
return emote.ToEntity(); return emote.ToEntity();
} }
public static async Task<GuildEmote> CreateEmoteAsync(IGuild guild, BaseDiscordClient client, string name, Image image, Optional<IEnumerable<IRole>> roles,
public static async Task<GuildEmote> CreateEmoteAsync(IGuild guild, BaseDiscordClient client, string name, Image image, Optional<IEnumerable<IRole>> roles,
RequestOptions options) RequestOptions options)
{ {
var apiargs = new CreateGuildEmoteParams var apiargs = new CreateGuildEmoteParams
@@ -466,7 +524,7 @@ namespace Discord.Rest
return emote.ToEntity(); return emote.ToEntity();
} }
/// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException"><paramref name="func"/> is <c>null</c>.</exception>
public static async Task<GuildEmote> ModifyEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, Action<EmoteProperties> func,
public static async Task<GuildEmote> ModifyEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, Action<EmoteProperties> func,
RequestOptions options) RequestOptions options)
{ {
if (func == null) throw new ArgumentNullException(paramName: nameof(func)); if (func == null) throw new ArgumentNullException(paramName: nameof(func));
@@ -484,7 +542,7 @@ namespace Discord.Rest
var emote = await client.ApiClient.ModifyGuildEmoteAsync(guild.Id, id, apiargs, options).ConfigureAwait(false); var emote = await client.ApiClient.ModifyGuildEmoteAsync(guild.Id, id, apiargs, options).ConfigureAwait(false);
return emote.ToEntity(); return emote.ToEntity();
} }
public static Task DeleteEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options)
public static Task DeleteEmoteAsync(IGuild guild, BaseDiscordClient client, ulong id, RequestOptions options)
=> client.ApiClient.DeleteGuildEmoteAsync(guild.Id, id, options); => client.ApiClient.DeleteGuildEmoteAsync(guild.Id, id, options);
} }
} }

Loading…
Cancel
Save