Skip to content

Commit

Permalink
GetRoleAsync W (#2989)
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha-133 authored Aug 29, 2024
1 parent 2aaa0fd commit 466b491
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,17 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// A role that is associated with the specified <paramref name="id"/>; <see langword="null" /> if none is found.
/// </returns>
IRole GetRole(ulong id);

/// <summary>
/// Gets a role in this guild.
/// </summary>
/// <param name="id">The snowflake identifier for the role.</param>
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the role
/// that is associated with the specified <paramref name="id"/>; <see langword="null" /> if none is found.
/// </returns>
Task<IRole> GetRoleAsync(ulong id, RequestOptions options = null);

/// <summary>
/// Creates a new role with the provided name.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/Discord.Net.Rest/DiscordRestApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,17 @@ public Task RemoveRoleAsync(ulong guildId, ulong userId, ulong roleId, RequestOp
var ids = new BucketIds(guildId: guildId);
return SendAsync("DELETE", () => $"guilds/{guildId}/members/{userId}/roles/{roleId}", ids, options: options);
}

public Task<API.Role> GetRoleAsync(ulong guildId, ulong roleId, RequestOptions options = null)
{
Preconditions.NotEqual(guildId, 0, nameof(guildId));
Preconditions.NotEqual(roleId, 0, nameof(roleId));
options = RequestOptions.CreateOrClone(options);

var ids = new BucketIds(guildId: guildId);
return SendAsync<API.Role>("GET", () => $"guilds/{guildId}/roles/{roleId}", ids, options: options);
}

#endregion

#region Channel Messages
Expand Down
7 changes: 7 additions & 0 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,13 @@ public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClie

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

public static async Task<RestRole> GetRoleAsync(IGuild guild, BaseDiscordClient client, ulong roleId, RequestOptions options)
{
var model = await client.ApiClient.GetRoleAsync(guild.Id, roleId, options).ConfigureAwait(false);
return model is null ? null : RestRole.Create(client, guild, model);
}

#endregion

#region Users
Expand Down
9 changes: 9 additions & 0 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1596,6 +1596,15 @@ async Task<IInviteMetadata> IGuild.GetVanityInviteAsync(RequestOptions options)
/// <inheritdoc />
IRole IGuild.GetRole(ulong id)
=> GetRole(id);

/// <inheritdoc cref="IGuild.GetRole" />
public Task<RestRole> GetRoleAsync(ulong id, RequestOptions options = null)
=> GuildHelper.GetRoleAsync(this, Discord, id, options);

/// <inheritdoc />
async Task<IRole> IGuild.GetRoleAsync(ulong id, RequestOptions options)
=> await GetRoleAsync(id);

/// <inheritdoc />
async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
=> await CreateRoleAsync(name, permissions, color, isHoisted, false, options).ConfigureAwait(false);
Expand Down
9 changes: 9 additions & 0 deletions src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2200,6 +2200,15 @@ async Task<IInviteMetadata> IGuild.GetVanityInviteAsync(RequestOptions options)
/// <inheritdoc />
IRole IGuild.GetRole(ulong id)
=> GetRole(id);

/// <inheritdoc cref="IGuild.GetRole" />
public Task<RestRole> GetRoleAsync(ulong id, RequestOptions options = null)
=> GuildHelper.GetRoleAsync(this, Discord, id, options);

/// <inheritdoc />
async Task<IRole> IGuild.GetRoleAsync(ulong id, RequestOptions options)
=> await GetRoleAsync(id);

/// <inheritdoc />
async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
=> await CreateRoleAsync(name, permissions, color, isHoisted, false, options).ConfigureAwait(false);
Expand Down

0 comments on commit 466b491

Please sign in to comment.