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