Browse Source

Allowed setting role properties on creation

pull/1384/head
Casino Boyale 6 years ago
parent
commit
6e23154dc0
8 changed files with 31 additions and 42 deletions
  1. +2
    -5
      src/Discord.Net.Core/Entities/Guilds/IGuild.cs
  2. +2
    -2
      src/Discord.Net.Rest/API/Rest/GuildRoleParams.cs
  3. +1
    -1
      src/Discord.Net.Rest/API/Rest/ModifyGuildRolesParams.cs
  4. +3
    -3
      src/Discord.Net.Rest/DiscordRestApiClient.cs
  5. +12
    -12
      src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
  6. +5
    -9
      src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
  7. +1
    -1
      src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs
  8. +5
    -9
      src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs

+ 2
- 5
src/Discord.Net.Core/Entities/Guilds/IGuild.cs View File

@@ -569,16 +569,13 @@ namespace Discord
/// <summary>
/// Creates a new role with the provided name.
/// </summary>
/// <param name="name">The new name for the role.</param>
/// <param name="permissions">The guild permission that the role should possess.</param>
/// <param name="color">The color of the role.</param>
/// <param name="isHoisted">Whether the role is separated from others on the sidebar.</param>
/// <param name="func">The delegate containing the properties to be applied to the role upon creation.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// role.
/// </returns>
Task<IRole> CreateRoleAsync(string name, GuildPermissions? permissions = null, Color? color = null, bool isHoisted = false, RequestOptions options = null);
Task<IRole> CreateRoleAsync(Action<RoleProperties> func, RequestOptions options = null);

/// <summary>
/// Adds a user to this guild.


src/Discord.Net.Rest/API/Rest/ModifyGuildRoleParams.cs → src/Discord.Net.Rest/API/Rest/GuildRoleParams.cs View File

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

namespace Discord.API.Rest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
internal class ModifyGuildRoleParams
internal class GuildRoleParams
{
[JsonProperty("name")]
public Optional<string> Name { get; set; }

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

@@ -4,7 +4,7 @@ using Newtonsoft.Json;
namespace Discord.API.Rest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
internal class ModifyGuildRolesParams : ModifyGuildRoleParams
internal class ModifyGuildRolesParams : GuildRoleParams
{
[JsonProperty("id")]
public ulong Id { get; }


+ 3
- 3
src/Discord.Net.Rest/DiscordRestApiClient.cs View File

@@ -1142,13 +1142,13 @@ namespace Discord.API
var ids = new BucketIds(guildId: guildId);
return await SendAsync<IReadOnlyCollection<Role>>("GET", () => $"guilds/{guildId}/roles", ids, options: options).ConfigureAwait(false);
}
public async Task<Role> CreateGuildRoleAsync(ulong guildId, RequestOptions options = null)
public async Task<Role> CreateGuildRoleAsync(ulong guildId, GuildRoleParams args, RequestOptions options = null)
{
Preconditions.NotEqual(guildId, 0, nameof(guildId));
options = RequestOptions.CreateOrClone(options);

var ids = new BucketIds(guildId: guildId);
return await SendAsync<Role>("POST", () => $"guilds/{guildId}/roles", ids, options: options).ConfigureAwait(false);
return await SendJsonAsync<Role>("POST", () => $"guilds/{guildId}/roles", args, ids, options: options).ConfigureAwait(false);
}
public async Task DeleteGuildRoleAsync(ulong guildId, ulong roleId, RequestOptions options = null)
{
@@ -1159,7 +1159,7 @@ namespace Discord.API
var ids = new BucketIds(guildId: guildId);
await SendAsync("DELETE", () => $"guilds/{guildId}/roles/{roleId}", ids, options: options).ConfigureAwait(false);
}
public async Task<Role> ModifyGuildRoleAsync(ulong guildId, ulong roleId, Rest.ModifyGuildRoleParams args, RequestOptions options = null)
public async Task<Role> ModifyGuildRoleAsync(ulong guildId, ulong roleId, Rest.GuildRoleParams args, RequestOptions options = null)
{
Preconditions.NotEqual(guildId, 0, nameof(guildId));
Preconditions.NotEqual(roleId, 0, nameof(roleId));


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

@@ -250,22 +250,22 @@ namespace Discord.Rest

//Roles
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClient client,
string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClient client, Action<RoleProperties> func, RequestOptions options)
{
if (name == null) throw new ArgumentNullException(paramName: nameof(name));
var args = new RoleProperties();
func(args);
var apiArgs = new API.Rest.GuildRoleParams
{
Color = args.Color.IsSpecified ? args.Color.Value.RawValue : Optional.Create<uint>(),
Hoist = args.Hoist,
Mentionable = args.Mentionable,
Name = args.Name,
Permissions = args.Permissions.IsSpecified ? args.Permissions.Value.RawValue : Optional.Create<ulong>()
};

var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, options).ConfigureAwait(false);
var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, apiArgs, options).ConfigureAwait(false);
var role = RestRole.Create(client, guild, model);

