Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add message_snapshots on Message #3092

Merged
merged 1 commit into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/model/channel/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ pub struct Message {
pub flags: Option<MessageFlags>,
/// The message that was replied to using this message.
pub referenced_message: Option<Box<Message>>, // Boxed to avoid recursion
/// An array of message snapshots, known as forwarded messages.
#[serde(default, deserialize_with = "deserialize_snapshots")]
pub message_snapshots: Vec<MessageSnapshot>,
#[cfg_attr(not(ignore_serenity_deprecated), deprecated = "Use interaction_metadata")]
pub interaction: Option<Box<MessageInteraction>>,
/// Sent if the message is a response to an [`Interaction`].
Expand Down Expand Up @@ -1190,6 +1193,47 @@ pub struct ChannelMention {
pub name: String,
}

/// [Discord docs](https://discord.com/developers/docs/resources/message#message-snapshot-structure)
///
/// For field documentation, see [`Message`].
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub struct MessageSnapshot {
pub content: String,
pub timestamp: Timestamp,
pub edited_timestamp: Option<Timestamp>,
pub mentions: Vec<User>,
#[serde(default)]
pub mention_roles: Vec<RoleId>,
pub attachments: Vec<Attachment>,
pub embeds: Vec<Embed>,
#[serde(rename = "type")]
pub kind: MessageType,
pub flags: Option<MessageFlags>,
#[serde(default)]
pub components: Vec<ActionRow>,
#[serde(default)]
pub sticker_items: Vec<StickerItem>,
}

/// Custom deserialization function to handle the nested "message" field
fn deserialize_snapshots<'de, D>(deserializer: D) -> Result<Vec<MessageSnapshot>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct MessageSnapshotWrapper {
pub message: MessageSnapshot,
}

let snapshots: Vec<MessageSnapshotWrapper> = Deserialize::deserialize(deserializer)?;

let result = snapshots.into_iter().map(|wrapper| wrapper.message).collect();

Ok(result)
}

bitflags! {
/// Describes extra features of the message.
///
Expand Down
Loading