twilight_http/request/application/command/
update_global_command.rs

1use crate::{
2    client::Client,
3    error::Error,
4    request::{Request, TryIntoRequest},
5    response::{Response, ResponseFuture},
6    routing::Route,
7};
8use serde::Serialize;
9use std::future::IntoFuture;
10use twilight_model::{
11    application::command::{Command, CommandOption},
12    id::{
13        marker::{ApplicationMarker, CommandMarker},
14        Id,
15    },
16};
17
18#[derive(Serialize)]
19struct UpdateGlobalCommandFields<'a> {
20    #[serde(skip_serializing_if = "Option::is_none")]
21    description: Option<&'a str>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    name: Option<&'a str>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    nsfw: Option<bool>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    options: Option<&'a [CommandOption]>,
28}
29
30/// Edit a global command, by ID.
31///
32/// You must specify a name and description. See
33/// [Discord Docs/Edit Global Application Command].
34///
35/// [Discord Docs/Edit Global Application Command]: https://discord.com/developers/docs/interactions/application-commands#edit-global-application-command
36#[must_use = "requests must be configured and executed"]
37pub struct UpdateGlobalCommand<'a> {
38    fields: UpdateGlobalCommandFields<'a>,
39    command_id: Id<CommandMarker>,
40    application_id: Id<ApplicationMarker>,
41    http: &'a Client,
42}
43
44impl<'a> UpdateGlobalCommand<'a> {
45    pub(crate) const fn new(
46        http: &'a Client,
47        application_id: Id<ApplicationMarker>,
48        command_id: Id<CommandMarker>,
49    ) -> Self {
50        Self {
51            application_id,
52            command_id,
53            fields: UpdateGlobalCommandFields {
54                description: None,
55                name: None,
56                nsfw: None,
57                options: None,
58            },
59            http,
60        }
61    }
62
63    /// Edit the name of the command.
64    pub const fn name(mut self, name: &'a str) -> Self {
65        self.fields.name = Some(name);
66
67        self
68    }
69
70    /// Edit the description of the command.
71    pub const fn description(mut self, description: &'a str) -> Self {
72        self.fields.description = Some(description);
73
74        self
75    }
76
77    /// Edit the command options of the command.
78    pub const fn command_options(mut self, options: &'a [CommandOption]) -> Self {
79        self.fields.options = Some(options);
80
81        self
82    }
83
84    /// Edit whether the command is age-restricted.
85    pub const fn nsfw(mut self, nsfw: bool) -> Self {
86        self.fields.nsfw = Some(nsfw);
87
88        self
89    }
90}
91
92impl IntoFuture for UpdateGlobalCommand<'_> {
93    type Output = Result<Response<Command>, Error>;
94
95    type IntoFuture = ResponseFuture<Command>;
96
97    fn into_future(self) -> Self::IntoFuture {
98        let http = self.http;
99
100        match self.try_into_request() {
101            Ok(request) => http.request(request),
102            Err(source) => ResponseFuture::error(source),
103        }
104    }
105}
106
107impl TryIntoRequest for UpdateGlobalCommand<'_> {
108    fn try_into_request(self) -> Result<Request, Error> {
109        Request::builder(&Route::UpdateGlobalCommand {
110            application_id: self.application_id.get(),
111            command_id: self.command_id.get(),
112        })
113        .json(&self.fields)
114        .build()
115    }
116}