twilight_http/request/application/command/
update_command_permissions.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{Request, TryIntoRequest},
5    response::{marker::ListBody, Response, ResponseFuture},
6    routing::Route,
7};
8use serde::Serialize;
9use std::future::IntoFuture;
10use twilight_model::{
11    application::command::permissions::CommandPermission,
12    id::{
13        marker::{ApplicationMarker, CommandMarker, GuildMarker},
14        Id,
15    },
16};
17use twilight_validate::command::{
18    guild_permissions as validate_guild_permissions, CommandValidationError,
19};
20
21#[derive(Serialize)]
22struct UpdateCommandPermissionsFields<'a> {
23    pub permissions: &'a [CommandPermission],
24}
25
26/// Update command permissions for a single command in a guild.
27///
28/// Note that this overwrites the command permissions, so the full set of
29/// permissions has to be sent every time.
30///
31/// This request requires that the client was configured with an OAuth2 Bearer
32/// token.
33#[must_use = "requests must be configured and executed"]
34pub struct UpdateCommandPermissions<'a> {
35    application_id: Id<ApplicationMarker>,
36    command_id: Id<CommandMarker>,
37    guild_id: Id<GuildMarker>,
38    fields: Result<UpdateCommandPermissionsFields<'a>, CommandValidationError>,
39    http: &'a Client,
40}
41
42impl<'a> UpdateCommandPermissions<'a> {
43    pub(crate) fn new(
44        http: &'a Client,
45        application_id: Id<ApplicationMarker>,
46        guild_id: Id<GuildMarker>,
47        command_id: Id<CommandMarker>,
48        permissions: &'a [CommandPermission],
49    ) -> Self {
50        let fields = Ok(UpdateCommandPermissionsFields { permissions }).and_then(|fields| {
51            validate_guild_permissions(permissions.len())?;
52
53            Ok(fields)
54        });
55
56        Self {
57            application_id,
58            command_id,
59            guild_id,
60            fields,
61            http,
62        }
63    }
64}
65
66impl IntoFuture for UpdateCommandPermissions<'_> {
67    type Output = Result<Response<ListBody<CommandPermission>>, Error>;
68
69    type IntoFuture = ResponseFuture<ListBody<CommandPermission>>;
70
71    fn into_future(self) -> Self::IntoFuture {
72        let http = self.http;
73
74        match self.try_into_request() {
75            Ok(request) => http.request(request),
76            Err(source) => ResponseFuture::error(source),
77        }
78    }
79}
80
81impl TryIntoRequest for UpdateCommandPermissions<'_> {
82    fn try_into_request(self) -> Result<Request, Error> {
83        let fields = self.fields.map_err(Error::validation)?;
84
85        Request::builder(&Route::UpdateCommandPermissions {
86            application_id: self.application_id.get(),
87            command_id: self.command_id.get(),
88            guild_id: self.guild_id.get(),
89        })
90        .json(&fields)
91        .build()
92    }
93}