twilight_http/request/application/interaction/update_response.rs
1//! Update a original response create for a interaction.
2
3use crate::{
4 client::Client,
5 error::Error,
6 request::{
7 attachment::{AttachmentManager, PartialAttachment},
8 Nullable, Request, TryIntoRequest,
9 },
10 response::{Response, ResponseFuture},
11 routing::Route,
12};
13use serde::Serialize;
14use std::future::IntoFuture;
15use twilight_model::{
16 channel::message::{AllowedMentions, Component, Embed, Message},
17 http::attachment::Attachment,
18 id::{
19 marker::{ApplicationMarker, AttachmentMarker},
20 Id,
21 },
22};
23use twilight_validate::message::{
24 attachment as validate_attachment, components as validate_components,
25 content as validate_content, embeds as validate_embeds, MessageValidationError,
26};
27
28#[derive(Serialize)]
29struct UpdateResponseFields<'a> {
30 #[serde(skip_serializing_if = "Option::is_none")]
31 allowed_mentions: Option<Nullable<&'a AllowedMentions>>,
32 /// List of attachments to keep, and new attachments to add.
33 #[serde(skip_serializing_if = "Option::is_none")]
34 attachments: Option<Nullable<Vec<PartialAttachment<'a>>>>,
35 #[serde(skip_serializing_if = "Option::is_none")]
36 components: Option<Nullable<&'a [Component]>>,
37 #[serde(skip_serializing_if = "Option::is_none")]
38 content: Option<Nullable<&'a str>>,
39 #[serde(skip_serializing_if = "Option::is_none")]
40 embeds: Option<Nullable<&'a [Embed]>>,
41 #[serde(skip_serializing_if = "Option::is_none")]
42 payload_json: Option<&'a [u8]>,
43}
44
45/// Edit the original message, by its token.
46///
47/// You can pass [`None`] to any of the methods to remove the associated field.
48/// Pass [`None`] to [`content`] to remove the content. You must ensure that the
49/// message still contains at least one of [`attachments`], [`components`],
50/// [`content`] or [`embeds`].
51///
52/// This endpoint is not bound to the application's global rate limit.
53///
54/// # Examples
55///
56/// Update the original response by setting the content to `test <@3>` -
57/// attempting to mention user ID 3 - while specifying that no entities can be
58/// mentioned.
59///
60/// ```no_run
61/// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
62/// use std::env;
63/// use twilight_http::Client;
64/// use twilight_model::{channel::message::AllowedMentions, id::Id};
65///
66/// let client = Client::new(env::var("DISCORD_TOKEN")?);
67/// let application_id = Id::new(1);
68///
69/// client
70/// .interaction(application_id)
71/// .update_response("token here")
72/// // By creating a default set of allowed mentions, no entity can be
73/// // mentioned.
74/// .allowed_mentions(Some(&AllowedMentions::default()))
75/// .content(Some("test <@3>"))
76/// .await?;
77/// # Ok(()) }
78/// ```
79///
80/// [`attachments`]: Self::attachments
81/// [`components`]: Self::components
82/// [`content`]: Self::content
83/// [`embeds`]: Self::embeds
84#[must_use = "requests must be configured and executed"]
85pub struct UpdateResponse<'a> {
86 application_id: Id<ApplicationMarker>,
87 attachment_manager: AttachmentManager<'a>,
88 fields: Result<UpdateResponseFields<'a>, MessageValidationError>,
89 http: &'a Client,
90 token: &'a str,
91}
92
93impl<'a> UpdateResponse<'a> {
94 pub(crate) const fn new(
95 http: &'a Client,
96 application_id: Id<ApplicationMarker>,
97 interaction_token: &'a str,
98 ) -> Self {
99 Self {
100 application_id,
101 attachment_manager: AttachmentManager::new(),
102 fields: Ok(UpdateResponseFields {
103 allowed_mentions: None,
104 attachments: None,
105 components: None,
106 content: None,
107 embeds: None,
108 payload_json: None,
109 }),
110 http,
111 token: interaction_token,
112 }
113 }
114
115 /// Specify the [`AllowedMentions`] for the message.
116 ///
117 /// If not called, the request will use the client's default allowed
118 /// mentions.
119 pub fn allowed_mentions(mut self, allowed_mentions: Option<&'a AllowedMentions>) -> Self {
120 if let Ok(fields) = self.fields.as_mut() {
121 fields.allowed_mentions = Some(Nullable(allowed_mentions));
122 }
123
124 self
125 }
126
127 /// Attach multiple new files to the message.
128 ///
129 /// This method clears previous calls.
130 ///
131 /// # Errors
132 ///
133 /// Returns an error of type [`AttachmentDescriptionTooLarge`] if
134 /// the attachments's description is too large.
135 ///
136 /// Returns an error of type [`AttachmentFilename`] if any filename is
137 /// invalid.
138 ///
139 /// [`AttachmentDescriptionTooLarge`]: twilight_validate::message::MessageValidationErrorType::AttachmentDescriptionTooLarge
140 /// [`AttachmentFilename`]: twilight_validate::message::MessageValidationErrorType::AttachmentFilename
141 pub fn attachments(mut self, attachments: &'a [Attachment]) -> Self {
142 if self.fields.is_ok() {
143 if let Err(source) = attachments.iter().try_for_each(validate_attachment) {
144 self.fields = Err(source);
145 } else {
146 self.attachment_manager = self
147 .attachment_manager
148 .set_files(attachments.iter().collect());
149 }
150 }
151
152 self
153 }
154
155 /// Set the message's list of [`Component`]s.
156 ///
157 /// Calling this method will clear previous calls.
158 ///
159 /// # Editing
160 ///
161 /// Pass [`None`] to clear existing components.
162 ///
163 /// # Errors
164 ///
165 /// Refer to the errors section of
166 /// [`twilight_validate::component::component`] for a list of errors that
167 /// may be returned as a result of validating each provided component.
168 pub fn components(mut self, components: Option<&'a [Component]>) -> Self {
169 self.fields = self.fields.and_then(|mut fields| {
170 if let Some(components) = components {
171 validate_components(components)?;
172 }
173
174 fields.components = Some(Nullable(components));
175
176 Ok(fields)
177 });
178
179 self
180 }
181
182 /// Set the message's content.
183 ///
184 /// The maximum length is 2000 UTF-16 characters.
185 ///
186 /// # Editing
187 ///
188 /// Pass [`None`] to remove the message content. This is impossible if it
189 /// would leave the message empty of `attachments`, `content`, or `embeds`.
190 ///
191 /// # Errors
192 ///
193 /// Returns an error of type [`ContentInvalid`] if the content length is too
194 /// long.
195 ///
196 /// [`ContentInvalid`]: twilight_validate::message::MessageValidationErrorType::ContentInvalid
197 pub fn content(mut self, content: Option<&'a str>) -> Self {
198 self.fields = self.fields.and_then(|mut fields| {
199 if let Some(content) = content {
200 validate_content(content)?;
201 }
202
203 fields.content = Some(Nullable(content));
204
205 Ok(fields)
206 });
207
208 self
209 }
210
211 /// Set the message's list of embeds.
212 ///
213 /// Calling this method will clear previous calls.
214 ///
215 /// The amount of embeds must not exceed [`EMBED_COUNT_LIMIT`]. The total
216 /// character length of each embed must not exceed [`EMBED_TOTAL_LENGTH`]
217 /// characters. Additionally, the internal fields also have character
218 /// limits. See [Discord Docs/Embed Limits].
219 ///
220 /// # Editing
221 ///
222 /// To keep all embeds, do not call this method. To modify one or more
223 /// embeds in the message, acquire them from the previous message, mutate
224 /// them in place, then pass that list to this method. To remove all embeds,
225 /// pass [`None`]. This is impossible if it would leave the message empty of
226 /// `attachments`, `content`, or `embeds`.
227 ///
228 /// # Examples
229 ///
230 /// Create an embed and update the message with the new embed. The content
231 /// of the original message is unaffected and only the embed(s) are
232 /// modified.
233 ///
234 /// ```no_run
235 /// # #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> {
236 /// use twilight_http::Client;
237 /// use twilight_model::id::Id;
238 /// use twilight_util::builder::embed::EmbedBuilder;
239 ///
240 /// let client = Client::new("token".to_owned());
241 /// let application_id = Id::new(1);
242 ///
243 /// let embed = EmbedBuilder::new()
244 /// .description(
245 /// "Powerful, flexible, and scalable ecosystem of Rust \
246 /// libraries for the Discord API.",
247 /// )
248 /// .title("Twilight")
249 /// .url("https://twilight.rs")
250 /// .validate()?
251 /// .build();
252 ///
253 /// client
254 /// .interaction(application_id)
255 /// .update_response("token")
256 /// .embeds(Some(&[embed]))
257 /// .await?;
258 /// # Ok(()) }
259 /// ```
260 ///
261 /// # Errors
262 ///
263 /// Returns an error of type [`TooManyEmbeds`] if there are too many embeds.
264 ///
265 /// Otherwise, refer to the errors section of
266 /// [`twilight_validate::embed::embed`] for a list of errors that may occur.
267 ///
268 /// [`EMBED_COUNT_LIMIT`]: twilight_validate::message::EMBED_COUNT_LIMIT
269 /// [`EMBED_TOTAL_LENGTH`]: twilight_validate::embed::EMBED_TOTAL_LENGTH
270 /// [`TooManyEmbeds`]: twilight_validate::message::MessageValidationErrorType::TooManyEmbeds
271 /// [Discord Docs/Embed Limits]: https://discord.com/developers/docs/resources/channel#embed-limits
272 pub fn embeds(mut self, embeds: Option<&'a [Embed]>) -> Self {
273 self.fields = self.fields.and_then(|mut fields| {
274 if let Some(embeds) = embeds {
275 validate_embeds(embeds)?;
276 }
277
278 fields.embeds = Some(Nullable(embeds));
279
280 Ok(fields)
281 });
282
283 self
284 }
285
286 /// Specify multiple [`Id<AttachmentMarker>`]s already present in the target
287 /// message to keep.
288 ///
289 /// If called, all unspecified attachments (except ones added with
290 /// [`attachments`]) will be removed from the message. This is impossible if
291 /// it would leave the message empty of `attachments`, `content`, or
292 /// `embeds`. If not called, all attachments will be kept.
293 ///
294 /// [`attachments`]: Self::attachments
295 pub fn keep_attachment_ids(mut self, attachment_ids: &'a [Id<AttachmentMarker>]) -> Self {
296 if let Ok(fields) = self.fields.as_mut() {
297 self.attachment_manager = self.attachment_manager.set_ids(attachment_ids.to_vec());
298
299 // Set an empty list. This will be overwritten in `TryIntoRequest` if
300 // the actual list is not empty.
301 fields.attachments = Some(Nullable(Some(Vec::new())));
302 }
303
304 self
305 }
306
307 /// JSON encoded body of any additional request fields.
308 ///
309 /// If this method is called, all other fields are ignored, except for
310 /// [`attachments`]. See [Discord Docs/Uploading Files].
311 ///
312 /// # Examples
313 ///
314 /// See [`ExecuteWebhook::payload_json`] for examples.
315 ///
316 /// [Discord Docs/Uploading Files]: https://discord.com/developers/docs/reference#uploading-files
317 /// [`ExecuteWebhook::payload_json`]: crate::request::channel::webhook::ExecuteWebhook::payload_json
318 /// [`attachments`]: Self::attachments
319 pub fn payload_json(mut self, payload_json: &'a [u8]) -> Self {
320 if let Ok(fields) = self.fields.as_mut() {
321 fields.payload_json = Some(payload_json);
322 }
323
324 self
325 }
326}
327
328impl IntoFuture for UpdateResponse<'_> {
329 type Output = Result<Response<Message>, Error>;
330
331 type IntoFuture = ResponseFuture<Message>;
332
333 fn into_future(self) -> Self::IntoFuture {
334 let http = self.http;
335
336 match self.try_into_request() {
337 Ok(request) => http.request(request),
338 Err(source) => ResponseFuture::error(source),
339 }
340 }
341}
342
343impl TryIntoRequest for UpdateResponse<'_> {
344 fn try_into_request(self) -> Result<Request, Error> {
345 let mut fields = self.fields.map_err(Error::validation)?;
346 let mut request = Request::builder(&Route::UpdateInteractionOriginal {
347 application_id: self.application_id.get(),
348 interaction_token: self.token,
349 });
350
351 // Interaction executions don't need the authorization token, only the
352 // interaction token.
353 request = request.use_authorization_token(false);
354
355 // Set the default allowed mentions if required.
356 if fields.allowed_mentions.is_none() {
357 if let Some(allowed_mentions) = self.http.default_allowed_mentions() {
358 fields.allowed_mentions = Some(Nullable(Some(allowed_mentions)));
359 }
360 }
361
362 // Determine whether we need to use a multipart/form-data body or a JSON
363 // body.
364 if !self.attachment_manager.is_empty() {
365 let form = if let Some(payload_json) = fields.payload_json {
366 self.attachment_manager.build_form(payload_json)
367 } else {
368 fields.attachments = Some(Nullable(Some(
369 self.attachment_manager.get_partial_attachments(),
370 )));
371
372 let fields = crate::json::to_vec(&fields).map_err(Error::json)?;
373
374 self.attachment_manager.build_form(fields.as_ref())
375 };
376
377 request = request.form(form);
378 } else if let Some(payload_json) = fields.payload_json {
379 request = request.body(payload_json.to_vec());
380 } else {
381 request = request.json(&fields);
382 }
383
384 request.build()
385 }
386}
387
388#[cfg(test)]
389mod tests {
390 use crate::{client::Client, request::TryIntoRequest};
391 use std::error::Error;
392 use twilight_http_ratelimiting::Path;
393 use twilight_model::id::Id;
394
395 #[test]
396 fn delete_followup_message() -> Result<(), Box<dyn Error>> {
397 let application_id = Id::new(1);
398 let token = "foo".to_owned();
399
400 let client = Client::new(String::new());
401 let req = client
402 .interaction(application_id)
403 .update_response(&token)
404 .content(Some("test"))
405 .try_into_request()?;
406
407 assert!(!req.use_authorization_token());
408 assert_eq!(
409 &Path::WebhooksIdTokenMessagesId(application_id.get(), token),
410 req.ratelimit_path()
411 );
412
413 Ok(())
414 }
415}