serenity/builder/
edit_interaction_response.rs

1#[cfg(feature = "http")]
2use super::Builder;
3use super::{
4    CreateActionRow,
5    CreateAllowedMentions,
6    CreateAttachment,
7    CreateEmbed,
8    EditAttachments,
9    EditWebhookMessage,
10};
11#[cfg(feature = "http")]
12use crate::http::CacheHttp;
13#[cfg(feature = "http")]
14use crate::internal::prelude::*;
15use crate::model::prelude::*;
16
17/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#edit-original-interaction-response)
18#[derive(Clone, Debug, Default, Serialize)]
19#[must_use]
20pub struct EditInteractionResponse(EditWebhookMessage);
21
22impl EditInteractionResponse {
23    /// Equivalent to [`Self::default`].
24    pub fn new() -> Self {
25        Self::default()
26    }
27
28    /// Set the content of the message.
29    ///
30    /// **Note**: Message contents must be under 2000 unicode code points.
31    #[inline]
32    pub fn content(self, content: impl Into<String>) -> Self {
33        Self(self.0.content(content))
34    }
35
36    /// Adds an embed for the message.
37    ///
38    /// Embeds from the original message are reset when adding new embeds and must be re-added.
39    pub fn add_embed(self, embed: CreateEmbed) -> Self {
40        Self(self.0.add_embed(embed))
41    }
42
43    /// Adds multiple embeds to the message.
44    ///
45    /// Embeds from the original message are reset when adding new embeds and must be re-added.
46    pub fn add_embeds(self, embeds: Vec<CreateEmbed>) -> Self {
47        Self(self.0.add_embeds(embeds))
48    }
49
50    /// Sets a single embed to include in the message
51    ///
52    /// Calling this will overwrite the embed list. To append embeds, call [`Self::add_embed`]
53    /// instead.
54    pub fn embed(self, embed: CreateEmbed) -> Self {
55        Self(self.0.embed(embed))
56    }
57
58    /// Sets the embeds for the message.
59    ///
60    /// **Note**: You can only have up to 10 embeds per message.
61    ///
62    /// Calling this will overwrite the embed list. To append embeds, call [`Self::add_embeds`]
63    /// instead.
64    pub fn embeds(self, embeds: Vec<CreateEmbed>) -> Self {
65        Self(self.0.embeds(embeds))
66    }
67
68    /// Set the allowed mentions for the message.
69    pub fn allowed_mentions(self, allowed_mentions: CreateAllowedMentions) -> Self {
70        Self(self.0.allowed_mentions(allowed_mentions))
71    }
72
73    /// Sets the components of this message.
74    pub fn components(self, components: Vec<CreateActionRow>) -> Self {
75        Self(self.0.components(components))
76    }
77    super::button_and_select_menu_convenience_methods!(self.0.components);
78
79    /// Sets attachments, see [`EditAttachments`] for more details.
80    pub fn attachments(self, attachments: EditAttachments) -> Self {
81        Self(self.0.attachments(attachments))
82    }
83
84    /// Adds a new attachment to the message.
85    ///
86    /// Resets existing attachments. See the documentation for [`EditAttachments`] for details.
87    pub fn new_attachment(self, attachment: CreateAttachment) -> Self {
88        Self(self.0.new_attachment(attachment))
89    }
90
91    /// Shorthand for [`EditAttachments::keep`].
92    pub fn keep_existing_attachment(self, id: AttachmentId) -> Self {
93        Self(self.0.keep_existing_attachment(id))
94    }
95
96    /// Shorthand for calling [`Self::attachments`] with [`EditAttachments::new`].
97    pub fn clear_attachments(self) -> Self {
98        Self(self.0.clear_attachments())
99    }
100}
101
102#[cfg(feature = "http")]
103#[async_trait::async_trait]
104impl Builder for EditInteractionResponse {
105    type Context<'ctx> = &'ctx str;
106    type Built = Message;
107
108    /// Edits the initial interaction response. Does not work for ephemeral messages.
109    ///
110    /// The `application_id` used will usually be the bot's [`UserId`], except if the bot is very
111    /// old.
112    ///
113    /// **Note**: Message contents must be under 2000 unicode code points, and embeds must be under
114    /// 6000 code points.
115    ///
116    /// # Errors
117    ///
118    /// Returns an [`Error::Model`] if the message content is too long. May also return an
119    /// [`Error::Http`] if the API returns an error, or an [`Error::Json`] if there is an error in
120    /// deserializing the API response.
121    async fn execute(
122        mut self,
123        cache_http: impl CacheHttp,
124        ctx: Self::Context<'_>,
125    ) -> Result<Self::Built> {
126        self.0.check_length()?;
127
128        let files = self.0.attachments.as_mut().map_or(Vec::new(), |a| a.take_files());
129
130        cache_http.http().edit_original_interaction_response(ctx, &self, files).await
131    }
132}