await role.ModifyAsync(x =>
{
x.Name = name;
x.Permissions = (permissions ?? role.Permissions);
x.Color = (color ?? Color.Default);
x.Hoist = isHoisted;
}, options).ConfigureAwait(false);

return role;
}



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

@@ -525,19 +525,15 @@ namespace Discord.Rest
/// <summary>
/// Creates a new role with the provided name.
/// </summary>
/// <param name="name">The new name for the role.</param>
/// <param name="permissions">The guild permission that the role should possess.</param>
/// <param name="color">The color of the role.</param>
/// <param name="isHoisted">Whether the role is separated from others on the sidebar.</param>
/// <param name="func">The delegate containing the properties to be applied to the role upon creation.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// role.
/// </returns>
public async Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?),
bool isHoisted = false, RequestOptions options = null)
public async Task<RestRole> CreateRoleAsync(Action<RoleProperties> func, RequestOptions options = null)
{
var role = await GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, options).ConfigureAwait(false);
var role = await GuildHelper.CreateRoleAsync(this, Discord, func, options).ConfigureAwait(false);
_roles = _roles.Add(role.Id, role);
return role;
}
@@ -824,8 +820,8 @@ namespace Discord.Rest
IRole IGuild.GetRole(ulong id)
=> GetRole(id);
/// <inheritdoc />
async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
=> await CreateRoleAsync(name, permissions, color, isHoisted, options).ConfigureAwait(false);
async Task<IRole> IGuild.CreateRoleAsync(Action<RoleProperties> func, RequestOptions options)
=> await CreateRoleAsync(func, options).ConfigureAwait(false);

/// <inheritdoc />
async Task<IGuildUser> IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action<AddGuildUserProperties> func, RequestOptions options)


+ 1
- 1
src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs View File

@@ -18,7 +18,7 @@ namespace Discord.Rest
{
var args = new RoleProperties();
func(args);
var apiArgs = new API.Rest.ModifyGuildRoleParams
var apiArgs = new API.Rest.GuildRoleParams
{
Color = args.Color.IsSpecified ? args.Color.Value.RawValue : Optional.Create<uint>(),
Hoist = args.Hoist,


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

@@ -674,19 +674,15 @@ namespace Discord.WebSocket
/// <summary>
/// Creates a new role with the provided name.
/// </summary>
/// <param name="name">The new name for the role.</param>
/// <param name="permissions">The guild permission that the role should possess.</param>
/// <param name="color">The color of the role.</param>
/// <param name="isHoisted">Whether the role is separated from others on the sidebar.</param>
/// <param name="func">The delegate containing the properties to be applied to the role upon creation.</param>
/// <param name="options">The options to be used when sending the request.</param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c>.</exception>
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// role.
/// </returns>
public Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?),
bool isHoisted = false, RequestOptions options = null)
=> GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, options);
public Task<RestRole> CreateRoleAsync(Action<RoleProperties> func, RequestOptions options = null)
=> GuildHelper.CreateRoleAsync(this, Discord, func, options);
internal SocketRole AddRole(RoleModel model)
{
var role = SocketRole.Create(this, Discord.State, model);
@@ -1142,8 +1138,8 @@ namespace Discord.WebSocket
IRole IGuild.GetRole(ulong id)
=> GetRole(id);
/// <inheritdoc />
async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
=> await CreateRoleAsync(name, permissions, color, isHoisted, options).ConfigureAwait(false);
async Task<IRole> IGuild.CreateRoleAsync(Action<RoleProperties> func, RequestOptions options)
=> await CreateRoleAsync(func, options).ConfigureAwait(false);

/// <inheritdoc />
Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)


Loading…
Cancel
Save