Browse Source

fix: Sending 2 requests instead of 1 to create a Guild role. (#1557)

The GuildHelper.CreateRoleAsync() was sending 2 requests to create a role. One to create the role, and one to modify the role that was created. This can be done in one request. So i have moved it to a single request to lower the amount of requests send to the api. This will also solve issue #1451.
tags/2.3.0
Bram GitHub 4 years ago
parent
commit
5430cc8df9
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 14 deletions
  1. +19
    -0
      src/Discord.Net.Rest/API/Rest/CreateGuildRoleParams.cs
  2. +2
    -2
      src/Discord.Net.Rest/DiscordRestApiClient.cs
  3. +11
    -12
      src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs

+ 19
- 0
src/Discord.Net.Rest/API/Rest/CreateGuildRoleParams.cs View File

@@ -0,0 +1,19 @@
using Newtonsoft.Json;

namespace Discord.API.Rest
{
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public class CreateGuildRoleParams
{
[JsonProperty("name")]
public Optional<string> Name { get; set; }
[JsonProperty("permissions")]
public Optional<ulong> Permissions { get; set; }
[JsonProperty("color")]
public Optional<uint> Color { get; set; }
[JsonProperty("hoist")]
public Optional<bool> Hoist { get; set; }
[JsonProperty("mentionable")]
public Optional<bool> Mentionable { get; set; }
}
}

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

@@ -1174,13 +1174,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, Rest.CreateGuildRoleParams 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)
{


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

@@ -264,19 +264,18 @@ namespace Discord.Rest
{
if (name == null) throw new ArgumentNullException(paramName: nameof(name));

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

await role.ModifyAsync(x =>
var createGuildRoleParams = new API.Rest.CreateGuildRoleParams
{
x.Name = name;
x.Permissions = (permissions ?? role.Permissions);
x.Color = (color ?? Color.Default);
x.Hoist = isHoisted;
x.Mentionable = isMentionable;
}, options).ConfigureAwait(false);

return role;
Color = color?.RawValue ?? Optional.Create<uint>(),
Hoist = isHoisted,
Mentionable = isMentionable,
Name = name,
Permissions = permissions?.RawValue ?? Optional.Create<ulong>()
};

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

return RestRole.Create(client, guild, model);
}

//Users


Loading…
Cancel
Save