Skip to main content

twilight_http/request/channel/
get_channel_member_permissions.rs

1//! Request builder and response body for getting a member'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, UserMarker},
18    },
19};
20
21/// Response body for the [`GetChannelMemberPermissions`] request.
22#[derive(Clone, Debug, Deserialize)]
23#[non_exhaustive]
24pub struct GetChannelMemberPermissionsResponseBody {
25    /// Computed permissions.
26    pub permissions: Permissions,
27}
28
29struct GetChannelMemberPermissionsPath {
30    channel_id: Id<ChannelMarker>,
31    implicit: Option<bool>,
32    user_id: Id<UserMarker>,
33}
34
35/// Get a member's computed permissions in a channel.
36///
37/// Includes their roles and member-specific overwrites. Threads use their
38/// parent channel's overwrites.
39///
40/// Requires the [`VIEW_CHANNEL`] permission. Also requires the [`CONNECT`]
41/// permission for [voice] and [stage] channels.
42///
43/// [`CONNECT`]: twilight_model::guild::Permissions::CONNECT
44/// [`VIEW_CHANNEL`]: twilight_model::guild::Permissions::VIEW_CHANNEL
45/// [stage]: twilight_model::channel::ChannelType::GuildStageVoice
46/// [voice]: twilight_model::channel::ChannelType::GuildVoice
47#[must_use = "requests must be configured and executed"]
48pub struct GetChannelMemberPermissions<'a> {
49    http: &'a Client,
50    path: GetChannelMemberPermissionsPath,
51}
52
53impl<'a> GetChannelMemberPermissions<'a> {
54    pub(crate) const fn new(
55        http: &'a Client,
56        channel_id: Id<ChannelMarker>,
57        user_id: Id<UserMarker>,
58    ) -> Self {
59        Self {
60            http,
61            path: GetChannelMemberPermissionsPath {
62                channel_id,
63                implicit: None,
64                user_id,
65            },
66        }
67    }
68
69    /// Whether to apply implicit permission masking for missing permissions.
70    ///
71    /// This applies implicit permission masking for [`VIEW_CHANNEL`] and
72    /// [`CONNECT`] for [voice] and [stage] channels. Not applied when the
73    /// target member is a bot.
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 GetChannelMemberPermissions<'_> {
89    type Output = Result<Response<GetChannelMemberPermissionsResponseBody>, Error>;
90
91    type IntoFuture = ResponseFuture<GetChannelMemberPermissionsResponseBody>;
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 GetChannelMemberPermissions<'_> {
104    fn try_into_request(self) -> Result<Request, Error> {
105        let GetChannelMemberPermissionsPath {
106            channel_id,
107            implicit,
108            user_id,
109        } = self.path;
110
111        Ok(Request::from_route(&Route::GetChannelMemberPermissions {
112            channel_id: channel_id.get(),
113            implicit,
114            user_id: user_id.get(),
115        }))
116    }
117}