Skip to main content

twilight_http/request/application/command/
update_global_command.rs

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