twilight_http/request/application/command/create_global_command/
chat_input.rs

1use super::super::CommandBorrowed;
2use crate::{
3    client::Client,
4    error::Error,
5    request::{Request, TryIntoRequest},
6    response::{Response, ResponseFuture},
7    routing::Route,
8};
9use std::{collections::HashMap, future::IntoFuture};
10use twilight_model::{
11    application::command::{Command, CommandOption, CommandType},
12    guild::Permissions,
13    id::{marker::ApplicationMarker, Id},
14};
15use twilight_validate::command::{
16    chat_input_name as validate_chat_input_name, description as validate_description,
17    options as validate_options, CommandValidationError,
18};
19
20struct CreateGlobalChatInputCommandFields<'a> {
21    default_member_permissions: Option<Permissions>,
22    description: &'a str,
23    description_localizations: Option<&'a HashMap<String, String>>,
24    dm_permission: Option<bool>,
25    name: &'a str,
26    name_localizations: Option<&'a HashMap<String, String>>,
27    nsfw: Option<bool>,
28    options: Option<&'a [CommandOption]>,
29}
30
31/// Create a new chat input global command.
32///
33/// The description must be between 1 and 100 characters in length. Creating a
34/// command with the same name as an already-existing global command will
35/// overwrite the old command. See
36/// [Discord Docs/Create Global Application Command].
37///
38/// [Discord Docs/Create Global Application Command]: https://discord.com/developers/docs/interactions/application-commands#create-global-application-command
39#[must_use = "requests must be configured and executed"]
40pub struct CreateGlobalChatInputCommand<'a> {
41    application_id: Id<ApplicationMarker>,
42    fields: Result<CreateGlobalChatInputCommandFields<'a>, CommandValidationError>,
43    http: &'a Client,
44}
45
46impl<'a> CreateGlobalChatInputCommand<'a> {
47    pub(crate) fn new(
48        http: &'a Client,
49        application_id: Id<ApplicationMarker>,
50        name: &'a str,
51        description: &'a str,
52    ) -> Self {
53        let fields = Ok(CreateGlobalChatInputCommandFields {
54            default_member_permissions: None,
55            description,
56            description_localizations: None,
57            dm_permission: None,
58            name,
59            name_localizations: None,
60            nsfw: None,
61            options: None,
62        })
63        .and_then(|fields| {
64            validate_description(description)?;
65
66            validate_chat_input_name(name)?;
67
68            Ok(fields)
69        });
70
71        Self {
72            application_id,
73            fields,
74            http,
75        }
76    }
77
78    /// Add a list of command options.
79    ///
80    /// Required command options must be added before optional options.
81    ///
82    /// # Errors
83    ///
84    /// Returns an error of type [`OptionsRequiredFirst`] if a required option
85    /// was added after an optional option. The problem option's index is
86    /// provided.
87    ///
88    /// [`OptionsRequiredFirst`]: twilight_validate::command::CommandValidationErrorType::OptionsRequiredFirst
89    pub fn command_options(mut self, options: &'a [CommandOption]) -> Self {
90        self.fields = self.fields.and_then(|mut fields| {
91            validate_options(options)?;
92
93            fields.options = Some(options);
94
95            Ok(fields)
96        });
97
98        self
99    }
100
101    /// Default permissions required for a member to run the command.
102    ///
103    /// Defaults to [`None`].
104    pub fn default_member_permissions(mut self, default: Permissions) -> Self {
105        if let Ok(fields) = self.fields.as_mut() {
106            fields.default_member_permissions = Some(default);
107        }
108
109        self
110    }
111
112    /// Set whether the command is available in DMs.
113    ///
114    /// Defaults to [`None`].
115    pub fn dm_permission(mut self, dm_permission: bool) -> Self {
116        if let Ok(fields) = self.fields.as_mut() {
117            fields.dm_permission = Some(dm_permission);
118        }
119
120        self
121    }
122
123    /// Set the localization dictionary for the command description.
124    ///
125    /// Defaults to [`None`].
126    ///
127    /// # Errors
128    ///
129    /// Returns an error of type [`DescriptionInvalid`] if the description is
130    /// invalid.
131    ///
132    /// [`DescriptionInvalid`]: twilight_validate::command::CommandValidationErrorType::DescriptionInvalid
133    pub fn description_localizations(mut self, localizations: &'a HashMap<String, String>) -> Self {
134        self.fields = self.fields.and_then(|mut fields| {
135            for description in localizations.values() {
136                validate_description(description)?;
137            }
138
139            fields.description_localizations = Some(localizations);
140
141            Ok(fields)
142        });
143
144        self
145    }
146
147    /// Set the localization dictionary for the command name.
148    ///
149    /// Defaults to [`None`].
150    ///
151    /// # Errors
152    ///
153    /// Returns an error of type [`NameLengthInvalid`] if the length is invalid.
154    ///
155    /// Returns an error of type [`NameCharacterInvalid`] if the name contains a
156    /// non-alphanumeric character or an uppercase character for which a
157    /// lowercase variant exists.
158    ///
159    /// [`NameLengthInvalid`]: twilight_validate::command::CommandValidationErrorType::NameLengthInvalid
160    /// [`NameCharacterInvalid`]: twilight_validate::command::CommandValidationErrorType::NameCharacterInvalid
161    pub fn name_localizations(mut self, localizations: &'a HashMap<String, String>) -> Self {
162        self.fields = self.fields.and_then(|mut fields| {
163            for name in localizations.values() {
164                validate_chat_input_name(name)?;
165            }
166
167            fields.name_localizations = Some(localizations);
168
169            Ok(fields)
170        });
171
172        self
173    }
174
175    /// Set whether the command is age-restricted.
176    ///
177    /// Defaults to not being specified, which uses Discord's default.
178    pub fn nsfw(mut self, nsfw: bool) -> Self {
179        if let Ok(fields) = self.fields.as_mut() {
180            fields.nsfw = Some(nsfw);
181        }
182
183        self
184    }
185}
186
187impl IntoFuture for CreateGlobalChatInputCommand<'_> {
188    type Output = Result<Response<Command>, Error>;
189
190    type IntoFuture = ResponseFuture<Command>;
191
192    fn into_future(self) -> Self::IntoFuture {
193        let http = self.http;
194
195        match self.try_into_request() {
196            Ok(request) => http.request(request),
197            Err(source) => ResponseFuture::error(source),
198        }
199    }
200}
201
202impl TryIntoRequest for CreateGlobalChatInputCommand<'_> {
203    fn try_into_request(self) -> Result<Request, Error> {
204        let fields = self.fields.map_err(Error::validation)?;
205
206        Request::builder(&Route::CreateGlobalCommand {
207            application_id: self.application_id.get(),
208        })
209        .json(&CommandBorrowed {
210            application_id: Some(self.application_id),
211            default_member_permissions: fields.default_member_permissions,
212            dm_permission: fields.dm_permission,
213            description: Some(fields.description),
214            description_localizations: fields.description_localizations,
215            kind: CommandType::ChatInput,
216            name: fields.name,
217            name_localizations: fields.name_localizations,
218            nsfw: fields.nsfw,
219            options: fields.options,
220        })
221        .build()
222    }
223}