Skip to main content

twilight_http/request/channel/
get_channel_role_permissions.rs

1//! Request builder and response body for getting a role's permissions in the
2//! context of a channel.
3
4use crate::{
5    client::Client,
6    error::Error,
7    request::{Request, TryIntoRequest},
8    response::{Response, ResponseFuture},
9    routing::Route,
10};
11use serde::Deserialize;
12use std::future::IntoFuture;
13use twilight_model::{
14    guild::Permissions,
15    id::{
16        Id,
17        marker::{ChannelMarker, RoleMarker},
18    },
19};
20
21/// Response body for the [`GetChannelRolePermissions`] request.
22#[derive(Clone, Debug, Deserialize)]
23#[non_exhaustive]
24pub struct GetChannelRolePermissionsResponseBody {
25    /// Computed permissions.
26    pub permissions: Permissions,
27}
28
29struct GetChannelRolePermissionsPath {
30    channel_id: Id<ChannelMarker>,
31    implicit: Option<bool>,
32    role_id: Id<RoleMarker>,
33}
34
35/// Get the permissions granted by the specified role together with `@everyone`,
36/// applying their channel overwrites.
37///
38/// Other roles and member-specific overwrites are not included. Threads use
39/// their parent channel's overwrites.
40///
41/// Requires the [`VIEW_CHANNEL`] permission. Also requires the [`CONNECT`]
42/// permission for [voice] and [stage] channels.
43///
44/// [`CONNECT`]: twilight_model::guild::Permissions::CONNECT
45/// [`VIEW_CHANNEL`]: twilight_model::guild::Permissions::VIEW_CHANNEL
46/// [stage]: twilight_model::channel::ChannelType::GuildStageVoice
47/// [voice]: twilight_model::channel::ChannelType::GuildVoice
48#[must_use = "requests must be configured and executed"]
49pub struct GetChannelRolePermissions<'a> {
50    http: &'a Client,
51    path: GetChannelRolePermissionsPath,
52}
53
54impl<'a> GetChannelRolePermissions<'a> {
55    pub(crate) const fn new(
56        http: &'a Client,
57        channel_id: Id<ChannelMarker>,
58        role_id: Id<RoleMarker>,
59    ) -> Self {
60        Self {
61            http,
62            path: GetChannelRolePermissionsPath {
63                channel_id,
64                implicit: None,
65                role_id,
66            },
67        }
68    }
69
70    /// Whether to apply implicit permission masking for missing permissions.
71    ///
72    /// This applies implicit permission masking for [`VIEW_CHANNEL`] and
73    /// [`CONNECT`] for [voice] and [stage] channels.
74    ///
75    /// Discord defaults this to true.
76    ///
77    /// [`CONNECT`]: twilight_model::guild::Permissions::CONNECT
78    /// [`VIEW_CHANNEL`]: twilight_model::guild::Permissions::VIEW_CHANNEL
79    /// [stage]: twilight_model::channel::ChannelType::GuildStageVoice
80    /// [voice]: twilight_model::channel::ChannelType::GuildVoice
81    pub const fn implicit(mut self, implicit: bool) -> Self {
82        self.path.implicit = Some(implicit);
83
84        self
85    }
86}
87
88impl IntoFuture for GetChannelRolePermissions<'_> {
89    type Output = Result<Response<GetChannelRolePermissionsResponseBody>, Error>;
90
91    type IntoFuture = ResponseFuture<GetChannelRolePermissionsResponseBody>;
92
93    fn into_future(self) -> Self::IntoFuture {
94        let http = self.http;
95
96        match self.try_into_request() {
97            Ok(request) => http.request(request),
98            Err(source) => ResponseFuture::error(source),
99        }
100    }
101}
102
103impl TryIntoRequest for GetChannelRolePermissions<'_> {
104    fn try_into_request(self) -> Result<Request, Error> {
105        let GetChannelRolePermissionsPath {
106            channel_id,
107            implicit,
108            role_id,
109        } = self.path;
110
111        Ok(Request::from_route(&Route::GetChannelRolePermissions {
112            channel_id: channel_id.get(),
113            implicit,
114            role_id: role_id.get(),
115        }))
116    }
117}