diff --git a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs index d463d86df..0e12318ab 100644 --- a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs +++ b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs @@ -569,16 +569,13 @@ namespace Discord /// /// Creates a new role with the provided name. /// - /// The new name for the role. - /// The guild permission that the role should possess. - /// The color of the role. - /// Whether the role is separated from others on the sidebar. + /// The delegate containing the properties to be applied to the role upon creation. /// The options to be used when sending the request. /// /// A task that represents the asynchronous creation operation. The task result contains the newly created /// role. /// - Task CreateRoleAsync(string name, GuildPermissions? permissions = null, Color? color = null, bool isHoisted = false, RequestOptions options = null); + Task CreateRoleAsync(Action func, RequestOptions options = null); /// /// Adds a user to this guild. diff --git a/src/Discord.Net.Rest/API/Rest/ModifyGuildRoleParams.cs b/src/Discord.Net.Rest/API/Rest/GuildRoleParams.cs similarity index 88% rename from src/Discord.Net.Rest/API/Rest/ModifyGuildRoleParams.cs rename to src/Discord.Net.Rest/API/Rest/GuildRoleParams.cs index 287e1cafe..14617c97c 100644 --- a/src/Discord.Net.Rest/API/Rest/ModifyGuildRoleParams.cs +++ b/src/Discord.Net.Rest/API/Rest/GuildRoleParams.cs @@ -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 Name { get; set; } diff --git a/src/Discord.Net.Rest/API/Rest/ModifyGuildRolesParams.cs b/src/Discord.Net.Rest/API/Rest/ModifyGuildRolesParams.cs index 0e816a260..ac98c6064 100644 --- a/src/Discord.Net.Rest/API/Rest/ModifyGuildRolesParams.cs +++ b/src/Discord.Net.Rest/API/Rest/ModifyGuildRolesParams.cs @@ -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; } diff --git a/src/Discord.Net.Rest/DiscordRestApiClient.cs b/src/Discord.Net.Rest/DiscordRestApiClient.cs index ff6d17240..cd5234baa 100644 --- a/src/Discord.Net.Rest/DiscordRestApiClient.cs +++ b/src/Discord.Net.Rest/DiscordRestApiClient.cs @@ -1142,13 +1142,13 @@ namespace Discord.API var ids = new BucketIds(guildId: guildId); return await SendAsync>("GET", () => $"guilds/{guildId}/roles", ids, options: options).ConfigureAwait(false); } - public async Task CreateGuildRoleAsync(ulong guildId, RequestOptions options = null) + public async Task 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("POST", () => $"guilds/{guildId}/roles", ids, options: options).ConfigureAwait(false); + return await SendJsonAsync("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 ModifyGuildRoleAsync(ulong guildId, ulong roleId, Rest.ModifyGuildRoleParams args, RequestOptions options = null) + public async Task ModifyGuildRoleAsync(ulong guildId, ulong roleId, Rest.GuildRoleParams args, RequestOptions options = null) { Preconditions.NotEqual(guildId, 0, nameof(guildId)); Preconditions.NotEqual(roleId, 0, nameof(roleId)); diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs index 3f5565ccf..b56f50f40 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs @@ -250,22 +250,22 @@ namespace Discord.Rest //Roles /// is null. - public static async Task CreateRoleAsync(IGuild guild, BaseDiscordClient client, - string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options) + public static async Task CreateRoleAsync(IGuild guild, BaseDiscordClient client, Action 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(), + Hoist = args.Hoist, + Mentionable = args.Mentionable, + Name = args.Name, + Permissions = args.Permissions.IsSpecified ? args.Permissions.Value.RawValue : Optional.Create() + }; - 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; } diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs index cd142b184..d3f9cb9ba 100644 --- a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs +++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs @@ -525,19 +525,15 @@ namespace Discord.Rest /// /// Creates a new role with the provided name. /// - /// The new name for the role. - /// The guild permission that the role should possess. - /// The color of the role. - /// Whether the role is separated from others on the sidebar. + /// The delegate containing the properties to be applied to the role upon creation. /// The options to be used when sending the request. /// /// A task that represents the asynchronous creation operation. The task result contains the newly created /// role. /// - public async Task CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?), - bool isHoisted = false, RequestOptions options = null) + public async Task CreateRoleAsync(Action 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); /// - async Task IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options) - => await CreateRoleAsync(name, permissions, color, isHoisted, options).ConfigureAwait(false); + async Task IGuild.CreateRoleAsync(Action func, RequestOptions options) + => await CreateRoleAsync(func, options).ConfigureAwait(false); /// async Task IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action func, RequestOptions options) diff --git a/src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs b/src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs index d570f078b..0a72696d6 100644 --- a/src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs +++ b/src/Discord.Net.Rest/Entities/Roles/RoleHelper.cs @@ -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(), Hoist = args.Hoist, diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs index cd0ab3db2..9efff5b28 100644 --- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs +++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs @@ -674,19 +674,15 @@ namespace Discord.WebSocket /// /// Creates a new role with the provided name. /// - /// The new name for the role. - /// The guild permission that the role should possess. - /// The color of the role. - /// Whether the role is separated from others on the sidebar. + /// The delegate containing the properties to be applied to the role upon creation. /// The options to be used when sending the request. /// is null. /// /// A task that represents the asynchronous creation operation. The task result contains the newly created /// role. /// - public Task 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 CreateRoleAsync(Action 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); /// - async Task IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options) - => await CreateRoleAsync(name, permissions, color, isHoisted, options).ConfigureAwait(false); + async Task IGuild.CreateRoleAsync(Action func, RequestOptions options) + => await CreateRoleAsync(func, options).ConfigureAwait(false); /// Task> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)