Skip to main content

twilight_http/request/application/command/
update_guild_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, GuildMarker},
16    },
17};
18
19#[derive(Serialize)]
20struct UpdateGuildCommandFields<'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 command in a guild, by ID.
32///
33/// You must specify a name and description. See
34/// [Discord Docs/Edit Guild Application Command].
35///
36/// [Discord Docs/Edit Guild Application Command]: https://discord.com/developers/docs/interactions/application-commands#edit-guild-application-command
37#[must_use = "requests must be configured and executed"]
38pub struct UpdateGuildCommand<'a> {
39    fields: UpdateGuildCommandFields<'a>,
40    application_id: Id<ApplicationMarker>,
41    command_id: Id<CommandMarker>,
42    guild_id: Id<GuildMarker>,
43    http: &'a Client,
44}
45
46impl<'a> UpdateGuildCommand<'a> {
47    pub(crate) const fn new(
48        http: &'a Client,
49        application_id: Id<ApplicationMarker>,
50        guild_id: Id<GuildMarker>,
51        command_id: Id<CommandMarker>,
52    ) -> Self {
53        Self {
54            application_id,
55            command_id,
56            fields: UpdateGuildCommandFields {
57                description: None,
58                name: None,
59                nsfw: None,
60                options: None,
61            },
62            guild_id,
63            http,
64        }
65    }
66
67    /// Edit the name of the command.
68    pub const fn name(mut self, name: &'a str) -> Self {
69        self.fields.name = Some(name);
70
71        self
72    }
73
74    /// Edit the description of the command.
75    pub const fn description(mut self, description: &'a str) -> Self {
76        self.fields.description = Some(description);
77
78        self
79    }
80
81    /// Edit the command options of the command.
82    pub const fn command_options(mut self, options: &'a [CommandOption]) -> Self {
83        self.fields.options = Some(options);
84
85        self
86    }
87
88    /// Edit whether the command is age-restricted.
89    pub const fn nsfw(mut self, nsfw: bool) -> Self {
90        self.fields.nsfw = Some(nsfw);
91
92        self
93    }
94}
95
96#[cfg(not(target_os = "wasi"))]
97impl IntoFuture for UpdateGuildCommand<'_> {
98    type Output = Result<Response<Command>, Error>;
99
100    type IntoFuture = ResponseFuture<Command>;
101
102    fn into_future(self) -> Self::IntoFuture {
103        let http = self.http;
104
105        match self.try_into_request() {
106            Ok(request) => http.request(request),
107            Err(source) => ResponseFuture::error(source),
108        }
109    }
110}
111
112impl TryIntoRequest for UpdateGuildCommand<'_> {
113    fn try_into_request(self) -> Result<Request, Error> {
114        Request::builder(&Route::UpdateGuildCommand {
115            application_id: self.application_id.get(),
116            command_id: self.command_id.get(),
117            guild_id: self.guild_id.get(),
118        })
119        .json(&self.fields)
120        .build()
121    }
122}