Skip to main content

djs_model/rest/
interaction.rs

1//! Bodies of the [interaction endpoints](https://docs.discord.com/developers/interactions/receiving-and-responding#endpoints).
2//!
3//! The initial response is an [`InteractionResponse`](crate::interaction::InteractionResponse);
4//! edits of the original response and of follow-ups use
5//! [`EditWebhookMessage`](super::EditWebhookMessage).
6
7use compact_str::CompactString;
8use serde::{Serialize, Serializer};
9
10use super::{Attachments, FileUpload, setters};
11use crate::{
12    component::Component,
13    message::{AllowedMentions, AttachmentRequest, Embed, MessageFlags},
14    poll::CreatePoll,
15};
16
17/// The body of [Create Followup Message](https://docs.discord.com/developers/interactions/receiving-and-responding#create-followup-message):
18/// the Execute Webhook body without the fields follow-ups do not support (D-107).
19///
20/// Right after a deferred response (`DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE`), the first
21/// follow-up edits the "thinking…" message instead of creating a new one.
22///
23/// discord.js counterpart: `InteractionReplyOptions` of `followUp` /
24/// `RESTPostAPIInteractionFollowupJSONBody`.
25///
26/// # Examples
27///
28/// ```
29/// use djs_model::rest::CreateFollowupMessage;
30///
31/// let body = CreateFollowupMessage::from("Only you can see this.").ephemeral();
32/// assert_eq!(
33///     serde_json::to_string(&body).unwrap(),
34///     r#"{"content":"Only you can see this.","flags":64}"#
35/// );
36/// ```
37#[derive(Clone, Debug, Default, PartialEq)]
38#[non_exhaustive]
39pub struct CreateFollowupMessage {
40    /// The text (up to 2000 characters).
41    pub content: Option<CompactString>,
42    /// Whether the message is read aloud.
43    pub tts: bool,
44    /// Up to 10 rich embeds.
45    pub embeds: Vec<Embed>,
46    /// Which mentions in the content notify.
47    pub allowed_mentions: Option<AllowedMentions>,
48    /// The components.
49    pub components: Vec<Component>,
50    /// Explicit attachment entries; files get theirs automatically (D-094).
51    pub attachments: Option<Vec<AttachmentRequest>>,
52    /// Only [`EPHEMERAL`](MessageFlags::EPHEMERAL),
53    /// [`SUPPRESS_EMBEDS`](MessageFlags::SUPPRESS_EMBEDS),
54    /// [`SUPPRESS_NOTIFICATIONS`](MessageFlags::SUPPRESS_NOTIFICATIONS) and
55    /// [`IS_COMPONENTS_V2`](MessageFlags::IS_COMPONENTS_V2) can be set.
56    pub flags: Option<MessageFlags>,
57    /// A poll to create with the message.
58    pub poll: Option<CreatePoll>,
59    /// Files to upload with the message (D-087).
60    pub files: Vec<FileUpload>,
61}
62
63impl Serialize for CreateFollowupMessage {
64    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
65        #[derive(Serialize)]
66        struct Wire<'a> {
67            #[serde(skip_serializing_if = "Option::is_none")]
68            content: Option<&'a CompactString>,
69            #[serde(skip_serializing_if = "core::ops::Not::not")]
70            tts: bool,
71            #[serde(skip_serializing_if = "<[_]>::is_empty")]
72            embeds: &'a [Embed],
73            #[serde(skip_serializing_if = "Option::is_none")]
74            allowed_mentions: Option<&'a AllowedMentions>,
75            #[serde(skip_serializing_if = "<[_]>::is_empty")]
76            components: &'a [Component],
77            #[serde(skip_serializing_if = "Attachments::is_absent")]
78            attachments: Attachments<'a>,
79            #[serde(skip_serializing_if = "Option::is_none")]
80            flags: Option<MessageFlags>,
81            #[serde(skip_serializing_if = "Option::is_none")]
82            poll: Option<&'a CreatePoll>,
83        }
84
85        // Destructured without `..`, so a new field cannot be forgotten here.
86        let Self {
87            content,
88            tts,
89            embeds,
90            allowed_mentions,
91            components,
92            attachments,
93            flags,
94            poll,
95            files,
96        } = self;
97        Wire {
98            content: content.as_ref(),
99            tts: *tts,
100            embeds,
101            allowed_mentions: allowed_mentions.as_ref(),
102            components,
103            attachments: Attachments::new(attachments.as_deref(), files),
104            flags: *flags,
105            poll: poll.as_ref(),
106        }
107        .serialize(serializer)
108    }
109}
110
111setters::message_body!(CreateFollowupMessage {
112    content,
113    embeds,
114    components,
115    allowed_mentions,
116    flags,
117    ephemeral,
118    suppress_notifications,
119    files,
120    attachments,
121    poll,
122    tts,
123});