Skip to content

Commit

Permalink
feat: get guild role async
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Aug 12, 2024
1 parent dddf4d9 commit 0836d3c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
11 changes: 9 additions & 2 deletions DisCatSharp/Entities/Guild/DiscordGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,14 +1898,21 @@ public Task<DiscordRole> CreateRoleAsync(string name = null, Permissions? permis
=> this.Discord.ApiClient.CreateGuildRoleAsync(this.Id, name, permissions, color?.Value, hoist, mentionable, reason);

/// <summary>
/// Gets a role from this guild by its ID.
/// Gets a role from this guild's cache by its ID.
/// </summary>
/// <param name="id">ID of the role to get.</param>
/// <returns>Requested role.</returns>
/// <exception cref="ServerErrorException">Thrown when Discord is unable to process the request.</exception>
public DiscordRole GetRole(ulong id)
=> this.RolesInternal.TryGetValue(id, out var role) ? role : null;

/// <summary>
/// Gets a role from this guild from the api by its ID.
/// </summary>
/// <param name="id">ID of the role to get.</param>
/// <returns>Requested role.</returns>
public async Task<DiscordRole> GetRoleAsync(ulong id)
=> await this.Discord.ApiClient.GetGuildRoleAsync(this.Id, id);

/// <summary>
/// Gets a channel from this guild by its ID.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions DisCatSharp/Net/Rest/DiscordApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4041,6 +4041,29 @@ internal async Task<IReadOnlyList<DiscordRole>> GetGuildRolesAsync(ulong guildId
return roles;
}

/// <summary>
/// Gets a guild role async.
/// </summary>
/// <param name="guildId">The guild_id.</param>
/// <param name="roleId">The role_id.</param>
internal async Task<DiscordRole> GetGuildRoleAsync(ulong guildId, ulong roleId)
{
var route = $"{Endpoints.GUILDS}/:guild_id{Endpoints.ROLES}/:role_id";
var bucket = this.Rest.GetBucket(RestRequestMethod.GET, route, new
{
guild_id = guildId,
role_id = roleId
}, out var path);

var url = Utilities.GetApiUriFor(path, this.Discord.Configuration);
var res = await this.DoRequestAsync(this.Discord, bucket, url, RestRequestMethod.GET, route).ConfigureAwait(false);

var ret = DiscordJson.DeserializeObject<DiscordRole>(res.Response, this.Discord);
ret.GuildId = guildId;

return ret;
}

/// <summary>
/// Modifies the guild role async.
/// </summary>
Expand Down

0 comments on commit 0836d3c

Please sign in to comment.