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