Skip to content

Commit 59a37f2

Browse files
boothedevviztea
authored andcommitted
feat(http): add get guild role endpoint (twilight-rs#2394)
Discord supports an endpoint that allows to fetch a single guild role. - discord/discord-api-docs#7074
1 parent c540296 commit 59a37f2

File tree

5 files changed

+88
-5
lines changed

5 files changed

+88
-5
lines changed

twilight-http/src/client/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ use crate::{
5757
AddGuildMember, AddRoleToMember, GetGuildMembers, GetMember, RemoveMember,
5858
RemoveRoleFromMember, SearchGuildMembers, UpdateGuildMember,
5959
},
60-
role::{CreateRole, DeleteRole, GetGuildRoles, UpdateRole, UpdateRolePositions},
60+
role::{
61+
CreateRole, DeleteRole, GetGuildRoles, GetRole, UpdateRole, UpdateRolePositions,
62+
},
6163
sticker::{
6264
CreateGuildSticker, DeleteGuildSticker, GetGuildSticker, GetGuildStickers,
6365
UpdateGuildSticker,
@@ -1669,6 +1671,11 @@ impl Client {
16691671
GetGuildRoles::new(self, guild_id)
16701672
}
16711673

1674+
/// Get a role of a guild.
1675+
pub const fn role(&self, guild_id: Id<GuildMarker>, role_id: Id<RoleMarker>) -> GetRole<'_> {
1676+
GetRole::new(self, guild_id, role_id)
1677+
}
1678+
16721679
/// Create a role in a guild.
16731680
///
16741681
/// # Examples
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use crate::{
2+
client::Client,
3+
error::Error,
4+
request::{Request, TryIntoRequest},
5+
response::{Response, ResponseFuture},
6+
routing::Route,
7+
};
8+
use std::future::IntoFuture;
9+
use twilight_model::{
10+
guild::Role,
11+
id::{
12+
marker::{GuildMarker, RoleMarker},
13+
Id,
14+
},
15+
};
16+
17+
/// Get a role of a guild.
18+
#[must_use = "requests must be configured and executed"]
19+
pub struct GetRole<'a> {
20+
guild_id: Id<GuildMarker>,
21+
role_id: Id<RoleMarker>,
22+
http: &'a Client,
23+
}
24+
25+
impl<'a> GetRole<'a> {
26+
pub(crate) const fn new(
27+
http: &'a Client,
28+
guild_id: Id<GuildMarker>,
29+
role_id: Id<RoleMarker>,
30+
) -> Self {
31+
Self {
32+
guild_id,
33+
role_id,
34+
http,
35+
}
36+
}
37+
}
38+
39+
impl IntoFuture for GetRole<'_> {
40+
type Output = Result<Response<Role>, Error>;
41+
42+
type IntoFuture = ResponseFuture<Role>;
43+
44+
fn into_future(self) -> Self::IntoFuture {
45+
let http = self.http;
46+
47+
match self.try_into_request() {
48+
Ok(request) => http.request(request),
49+
Err(source) => ResponseFuture::error(source),
50+
}
51+
}
52+
}
53+
54+
impl TryIntoRequest for GetRole<'_> {
55+
fn try_into_request(self) -> Result<Request, Error> {
56+
Ok(Request::from_route(&Route::GetRole {
57+
guild_id: self.guild_id.get(),
58+
role_id: self.role_id.get(),
59+
}))
60+
}
61+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
mod create_role;
22
mod delete_role;
33
mod get_guild_roles;
4+
mod get_role;
45
mod update_role;
56
mod update_role_positions;
67

78
pub use self::{
89
create_role::CreateRole, delete_role::DeleteRole, get_guild_roles::GetGuildRoles,
9-
update_role::UpdateRole, update_role_positions::UpdateRolePositions,
10+
get_role::GetRole, update_role::UpdateRole, update_role_positions::UpdateRolePositions,
1011
};

twilight-http/src/request/try_into_request.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ mod private {
6666
AddGuildMember, AddRoleToMember, GetGuildMembers, GetMember, RemoveMember,
6767
RemoveRoleFromMember, SearchGuildMembers, UpdateGuildMember,
6868
},
69-
role::{CreateRole, DeleteRole, GetGuildRoles, UpdateRole, UpdateRolePositions},
69+
role::{
70+
CreateRole, DeleteRole, GetGuildRoles, GetRole, UpdateRole, UpdateRolePositions,
71+
},
7072
sticker::{
7173
CreateGuildSticker, DeleteGuildSticker, GetGuildSticker, GetGuildStickers,
7274
UpdateGuildSticker,
@@ -236,6 +238,7 @@ mod private {
236238
impl Sealed for GetPublicArchivedThreads<'_> {}
237239
impl Sealed for GetReactions<'_> {}
238240
impl Sealed for GetResponse<'_> {}
241+
impl Sealed for GetRole<'_> {}
239242
impl Sealed for GetSKUs<'_> {}
240243
impl Sealed for GetStageInstance<'_> {}
241244
impl Sealed for GetSticker<'_> {}

twilight-http/src/routing.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ pub enum Route<'a> {
642642
/// pruned.
643643
include_roles: &'a [Id<RoleMarker>],
644644
},
645-
/// Route information to get a guild's roles.
645+
/// Route information to get guild's roles.
646646
GetGuildRoles {
647647
/// The ID of the guild.
648648
guild_id: u64,
@@ -830,6 +830,13 @@ pub enum Route<'a> {
830830
/// The type of reactions to fetch.
831831
kind: Option<u8>,
832832
},
833+
/// Route information to get a guild's role.
834+
GetRole {
835+
/// The ID of the guild.
836+
guild_id: u64,
837+
/// The ID of the role.
838+
role_id: u64,
839+
},
833840
GetSKUs {
834841
/// The ID of the application.
835842
application_id: u64,
@@ -1301,6 +1308,7 @@ impl Route<'_> {
13011308
| Self::GetPrivateArchivedThreads { .. }
13021309
| Self::GetPublicArchivedThreads { .. }
13031310
| Self::GetReactionUsers { .. }
1311+
| Self::GetRole { .. }
13041312
| Self::GetSKUs { .. }
13051313
| Self::GetStageInstance { .. }
13061314
| Self::GetSticker { .. }
@@ -1574,6 +1582,7 @@ impl Route<'_> {
15741582
Path::ChannelsIdPermissionsOverwriteId(channel_id)
15751583
}
15761584
Self::DeleteRole { guild_id, .. }
1585+
| Self::GetRole { guild_id, .. }
15771586
| Self::UpdateRole { guild_id, .. }
15781587
| Self::UpdateRolePositions { guild_id } => Path::GuildsIdRolesId(guild_id),
15791588
Self::DeleteTemplate {
@@ -2307,7 +2316,9 @@ impl Display for Route<'_> {
23072316

23082317
Display::fmt(user_id, f)
23092318
}
2310-
Route::DeleteRole { guild_id, role_id } | Route::UpdateRole { guild_id, role_id } => {
2319+
Route::DeleteRole { guild_id, role_id }
2320+
| Route::GetRole { guild_id, role_id }
2321+
| Route::UpdateRole { guild_id, role_id } => {
23112322
f.write_str("guilds/")?;
23122323
Display::fmt(guild_id, f)?;
23132324
f.write_str("/roles/")?;

0 commit comments

Comments
 (0)